1 14 package org.compiere.model; 15 16 import java.util.*; 17 import java.sql.*; 18 import java.math.*; 19 import java.io.*; 20 21 import org.compiere.util.*; 22 import org.compiere.print.*; 23 import org.compiere.process.*; 24 25 26 32 public class MInvoice extends X_C_Invoice 33 { 34 39 public MInvoice (Properties ctx, int C_Invoice_ID) 40 { 41 super (ctx, C_Invoice_ID); 42 if (C_Invoice_ID == 0) 43 { 44 setDocStatus (DOCSTATUS_Drafted); setDocAction (DOCACTION_Complete); 46 setPaymentRule(PAYMENTRULE_OnCredit); 49 setDateInvoiced (new Timestamp (System.currentTimeMillis ())); 50 setDateAcct (new Timestamp (System.currentTimeMillis ())); 51 setChargeAmt (Env.ZERO); 53 setTotalLines (Env.ZERO); 54 setGrandTotal (Env.ZERO); 55 setIsSOTrx (true); 57 setIsTaxIncluded (false); 58 setIsApproved (false); 59 setIsDiscountPrinted (false); 60 setIsPaid (false); 61 setSendEMail (false); 62 setIsPrinted (false); 63 setIsTransferred (false); 64 setIsSelfService(false); 65 setPosted(false); 66 setProcessed (false); 67 } 68 } 70 75 public MInvoice (Properties ctx, ResultSet rs) 76 { 77 super (ctx, rs); 78 } 80 public static final String DocBaseType_ARI = "ARI"; 81 public static final String DocBaseType_API = "API"; 82 83 84 private BigDecimal m_openAmt = null; 85 86 87 private MInvoiceLine[] m_lines; 88 89 94 public void setClientOrg (int AD_Client_ID, int AD_Org_ID) 95 { 96 super.setClientOrg(AD_Client_ID, AD_Org_ID); 97 } 99 103 public void setBPartner (MBPartner bp) 104 { 105 if (bp == null) 106 return; 107 108 setC_BPartner_ID(bp.getC_BPartner_ID()); 109 int ii = 0; 111 if (isSOTrx()) 112 ii = bp.getC_PaymentTerm_ID(); 113 else 114 ii = bp.getPO_PaymentTerm_ID(); 115 if (ii != 0) 116 setC_PaymentTerm_ID(ii); 117 if (isSOTrx()) 119 ii = bp.getM_PriceList_ID(); 120 else 121 ii = bp.getPO_PriceList_ID(); 122 if (ii != 0) 123 setM_PriceList_ID(ii); 124 String ss = bp.getPaymentRule(); 126 if (ss != null) 127 setPaymentRule(ss); 128 129 130 MBPartner_Location[] locs = bp.getLocations(); 132 if (locs != null) 133 { 134 for (int i = 0; i < locs.length; i++) 135 { 136 if ((locs[i].isBillTo() && isSOTrx()) || (locs[i].isPayFrom() && !isSOTrx())) 137 setC_BPartner_Location_ID(locs[i].getC_BPartner_Location_ID()); 138 } 139 if (getC_BPartner_Location_ID() == 0 && locs.length > 0) 141 setC_BPartner_Location_ID(locs[0].getC_BPartner_Location_ID()); 142 } 143 if (getC_BPartner_Location_ID() == 0) 144 log.error("setBPartner - Has no To Address: " + bp); 145 146 MUser[] contacts = bp.getContacts(); 148 if (contacts != null && contacts.length > 0) setAD_User_ID(contacts[0].getAD_User_ID()); 150 } 152 156 public void setC_DocTypeTarget_ID (String DocBaseType) 157 { 158 int C_DocType_ID = 0; 159 String sql = "SELECT C_DocType_ID FROM C_DocType WHERE AD_Client_ID=? AND DocBaseType=? ORDER BY IsDefault DESC"; 161 PreparedStatement pstmt = null; 162 try 163 { 164 pstmt = DB.prepareStatement(sql); 165 pstmt.setInt(1, getAD_Client_ID()); 166 pstmt.setString(2, DocBaseType); 167 ResultSet rs = pstmt.executeQuery(); 168 if (rs.next()) 169 C_DocType_ID = rs.getInt(1); 170 rs.close(); 171 pstmt.close(); 172 pstmt = null; 173 } 174 catch (Exception e) 175 { 176 log.error("setC_DocTypeTarget_ID", e); 177 } 178 finally 179 { 180 try 181 { 182 if (pstmt != null) 183 pstmt.close (); 184 } 185 catch (Exception e) 186 {} 187 pstmt = null; 188 } 189 if (C_DocType_ID == 0) 190 log.error("setC_DocTypeTarget_ID - Not found for AC_Client_ID=" + getAD_Client_ID() + " - " + DocBaseType); 191 else 192 { 193 log.debug ("setC_DocTypeTarget_ID - " + DocBaseType); 194 setC_DocTypeTarget_ID (C_DocType_ID); 195 setIsSOTrx (DocBaseType_ARI.equals(DocBaseType)); 196 } 197 } 199 202 private void setDefaults () 203 { 204 log.debug("setDefaults"); 205 int AD_Client_ID = getAD_Client_ID(); 206 if (getC_BPartner_ID() == 0) 208 setBPartner(MBPartner.getTemplate(getCtx(), AD_Client_ID)); 209 if (getC_BPartner_Location_ID() == 0) 210 setBPartner(new MBPartner(getCtx(), getC_BPartner_ID())); 211 212 if (getM_PriceList_ID() == 0) 214 { 215 int ii = Env.getContextAsInt(getCtx(), "#M_PriceList_ID"); 216 if (ii != 0) 217 setM_PriceList_ID(ii); 218 else 219 { 220 String sql = "SLECT M_PriceList_ID FROM M_PriceList WHERE AD_Client_ID=? AND IsDefault='Y'"; 221 ii = DB.getSQLValue (sql, AD_Client_ID); 222 if (ii != 0) 223 setM_PriceList_ID (ii); 224 } 225 } 226 if (getC_Currency_ID() == 0) 228 { 229 String sql = "SLECT C_Currency_ID FROM M_PriceList WHERE M_PriceList_ID=?"; 230 int ii = DB.getSQLValue (sql, getM_PriceList_ID()); 231 if (ii != 0) 232 setC_Currency_ID (ii); 233 else 234 setC_Currency_ID(Env.getContextAsInt(getCtx(), "#C_Currency_ID")); 235 } 236 237 if (getSalesRep_ID() == 0) 239 { 240 int ii = Env.getContextAsInt(getCtx(), "#SalesRep_ID"); 241 if (ii != 0) 242 setSalesRep_ID (ii); 243 } 244 245 if (getC_DocType_ID() == 0) 247 setC_DocType_ID (0); if (getC_DocTypeTarget_ID() == 0) 249 setC_DocTypeTarget_ID(isSOTrx() ? DocBaseType_ARI : DocBaseType_API); 250 251 if (getC_PaymentTerm_ID() == 0) 253 { 254 int ii = Env.getContextAsInt(getCtx(), "#C_PaymentTerm_ID"); 255 if (ii != 0) 256 setC_PaymentTerm_ID (ii); 257 else 258 { 259 String sql = "SELECT C_PaymentTerm_ID FROM C_PaymentTerm WHERE AD_Client_ID=? AND IsDefault='Y'"; 260 ii = DB.getSQLValue(sql, AD_Client_ID); 261 if (ii != 0) 262 setC_PaymentTerm_ID (ii); 263 } 264 } 265 } 267 271 public MInvoiceLine[] getLines() 272 { 273 if (m_lines == null || m_lines.length == 0) 274 m_lines = getLines(getC_Invoice_ID()); 275 return m_lines; 276 } 278 283 private MInvoiceLine[] getLines (int C_Invoice_ID) 284 { 285 ArrayList list = new ArrayList(); 286 String sql = "SELECT * FROM C_InvoiceLine WHERE C_Invoice_ID=? ORDER BY Line"; 287 PreparedStatement pstmt = null; 288 try 289 { 290 pstmt = DB.prepareStatement(sql); 291 pstmt.setInt(1, C_Invoice_ID); 292 ResultSet rs = pstmt.executeQuery(); 293 while (rs.next()) 294 list.add(new MInvoiceLine(getCtx(), rs)); 295 rs.close(); 296 pstmt.close(); 297 pstmt = null; 298 } 299 catch (Exception e) 300 { 301 log.error("getLines", e); 302 } 303 finally 304 { 305 try 306 { 307 if (pstmt != null) 308 pstmt.close (); 309 } 310 catch (Exception e) 311 {} 312 pstmt = null; 313 } 314 315 MInvoiceLine[] lines = new MInvoiceLine[list.size()]; 317 list.toArray(lines); 318 return lines; 319 } 321 327 public int copyLinesFrom (MInvoice invoice) 328 { 329 if (isProcessed() || isPosted() || invoice == null) 330 return 0; 331 MInvoiceLine[] fromLines = invoice.getLines(); 332 int count = 0; 333 for (int i = 0; i < fromLines.length; i++) 334 { 335 MInvoiceLine line = new MInvoiceLine (invoice.getCtx(), 0); 336 PO.copyValues(fromLines[i], line, getAD_Client_ID(), getAD_Org_ID()); 337 line.setC_Invoice_ID(getC_Invoice_ID()); 338 line.setInvoice(invoice); 339 line.setC_InvoiceLine_ID(0); 340 line.setTax(); 341 if (line.save()) 342 count++; 343 } 344 if (fromLines.length != count) 345 log.error("copyLinesFrom - Line difference - From=" + fromLines.length + " <> Saved=" + count); 346 return count; 347 } 349 350 351 352 356 public boolean save () 357 { 358 log.debug ("save"); 359 setDefaults(); 360 if (getDocumentNo() == null) 361 { 362 String DocumentNo = DB.getDocumentNo (getAD_Client_ID (), getC_DocTypeTarget_ID ()); 363 if (DocumentNo == null || DocumentNo.length() == 0) 364 DocumentNo = DB.getDocumentNo(getAD_Client_ID(), "N", "C_Invoice"); 365 setDocumentNo (DocumentNo); 366 } 367 return super.save (); 368 } 370 375 public boolean process (String docAction) 376 { 377 setDocAction(docAction); 378 return process(); 379 } 381 385 public boolean process () 386 { 387 save(); 388 log.debug ("process - " + getDocAction()); 389 int AD_Process_ID = 111; MProcess pp = new MProcess (getCtx(), AD_Process_ID); 391 boolean ok = pp.process(getC_Invoice_ID()).isOK(); 392 load(); log.debug("process - ok=" + ok + " - GrandTotal=" + getGrandTotal()); 394 return ok; 395 } 397 public String toString () 398 { 399 StringBuffer sb = new StringBuffer ("MInvoice[") 400 .append(getID()) 401 .append ("]"); 402 return sb.toString (); 403 } 404 405 409 public void setM_PriceList_ID (int M_PriceList_ID) 410 { 411 String sql = "SELECT M_PriceList_ID, C_Currency_ID " 412 + "FROM M_PriceList WHERE M_PriceList_ID=?"; 413 PreparedStatement pstmt = null; 414 try 415 { 416 pstmt = DB.prepareStatement(sql); 417 pstmt.setInt(1, M_PriceList_ID); 418 ResultSet rs = pstmt.executeQuery(); 419 if (rs.next()) 420 { 421 super.setM_PriceList_ID (rs.getInt(1)); 422 setC_Currency_ID (rs.getInt(2)); 423 } 424 rs.close(); 425 pstmt.close(); 426 pstmt = null; 427 } 428 catch (Exception e) 429 { 430 log.error("setM_PriceList_ID", e); 431 } 432 finally 433 { 434 try 435 { 436 if (pstmt != null) 437 pstmt.close (); 438 } 439 catch (Exception e) 440 {} 441 pstmt = null; 442 } 443 } 445 446 450 public BigDecimal getOpenAmt () 451 { 452 if (isPaid()) 453 return Env.ZERO; 454 if (m_openAmt != null) 455 return m_openAmt; 456 String sql = "SELECT C_Invoice_Open(?) FROM DUAL"; 458 PreparedStatement pstmt = null; 459 try 460 { 461 pstmt = DB.prepareStatement(sql); 462 pstmt.setInt(1, getC_Invoice_ID()); 463 ResultSet rs = pstmt.executeQuery(); 464 if (rs.next()) 465 m_openAmt = rs.getBigDecimal(1); 466 rs.close(); 467 pstmt.close(); 468 pstmt = null; 469 } 470 catch (Exception e) 471 { 472 log.error("getOpenAmt", e); 473 } 474 finally 475 { 476 try 477 { 478 if (pstmt != null) 479 pstmt.close (); 480 } 481 catch (Exception e) 482 {} 483 pstmt = null; 484 } 485 return m_openAmt; 487 } 489 490 495 public void setDocumentNo (String DocumentNo) 496 { 497 super.setDocumentNo(DocumentNo);; 498 } 500 504 public void setIsSOTrx (boolean IsSOTrx) 505 { 506 super.setIsSOTrx(IsSOTrx); 507 } 509 513 public String getDocStatusName() 514 { 515 return MRef_List.getListName(getCtx(), 131, getDocStatus()); 516 } 518 519 520 521 525 public File getPDF () 526 { 527 return getPDF(null); 528 } 530 535 public File getPDF (File file) 536 { 537 int AD_PrintFormat_ID = 0; 538 Language language = Language.getLanguage(); 540 String sql = "SELECT bp.AD_Language, c.IsMultiLingualDocument," 541 + " pf.Invoice_PrintFormat_ID, c.DocumentDir " 542 + "FROM C_Invoice i" 543 + " INNER JOIN C_BPartner bp ON (i.C_BPartner_ID=bp.C_BPartner_ID)" 544 + " INNER JOIN AD_Client c ON (i.AD_Client_ID=c.AD_Client_ID)" 545 + " INNER JOIN AD_PrintForm pf ON (c.AD_Client_ID=pf.AD_Client_ID) " 546 + "WHERE i.C_Invoice_ID=?"; 547 PreparedStatement pstmt = null; 548 try 549 { 550 pstmt = DB.prepareStatement(sql); 551 pstmt.setInt(1, getC_Invoice_ID()); 552 ResultSet rs = pstmt.executeQuery(); 553 if (rs.next()) 554 { 555 String AD_Language = rs.getString(1); 556 if (AD_Language != null && "Y".equals(rs.getString(2))) 557 language = Language.getLanguage(AD_Language); 558 AD_PrintFormat_ID = rs.getInt(3); 560 if (file == null) 562 file = new File (getPDFFileName(rs.getString(4))); 563 } 564 rs.close(); 565 pstmt.close(); 566 pstmt = null; 567 } 568 catch (Exception e) 569 { 570 Log.error("getPDF", e); 571 } 572 finally 573 { 574 try 575 { 576 if (pstmt != null) 577 pstmt.close (); 578 } 579 catch (Exception e) 580 {} 581 pstmt = null; 582 } 583 if (AD_PrintFormat_ID == 0) 584 { 585 log.error("getPDF - No PrintFormat found"); 586 return null; 587 } 588 589 MPrintFormat format = MPrintFormat.get (AD_PrintFormat_ID, false); 591 format.setLanguage(language); 592 format.setTranslationLanguage(language); 593 594 MQuery query = new MQuery("C_Invoice_Header_v"); 596 query.addRestriction("C_Invoice_ID", MQuery.EQUAL, new Integer (getC_Invoice_ID())); 597 598 ReportEngine re = new ReportEngine(getCtx(), format, query); 600 return re.getPDF(file); 601 } 603 608 public String getPDFFileName (String documentDir) 609 { 610 return getPDFFileName(documentDir, getC_Invoice_ID()); 611 } 613 619 public static String getPDFFileName (String documentDir, int C_Invoice_ID) 620 { 621 StringBuffer sb = new StringBuffer (documentDir); 622 if (sb.length() == 0) 623 sb.append("."); 624 if (!sb.toString().endsWith(File.separator)) 625 sb.append(File.separator); 626 sb.append("C_Invoice_ID_") 627 .append(C_Invoice_ID) 628 .append(".pdf"); 629 return sb.toString(); 630 } 632 636 public String getCurrencyISO() 637 { 638 return MCurrency.getISO_Code (getCtx(), getC_Currency_ID()); 639 } 641 648 public static MInvoice copyFrom (Properties ctx, int C_Invoice_ID, Timestamp dateDoc) 649 { 650 MInvoice from = new MInvoice (ctx, C_Invoice_ID); 651 if (from.getC_Invoice_ID() == 0) 652 throw new IllegalArgumentException ("From Invoice not found C_Invoice_ID=" + C_Invoice_ID); 653 MInvoice to = new MInvoice (ctx, 0); 655 PO.copyValues(from, to, from.getAD_Client_ID(), from.getAD_Org_ID()); 656 to.setC_Invoice_ID(0); 657 to.setValueNoCheck ("DocumentNo", null); 658 to.setDocStatus (DOCSTATUS_Drafted); to.setDocAction(DOCACTION_Process); 661 to.setC_DocTypeTarget_ID(to.getC_DocType_ID()); 662 to.setDateInvoiced (dateDoc); 663 to.setDateAcct (dateDoc); 664 to.setIsApproved (false); 666 to.setC_Payment_ID(0); 667 to.setC_CashLine_ID(0); 668 to.setIsPaid (false); 669 to.setGrandTotal(Env.ZERO); 672 to.setTotalLines(Env.ZERO); 673 to.setDatePrinted(null); 675 to.setIsPrinted (false); 676 to.setIsTransferred (false); 677 to.setPosted (false); 678 to.setProcessed (false); 679 to.setC_Order_ID(0); 681 to.setIsSelfService(false); 682 if (!to.save()) 683 throw new IllegalStateException ("Could not create Invoice"); 684 685 if (to.copyLinesFrom(from) == 0) 686 throw new IllegalStateException ("Could not create Invoice Lines"); 687 688 return to; 689 } 691 } | Popular Tags |