IDEMPIERE-1055 Print Dunning Letters on remote machine throwing errors

This commit is contained in:
Juliana Corredor 2013-06-19 19:19:09 -05:00
parent b3c1e844df
commit f8b6a4f726
1 changed files with 143 additions and 108 deletions

View File

@ -1,8 +1,8 @@
/****************************************************************************** /******************************************************************************
* Product: Adempiere ERP & CRM Smart Business Solution * * Product: Adempiere ERP & CRM Smart Business Solution *
* Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved. * * Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved. *
* This program is free software; you can redistribute it and/or modify it * * This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published * * under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. This program is distributed in the hope * * by the Free Software Foundation. This program is distributed in the hope *
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
@ -14,33 +14,42 @@
* ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA * * ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA *
* or via info@compiere.org or http://www.compiere.org/license.html * * or via info@compiere.org or http://www.compiere.org/license.html *
*****************************************************************************/ *****************************************************************************/
package org.compiere.process; package org.adempiere.webui.process;
import java.io.File; import java.io.File;
import java.util.logging.Level; import java.io.FileInputStream;
import java.util.ArrayList;
import org.compiere.model.MBPartner; import java.util.List;
import org.compiere.model.MClient; import java.util.logging.Level;
import org.compiere.model.MDunningLevel;
import org.compiere.model.MDunningRun; import org.adempiere.webui.apps.AEnv;
import org.compiere.model.MDunningRunEntry; import org.adempiere.webui.component.Window;
import org.compiere.model.MMailText; import org.adempiere.webui.session.SessionManager;
import org.compiere.model.MQuery; import org.adempiere.webui.window.SimplePDFViewer;
import org.compiere.model.MUser; import org.compiere.model.MBPartner;
import org.compiere.model.MUserMail; import org.compiere.model.MClient;
import org.compiere.model.PrintInfo; import org.compiere.model.MDunningLevel;
import org.compiere.print.MPrintFormat; import org.compiere.model.MDunningRun;
import org.compiere.print.ReportEngine; import org.compiere.model.MDunningRunEntry;
import org.compiere.util.AdempiereUserError; import org.compiere.model.MMailText;
import org.compiere.util.EMail; import org.compiere.model.MQuery;
import org.compiere.model.MUser;
import org.compiere.model.MUserMail;
import org.compiere.model.PrintInfo;
import org.compiere.print.MPrintFormat;
import org.compiere.print.ReportEngine;
import org.compiere.process.ProcessInfoParameter;
import org.compiere.process.SvrProcess;
import org.compiere.util.AdempiereUserError;
import org.compiere.util.EMail;
/** /**
* Dunning Letter Print * Dunning Letter Print
* *
* @author Jorg Janke * @author Jorg Janke
* @version $Id: DunningPrint.java,v 1.2 2006/07/30 00:51:02 jjanke Exp $ * @version $Id: DunningPrint.java,v 1.2 2006/07/30 00:51:02 jjanke Exp $
* *
* FR 2872010 - Dunning Run for a complete Dunning (not just level) - Developer: Carlos Ruiz - globalqss - Sponsor: Metas * FR 2872010 - Dunning Run for a complete Dunning (not just level) - Developer: Carlos Ruiz - globalqss - Sponsor: Metas
*/ */
public class DunningPrint extends SvrProcess public class DunningPrint extends SvrProcess
{ {
@ -51,8 +60,8 @@ public class DunningPrint extends SvrProcess
/** Dunning Run */ /** Dunning Run */
private int p_C_DunningRun_ID = 0; private int p_C_DunningRun_ID = 0;
/** Print only Outstanding */ /** Print only Outstanding */
private boolean p_IsOnlyIfBPBalance = true; private boolean p_IsOnlyIfBPBalance = true;
/** Print only unprocessed lines */ /** Print only unprocessed lines */
private boolean p_PrintUnprocessedOnly = true; private boolean p_PrintUnprocessedOnly = true;
@ -74,8 +83,8 @@ public class DunningPrint extends SvrProcess
else if (name.equals("C_DunningRun_ID")) else if (name.equals("C_DunningRun_ID"))
p_C_DunningRun_ID = para[i].getParameterAsInt(); p_C_DunningRun_ID = para[i].getParameterAsInt();
else if (name.equals("IsOnlyIfBPBalance")) else if (name.equals("IsOnlyIfBPBalance"))
p_IsOnlyIfBPBalance = "Y".equals(para[i].getParameter()); p_IsOnlyIfBPBalance = "Y".equals(para[i].getParameter());
else if (name.equals("PrintUnprocessedOnly")) else if (name.equals("PrintUnprocessedOnly"))
p_PrintUnprocessedOnly = "Y".equals(para[i].getParameter()); p_PrintUnprocessedOnly = "Y".equals(para[i].getParameter());
else else
log.log(Level.SEVERE, "Unknown Parameter: " + name); log.log(Level.SEVERE, "Unknown Parameter: " + name);
@ -89,51 +98,52 @@ public class DunningPrint extends SvrProcess
*/ */
protected String doIt () throws Exception protected String doIt () throws Exception
{ {
if (log.isLoggable(Level.INFO)) log.info("C_DunningRun_ID=" + p_C_DunningRun_ID + ",R_MailText_ID=" + p_R_MailText_ID if (log.isLoggable(Level.INFO)) log.info("C_DunningRun_ID=" + p_C_DunningRun_ID + ",R_MailText_ID=" + p_R_MailText_ID
+ ", EmailPDF=" + p_EMailPDF + ",IsOnlyIfBPBalance=" + p_IsOnlyIfBPBalance + ", EmailPDF=" + p_EMailPDF + ",IsOnlyIfBPBalance=" + p_IsOnlyIfBPBalance
+ ",PrintUnprocessedOnly=" + p_PrintUnprocessedOnly); + ",PrintUnprocessedOnly=" + p_PrintUnprocessedOnly);
// Need to have Template // Need to have Template
if (p_EMailPDF && p_R_MailText_ID == 0) if (p_EMailPDF && p_R_MailText_ID == 0)
throw new AdempiereUserError ("@NotFound@: @R_MailText_ID@"); throw new AdempiereUserError ("@NotFound@: @R_MailText_ID@");
// String subject = ""; // String subject = "";
MMailText mText = null; MMailText mText = null;
if (p_EMailPDF) if (p_EMailPDF)
{ {
mText = new MMailText (getCtx(), p_R_MailText_ID, get_TrxName()); mText = new MMailText (getCtx(), p_R_MailText_ID, get_TrxName());
if (p_EMailPDF && mText.get_ID() == 0) if (p_EMailPDF && mText.get_ID() == 0)
throw new AdempiereUserError ("@NotFound@: @R_MailText_ID@ - " + p_R_MailText_ID); throw new AdempiereUserError ("@NotFound@: @R_MailText_ID@ - " + p_R_MailText_ID);
// subject = mText.getMailHeader(); // subject = mText.getMailHeader();
} }
// //
MDunningRun run = new MDunningRun (getCtx(), p_C_DunningRun_ID, get_TrxName()); MDunningRun run = new MDunningRun (getCtx(), p_C_DunningRun_ID, get_TrxName());
if (run.get_ID() == 0) if (run.get_ID() == 0)
throw new AdempiereUserError ("@NotFound@: @C_DunningRun_ID@ - " + p_C_DunningRun_ID); throw new AdempiereUserError ("@NotFound@: @C_DunningRun_ID@ - " + p_C_DunningRun_ID);
MClient client = MClient.get(getCtx()); MClient client = MClient.get(getCtx());
int count = 0; int count = 0;
int errors = 0; int errors = 0;
MDunningRunEntry[] entries = run.getEntries(false); MDunningRunEntry[] entries = run.getEntries(false);
List<File> pdfList = new ArrayList<File>();
for (int i = 0; i < entries.length; i++) for (int i = 0; i < entries.length; i++)
{ {
MDunningRunEntry entry = entries[i]; MDunningRunEntry entry = entries[i];
// Print Format on Dunning Level // Print Format on Dunning Level
MDunningLevel level = new MDunningLevel (getCtx(), entry.getC_DunningLevel_ID(), get_TrxName()); MDunningLevel level = new MDunningLevel (getCtx(), entry.getC_DunningLevel_ID(), get_TrxName());
MPrintFormat format = null; MPrintFormat format = null;
if (level.getDunning_PrintFormat_ID() > 0) if (level.getDunning_PrintFormat_ID() > 0)
format = MPrintFormat.get (getCtx(), level.getDunning_PrintFormat_ID(), false); format = MPrintFormat.get (getCtx(), level.getDunning_PrintFormat_ID(), false);
if (p_IsOnlyIfBPBalance && entry.getAmt().signum() <= 0) if (p_IsOnlyIfBPBalance && entry.getAmt().signum() <= 0)
continue; continue;
if (p_PrintUnprocessedOnly && entry.isProcessed()) if (p_PrintUnprocessedOnly && entry.isProcessed())
continue; continue;
// To BPartner // To BPartner
MBPartner bp = new MBPartner (getCtx(), entry.getC_BPartner_ID(), get_TrxName()); MBPartner bp = new MBPartner (getCtx(), entry.getC_BPartner_ID(), get_TrxName());
if (bp.get_ID() == 0) if (bp.get_ID() == 0)
{ {
StringBuilder msglog = new StringBuilder("@NotFound@: @C_BPartner_ID@ ").append(entry.getC_BPartner_ID()); StringBuilder msglog = new StringBuilder("@NotFound@: @C_BPartner_ID@ ").append(entry.getC_BPartner_ID());
addLog (entry.get_ID(), null, null,msglog.toString() ); addLog (entry.get_ID(), null, null,msglog.toString() );
errors++; errors++;
continue; continue;
} }
@ -143,19 +153,20 @@ public class DunningPrint extends SvrProcess
{ {
if (to.get_ID() == 0) if (to.get_ID() == 0)
{ {
StringBuilder msglog = new StringBuilder("@NotFound@: @AD_User_ID@ - ").append(bp.getName()); StringBuilder msglog = new StringBuilder("@NotFound@: @AD_User_ID@ - ").append(bp.getName());
addLog (entry.get_ID(), null, null,msglog.toString()); addLog (entry.get_ID(), null, null,msglog.toString());
errors++; errors++;
continue; continue;
} }
else if (to.getEMail() == null || to.getEMail().length() == 0) else if (to.getEMail() == null || to.getEMail().length() == 0)
{ {
StringBuilder msglog = new StringBuilder("@NotFound@: @EMail@ - ").append(to.getName()); StringBuilder msglog = new StringBuilder("@NotFound@: @EMail@ - ").append(to.getName());
addLog (entry.get_ID(), null, null, msglog.toString()); addLog (entry.get_ID(), null, null, msglog.toString());
errors++; errors++;
continue; continue;
} }
} }
// query // query
MQuery query = new MQuery("C_Dunning_Header_v"); MQuery query = new MQuery("C_Dunning_Header_v");
query.addRestriction("C_DunningRunEntry_ID", MQuery.EQUAL, query.addRestriction("C_DunningRunEntry_ID", MQuery.EQUAL,
@ -164,23 +175,23 @@ public class DunningPrint extends SvrProcess
// Engine // Engine
PrintInfo info = new PrintInfo( PrintInfo info = new PrintInfo(
bp.getName(), bp.getName(),
MDunningRunEntry.Table_ID, MDunningRunEntry.Table_ID,
entry.getC_DunningRunEntry_ID(), entry.getC_DunningRunEntry_ID(),
entry.getC_BPartner_ID()); entry.getC_BPartner_ID());
StringBuilder msginfo = new StringBuilder().append(bp.getName()).append(", Amt=").append(entry.getAmt()); StringBuilder msginfo = new StringBuilder().append(bp.getName()).append(", Amt=").append(entry.getAmt());
info.setDescription(msginfo.toString()); info.setDescription(msginfo.toString());
ReportEngine re = null; ReportEngine re = null;
if (format != null) if (format != null)
re = new ReportEngine(getCtx(), format, query, info); re = new ReportEngine(getCtx(), format, query, info);
boolean printed = false; boolean printed = false;
if (p_EMailPDF) if (p_EMailPDF)
{ {
EMail email = client.createEMail(to.getEMail(), null, null); EMail email = client.createEMail(to.getEMail(), null, null);
if (!email.isValid()) if (!email.isValid())
{ {
StringBuilder msglog = new StringBuilder( StringBuilder msglog = new StringBuilder(
"@RequestActionEMailError@ Invalid EMail: ").append(to); "@RequestActionEMailError@ Invalid EMail: ").append(to);
addLog (entry.get_ID(), null, null,msglog.toString() ); addLog (entry.get_ID(), null, null,msglog.toString() );
errors++; errors++;
continue; continue;
} }
@ -196,56 +207,80 @@ public class DunningPrint extends SvrProcess
email.setMessageText (message); email.setMessageText (message);
} }
// //
if (re != null) { if (re != null) {
File attachment = re.getPDF(File.createTempFile("Dunning", ".pdf")); File attachment = re.getPDF(File.createTempFile("Dunning", ".pdf"));
StringBuilder msglog = new StringBuilder().append(to.toString()).append(" - ").append(attachment); StringBuilder msglog = new StringBuilder().append(to.toString()).append(" - ").append(attachment);
if (log.isLoggable(Level.FINE)) log.fine(msglog.toString()); if (log.isLoggable(Level.FINE)) log.fine(msglog.toString());
email.addAttachment(attachment); email.addAttachment(attachment);
} }
// //
String msg = email.send(); String msg = email.send();
MUserMail um = new MUserMail(mText, entry.getAD_User_ID(), email); MUserMail um = new MUserMail(mText, entry.getAD_User_ID(), email);
um.saveEx(); um.saveEx();
if (msg.equals(EMail.SENT_OK)) if (msg.equals(EMail.SENT_OK))
{ {
StringBuilder msglog = new StringBuilder() StringBuilder msglog = new StringBuilder()
.append(bp.getName()).append(" @RequestActionEMailOK@"); .append(bp.getName()).append(" @RequestActionEMailOK@");
addLog (entry.get_ID(), null, null,msglog.toString()); addLog (entry.get_ID(), null, null,msglog.toString());
count++; count++;
printed = true; printed = true;
} }
else else
{ {
StringBuilder msglog = new StringBuilder().append(bp.getName()).append(" @RequestActionEMailError@ ").append(msg); StringBuilder msglog = new StringBuilder().append(bp.getName()).append(" @RequestActionEMailError@ ").append(msg);
addLog (entry.get_ID(), null, null,msglog.toString() ); addLog (entry.get_ID(), null, null,msglog.toString() );
errors++; errors++;
} }
} }
else else
{ {
if (re != null) { if (re != null) {
re.print (); re.print ();
count++; count++;
printed = true; printed = true;
} pdfList.add(re.getPDF());
} }
if (printed)
{
entry.setProcessed (true); }
entry.save (); if (printed)
} {
entry.setProcessed (true);
} // for all dunning letters entry.save ();
if (errors==0) { }
run.setProcessed(true);
run.saveEx(); } // for all dunning letters
} if (errors==0) {
if (p_EMailPDF){ run.setProcessed(true);
StringBuilder msgreturn = new StringBuilder("@Sent@=").append(count).append(" - @Errors@=").append(errors); run.saveEx();
return msgreturn.toString(); }
} if (p_EMailPDF){
StringBuilder msgreturn = new StringBuilder("@Printed@=").append(count); StringBuilder msgreturn = new StringBuilder("@Sent@=").append(count).append(" - @Errors@=").append(errors);
return msgreturn.toString(); return msgreturn.toString();
}
if (pdfList.size() > 1) {
try {
File outFile = File.createTempFile("DunningPrint", ".pdf");
AEnv.mergePdf(pdfList, outFile);
Window win = new SimplePDFViewer(this.getName(), new FileInputStream(outFile));
win.setAttribute(Window.MODE_KEY, Window.MODE_HIGHLIGHTED);
SessionManager.getAppDesktop().showWindow(win, "center");
} catch (Exception e) {
log.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
} else if (pdfList.size() > 0) {
try {
Window win = new SimplePDFViewer(this.getName(), new FileInputStream(pdfList.get(0)));
win.setAttribute(Window.MODE_KEY, Window.MODE_HIGHLIGHTED);
SessionManager.getAppDesktop().showWindow(win, "center");
} catch (Exception e)
{
log.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
}
StringBuilder msgreturn = new StringBuilder("@Printed@=").append(count);
return msgreturn.toString();
} // doIt } // doIt
} // DunningPrint } // DunningPrint