1 14 package org.compiere.model; 15 16 import java.util.*; 17 import java.sql.*; 18 import java.math.*; 19 import java.io.Serializable ; 20 21 import org.compiere.process.*; 22 import org.compiere.util.DB; 23 import org.compiere.util.Env; 24 import org.compiere.util.Log; 25 26 32 public class MOrder extends X_C_Order 33 { 34 39 public MOrder(Properties ctx, int C_Order_ID) 40 { 41 super (ctx, C_Order_ID); 42 if (C_Order_ID == 0) 44 { 45 setDocStatus(DOCSTATUS_Drafted); 46 setDocAction (DOCACTION_Process); 47 setDeliveryRule (DELIVERYRULE_Availability); 49 setFreightCostRule (FREIGHTCOSTRULE_FreightIncluded); 50 setInvoiceRule (INVOICERULE_Immediate); 51 setPaymentRule(PAYMENTRULE_OnCredit); 52 setPriorityRule (PRIORITYRULE_Medium); 53 setDeliveryViaRule (DELIVERYVIARULE_Pickup); 54 setIsDiscountPrinted (false); 56 setIsSelected (false); 57 setIsTaxIncluded (false); 58 setIsSOTrx (true); 59 setSendEMail (false); 60 setIsApproved(false); 62 setIsPrinted(false); 63 setIsCreditApproved(false); 64 setIsDelivered(false); 65 setIsInvoiced(false); 66 setIsTransferred(false); 67 setIsSelfService(false); 68 setProcessed(false); 70 setPosted(false); 71 72 setDateAcct (new Timestamp(System.currentTimeMillis())); 73 setDatePromised (new Timestamp(System.currentTimeMillis())); 74 setDateOrdered (new Timestamp(System.currentTimeMillis())); 75 76 setFreightAmt (Env.ZERO); 77 setChargeAmt (Env.ZERO); 78 setTotalLines (Env.ZERO); 79 setGrandTotal (Env.ZERO); 80 } 81 } 83 88 public MOrder (MProject project, String DocSubTypeSO) 89 { 90 this (project.getCtx(), 0); 91 setAD_Client_ID(project.getAD_Client_ID()); 92 setAD_Org_ID(project.getAD_Org_ID()); 93 setC_Campaign_ID(project.getC_Campaign_ID()); 94 setSalesRep_ID(project.getSalesRep_ID()); 95 setC_Project_ID(getC_Project_ID()); 97 setDescription(project.getName()); 98 Timestamp ts = project.getDateContract(); 99 if (ts != null) 100 setDateOrdered (ts); 101 ts = project.getDateFinish(); 102 if (ts != null) 103 setDatePromised (ts); 104 setC_BPartner_ID(project.getC_BPartner_ID()); 106 setC_BPartner_Location_ID(project.getC_BPartner_Location_ID()); 107 setBillTo_ID(project.getC_BPartner_Location_ID()); 108 setAD_User_ID(project.getAD_User_ID()); 109 setM_Warehouse_ID(project.getM_Warehouse_ID()); 111 setM_PriceList_ID(project.getM_PriceList_ID()); 112 setC_PaymentTerm_ID(project.getC_PaymentTerm_ID()); 113 if (DocSubTypeSO == null || DocSubTypeSO.length() == 0) 115 setC_DocTypeTarget_ID(DocSubTypeSO_OnCredit); 116 else 117 setC_DocTypeTarget_ID(DocSubTypeSO); 118 } 120 125 public MOrder (Properties ctx, ResultSet rs) 126 { 127 super (ctx, rs); 128 } 130 135 public void setClientOrg (int AD_Client_ID, int AD_Org_ID) 136 { 137 super.setClientOrg(AD_Client_ID, AD_Org_ID); 138 } 140 141 142 public static final String DocSubTypeSO_Standard = "SO"; 143 public static final String DocSubTypeSO_Quotation = "OB"; 144 public static final String DocSubTypeSO_Proposal = "ON"; 145 public static final String DocSubTypeSO_Prepay = "PR"; 146 public static final String DocSubTypeSO_POS = "WR"; 147 public static final String DocSubTypeSO_Warehouse = "WP"; 148 public static final String DocSubTypeSO_OnCredit = "WI"; 149 public static final String DocSubTypeSO_RMA = "RM"; 150 151 155 public void setC_DocTypeTarget_ID (String DocSubTypeSO_x) 156 { 157 String sql = "SELECT C_DocType_ID FROM C_DocType WHERE AD_Client_ID=? AND DocSubTypeSO=?"; 158 int C_DocType_ID = DB.getSQLValue(sql, getAD_Client_ID(), DocSubTypeSO_x); 159 if (C_DocType_ID <= 0) 160 log.error ("setC_DocTypeTarget_ID - Not found for AD_Client_ID=" + getAD_Client_ID () + ", SubType=" + DocSubTypeSO_x); 161 else 162 { 163 log.debug("setC_DocTypeTarget_ID - " + DocSubTypeSO_x); 164 setC_DocTypeTarget_ID (C_DocType_ID); 165 setIsSOTrx(true); 166 } 167 } 169 173 public void setC_DocTypeTarget_ID () 174 { 175 if (isSOTrx()) { 177 setC_DocTypeTarget_ID(DocSubTypeSO_Standard); 178 return; 179 } 180 String sql = "SELECT C_DocType_ID FROM C_DocType WHERE AD_Client_ID=? AND DocBaseType='POO'"; 182 int C_DocType_ID = DB.getSQLValue(sql, getAD_Client_ID()); 183 if (C_DocType_ID <= 0) 184 log.error ("setC_DocTypeTarget_ID - No POO found for AD_Client_ID=" + getAD_Client_ID ()); 185 else 186 { 187 log.debug("setC_DocTypeTarget_ID (PO) - " + C_DocType_ID); 188 setC_DocTypeTarget_ID (C_DocType_ID); 189 } 190 } 192 193 197 public void setDocumentNo (String DocumentNo) 198 { 199 super.setDocumentNo(DocumentNo); 200 } 202 203 208 public void setBPartner (MBPartner bp) 209 { 210 if (bp == null) 211 return; 212 213 setC_BPartner_ID(bp.getC_BPartner_ID()); 214 int ii = 0; 216 if (isSOTrx()) 217 ii = bp.getC_PaymentTerm_ID(); 218 else 219 ii = bp.getPO_PaymentTerm_ID(); 220 if (ii != 0) 221 setC_PaymentTerm_ID(ii); 222 if (isSOTrx()) 224 ii = bp.getM_PriceList_ID(); 225 else 226 ii = bp.getPO_PriceList_ID(); 227 if (ii != 0) 228 setM_PriceList_ID(ii); 229 String ss = bp.getDeliveryRule(); 231 if (ss != null) 232 setDeliveryRule(ss); 233 ss = bp.getDeliveryViaRule(); 234 if (ss != null) 235 setDeliveryViaRule(ss); 236 ss = bp.getInvoiceRule(); 238 if (ss != null) 239 setInvoiceRule(ss); 240 ss = bp.getPaymentRule(); 241 if (ss != null) 242 setPaymentRule(ss); 243 244 245 MBPartner_Location[] locs = bp.getLocations(); 247 if (locs != null) 248 { 249 for (int i = 0; i < locs.length; i++) 250 { 251 if (locs[i].isBillTo()) 252 setBillTo_ID(locs[i].getC_BPartner_Location_ID()); 253 if (locs[i].isShipTo()) 254 setC_BPartner_Location_ID(locs[i].getC_BPartner_Location_ID()); 255 } 256 if (getBillTo_ID() == 0 && locs.length > 0) 258 setBillTo_ID(locs[0].getC_BPartner_Location_ID()); 259 if (getC_BPartner_Location_ID() == 0 && locs.length > 0) 260 setC_BPartner_Location_ID(locs[0].getC_BPartner_Location_ID()); 261 } 262 if (getBillTo_ID() == 0) 263 log.error("MOrder.setBPartner - Has no Bill To Address: " + bp); 264 if (getC_BPartner_Location_ID() == 0) 265 log.error("MOrder.setBPartner - Has no Ship To Address: " + bp); 266 267 MUser[] contacts = bp.getContacts(); 269 if (contacts != null && contacts.length == 1) 270 setAD_User_ID(contacts[0].getAD_User_ID()); 271 } 273 276 private void setDefaults () 277 { 278 int AD_Client_ID = getAD_Client_ID(); 279 int AD_Org_ID = getAD_Org_ID(); 280 log.debug("setDefaults - " + "Client/Org=" + AD_Client_ID + "/" + AD_Org_ID); 281 282 if (getC_BPartner_ID() == 0) 284 setBPartner(MBPartner.getTemplate(getCtx(), getAD_Client_ID())); 285 if (getC_BPartner_Location_ID() == 0 || getBillTo_ID() == 0) 286 setBPartner(new MBPartner(getCtx(), getC_BPartner_ID())); 287 288 if (getM_PriceList_ID() == 0) 290 { 291 int ii = Env.getContextAsInt(getCtx(), "#M_PriceList_ID"); 292 if (ii != 0) 293 setM_PriceList_ID(ii); 294 else 295 { 296 String sql = "SELECT M_PriceList_ID FROM M_PriceList WHERE AD_Client_ID=? AND IsDefault='Y'"; 297 ii = DB.getSQLValue (sql, AD_Client_ID); 298 if (ii != 0) 299 setM_PriceList_ID (ii); 300 } 301 } 302 if (getC_Currency_ID() == 0) 304 { 305 String sql = "SELECT C_Currency_ID FROM M_PriceList WHERE M_PriceList_ID=?"; 306 int ii = DB.getSQLValue (sql, getM_PriceList_ID()); 307 if (ii != 0) 308 setC_Currency_ID (ii); 309 else 310 setC_Currency_ID(Env.getContextAsInt(getCtx(), "#C_Currency_ID")); 311 } 312 313 if (getSalesRep_ID() == 0) 315 { 316 int ii = Env.getContextAsInt(getCtx(), "#SalesRep_ID"); 317 if (ii != 0) 318 setSalesRep_ID (ii); 319 } 320 321 if (getC_DocType_ID() == 0) 323 setC_DocType_ID (0); if (getC_DocTypeTarget_ID() == 0) 325 setC_DocTypeTarget_ID(DocSubTypeSO_Standard); 326 327 if (getC_PaymentTerm_ID() == 0) 329 { 330 int ii = Env.getContextAsInt(getCtx(), "#C_PaymentTerm_ID"); 331 if (ii != 0) 332 setC_PaymentTerm_ID(ii); 333 else 334 { 335 String sql = "SELECT C_PaymentTerm_ID FROM C_PaymentTerm WHERE AD_Client_ID=? AND IsDefault='Y'"; 336 ii = DB.getSQLValue(sql, AD_Client_ID); 337 if (ii != 0) 338 setC_PaymentTerm_ID (ii); 339 } 340 } 341 if (getM_Warehouse_ID() == 0) 342 { 343 int ii = Env.getContextAsInt(getCtx(), "#M_Warehouse_ID"); 344 if (ii != 0) 345 setM_Warehouse_ID(ii); 346 } 347 } 349 354 public int copyLinesFrom (MOrder order) 355 { 356 if (isProcessed() || isPosted() || order == null) 357 return 0; 358 MOrderLine[] fromLines = order.getLines(); 359 int count = 0; 360 for (int i = 0; i < fromLines.length; i++) 361 { 362 MOrderLine line = new MOrderLine (this); 363 PO.copyValues(fromLines[i], line, getAD_Client_ID(), getAD_Org_ID()); 364 line.setC_Order_ID(getC_Order_ID()); 365 line.setOrder(order); 366 line.setC_OrderLine_ID(0); 367 line.setQtyDelivered(Env.ZERO); 369 line.setQtyInvoiced(Env.ZERO); 370 line.setDateDelivered(null); 371 line.setDateInvoiced(null); 372 line.setRef_OrderLine_ID(0); 373 line.setTax(); 374 if (line.save()) 375 count++; 376 } 377 if (fromLines.length != count) 378 log.error("copyLinesFrom - Line difference - From=" + fromLines.length + " <> Saved=" + count); 379 return count; 380 } 382 383 384 388 public String toString () 389 { 390 StringBuffer sb = new StringBuffer ("MOrder[") 391 .append(getID()).append("-").append(getDocumentNo()) 392 .append ("]"); 393 return sb.toString (); 394 } 396 400 public boolean save () 401 { 402 log.debug ("save"); 403 setDefaults(); 404 if (getDocumentNo() == null) 405 { 406 String DocumentNo = DB.getDocumentNo (getAD_Client_ID(), getC_DocTypeTarget_ID()); 407 if (DocumentNo == null || DocumentNo.length() == 0) 408 DocumentNo = DB.getDocumentNo(getAD_Client_ID(), "N", "C_Order"); 409 setDocumentNo (DocumentNo); 410 } 411 return super.save (); 412 } 414 415 416 421 public boolean process (String docAction) 422 { 423 setDocAction(docAction); 424 return process(); 425 } 427 431 public boolean process () 432 { 433 save(); 434 log.debug ("process - " + getDocAction()); 435 int AD_Process_ID = 104; MProcess pp = new MProcess (getCtx(), AD_Process_ID); 437 boolean ok = pp.process(getC_Order_ID()).isOK(); 438 load(); log.debug("process - ok=" + ok + " - GrandTotal=" + getGrandTotal()); 440 return ok; 441 } 443 447 public void setM_PriceList_ID (int M_PriceList_ID) 448 { 449 String sql = "SELECT M_PriceList_ID, C_Currency_ID " 450 + "FROM M_PriceList WHERE M_PriceList_ID=?"; 451 PreparedStatement pstmt = null; 452 try 453 { 454 pstmt = DB.prepareStatement(sql); 455 pstmt.setInt(1, M_PriceList_ID); 456 ResultSet rs = pstmt.executeQuery(); 457 if (rs.next()) 458 { 459 super.setM_PriceList_ID(rs.getInt(1)); 460 setC_Currency_ID (rs.getInt(2)); 461 } 462 rs.close(); 463 pstmt.close(); 464 pstmt = null; 465 } 466 catch (Exception e) 467 { 468 log.error("MOrder.setM_PriceList_ID", e); 469 } 470 finally 471 { 472 try 473 { 474 if (pstmt != null) 475 pstmt.close (); 476 } 477 catch (Exception e) 478 {} 479 pstmt = null; 480 } 481 } 483 484 485 489 public MOrderLine[] getLines() 490 { 491 return getLines (getC_Order_ID()); 492 } 493 494 499 public MOrderLine[] getLines(int C_Order_ID) 500 { 501 ArrayList list = new ArrayList(); 502 String sql = "SELECT * FROM C_OrderLine WHERE C_Order_ID=? ORDER BY Line DESC"; 503 PreparedStatement pstmt = null; 504 try 505 { 506 pstmt = DB.prepareStatement(sql); 507 pstmt.setInt(1, C_Order_ID); 508 ResultSet rs = pstmt.executeQuery(); 509 while (rs.next()) 510 { 511 list.add(new MOrderLine(getCtx(), rs)); 512 } 513 rs.close(); 514 pstmt.close(); 515 pstmt = null; 516 } 517 catch (Exception e) 518 { 519 log.error("getLines", e); 520 } 521 finally 522 { 523 try 524 { 525 if (pstmt != null) 526 pstmt.close (); 527 } 528 catch (Exception e) 529 {} 530 pstmt = null; 531 } 532 MOrderLine[] retValue = new MOrderLine[list.size()]; 534 list.toArray(retValue); 535 return retValue; 536 } 538 539 543 public MInvoice[] getInvoices() 544 { 545 ArrayList list = new ArrayList(); 546 String sql = "SELECT * FROM C_Invoice WHERE C_Order_ID=? ORDER BY Created DESC"; 547 PreparedStatement pstmt = null; 548 try 549 { 550 pstmt = DB.prepareStatement(sql); 551 pstmt.setInt(1, getC_Order_ID()); 552 ResultSet rs = pstmt.executeQuery(); 553 while (rs.next()) 554 { 555 list.add(new MInvoice(getCtx(), rs)); 556 } 557 rs.close(); 558 pstmt.close(); 559 pstmt = null; 560 } 561 catch (Exception e) 562 { 563 log.error("getInvoices", e); 564 } 565 finally 566 { 567 try 568 { 569 if (pstmt != null) 570 pstmt.close (); 571 } 572 catch (Exception e) 573 {} 574 pstmt = null; 575 } 576 MInvoice[] retValue = new MInvoice[list.size()]; 578 list.toArray(retValue); 579 return retValue; 580 } 582 586 public int getC_Invoice_ID() 587 { 588 int C_Invoice_ID = 0; 589 ArrayList list = new ArrayList(); 590 String sql = "SELECT C_Invoice_ID FROM C_Invoice " 591 + "WHERE C_Order_ID=? AND DocStatus IN ('CO','CL') " 592 + "ORDER BY Created DESC"; 593 PreparedStatement pstmt = null; 594 try 595 { 596 pstmt = DB.prepareStatement(sql); 597 pstmt.setInt(1, getC_Order_ID()); 598 ResultSet rs = pstmt.executeQuery(); 599 if (rs.next()) 600 C_Invoice_ID = rs.getInt(1); 601 rs.close(); 602 pstmt.close(); 603 pstmt = null; 604 } 605 catch (Exception e) 606 { 607 log.error("getC_Invoice_ID", e); 608 } 609 finally 610 { 611 try 612 { 613 if (pstmt != null) 614 pstmt.close (); 615 } 616 catch (Exception e) 617 {} 618 pstmt = null; 619 } 620 return C_Invoice_ID; 621 } 623 624 628 public MInOut[] getShipments() 629 { 630 ArrayList list = new ArrayList(); 631 String sql = "SELECT * FROM M_InOut WHERE C_Order_ID=? ORDER BY Created DESC"; 632 PreparedStatement pstmt = null; 633 try 634 { 635 pstmt = DB.prepareStatement(sql); 636 pstmt.setInt(1, getC_Order_ID()); 637 ResultSet rs = pstmt.executeQuery(); 638 while (rs.next()) 639 { 640 list.add(new MInOut(getCtx(), rs)); 641 } 642 rs.close(); 643 pstmt.close(); 644 pstmt = null; 645 } 646 catch (Exception e) 647 { 648 log.error("getShipments", e); 649 } 650 finally 651 { 652 try 653 { 654 if (pstmt != null) 655 pstmt.close (); 656 } 657 catch (Exception e) 658 {} 659 pstmt = null; 660 } 661 MInOut[] retValue = new MInOut[list.size()]; 663 list.toArray(retValue); 664 return retValue; 665 } 667 671 public String getCurrencyISO() 672 { 673 return MCurrency.getISO_Code (getCtx(), getC_Currency_ID()); 674 } 676 680 public String getDocStatusName() 681 { 682 return MRef_List.getListName(getCtx(), 131, getDocStatus()); 683 } 685 686 693 public static MOrder copyFrom (Properties ctx, int C_Order_ID, Timestamp dateDoc) 694 { 695 MOrder from = new MOrder (ctx, C_Order_ID); 696 if (from.getC_Order_ID() == 0) 697 throw new IllegalArgumentException ("From Invoice not found C_Invoice_ID=" + C_Order_ID); 698 MOrder to = new MOrder (ctx, 0); 700 PO.copyValues(from, to, from.getAD_Client_ID(), from.getAD_Org_ID()); 701 to.setC_Order_ID(0); 702 to.setValueNoCheck ("DocumentNo", null); 703 to.setDocStatus (DOCSTATUS_Drafted); to.setDocAction(DOCACTION_Process); 706 to.setC_DocTypeTarget_ID(to.getC_DocType_ID()); 707 to.setC_DocType_ID(0); 708 to.setIsSelected (false); 709 to.setDateOrdered (dateDoc); 710 to.setDateAcct (dateDoc); 711 to.setDatePromised (dateDoc); 712 to.setIsApproved (false); 714 to.setIsCreditApproved(false); 715 to.setC_Payment_ID(0); 716 to.setC_CashLine_ID(0); 717 to.setGrandTotal(Env.ZERO); 719 to.setTotalLines(Env.ZERO); 720 to.setIsDelivered(false); 722 to.setIsInvoiced(false); 723 to.setIsSelfService(false); 724 to.setDatePrinted(null); 725 to.setIsPrinted (false); 726 to.setIsTransferred (false); 727 to.setPosted (false); 728 to.setProcessed (false); 729 if (!to.save()) 731 throw new IllegalStateException ("Could not create Order"); 732 733 if (to.copyLinesFrom(from) == 0) 734 throw new IllegalStateException ("Could not create Order Lines"); 735 736 return to; 737 } 739 } | Popular Tags |