KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > process > InvoicePrint


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.process;
15
16 import java.sql.*;
17 import java.math.*;
18 import java.io.*;
19
20 import org.compiere.model.*;
21 import org.compiere.print.*;
22 import org.compiere.util.*;
23
24 /**
25  * Print Invoices on Paperor send PDFs
26  *
27  * @author Jorg Janke
28  * @version $Id: InvoicePrint.java,v 1.8 2003/10/06 05:53:35 jjanke Exp $
29  */

30 public class InvoicePrint extends SvrProcess
31 {
32     /**
33      * Constructor
34      */

35     public InvoicePrint()
36     {
37         super();
38     } // InvoicePrint
39

40     private boolean m_emailPDF = false;
41     private int m_R_MailText_ID = 0;
42     private Timestamp m_dateInvoiced_From = null;
43     private Timestamp m_dateInvoiced_To = null;
44     private int m_C_BPartner_ID = 0;
45     private int m_C_Invoice_ID = 0;
46     private String JavaDoc m_DocumentNo_From = null;
47     private String JavaDoc m_DocumentNo_To = null;
48
49     /**
50      * Prepare - e.g., get Parameters.
51      */

52     protected void prepare()
53     {
54         ProcessInfoParameter[] para = getParameter();
55         for (int i = 0; i < para.length; i++)
56         {
57             String JavaDoc name = para[i].getParameterName();
58             if (para[i].getParameter() == null)
59                 ;
60             else if (name.equals("DateInvoiced"))
61             {
62                 m_dateInvoiced_From = ((Timestamp)para[i].getParameter());
63                 m_dateInvoiced_To = ((Timestamp)para[i].getParameter_To());
64             }
65             else if (name.equals("EmailPDF"))
66                 m_emailPDF = "Y".equals(para[i].getParameter());
67             else if (name.equals("R_MailText_ID"))
68                 m_R_MailText_ID = ((BigDecimal)para[i].getParameter()).intValue();
69             else if (name.equals("C_BPartner_ID"))
70                 m_C_BPartner_ID = ((BigDecimal)para[i].getParameter()).intValue();
71             else if (name.equals("C_Invoice_ID"))
72                 m_C_Invoice_ID = ((BigDecimal)para[i].getParameter()).intValue();
73             else if (name.equals("DocumentNo"))
74             {
75                 m_DocumentNo_From = (String JavaDoc)para[i].getParameter();
76                 m_DocumentNo_To = (String JavaDoc)para[i].getParameter_To();
77             }
78             else
79                 log.error("prepare - Unknown Parameter: " + name);
80         }
81         if (m_DocumentNo_From != null && m_DocumentNo_From.length() == 0)
82             m_DocumentNo_From = null;
83         if (m_DocumentNo_To != null && m_DocumentNo_To.length() == 0)
84             m_DocumentNo_To = null;
85     } // prepare
86

87     /**
88      * Perrform process.
89      * @return Message
90      * @throws Exception
91      */

92     protected String JavaDoc doIt() throws java.lang.Exception JavaDoc
93     {
94         // Need to have Template
95
if (m_emailPDF && m_R_MailText_ID == 0)
96             throw new Exception JavaDoc ("EMail requires Mail Template");
97         // Too broad selection
98
if (m_C_BPartner_ID == 0 && m_C_Invoice_ID == 0 && m_dateInvoiced_From == null && m_dateInvoiced_To == null
99             && m_DocumentNo_From == null && m_DocumentNo_To == null)
100             throw new Exception JavaDoc ("@RestrictSelection@");
101
102         // Get Info
103
StringBuffer JavaDoc sql = new StringBuffer JavaDoc (
104             "SELECT i.C_Invoice_ID,bp.AD_Language,c.IsMultiLingualDocument," // 1..3
105
+ " pf.Invoice_PrintFormat_ID, dt.DocumentCopies+bp.DocumentCopies," // 4..5
106
+ " bpc.EMail, i.DocumentNo," // 6..7
107
+ " c.SMTPHost,c.RequestEMail,c.RequestUser,RequestUserPW," // 8..11
108
+ " mt.MailHeader, mt.MailText, c.DocumentDir " // 12..14
109
+ "FROM C_Invoice i"
110             + " INNER JOIN C_BPartner bp ON (i.C_BPartner_ID=bp.C_BPartner_ID)"
111             + " LEFT OUTER JOIN AD_User bpc ON (i.AD_User_ID=bpc.AD_User_ID)"
112             + " INNER JOIN AD_Client c ON (i.AD_Client_ID=c.AD_Client_ID)"
113             + " INNER JOIN AD_PrintForm pf ON (c.AD_Client_ID=pf.AD_Client_ID)"
114             + " INNER JOIN C_DocType dt ON (i.C_DocType_ID=dt.C_DocType_ID)"
115             + " LEFT OUTER JOIN R_MailText mt ON (i.AD_Client_ID=mt.AD_Client_ID AND mt.R_MailText_ID=")
116             .append(m_R_MailText_ID)
117             .append(") WHERE ");
118         boolean needAnd = false;
119         if (m_C_Invoice_ID != 0)
120             sql.append("i.C_Invoice_ID=").append(m_C_Invoice_ID);
121         else
122         {
123             if (m_C_BPartner_ID != 0)
124             {
125                 sql.append ("i.C_BPartner_ID=").append (m_C_BPartner_ID);
126                 needAnd = true;
127             }
128             if (m_dateInvoiced_From != null && m_dateInvoiced_To != null)
129             {
130                 if (needAnd)
131                     sql.append(" AND ");
132                 sql.append("TRUNC(i.DateInvoiced) BETWEEN ")
133                     .append(DB.TO_DATE(m_dateInvoiced_From, true)).append(" AND ")
134                     .append(DB.TO_DATE(m_dateInvoiced_To, true));
135                 needAnd = true;
136             }
137             else if (m_dateInvoiced_From != null)
138             {
139                 if (needAnd)
140                     sql.append(" AND ");
141                 sql.append("TRUNC(i.DateInvoiced) >= ")
142                     .append(DB.TO_DATE(m_dateInvoiced_From, true));
143                 needAnd = true;
144             }
145             else if (m_dateInvoiced_To != null)
146             {
147                 if (needAnd)
148                     sql.append(" AND ");
149                 sql.append("TRUNC(i.DateInvoiced) <= ")
150                     .append(DB.TO_DATE(m_dateInvoiced_To, true));
151                 needAnd = true;
152             }
153             else if (m_DocumentNo_From != null && m_DocumentNo_To != null)
154             {
155                 if (needAnd)
156                     sql.append(" AND ");
157                 sql.append("i.DocumentNo BETWEEN ")
158                     .append(DB.TO_STRING(m_DocumentNo_From)).append(" AND ")
159                     .append(DB.TO_STRING(m_DocumentNo_To));
160             }
161             else if (m_DocumentNo_From != null)
162             {
163                 if (needAnd)
164                     sql.append(" AND ");
165                 if (m_DocumentNo_From.indexOf('%') == -1)
166                     sql.append("i.DocumentNo >= ")
167                         .append(DB.TO_STRING(m_DocumentNo_From));
168                 else
169                     sql.append("i.DocumentNo LIKE ")
170                         .append(DB.TO_STRING(m_DocumentNo_From));
171             }
172         }
173         log.debug(sql.toString());
174
175         MPrintFormat format = null;
176         int old_AD_PrintFormat_ID = -1;
177         int count = 0;
178         int errors = 0;
179         try
180         {
181             Statement stmt = DB.createStatement();
182             ResultSet rs = stmt.executeQuery(sql.toString());
183             while (rs.next())
184             {
185                 int C_Invoice_ID = rs.getInt(1);
186                 // Set Language when enabled
187
Language language = Language.getLanguage(); // Base Language
188
String JavaDoc AD_Language = rs.getString(2);
189                 if (AD_Language != null && "Y".equals(rs.getString(3)))
190                     language = Language.getLanguage(AD_Language);
191                 //
192
int AD_PrintFormat_ID = rs.getInt(4);
193                 int copies = rs.getInt(5);
194                 if (copies == 0)
195                     copies = 1;
196                 String JavaDoc to = rs.getString(6);
197                 String JavaDoc DocumentNo = rs.getString(7);
198                 //
199
String JavaDoc smtpHost = rs.getString(8);
200                 String JavaDoc from = rs.getString(9);
201                 String JavaDoc uid = rs.getString(10);
202                 String JavaDoc pwd = rs.getString(11);
203                 String JavaDoc subject = rs.getString(12);
204                 String JavaDoc message = rs.getString(13);
205                 String JavaDoc documentDir = rs.getString(14);
206                 if (documentDir == null || documentDir.length() == 0)
207                     documentDir = ".";
208
209                 //
210
if (m_emailPDF && to == null)
211                 {
212                     addLog (C_Invoice_ID, null, null, DocumentNo + " @RequestActionEMailNoTo@");
213                     errors++;
214                     continue;
215                 }
216                 if (AD_PrintFormat_ID == 0)
217                 {
218                     addLog (C_Invoice_ID, null, null, DocumentNo + " No Print Format");
219                     errors++;
220                     continue;
221                 }
222                 // Get Format & Data
223
if (AD_PrintFormat_ID != old_AD_PrintFormat_ID)
224                 {
225                     format = MPrintFormat.get (AD_PrintFormat_ID, false);
226                     old_AD_PrintFormat_ID = AD_PrintFormat_ID;
227                 }
228                 format.setLanguage(language);
229                 format.setTranslationLanguage(language);
230                 // query
231
MQuery query = new MQuery("C_Invoice_Header_v");
232                 query.addRestriction("C_Invoice_ID", MQuery.EQUAL, new Integer JavaDoc(C_Invoice_ID));
233
234                 // Engine
235
ReportEngine re = new ReportEngine(getCtx(), format, query);
236                 boolean printed = false;
237                 if (m_emailPDF)
238                 {
239                     EMail email = new EMail (smtpHost, from, to);
240                     if (!email.isValid())
241                     {
242                         addLog (C_Invoice_ID, null, null,
243                           DocumentNo + " @RequestActionEMailError@ Invalid EMail: " + to);
244                         errors++;
245                         continue;
246                     }
247
248                     email.setEMailUser(uid, pwd);
249                     email.setMessageHTML(subject, message);
250                     //
251
File attachment = re.getPDF(new File(MInvoice.getPDFFileName(documentDir, C_Invoice_ID)));
252                     log.debug("doIt - " + to + " - " + attachment);
253                     email.addAttachment(attachment);
254                     //
255
String JavaDoc msg = email.send();
256                     if (msg.equals(EMail.SENT_OK))
257                     {
258                         addLog (C_Invoice_ID, null, null,
259                           DocumentNo + " @RequestActionEMailOK@");
260                         count++;
261                         printed = true;
262                     }
263                     else
264                     {
265                         addLog (C_Invoice_ID, null, null,
266                           DocumentNo + " @RequestActionEMailError@ " + msg);
267                         errors++;
268                     }
269                 }
270                 else
271                 {
272                     re.print (false, copies, false, null);
273                     count++;
274                     printed = true;
275                 }
276                 // Print Confirm
277
if (printed)
278                 {
279                     StringBuffer JavaDoc sb = new StringBuffer JavaDoc ("UPDATE C_Invoice "
280                         + "SET DatePrinted=SysDate, IsPrinted='Y' WHERE C_Invoice_ID=")
281                         .append (C_Invoice_ID);
282                     int no = DB.executeUpdate(sb.toString());
283                 }
284             }
285             rs.close();
286             stmt.close();
287         }
288         catch (Exception JavaDoc e)
289         {
290             log.error("doIt - " + sql, e);
291             throw new Exception JavaDoc (e);
292         }
293         //
294
if (m_emailPDF)
295             return "@Sent@=" + count + " - @Errors@=" + errors;
296         return "@Printed@=" + count;
297     } // doIt
298

299 } // InvoicePrint
300
Popular Tags