1 14 package org.compiere.model; 15 16 import java.math.BigDecimal ; 17 import java.sql.PreparedStatement ; 18 import java.sql.ResultSet ; 19 import java.sql.SQLException ; 20 import java.sql.Timestamp ; 21 import java.util.Properties ; 22 23 import org.compiere.util.*; 24 25 34 public final class CalloutSystem extends CalloutEngine 35 { 36 46 public String Conversion_Rate (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 47 { 48 if (isCalloutActive() || value == null) return ""; 50 setCalloutActive(true); 51 52 BigDecimal rate1 = (BigDecimal )value; 53 BigDecimal rate2 = Env.ZERO; 54 BigDecimal one = new BigDecimal (1.0); 55 56 if (rate1.doubleValue() != 0.0) rate2 = one.divide(rate1, 12, BigDecimal.ROUND_HALF_UP); 58 if (mField.getColumnName().equals("MultiplyRate")) 60 mTab.setValue("DivideRate", rate2); 61 else 62 mTab.setValue("MultiplyRate", rate2); 63 log.info(mField.getColumnName() + "=" + rate1 + " => " + rate2); 64 setCalloutActive(false); 65 return ""; 66 } 68 69 70 75 public String InOut_DocType (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 76 { 77 Integer C_DocType_ID = (Integer )value; 78 if (C_DocType_ID == null || C_DocType_ID.intValue() == 0) 79 return ""; 80 81 try 82 { 83 Env.setContext(ctx, WindowNo, "C_DocTypeTarget_ID", C_DocType_ID.intValue()); 84 85 String SQL = "SELECT d.DocBaseType, d.IsDocNoControlled, s.CurrentNext " 86 + "FROM C_DocType d, AD_Sequence s " 87 + "WHERE C_DocType_ID=?" + " AND d.DocNoSequence_ID=s.AD_Sequence_ID(+)"; 89 90 PreparedStatement pstmt = DB.prepareStatement(SQL); 91 pstmt.setInt(1, C_DocType_ID.intValue()); 92 ResultSet rs = pstmt.executeQuery(); 93 if (rs.next()) 94 { 95 String DocBaseType = rs.getString("DocBaseType"); 97 if (DocBaseType.equals("MMS")) mTab.setValue("MovementType", "C-"); else if (DocBaseType.equals("MMR")) mTab.setValue("MovementType", "V+"); 102 if (rs.getString("IsDocNoControlled").equals("Y")) 104 mTab.setValue("DocumentNo", "<" + rs.getString("CurrentNext") + ">"); 105 } 106 rs.close(); 107 pstmt.close(); 108 } 109 catch (SQLException e) 110 { 111 log.error("InOut_DocType", e); 112 return e.getLocalizedMessage(); 113 } 114 return ""; 115 } 117 118 123 public String InOut_BPartner (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 124 { 125 Integer C_BPartner_ID = (Integer )value; 126 if (C_BPartner_ID == null || C_BPartner_ID.intValue() == 0) 127 return ""; 128 129 String SQL = "SELECT p.AD_Language,p.C_PaymentTerm_ID," 130 + "p.M_PriceList_ID,p.PaymentRule,p.POReference," 131 + "p.SO_Description,p.IsDiscountPrinted," 132 + "p.SO_CreditLimit-p.SO_CreditUsed AS CreditAvailable," 133 + "l.C_BPartner_Location_ID,c.AD_User_ID " 134 + "FROM C_BPartner p, C_BPartner_Location l, AD_User c " 135 + "WHERE p.C_BPartner_ID=l.C_BPartner_ID(+)" 136 + " AND p.C_BPartner_ID=c.C_BPartner_ID(+)" 137 + " AND p.C_BPartner_ID=?"; 139 try 140 { 141 PreparedStatement pstmt = DB.prepareStatement(SQL); 142 pstmt.setInt(1, C_BPartner_ID.intValue()); 143 ResultSet rs = pstmt.executeQuery(); 144 BigDecimal bd; 145 if (rs.next()) 146 { 147 Integer ii = new Integer (rs.getInt("C_BPartner_Location_ID")); 149 if (rs.wasNull()) 150 mTab.setValue("C_BPartner_Location_ID", null); 151 else 152 mTab.setValue("C_BPartner_Location_ID", ii); 153 ii = new Integer (rs.getInt("AD_User_ID")); 155 if (rs.wasNull()) 156 mTab.setValue("AD_User_ID", null); 157 else 158 mTab.setValue("AD_User_ID", ii); 159 160 double CreditAvailable = rs.getDouble("CreditAvailable"); 162 if (!rs.wasNull() && CreditAvailable < 0) 163 mTab.fireDataStatusEEvent("CreditLimitOver", 164 DisplayType.getNumberFormat(DisplayType.Amount).format(CreditAvailable)); 165 } 166 rs.close(); 167 pstmt.close(); 168 } 169 catch (SQLException e) 170 { 171 log.error("InOut_BPartner", e); 172 return e.getLocalizedMessage(); 173 } 174 175 return ""; 176 } 178 181 public String InOutLine_Product (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 182 { 183 Integer M_Product_ID = (Integer )value; 184 if (M_Product_ID == null || M_Product_ID.intValue() == 0) 185 return ""; 186 if (Env.getContextAsInt(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "M_Product_ID") == M_Product_ID.intValue() 188 && Env.getContextAsInt(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "M_AttributeSetInstance_ID") != 0) 189 mTab.setValue("M_AttributeSetInstance_ID", new Integer (Env.getContextAsInt(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "M_AttributeSetInstance_ID"))); 190 else 191 mTab.setValue("M_AttributeSetInstance_ID", null); 192 193 int M_Warehouse_ID = Env.getContextAsInt(ctx, WindowNo, "M_Warehouse_ID"); 194 boolean IsSOTrx = "Y".equals(Env.getContext(ctx, WindowNo, "IsSOTrx")); 195 if (IsSOTrx) 196 return ""; 197 198 String SQL = "SELECT p.M_Locator_ID " 199 + "FROM M_Product p INNER JOIN M_Locator l ON (p.M_Locator_ID=l.M_Locator_ID) " 200 + "WHERE p.IsActive='Y' AND l.IsActive='Y'" 201 + " AND p.M_Product_ID=? AND l.M_Warehouse_ID=?"; int M_Locator_ID = 0; 203 try 204 { 205 PreparedStatement pstmt = DB.prepareStatement(SQL); 206 pstmt.setInt(1, M_Product_ID.intValue()); 207 pstmt.setInt(2, M_Warehouse_ID); 208 ResultSet rs = pstmt.executeQuery(); 209 if (rs.next()) 210 { 211 M_Locator_ID = rs.getInt(1); 212 if (M_Locator_ID != 0) 213 mTab.setValue("M_Locator_ID", new Integer (M_Locator_ID)); 214 } 215 rs.close(); 216 pstmt.close(); 217 } 218 catch (SQLException e) 219 { 220 log.error("InOutLine_Product", e); 221 return e.getLocalizedMessage(); 222 } 223 if (M_Locator_ID == 0) 224 log.debug("InOutLine_Product - No Locator for M_Product_ID=" + M_Product_ID + " and M_Warehouse_ID=" + M_Warehouse_ID); 225 return ""; 226 } 228 229 230 235 public String BankStmt_Amount (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 236 { 237 if (isCalloutActive()) 238 return ""; 239 setCalloutActive(true); 240 241 BigDecimal stmt = (BigDecimal )mTab.getValue("StmtAmt"); 243 if (stmt == null) 244 stmt = Env.ZERO; 245 BigDecimal trx = (BigDecimal )mTab.getValue("TrxAmt"); 246 if (trx == null) 247 trx = Env.ZERO; 248 BigDecimal bd = stmt.subtract(trx); 249 250 if (mField.getColumnName().equals("ChargeAmt")) 252 { 253 BigDecimal charge = (BigDecimal )value; 254 if (charge == null) 255 charge = Env.ZERO; 256 bd = bd.subtract(charge); 257 mTab.setValue("InterestAmt", bd); 259 } 260 else 262 { 263 BigDecimal interest = (BigDecimal )mTab.getValue("InterestAmt"); 264 if (interest == null) 265 interest = Env.ZERO; 266 bd = bd.subtract(interest); 267 mTab.setValue("ChargeAmt", bd); 269 } 270 setCalloutActive(false); 271 return ""; 272 } 274 275 279 public String BankStmt_Payment (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 280 { 281 Integer C_Payment_ID = (Integer )value; 282 if (C_Payment_ID == null || C_Payment_ID.intValue() == 0) 283 return ""; 284 285 String sql = "SELECT PayAmt FROM C_Payment_v WHERE C_Payment_ID=?"; try 287 { 288 PreparedStatement pstmt = DB.prepareStatement(sql); 289 pstmt.setInt(1, C_Payment_ID.intValue()); 290 ResultSet rs = pstmt.executeQuery(); 291 if (rs.next()) 292 { 293 BigDecimal bd = rs.getBigDecimal(1); 294 mTab.setValue("TrxAmt", bd); 295 } 296 rs.close(); 297 pstmt.close(); 298 } 299 catch (SQLException e) 300 { 301 log.error("BankStmt_Payment", e); 302 return e.getLocalizedMessage(); 303 } 304 BankStmt_Amount (ctx, WindowNo, mTab, mField, value); 306 return ""; 307 } 309 310 311 322 public String Payment_Invoice (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 323 { 324 if (isCalloutActive()) return ""; 326 setCalloutActive(true); 327 328 Integer C_Invoice_ID = (Integer )value; 329 if (C_Invoice_ID == null || C_Invoice_ID.intValue() == 0) 330 { 331 Env.setContext(ctx, WindowNo, "InvCurrency_ID", null); 332 Env.setContext(ctx, WindowNo, "InvTotalAmt", null); 333 setCalloutActive(false); 334 return ""; 335 } 336 337 Timestamp ts = (Timestamp )mTab.getValue("DateTrx"); 339 if (ts == null) 340 ts = new Timestamp (System.currentTimeMillis()); 341 boolean isSOTrx = true; 342 String sql = "SELECT C_BPartner_ID,C_Currency_ID," + " C_Invoice_Open(C_Invoice_ID)," + " C_Invoice_Discount(C_Invoice_ID,?), IsSOTrx " + "FROM C_Invoice WHERE C_Invoice_ID=?"; 347 try 348 { 349 PreparedStatement pstmt = DB.prepareStatement(sql); 350 pstmt.setTimestamp(1, ts); 351 pstmt.setInt(2, C_Invoice_ID.intValue()); 352 ResultSet rs = pstmt.executeQuery(); 353 if (rs.next()) 354 { 355 mTab.setValue("C_BPartner_ID", new Integer (rs.getInt(1))); 356 int C_Currency_ID = rs.getInt(2); 357 mTab.setValue("C_Currency_ID", new Integer (C_Currency_ID)); 358 Env.setContext(ctx, WindowNo, "InvCurrency_ID", C_Currency_ID); 359 BigDecimal InvoiceOpen = rs.getBigDecimal(3); 361 if (InvoiceOpen == null) 362 InvoiceOpen = Env.ZERO; 363 Env.setContext(ctx, WindowNo, "InvTotalAmt", InvoiceOpen.toString()); 364 BigDecimal DiscountAmt = rs.getBigDecimal(4); 365 if (DiscountAmt == null) 366 DiscountAmt = Env.ZERO; 367 mTab.setValue("PayAmt", InvoiceOpen.subtract(DiscountAmt)); 368 mTab.setValue("DiscountAmt", DiscountAmt); 369 mTab.setValue("WriteOffAmt", Env.ZERO); 370 Env.setContext(ctx, WindowNo, "C_Invoice_ID", C_Invoice_ID.toString()); 372 mTab.setValue("C_Invoice_ID", C_Invoice_ID); 373 isSOTrx = "Y".equals(rs.getString(5)); 374 } 375 rs.close(); 376 pstmt.close(); 377 } 378 catch (SQLException e) 379 { 380 log.error("Payment_Invoice", e); 381 setCalloutActive(false); 382 return e.getLocalizedMessage(); 383 } 384 385 setCalloutActive(false); 386 return Payment_DocType(ctx, WindowNo, mTab, mField, value); 387 } 389 390 394 public String Payment_DocType (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 395 { 396 int C_Invoice_ID = Env.getContextAsInt(ctx, WindowNo, "C_Invoice_ID"); 397 int C_DocType_ID = Env.getContextAsInt(ctx, WindowNo, "C_DocType_ID"); 398 log.debug("Payment_DocType - C_Invoice_ID=" + C_Invoice_ID + ", C_DocType_ID=" + C_DocType_ID); 402 if (C_Invoice_ID == 0 || C_DocType_ID == 0) 403 return ""; 404 405 String sql = "SELECT CASE WHEN (i.IsSOTrx='Y' AND dt.DocBaseType='ARR')" 406 + " OR (i.IsSOTrx='N' AND dt.DocBaseType='APP') THEN 'Y'" 407 + " ELSE 'N' END CASE " 408 + "FROM C_Invoice i, C_DocType dt " 409 + "WHERE i.C_Invoice_ID=? AND dt.C_DocType_ID=?"; 410 String result = null; 411 PreparedStatement pstmt = null; 412 try 413 { 414 pstmt = DB.prepareStatement(sql); 415 pstmt.setInt(1, C_Invoice_ID); 416 pstmt.setInt(2, C_DocType_ID); 417 ResultSet rs = pstmt.executeQuery(); 418 if (rs.next()) 419 result = rs.getString(1); 420 rs.close(); 421 pstmt.close(); 422 pstmt = null; 423 } 424 catch (Exception e) 425 { 426 log.error("Payment_DocType", e); 427 } 428 finally 429 { 430 try 431 { 432 if (pstmt != null) 433 pstmt.close (); 434 } 435 catch (Exception e) 436 {} 437 pstmt = null; 438 } 439 440 if ("N".equals(result)) 441 return "PaymentDocTypeInvoiceInconsistent"; 442 443 return ""; 444 } 446 447 451 public String Payment_No_Verify (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 452 { 453 String colName = mField.getColumnName(); 454 455 if (colName.equals("CreditCardNumber")) 457 { 458 String cc = (String )value; 459 if (cc == null || cc.length() == 0) 460 return ""; 461 return MPayment.validateCreditCardNumber(cc); 462 } 463 if (colName.equals("RoutingNo")) 464 { 465 String rt = (String )value; 466 if (rt == null || rt.length() == 0) 467 return ""; 468 return MPayment.validateRoutingNo(rt); 469 } 470 471 log.warn("No Verification available"); 472 return ""; 473 } 475 476 481 public String Payment_Amounts (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value, Object oldValue) 482 { 483 if (isCalloutActive()) return ""; 485 if (Env.getContextAsInt(ctx, WindowNo, "C_Payment_ID") == 0 487 && Env.getContextAsInt(ctx, WindowNo, "C_BPartner_ID") == 0 488 && Env.getContextAsInt(ctx, WindowNo, "C_Invoice_ID") == 0) 489 return ""; 490 setCalloutActive(true); 491 492 String total = Env.getContext(ctx, WindowNo, "InvTotalAmt"); 494 if (total == null || total.length() == 0) 495 { 496 setCalloutActive(false); 497 return "InvoiceReSelect"; 498 } 499 BigDecimal InvTotalAmt = new BigDecimal (total); 500 int InvCurrency_ID = Env.getContextAsInt (ctx, WindowNo, "InvCurrency_ID"); 501 String colName = mField.getColumnName(); 503 if (colName.equals("IsOverUnderPayment")) { 505 mTab.setValue("OverUnderAmt", Env.ZERO); 506 setCalloutActive(false); 507 return ""; 508 } 509 BigDecimal PayAmt = (BigDecimal )mTab.getValue("PayAmt"); 511 BigDecimal DiscountAmt = (BigDecimal )mTab.getValue("DiscountAmt"); 512 BigDecimal WriteOffAmt = (BigDecimal )mTab.getValue("WriteOffAmt"); 513 BigDecimal OverUnderAmt = (BigDecimal )mTab.getValue("OverUnderAmt"); 514 int C_Currency_ID = ((Integer )mTab.getValue("C_Currency_ID")).intValue(); 515 Timestamp ConvDate = (Timestamp )mTab.getValue("DateTrx"); 516 log.debug("Invoice=" + InvTotalAmt + " in Currency=" + InvCurrency_ID 517 + " - Pay=" + PayAmt + ", Discount=" + DiscountAmt 518 + ", WriteOff=" + WriteOffAmt + ", OverUnderAmt=" + OverUnderAmt 519 + " in Currency=" + C_Currency_ID + " on " + ConvDate); 520 int AD_Client_ID = Env.getContextAsInt(ctx, WindowNo, "AD_Client_ID"); 521 int AD_Org_ID = Env.getContextAsInt(ctx, WindowNo, "AD_Org_ID"); 522 523 if (colName.equals("C_Currency_ID")) 525 { 526 int oldCur = ((Integer )oldValue).intValue(); 527 log.debug("FromCurrency=" + oldCur + ", ToCurrency=" + C_Currency_ID + ", Date=" + ConvDate); 528 BigDecimal CurrencyRate = DB.getConvesionRate(oldCur, C_Currency_ID, ConvDate, null, AD_Client_ID, AD_Org_ID); 529 log.debug("Rate=" + CurrencyRate); 530 if (CurrencyRate == null || CurrencyRate.compareTo(Env.ZERO) == 0) 531 { 532 mTab.setValue("C_Currency_ID", new Integer (InvCurrency_ID)); 533 setCalloutActive(false); 534 return "NoCurrencyConversion"; 535 } 536 PayAmt = PayAmt.multiply(CurrencyRate).setScale(2, BigDecimal.ROUND_HALF_UP); 537 mTab.setValue("PayAmt", PayAmt); 538 DiscountAmt = DiscountAmt.multiply(CurrencyRate).setScale(2, BigDecimal.ROUND_HALF_UP); 539 mTab.setValue("DiscountAmt", DiscountAmt); 540 WriteOffAmt = WriteOffAmt.multiply(CurrencyRate).setScale(2, BigDecimal.ROUND_HALF_UP); 541 mTab.setValue("WriteOffAmt", WriteOffAmt); 542 OverUnderAmt = OverUnderAmt.multiply(CurrencyRate).setScale(2, BigDecimal.ROUND_HALF_UP); 543 mTab.setValue("OverUnderAmt", OverUnderAmt); 544 } 545 if (C_Currency_ID != InvCurrency_ID) 546 { 547 InvTotalAmt = DB.getConvertedAmt(InvTotalAmt, InvCurrency_ID, C_Currency_ID, ConvDate, null, AD_Client_ID, AD_Org_ID); 548 if (InvTotalAmt == null || InvTotalAmt.compareTo(Env.ZERO) == 0) 549 { 550 mTab.setValue("C_Currency_ID", new Integer (InvCurrency_ID)); 551 setCalloutActive(false); 552 return "NoCurrencyConversion"; 553 } 554 log.debug("Converted InvTotalAmt=" + InvTotalAmt.toString()); 555 } 556 557 if (colName.equals("PayAmt")) 559 { 560 WriteOffAmt = InvTotalAmt.subtract(PayAmt).subtract(DiscountAmt).subtract(OverUnderAmt); 561 mTab.setValue("WriteOffAmt", WriteOffAmt); 562 } 563 else { 565 PayAmt = InvTotalAmt.subtract(DiscountAmt).subtract(WriteOffAmt).subtract(OverUnderAmt); 566 mTab.setValue("PayAmt", PayAmt); 567 } 568 569 setCalloutActive(false); 570 return ""; 571 } 573 574 575 580 public String CashJournal_Invoice (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 581 { 582 if (isCalloutActive()) return ""; 584 setCalloutActive(true); 585 586 Integer C_Invoice_ID = (Integer )value; 587 if (C_Invoice_ID == null || C_Invoice_ID.intValue() == 0) 588 { 589 setCalloutActive(false); 590 return ""; 591 } 592 593 Timestamp ts = Env.getContextAsDate(ctx, WindowNo, "DateAcct"); if (ts == null) 596 ts = new Timestamp (System.currentTimeMillis()); 597 String sql = "SELECT C_BPartner_ID, C_Currency_ID, C_Invoice_Open(C_Invoice_ID)," 599 + "C_PaymentTerm_Discount(C_Invoice_Open(C_Invoice_ID),C_PaymentTerm_ID,DateInvoiced,?) " 600 + "FROM C_Invoice WHERE C_Invoice_ID=?"; 601 try 602 { 603 PreparedStatement pstmt = DB.prepareStatement(sql); 604 pstmt.setTimestamp(1, ts); 605 pstmt.setInt(2, C_Invoice_ID.intValue()); 606 ResultSet rs = pstmt.executeQuery(); 607 if (rs.next()) 608 { 609 mTab.setValue("C_Currency_ID", new Integer (rs.getInt(2))); 610 BigDecimal PayAmt = rs.getBigDecimal(3); 611 BigDecimal DiscountAmt = rs.getBigDecimal(4); 612 mTab.setValue("Amount", PayAmt.subtract(DiscountAmt)); 613 mTab.setValue("DiscountAmt", DiscountAmt); 614 mTab.setValue("WriteOffAmt", Env.ZERO); 615 Env.setContext(ctx, WindowNo, "InvTotalAmt", PayAmt.toString()); 616 } 617 rs.close(); 618 pstmt.close(); 619 } 620 catch (SQLException e) 621 { 622 log.error("CashJournal_Invoice", e); 623 setCalloutActive(false); 624 return e.getLocalizedMessage(); 625 } 626 setCalloutActive(false); 627 return ""; 628 } 630 635 public String CashJournal_Amounts (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 636 { 637 if (isCalloutActive() || !"I".equals(mTab.getValue("CashType"))) 639 return ""; 640 String total = Env.getContext(ctx, WindowNo, "InvTotalAmt"); 642 if (total == null || total.length() == 0) 643 return ""; 644 BigDecimal InvTotalAmt = new BigDecimal (total); 645 setCalloutActive(true); 646 647 BigDecimal PayAmt = (BigDecimal )mTab.getValue("Amount"); 648 BigDecimal DiscountAmt = (BigDecimal )mTab.getValue("DiscountAmt"); 649 BigDecimal WriteOffAmt = (BigDecimal )mTab.getValue("WriteOffAmt"); 650 String colName = mField.getColumnName(); 651 log.debug(colName + " - Invoice=" + InvTotalAmt 652 + " - Amount=" + PayAmt + ", Discount=" + DiscountAmt + ", WriteOff=" + WriteOffAmt); 653 654 if (colName.equals("Amount")) 656 { 657 WriteOffAmt = InvTotalAmt.subtract(PayAmt).subtract(DiscountAmt); 658 mTab.setValue("WriteOffAmt", WriteOffAmt); 659 } 660 else { 662 PayAmt = InvTotalAmt.subtract(DiscountAmt).subtract(WriteOffAmt); 663 mTab.setValue("Amount", PayAmt); 664 } 665 666 setCalloutActive(false); 667 return ""; 668 } 670 671 672 677 public String Journal_Period (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 678 { 679 String colName = mField.getColumnName(); 680 if (value == null || isCalloutActive()) 681 return ""; 682 setCalloutActive(true); 683 684 int AD_Client_ID = Env.getContextAsInt(ctx, WindowNo, "AD_Client_ID"); 685 Timestamp DateAcct = null; 686 if (colName.equals("DateAcct")) 687 DateAcct = (Timestamp )value; 688 else 689 DateAcct = (Timestamp )mTab.getValue("DateAcct"); 690 int C_Period_ID = 0; 691 if (colName.equals("C_Period_ID")) 692 C_Period_ID = ((Integer )value).intValue(); 693 694 if (colName.equals("DateDoc")) 696 { 697 mTab.setValue("DateAcct", value); 698 } 699 700 else if (colName.equals("DateAcct")) 702 { 703 String sql = "SELECT C_Period_ID " 704 + "FROM C_Period " 705 + "WHERE C_Year_ID IN " 706 + " (SELECT C_Year_ID FROM C_Year WHERE C_Calendar_ID =" 707 + " (SELECT C_Calendar_ID FROM AD_ClientInfo WHERE AD_Client_ID=?))" 708 + " AND ? BETWEEN StartDate AND EndDate" 709 + " AND PeriodType='S'"; 710 try 711 { 712 PreparedStatement pstmt = DB.prepareStatement(sql); 713 pstmt.setInt(1, AD_Client_ID); 714 pstmt.setTimestamp(2, DateAcct); 715 ResultSet rs = pstmt.executeQuery(); 716 if (rs.next()) 717 C_Period_ID = rs.getInt(1); 718 rs.close(); 719 pstmt.close(); 720 } 721 catch (SQLException e) 722 { 723 log.error("Journal_Period - DateAcct", e); 724 setCalloutActive(false); 725 return e.getLocalizedMessage(); 726 } 727 if (C_Period_ID != 0) 728 mTab.setValue("C_Period_ID", new Integer (C_Period_ID)); 729 } 730 731 else 733 { 734 String sql = "SELECT PeriodType, StartDate, EndDate " 735 + "FROM C_Period WHERE C_Period_ID=?"; 736 try 737 { 738 PreparedStatement pstmt = DB.prepareStatement(sql); 739 pstmt.setInt(1, C_Period_ID); 740 ResultSet rs = pstmt.executeQuery(); 741 if (rs.next()) 742 { 743 String PeriodType = rs.getString(1); 744 Timestamp StartDate = rs.getTimestamp(2); 745 Timestamp EndDate = rs.getTimestamp(3); 746 if (PeriodType.equals("S")) { 748 if (DateAcct.before(StartDate) || DateAcct.after(EndDate)) 750 mTab.setValue("DateAcct", EndDate); 751 } 752 } 753 rs.close(); 754 pstmt.close(); 755 } 756 catch (SQLException e) 757 { 758 log.error("Journal_Period - Period", e); 759 setCalloutActive(false); 760 return e.getLocalizedMessage(); 761 } 762 } 763 setCalloutActive(false); 764 return ""; 765 } 767 768 774 public String JournalLine_Amt (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 775 { 776 String colName = mField.getColumnName(); 777 if (value == null || isCalloutActive()) 778 return ""; 779 if (mField.getColumnName().equals("CurrencyRate") && value == null) 781 return ""; 782 if (mTab.getValue("C_Currency_ID") == null) 783 return ""; 784 785 setCalloutActive(true); 786 787 Integer Currency_ID = (Integer )mTab.getValue("C_Currency_ID"); 789 int C_Currency_ID = Currency_ID.intValue(); 790 791 792 int C_AcctSchema_ID = Env.getContextAsInt(ctx, WindowNo, "C_AcctSchema_ID"); 794 int Target_Currency_ID = 0; 795 int Precision = 2; 796 String sql = "SELECT c.C_Currency_ID, c.StdPrecision FROM C_AcctSchema a, C_Currency c " 797 + "WHERE a.C_Currency_ID=c.C_Currency_ID AND a.C_AcctSchema_ID=?"; 798 try 799 { 800 PreparedStatement pstmt = DB.prepareStatement(sql); 801 pstmt.setInt(1, C_AcctSchema_ID); 802 ResultSet rs = pstmt.executeQuery(); 803 if (rs.next()) 804 { 805 Target_Currency_ID = rs.getInt(1); 806 Precision = rs.getInt(2); 807 } 808 rs.close(); 809 pstmt.close(); 810 } 811 catch (SQLException e) 812 { 813 log.error("JournalLine_Amt - Currency", e); 814 setCalloutActive(false); 815 return e.getLocalizedMessage(); 816 } 817 818 Timestamp DateAcct = (Timestamp )mTab.getValue("DateAcct"); 820 if (DateAcct == null) 821 DateAcct = new Timestamp (System.currentTimeMillis()); 822 String CurrencyRateType = (String )mTab.getValue("CurrencyRateType"); 823 if (CurrencyRateType == null) 824 { 825 CurrencyRateType = "S"; 826 mTab.setValue("CurrencyRateType", CurrencyRateType); 827 } 828 BigDecimal CurrencyRate = (BigDecimal )mTab.getValue("CurrencyRate"); 829 if (CurrencyRate == null) 830 { 831 CurrencyRate = new BigDecimal (1.0); 832 mTab.setValue("CurrencyRate", CurrencyRate); 833 } 834 835 if (colName.equals("C_Currency_ID") || colName.equals("CurrencyRateType") 839 || colName.equals("DateAcct")) 840 { 841 log.debug("CurrencyConversion From=" + C_Currency_ID 842 + "; To=" + Target_Currency_ID + "; Date=" + DateAcct.toString() 843 + "; Type=" + CurrencyRateType); 844 845 int AD_Client_ID = Env.getContextAsInt(ctx, WindowNo, "AD_Client_ID"); 846 int AD_Org_ID = Env.getContextAsInt(ctx, WindowNo, "AD_Org_ID"); 847 CurrencyRate = DB.getConvesionRate(C_Currency_ID, Target_Currency_ID, DateAcct, CurrencyRateType, AD_Client_ID, AD_Org_ID); 848 log.debug("Rate From Rate Table=" + CurrencyRate); 849 if (CurrencyRate == null) 850 { 851 sql = "SELECT CurrencyRate FROM GL_Journal " 852 + "WHERE GL_Journal_ID=? AND C_Currency_ID=? AND CurrencyRateType=?"; 853 PreparedStatement pstmt = null; 854 try 855 { 856 pstmt = DB.prepareStatement(sql); 857 pstmt.setInt(1, ((Integer )mTab.getValue("GL_Journal_ID")).intValue()); 858 pstmt.setInt(2, C_Currency_ID); 859 pstmt.setString(3, CurrencyRateType); 860 ResultSet rs = pstmt.executeQuery(); 861 if (rs.next()) 862 { 863 CurrencyRate = rs.getBigDecimal (1); 864 log.debug("CurrencyConversion From Journal=" + CurrencyRate); 865 } 866 rs.close(); 867 pstmt.close(); 868 pstmt = null; 869 } 870 catch (SQLException ex) 871 { 872 log.error("JournalLine_Amt - Rate from Journal", ex); 873 } 874 try 875 { 876 if (pstmt != null) 877 pstmt.close(); 878 } 879 catch (SQLException ex1){} 880 pstmt = null; 881 } 882 if (CurrencyRate == null) 883 CurrencyRate = Env.ZERO; 884 mTab.setValue("CurrencyRate", CurrencyRate); 885 } 886 887 BigDecimal AmtSourceDr = (BigDecimal )mTab.getValue("AmtSourceDr"); 889 if (AmtSourceDr == null) 890 AmtSourceDr = Env.ZERO; 891 BigDecimal AmtSourceCr = (BigDecimal )mTab.getValue("AmtSourceCr"); 892 if (AmtSourceCr == null) 893 AmtSourceCr = Env.ZERO; 894 895 BigDecimal AmtAcctDr = AmtSourceDr.multiply(CurrencyRate); 896 AmtAcctDr = AmtAcctDr.setScale(Precision, BigDecimal.ROUND_HALF_UP); 897 mTab.setValue("AmtAcctDr", AmtAcctDr); 898 BigDecimal AmtAcctCr = AmtSourceCr.multiply(CurrencyRate); 899 AmtAcctCr = AmtAcctCr.setScale(Precision, BigDecimal.ROUND_HALF_UP); 900 mTab.setValue("AmtAcctCr", AmtAcctCr); 901 902 setCalloutActive(false); 903 return ""; 904 } 906 907 908 912 public String Expense_Product (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 913 { 914 Integer M_Product_ID = (Integer )value; 915 if (M_Product_ID == null || M_Product_ID.intValue() == 0) 916 return ""; 917 setCalloutActive(true); 918 919 Timestamp DateExpense = Env.getContextAsDate(ctx, WindowNo, "DateExpense"); 921 if (DateExpense == null) 922 DateExpense = new Timestamp (System.currentTimeMillis()); 923 924 try 925 { 926 boolean noPrice = true; 927 BigDecimal priceActual = null; 928 929 String sql = "SELECT BOM_PriceStd(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceStd," 931 + "BOM_PriceList(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceList," 932 + "BOM_PriceLimit(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceLimit," 933 + "p.C_UOM_ID,pv.ValidFrom,pl.C_Currency_ID " 934 + "FROM M_Product p, M_ProductPrice pp, M_Pricelist pl, M_PriceList_Version pv " 935 + "WHERE p.M_Product_ID=pp.M_Product_ID" 936 + " AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID" 937 + " AND pv.M_PriceList_ID=pl.M_PriceList_ID" 938 + " AND pv.IsActive='Y'" 939 + " AND p.M_Product_ID=?" + " AND pl.M_PriceList_ID=?" + " ORDER BY pv.ValidFrom DESC"; 942 PreparedStatement pstmt = DB.prepareStatement(sql); 943 pstmt.setInt(1, M_Product_ID.intValue()); 944 pstmt.setInt(2, Env.getContextAsInt(ctx, WindowNo, "M_PriceList_ID")); 945 ResultSet rs = pstmt.executeQuery(); 946 while (rs.next() && noPrice) 947 { 948 java.sql.Date plDate = rs.getDate("ValidFrom"); 949 if (plDate == null || !DateExpense.before(plDate)) 952 { 953 noPrice = false; 954 priceActual = rs.getBigDecimal("PriceStd"); 956 if (priceActual == null) 957 priceActual = rs.getBigDecimal("PriceList"); 958 if (priceActual == null) 959 priceActual = rs.getBigDecimal("PriceLimit"); 960 Integer ii = new Integer (rs.getInt("C_Currency_ID")); 962 if (!rs.wasNull()) 963 mTab.setValue("C_Currency_ID", ii); 964 } 965 } 966 rs.close(); 967 pstmt.close(); 968 969 if (noPrice) 971 { 972 sql = "SELECT BOM_PriceStd(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceStd," 974 + "BOM_PriceList(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceList," 975 + "BOM_PriceLimit(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceLimit," 976 + "p.C_UOM_ID,pv.ValidFrom,pl.C_Currency_ID " 977 + "FROM M_Product p, M_ProductPrice pp, M_Pricelist pl, M_Pricelist bpl, M_PriceList_Version pv " 978 + "WHERE p.M_Product_ID=pp.M_Product_ID" 979 + " AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID" 980 + " AND pv.M_PriceList_ID=bpl.M_PriceList_ID" 981 + " AND pv.IsActive='Y'" 982 + " AND bpl.M_PriceList_ID=pl.BasePriceList_ID" + " AND p.M_Product_ID=?" + " AND pl.M_PriceList_ID=?" + " ORDER BY pv.ValidFrom DESC"; 986 987 pstmt = DB.prepareStatement(sql); 988 pstmt.setInt(1, M_Product_ID.intValue()); 989 pstmt.setInt(2, Env.getContextAsInt(ctx, WindowNo, "M_PriceList_ID")); 990 rs = pstmt.executeQuery(); 991 while (rs.next() && noPrice) 992 { 993 java.sql.Date plDate = rs.getDate("ValidFrom"); 994 if (plDate == null || !DateExpense.before(plDate)) 997 { 998 noPrice = false; 999 priceActual = rs.getBigDecimal("PriceStd"); 1001 if (priceActual == null) 1002 priceActual = rs.getBigDecimal("PriceList"); 1003 if (priceActual == null) 1004 priceActual = rs.getBigDecimal("PriceLimit"); 1005 Integer ii = new Integer (rs.getInt("C_Currency_ID")); 1007 if (!rs.wasNull()) 1008 mTab.setValue("C_Currency_ID", ii); 1009 } 1010 } 1011 rs.close(); 1012 pstmt.close(); 1013 } 1014 setCalloutActive(false); if (priceActual == null) 1017 priceActual = Env.ZERO; 1018 mTab.setValue("ExpenseAmt", priceActual); 1019 } 1020 catch (SQLException e) 1021 { 1022 log.error("Expense_Product", e); 1023 setCalloutActive(false); 1024 return e.getLocalizedMessage(); 1025 } 1026 1027 return ""; 1029 } 1031 1036 public String Expense_Amount (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 1037 { 1038 if (isCalloutActive()) 1039 return ""; 1040 setCalloutActive(true); 1041 1042 BigDecimal Qty = (BigDecimal )mTab.getValue("Qty"); 1044 BigDecimal ExpenseAmt = (BigDecimal )mTab.getValue("ExpenseAmt"); 1045 Integer C_Currency_ID = (Integer )mTab.getValue("C_Currency_ID"); 1046 int C_Currency_To_ID = Env.getContextAsInt(ctx, "$C_Currency_ID"); 1047 Timestamp DateExpense = Env.getContextAsDate(ctx, WindowNo, "DateExpense"); 1048 log.debug("Qty=" + Qty + ", Amt=" + ExpenseAmt + ", C_Currency_ID=" + C_Currency_ID); 1050 BigDecimal ConvertedAmt = ExpenseAmt; 1052 if (!ConvertedAmt.equals(Env.ZERO) && C_Currency_To_ID != C_Currency_ID.intValue()) 1054 { 1055 int AD_Client_ID = Env.getContextAsInt (ctx, WindowNo, "AD_Client_ID"); 1056 int AD_Org_ID = Env.getContextAsInt (ctx, WindowNo, "AD_Org_ID"); 1057 ConvertedAmt = DB.getConvertedAmt (ConvertedAmt, C_Currency_ID.intValue (), C_Currency_To_ID, DateExpense, null, AD_Client_ID, AD_Org_ID); 1058 } 1059 mTab.setValue("ConvertedAmt", ConvertedAmt); 1060 log.debug("= ConvertedAmt=" + ConvertedAmt); 1061 1062 setCalloutActive(false); 1063 return ""; 1064 } 1066 1071 public String Assignment_Product (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 1072 { 1073 if (isCalloutActive() || value == null) 1074 return ""; 1075 int S_ResourceAssignment_ID = ((Integer )value).intValue(); 1077 if (S_ResourceAssignment_ID == 0) 1078 return ""; 1079 setCalloutActive(true); 1080 1081 int M_Product_ID = 0; 1082 String Name = null; 1083 String Description = null; 1084 BigDecimal Qty = null; 1085 String sql = "SELECT p.M_Product_ID, ra.Name, ra.Description, ra.Qty " 1086 + "FROM S_ResourceAssignment ra" 1087 + " INNER JOIN M_Product p ON (p.S_Resource_ID=ra.S_Resource_ID) " 1088 + "WHERE ra.S_ResourceAssignment_ID=?"; 1089 try 1090 { 1091 PreparedStatement pstmt = DB.prepareStatement(sql); 1092 pstmt.setInt(1, S_ResourceAssignment_ID); 1093 ResultSet rs = pstmt.executeQuery(); 1094 if (rs.next()) 1095 { 1096 M_Product_ID = rs.getInt (1); 1097 Name = rs.getString(2); 1098 Description = rs.getString(3); 1099 Qty = rs.getBigDecimal(4); 1100 } 1101 rs.close(); 1102 pstmt.close(); 1103 } 1104 catch (SQLException e) 1105 { 1106 log.error("Assignment_Product", e); 1107 } 1108 1109 log.debug("S_ResourceAssignment_ID=" + S_ResourceAssignment_ID + " - M_Product_ID=" + M_Product_ID); 1110 if (M_Product_ID != 0) 1111 { 1112 mTab.setValue ("M_Product_ID", new Integer (M_Product_ID)); 1113 if (Description != null) 1114 Name += " (" + Description + ")"; 1115 if (!".".equals(Name)) 1116 mTab.setValue("Description", Name); 1117 String variable = "Qty"; 1119 if (mTab.getTableName().startsWith("C_Order")) 1120 variable = "QtyOrdered"; 1121 else if (mTab.getTableName().startsWith("C_Invoice")) 1122 variable = "QtyInvoiced"; 1123 if (Qty != null) 1124 mTab.setValue(variable, Qty); 1125 } 1126 setCalloutActive(false); 1127 return ""; 1128 } 1130 1131 1132 1137 public String PaySel_PayAmt (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 1138 { 1139 if (isCalloutActive() || value == null) 1140 return ""; 1141 BigDecimal PayAmt = (BigDecimal )value; 1143 Integer ii = (Integer )mTab.getValue("C_Invoice_ID"); 1144 if (ii == null) 1145 return ""; 1146 int C_Invoice_ID = ii.intValue(); 1147 if (C_Invoice_ID == 0) 1148 return ""; 1149 setCalloutActive(true); 1150 1151 BigDecimal OpenAmt = Env.ZERO; 1152 String sql = "SELECT C_Currency_Convert(C_Invoice_Open(C_Invoice_ID), C_Currency_ID, 100, DateInvoiced, 'S', AD_Client_ID, AD_Org_ID) " 1153 + "FROM C_Invoice WHERE C_Invoice_ID=?"; 1154 try 1155 { 1156 PreparedStatement pstmt = DB.prepareStatement(sql); 1157 pstmt.setInt(1, C_Invoice_ID); 1158 ResultSet rs = pstmt.executeQuery(); 1159 if (rs.next()) 1160 OpenAmt = rs.getBigDecimal(1); 1161 rs.close(); 1162 pstmt.close(); 1163 } 1164 catch (SQLException e) 1165 { 1166 log.error("PaySel_PayAmt", e); 1167 } 1168 1169 BigDecimal DiffAmt = OpenAmt.subtract(PayAmt); 1170 log.debug("OpenAmt=" + OpenAmt + ", PayAmt=" + PayAmt + ", DiffAmt=" + DiffAmt); 1171 mTab.setValue("DifferenceAmt", DiffAmt); 1172 1173 setCalloutActive(false); 1174 return ""; 1175 } 1177 1182 public String PaySel_Invoice (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 1183 { 1184 if (isCalloutActive() || value == null) 1185 return ""; 1186 int C_Invoice_ID = ((Integer )value).intValue(); 1188 if (C_Invoice_ID == 0) 1189 return ""; 1190 setCalloutActive(true); 1191 1192 BigDecimal OpenAmt = Env.ZERO; 1193 String sql = "SELECT C_Currency_Convert(C_Invoice_Open(C_Invoice_ID), C_Currency_ID, 100, DateInvoiced, 'S', AD_Client_ID, AD_Org_ID) " 1194 + "FROM C_Invoice WHERE C_Invoice_ID=?"; 1195 try 1196 { 1197 PreparedStatement pstmt = DB.prepareStatement(sql); 1198 pstmt.setInt(1, C_Invoice_ID); 1199 ResultSet rs = pstmt.executeQuery(); 1200 if (rs.next()) 1201 OpenAmt = rs.getBigDecimal(1); 1202 rs.close(); 1203 pstmt.close(); 1204 } 1205 catch (SQLException e) 1206 { 1207 log.error("PaySel_Invoice", e); 1208 } 1209 1210 log.debug("OpenAmt=" + OpenAmt); 1211 mTab.setValue("PayAmt", OpenAmt); 1212 mTab.setValue("DifferenceAmt", Env.ZERO); 1213 1214 setCalloutActive(false); 1215 return ""; 1216 } 1218 1219 1220 1225 public String Project_Planned (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 1226 { 1227 if (isCalloutActive() || value == null) 1228 return ""; 1229 setCalloutActive(true); 1230 1231 BigDecimal PlannedQty, PlannedPrice; 1232 int StdPrecision = Env.getContextAsInt(ctx, WindowNo, "StdPrecision"); 1233 1234 1235 PlannedQty = (BigDecimal )mTab.getValue("PlannedQty"); 1237 if (PlannedQty == null) 1238 PlannedQty = Env.ONE; 1239 PlannedPrice = ((BigDecimal )mTab.getValue("PlannedPrice")); 1240 if (PlannedPrice == null) 1241 PlannedPrice = Env.ZERO; 1242 BigDecimal PlannedAmt = PlannedQty.multiply(PlannedPrice); 1244 if (PlannedAmt.scale() > StdPrecision) 1245 PlannedAmt = PlannedAmt.setScale(StdPrecision, BigDecimal.ROUND_HALF_UP); 1246 log.debug("PlannedQty=" + PlannedQty + " * PlannedPrice=" + PlannedPrice + " -> PlannedAmt=" + PlannedAmt + " (Precision=" + StdPrecision+ ")"); 1248 mTab.setValue("PlannedAmt", PlannedAmt); 1249 setCalloutActive(false); 1250 return ""; 1251 } 1253 1254 public static final String DocSubTypeSO_Standard = "SO"; 1255 public static final String DocSubTypeSO_Quotation = "OB"; 1256 public static final String DocSubTypeSO_Proposal = "ON"; 1257 public static final String DocSubTypeSO_Prepay = "PR"; 1258 public static final String DocSubTypeSO_POS = "WR"; 1259 public static final String DocSubTypeSO_Warehouse = "WP"; 1260 public static final String DocSubTypeSO_OnCredit = "WI"; 1261 public static final String DocSubTypeSO_RMA = "RM"; 1262 1263}
| Popular Tags
|