1 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 30 public class InvoicePrint extends SvrProcess 31 { 32 35 public InvoicePrint() 36 { 37 super(); 38 } 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 m_DocumentNo_From = null; 47 private String m_DocumentNo_To = null; 48 49 52 protected void prepare() 53 { 54 ProcessInfoParameter[] para = getParameter(); 55 for (int i = 0; i < para.length; i++) 56 { 57 String 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 )para[i].getParameter(); 76 m_DocumentNo_To = (String )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 } 87 92 protected String doIt() throws java.lang.Exception 93 { 94 if (m_emailPDF && m_R_MailText_ID == 0) 96 throw new Exception ("EMail requires Mail Template"); 97 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 ("@RestrictSelection@"); 101 102 StringBuffer sql = new StringBuffer ( 104 "SELECT i.C_Invoice_ID,bp.AD_Language,c.IsMultiLingualDocument," + " pf.Invoice_PrintFormat_ID, dt.DocumentCopies+bp.DocumentCopies," + " bpc.EMail, i.DocumentNo," + " c.SMTPHost,c.RequestEMail,c.RequestUser,RequestUserPW," + " mt.MailHeader, mt.MailText, c.DocumentDir " + "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 Language language = Language.getLanguage(); String AD_Language = rs.getString(2); 189 if (AD_Language != null && "Y".equals(rs.getString(3))) 190 language = Language.getLanguage(AD_Language); 191 int AD_PrintFormat_ID = rs.getInt(4); 193 int copies = rs.getInt(5); 194 if (copies == 0) 195 copies = 1; 196 String to = rs.getString(6); 197 String DocumentNo = rs.getString(7); 198 String smtpHost = rs.getString(8); 200 String from = rs.getString(9); 201 String uid = rs.getString(10); 202 String pwd = rs.getString(11); 203 String subject = rs.getString(12); 204 String message = rs.getString(13); 205 String documentDir = rs.getString(14); 206 if (documentDir == null || documentDir.length() == 0) 207 documentDir = "."; 208 209 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 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 MQuery query = new MQuery("C_Invoice_Header_v"); 232 query.addRestriction("C_Invoice_ID", MQuery.EQUAL, new Integer (C_Invoice_ID)); 233 234 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 File attachment = re.getPDF(new File(MInvoice.getPDFFileName(documentDir, C_Invoice_ID))); 252 log.debug("doIt - " + to + " - " + attachment); 253 email.addAttachment(attachment); 254 String 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 if (printed) 278 { 279 StringBuffer sb = new StringBuffer ("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 e) 289 { 290 log.error("doIt - " + sql, e); 291 throw new Exception (e); 292 } 293 if (m_emailPDF) 295 return "@Sent@=" + count + " - @Errors@=" + errors; 296 return "@Printed@=" + count; 297 } 299 } | Popular Tags |