www.pudn.com > code_source_compiere_erp_crm_logiciel_java.zip > CalloutSystem.java


/****************************************************************************** 
 * The contents of this file are subject to the   Compiere License  Version 1.1 
 * ("License"); You may not use this file except in compliance with the License 
 * You may obtain a copy of the License at http://www.compiere.org/license.html 
 * Software distributed under the License is distributed on an  "AS IS"  basis, 
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for 
 * the specific language governing rights and limitations under the License. 
 * The Original Code is                  Compiere  ERP & CRM  Business Solution 
 * The Initial Developer of the Original Code is Jorg Janke  and ComPiere, Inc. 
 * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts 
 * created by ComPiere are Copyright (C) ComPiere, Inc.;   All Rights Reserved. 
 * Contributor(s): ______________________________________. 
 *****************************************************************************/ 
package org.compiere.model; 
 
import java.sql.*; 
import java.math.*; 
import java.util.*; 
 
import org.apache.log4j.Logger; 
 
import org.compiere.util.*; 
import org.compiere.process.*; 
 
/** 
 *	Callout's are used for cross field validation and setting values in other fields 
 *	when returning a non empty (error message) string, an exception is raised 
 * 
 *	When invoked, the grid controller has the new value! 
 * 
 *  @author 	Jorg Janke 
 *  @version 	$Id: CalloutSystem.java,v 1.38 2003/04/30 06:24:21 jjanke Exp $ 
 */ 
public final class CalloutSystem implements Callout 
{ 
	/** 
	 *	Start Callout. 
	 *  

* Callout's are used for cross field validation and setting values in other fields * when returning a non empty (error message) string, an exception is raised *

* When invoked, the Tab model has the new value! * * @param ctx Context * @param method Method name * @param WindowNo current Window No * @param mTab Model Tab * @param mField Model Field * @param value The new value * @param oldValue The old value * @return Error message or "" */ public String start(Properties ctx, String method, int WindowNo, MTab mTab, MField mField, Object value, Object oldValue) { String retValue = ""; StringBuffer msg = new StringBuffer(method).append(" - ") .append(mField.getColumnName()) .append("=").append(value) .append(" (old=").append(oldValue) .append(") {active=").append(calloutActive).append("}"); s_log.info (msg); try { if (method.equals("Conversion_Rate")) retValue = Conversion_Rate(ctx, WindowNo, mTab, mField, value); // else if (method.equals("Charge")) retValue = Charge(ctx, WindowNo, mTab, mField, value); // else if (method.equals("Order_DocType")) retValue = Order_DocType(ctx, WindowNo, mTab, mField, value); else if (method.equals("Order_BPartner")) retValue = Order_BPartner(ctx, WindowNo, mTab, mField, value); else if (method.equals("Order_PriceList")) retValue = Order_PriceList(ctx, WindowNo, mTab, mField, value); // else if (method.equals("Order_Product")) retValue = Order_Product(ctx, WindowNo, mTab, mField, value); else if (method.equals("Order_Tax")) retValue = Order_Tax(ctx, WindowNo, mTab, mField, value); else if (method.equals("Order_Amt")) retValue = Order_Amt(ctx, WindowNo, mTab, mField, value); // else if (method.equals("Invoice_DocType")) retValue = Invoice_DocType(ctx, WindowNo, mTab, mField, value); else if (method.equals("Invoice_BPartner")) retValue = Invoice_BPartner(ctx, WindowNo, mTab, mField, value); else if (method.equals("Invoice_PriceList")) retValue = Order_PriceList(ctx, WindowNo, mTab, mField, value); // else if (method.equals("Invoice_Product")) retValue = Invoice_Product(ctx, WindowNo, mTab, mField, value); else if (method.equals("Invoice_Tax")) retValue = Invoice_Tax(ctx, WindowNo, mTab, mField, value); else if (method.equals("Invoice_Amt")) retValue = Invoice_Amt(ctx, WindowNo, mTab, mField, value); // else if (method.equals("Inventory_Product")) retValue = Inventory_Product(ctx, WindowNo, mTab, mField, value); // else if (method.equals("InOut_DocType")) retValue = InOut_DocType(ctx, WindowNo, mTab, mField, value); else if (method.equals("InOut_BPartner")) retValue = InOut_BPartner(ctx, WindowNo, mTab, mField, value); // else if (method.equals("BankStmt_Amount")) retValue = BankStmt_Amount(ctx, WindowNo, mTab, mField, value); else if (method.equals("BankStmt_Payment")) retValue = BankStmt_Payment(ctx, WindowNo, mTab, mField, value); // else if (method.equals("Payment_Invoice")) retValue = Payment_Invoice(ctx, WindowNo, mTab, mField, value); else if (method.equals("Payment_DocType")) retValue = Payment_DocType(ctx, WindowNo, mTab, mField, value); else if (method.equals("Payment_No_Verify")) retValue = Payment_No_Verify(ctx, WindowNo, mTab, mField, value); else if (method.equals("Payment_Amounts")) retValue = Payment_Amounts(ctx, WindowNo, mTab, mField, value, oldValue); // else if (method.equals("CashJournal_Invoice")) retValue = CashJournal_Invoice(ctx, WindowNo, mTab, mField, value); else if (method.equals("CashJournal_Amounts")) retValue = CashJournal_Amounts(ctx, WindowNo, mTab, mField, value); // else if (method.equals("Journal_Period")) retValue = Journal_Period (ctx, WindowNo, mTab, mField, value); else if (method.equals("JournalLine_Amt")) retValue = JournalLine_Amt(ctx, WindowNo, mTab, mField, value); // else if (method.equals("Request_Action")) retValue = RequestAction.action (ctx, WindowNo, mTab, mField, value); else if (method.equals("Request_CopyText")) retValue = RequestAction.copyText(ctx, WindowNo, mTab, mField, value); // else if (method.equals("Expense_Product")) retValue = Expense_Product (ctx, WindowNo, mTab, mField, value); else if (method.equals("Expense_Amount")) retValue = Expense_Amount (ctx, WindowNo, mTab, mField, value); // else if (method.equals("Assignment_Product")) retValue = Assignment_Product (ctx, WindowNo, mTab, mField, value); // else if (method.equals("PaySel_PayAmt")) retValue = PaySel_PayAmt (ctx, WindowNo, mTab, mField, value); else if (method.equals("PaySel_Invoice")) retValue = PaySel_Invoice (ctx, WindowNo, mTab, mField, value); // else { retValue = "CalloutMethodInvalid - " + method; s_log.warn(retValue); } } catch (Exception e) { calloutActive = false; s_log.error (method, e); e.printStackTrace(System.err); retValue = e.getLocalizedMessage(); } return retValue; } // start private static Logger s_log = Logger.getLogger(CalloutSystem.class); // static boolean calloutActive = false; /*************************************************************************/ /** * Conversion Rate - set Multiply Rate from Divide Rate and vice versa * * @param ctx context * @param WindowNo window * @param mTab tab * @param mField field * @param value value * @return error message */ private static String Conversion_Rate (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { if (calloutActive) // assuming it is Conversion_Rate return ""; calloutActive = true; BigDecimal rate1 = (BigDecimal)value; BigDecimal rate2 = Env.ZERO; BigDecimal one = new BigDecimal(1.0); if (rate1.doubleValue() != 0.0) // no divide by zero rate2 = one.divide(rate1, 12, BigDecimal.ROUND_HALF_UP); // if (mField.getColumnName().equals("MultiplyRate")) mTab.setValue("DivideRate", rate2); else mTab.setValue("MultiplyRate", rate2); s_log.info(mField.getColumnName() + "=" + rate1 + " => " + rate2); calloutActive = false; return ""; } // Conversion_Rate /** * Charge. * updates PriceActual, PriceLimit, PriceList from C_Charge_ID */ private static String Charge (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { Integer C_Charge_ID = (Integer)value; if (C_Charge_ID == null || C_Charge_ID.intValue() == 0) return ""; if (mTab.getValue("M_Product_ID") != null) { mTab.setValue("C_Charge_ID", null); return "ChargeExclusively"; } try { String SQL = "SELECT ChargeAmt FROM C_Charge WHERE C_Charge_ID=?"; PreparedStatement pstmt = DB.prepareStatement(SQL); pstmt.setInt(1, C_Charge_ID.intValue()); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { mTab.setValue ("PriceActual", rs.getBigDecimal (1)); mTab.setValue ("PriceLimit", Env.ZERO); mTab.setValue ("PriceList", Env.ZERO); } rs.close(); pstmt.close(); } catch (SQLException e) { s_log.error("Charge" + e); return e.getLocalizedMessage(); } return ""; } // Charge /*************************************************************************/ /** * Order - DocType. * - sets OrderType (DocSubTypeSO) * - sets HasCharges * - sets Approval * - gets DocNo */ private static String Order_DocType (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { Integer C_DocType_ID = (Integer)value; // Actually C_DocTypeTarget_ID if (C_DocType_ID == null || C_DocType_ID.intValue() == 0) return ""; // Re-Create new DocNo, if there is a doc number already // and the existing source used a different Sequence number String oldDocNo = (String)mTab.getValue("DocumentNo"); boolean newDocNo = (oldDocNo == null); if (!newDocNo && oldDocNo.startsWith("<") && oldDocNo.endsWith(">")) newDocNo = true; Integer oldC_DocType_ID = (Integer)mTab.getValue("C_DocType_ID"); try { String SQL = "SELECT d.DocSubTypeSO,d.HasCharges,d.IsApproved," // 1..3 + "d.IsDocNoControlled,s.CurrentNext,s.CurrentNextSys," // 4..6 + "s.AD_Sequence_ID,d.IsSOTrx " // 7..8 + "FROM C_DocType d, AD_Sequence s " + "WHERE C_DocType_ID=?" // #1 + " AND d.DocNoSequence_ID=s.AD_Sequence_ID(+)"; int AD_Sequence_ID = 0; // Get old AD_SeqNo for comparison if (!newDocNo && oldC_DocType_ID.intValue() != 0) { PreparedStatement pstmt = DB.prepareStatement(SQL); pstmt.setInt(1, oldC_DocType_ID.intValue()); ResultSet rs = pstmt.executeQuery(); if (rs.next()) AD_Sequence_ID = rs.getInt(6); rs.close(); pstmt.close(); } PreparedStatement pstmt = DB.prepareStatement(SQL); pstmt.setInt(1, C_DocType_ID.intValue()); ResultSet rs = pstmt.executeQuery(); String DocSubTypeSO = ""; boolean IsSOTrx = true; if (rs.next()) // we found document type { // Set Context: Document Sub Type for Sales Orders DocSubTypeSO = rs.getString(1); if (DocSubTypeSO == null) DocSubTypeSO = "--"; Env.setContext(ctx, WindowNo, "OrderType", DocSubTypeSO); if (DocSubTypeSO.equals(DocSubTypeSO_Prepay)) { mTab.setValue ("InvoiceRule", MOrder.InvoiceRule_Immediate); mTab.setValue ("DeliveryRule", MOrder.DeliveryRule_Receipt); } else { mTab.setValue ("InvoiceRule", MOrder.InvoiceRule_AfterDelivery); mTab.setValue ("DeliveryRule", MOrder.DeliveryRule_Availability); } if (DocSubTypeSO.equals(DocSubTypeSO_POS)) // POS Order mTab.setValue("PaymentRule", MOrder.PaymentRule_Cash); else mTab.setValue("PaymentRule", MOrder.PaymentRule_PaymentTerm); // IsSOTrx if ("N".equals(rs.getString(8))) IsSOTrx = false; // Set Context: Env.setContext(ctx, WindowNo, "HasCharges", rs.getString(2)); // Approval required? String YN = "Y"; if (rs.getString(3).equals("Y")) YN = "N"; mTab.setValue("IsApproved", YN); Env.setContext(ctx, WindowNo, "IsApproved", YN); // otherwise overwritten by default // DocumentNo if (rs.getString(4).equals("Y")) // IsDocNoControlled { if (!newDocNo && AD_Sequence_ID != rs.getInt(7)) newDocNo = true; if (newDocNo) if (Env.getContext(ctx, "#CompiereSys").equals("Y") && Env.getContextAsInt(ctx, "#AD_Client_ID") < 1000000) mTab.setValue("DocumentNo", "<" + rs.getString(6) + ">"); else mTab.setValue("DocumentNo", "<" + rs.getString(5) + ">"); } } rs.close(); pstmt.close(); // When BPartner is changed, the Rules are not set if // it is a POS or Credit Order (i.e. defaults from Standard BPartner) // This re-reads the Rules and applies them. if (DocSubTypeSO.equals(DocSubTypeSO_POS) || DocSubTypeSO.equals(DocSubTypeSO_Prepay)) // not for POS/PrePay ; else { SQL = "SELECT PaymentRule,C_PaymentTerm_ID," // 1..2 + "InvoiceRule,DeliveryRule," // 3..4 + "FreightCostRule,DeliveryViaRule, " // 5..6 + "PaymentRulePO,PO_PaymentTerm_ID " + "FROM C_BPartner " + "WHERE C_BPartner_ID=?"; // #1 pstmt = DB.prepareStatement(SQL); int C_BPartner_ID = Env.getContextAsInt(ctx, WindowNo, "C_BPartner_ID"); pstmt.setInt(1, C_BPartner_ID); // rs = pstmt.executeQuery(); if (rs.next()) { // PaymentRule String s = rs.getString(IsSOTrx ? "PaymentRule" : "PaymentRulePO"); if (s != null && s.length() != 0) { if (IsSOTrx && (s.equals("B") || s.equals("S") || s.equals("U"))) // No Cash/Check/Transfer for SO_Trx s = "P"; // Payment Term if (!IsSOTrx && (s.equals("B"))) // No Cash for PO_Trx s = "P"; // Payment Term mTab.setValue("PaymentRule", s); } // Payment Term Integer ii =new Integer(rs.getInt(IsSOTrx ? "C_PaymentTerm_ID" : "PO_PaymentTerm_ID")); if (!rs.wasNull()) mTab.setValue("C_PaymentTerm_ID", ii); // InvoiceRule s = rs.getString(3); if (s != null && s.length() != 0) mTab.setValue("InvoiceRule", s); // DeliveryRule s = rs.getString(4); if (s != null && s.length() != 0) mTab.setValue("DeliveryRule", s); // FreightCostRule s = rs.getString(5); if (s != null && s.length() != 0) mTab.setValue("FreightCostRule", s); // DeliveryViaRule s = rs.getString(6); if (s != null && s.length() != 0) mTab.setValue("DeliveryViaRule", s); } rs.close(); pstmt.close(); } // re-read customer rules } catch (SQLException e) { s_log.error("Order_DocType", e); return e.getLocalizedMessage(); } return ""; } // Order_DocType /** * Order - Defaults for BPartner. * - PriceList * - Currency * - Location/BillTo * - Contact * - Defaults: PaymentRule, PaymentTerm */ private static String Order_BPartner (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { Integer C_BPartner_ID = (Integer)value; if (C_BPartner_ID == null || C_BPartner_ID.intValue() == 0) return ""; String SQL = "SELECT p.AD_Language,p.C_PaymentTerm_ID," + "p.M_PriceList_ID,p.PaymentRule,p.POReference," + "p.SO_Description,p.IsDiscountPrinted," + "p.InvoiceRule,p.DeliveryRule,p.FreightCostRule,DeliveryViaRule," + "p.SO_CreditLimit-p.SO_CreditUsed AS CreditAvailable," + "l.C_BPartner_Location_ID,c.C_BPartner_Contact_ID," + "p.PO_PriceList_ID, p.PaymentRulePO, p.PO_PaymentTerm_ID " + "FROM C_BPartner p, C_BPartner_Location l, C_BPartner_Contact c " + "WHERE p.C_BPartner_ID=l.C_BPartner_ID(+)" + " AND p.C_BPartner_ID=c.C_BPartner_ID(+)" + " AND p.C_BPartner_ID=?"; // 1 boolean IsSOTrx = Env.getContext(ctx, WindowNo, "IsSOTrx").equals("Y"); try { PreparedStatement pstmt = DB.prepareStatement(SQL); pstmt.setInt(1, C_BPartner_ID.intValue()); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { // PriceList (indirect: IsTaxIncluded & Currency) Integer ii = new Integer(rs.getInt(IsSOTrx ? "M_PriceList_ID" : "PO_PriceList_ID")); if (!rs.wasNull()) mTab.setValue("M_PriceList_ID", ii); else { // get default PriceList int i = Env.getContextAsInt(ctx, "#M_PriceList_ID"); if (i != 0) mTab.setValue("M_PriceList_ID", new Integer(i)); } // Location(s) - overwritten by InfoBP selectiion int locID = rs.getInt("C_BPartner_Location_ID"); if (C_BPartner_ID.toString().equals(Env.getContext(ctx, WindowNo, Env.TAB_INFO, "C_BPartner_ID"))) { String loc = Env.getContext(ctx, WindowNo, Env.TAB_INFO, "C_BPartner_Location_ID"); if (loc.length() > 0) locID = Integer.parseInt(loc); } if (locID == 0) { mTab.setValue("C_BPartner_Location_ID", null); mTab.setValue("BillTo_ID", null); } else { mTab.setValue("C_BPartner_Location_ID", new Integer(locID)); mTab.setValue("BillTo_ID", new Integer(locID)); } // Contact - overwritten by InfoBP selection int contID = rs.getInt("C_BPartner_Contact_ID"); if (C_BPartner_ID.toString().equals(Env.getContext(ctx, WindowNo, Env.TAB_INFO, "C_BPartner_ID"))) { String cont = Env.getContext(ctx, WindowNo, Env.TAB_INFO, "C_BPartner_Contact_ID"); if (cont.length() > 0) contID = Integer.parseInt(cont); } if (contID == 0) mTab.setValue("C_BPartner_Contact_ID", null); else mTab.setValue("C_BPartner_Contact_ID", new Integer(contID)); // CreditAvailable double CreditAvailable = rs.getDouble("CreditAvailable"); if (!rs.wasNull() && CreditAvailable < 0) mTab.fireDataStatusEEvent("CreditLimitOver", DisplayType.getNumberFormat(DisplayType.Amount).format(CreditAvailable)); // PO Reference String s = rs.getString("POReference"); if (s != null && s.length() != 0) mTab.setValue("POReference", s); else mTab.setValue("POReference", null); // SO Description s = rs.getString("SO_Description"); if (s != null && s.trim().length() != 0) mTab.setValue("Description", s); // IsDiscountPrinted s = rs.getString("IsDiscountPrinted"); if (s != null && s.length() != 0) mTab.setValue("IsDiscountPrinted", s); else mTab.setValue("IsDiscountPrinted", "N"); // Defaults, if not Walkin Receipt or Walkin Invoice String OrderType = Env.getContext(ctx, WindowNo, "OrderType"); mTab.setValue("InvoiceRule", MOrder.InvoiceRule_AfterDelivery); mTab.setValue("DeliveryRule", MOrder.DeliveryRule_Availability); mTab.setValue("PaymentRule", MOrder.PaymentRule_PaymentTerm); if (OrderType.equals(DocSubTypeSO_Prepay)) { mTab.setValue("InvoiceRule", MOrder.InvoiceRule_Immediate); mTab.setValue("DeliveryRule", MOrder.DeliveryRule_Receipt); } else if (OrderType.equals(DocSubTypeSO_POS)) // for POS mTab.setValue("PaymentRule", MOrder.PaymentRule_Cash); else { // PaymentRule s = rs.getString(IsSOTrx ? "PaymentRule" : "PaymentRulePO"); if (s != null && s.length() != 0) { if (s.equals("B")) // No Cache in Non POS s = "P"; if (IsSOTrx && (s.equals("S") || s.equals("U"))) // No Check/Transfer for SO_Trx s = "P"; // Payment Term mTab.setValue("PaymentRule", s); } // Payment Term ii = new Integer(rs.getInt(IsSOTrx ? "C_PaymentTerm_ID" : "PO_PaymentTerm_ID")); if (!rs.wasNull()) mTab.setValue("C_PaymentTerm_ID", ii); // InvoiceRule s = rs.getString("InvoiceRule"); if (s != null && s.length() != 0) mTab.setValue("InvoiceRule", s); // DeliveryRule s = rs.getString("DeliveryRule"); if (s != null && s.length() != 0) mTab.setValue("DeliveryRule", s); // FreightCostRule s = rs.getString("FreightCostRule"); if (s != null && s.length() != 0) mTab.setValue("FreightCostRule", s); // DeliveryViaRule s = rs.getString("DeliveryViaRule"); if (s != null && s.length() != 0) mTab.setValue("DeliveryViaRule", s); } } rs.close(); pstmt.close(); } catch (SQLException e) { s_log.error("Order_BPartner", e); return e.getLocalizedMessage(); } return ""; } // Order_BPartner /** * Order - Defaults for PriceList. (used also in Invoice) * - Currency * - IsTaxIncluded, EnforcePriceLimit */ private static String Order_PriceList (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { Integer M_PriceList_ID = (Integer)value; if (M_PriceList_ID == null || M_PriceList_ID.intValue()== 0) return ""; String SQL = "SELECT pl.IsTaxIncluded,pl.EnforcePriceLimit,pl.C_Currency_ID,c.StdPrecision," + "plv.M_PriceList_Version_ID,plv.ValidFrom " + "FROM M_PriceList pl,C_Currency c,M_PriceList_Version plv " + "WHERE pl.C_Currency_ID=c.C_Currency_ID" + " AND pl.M_PriceList_ID=plv.M_PriceList_ID" + " AND pl.M_PriceList_ID=? " // 1 + "ORDER BY plv.ValidFrom DESC"; // Use newest price list - may not be future try { PreparedStatement pstmt = DB.prepareStatement(SQL); pstmt.setInt(1, M_PriceList_ID.intValue()); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { // Tax Included mTab.setValue("IsTaxIncluded", rs.getString(1)); // Price Limit Enforce Env.setContext(ctx, WindowNo, "EnforcePriceLimit", rs.getString(2)); // Currency Integer ii = new Integer(rs.getInt(3)); mTab.setValue("C_Currency_ID", ii); // Precision Env.setContext(ctx, WindowNo, "StdPrecision", rs.getInt(4)); // PriceList Version Env.setContext(ctx, WindowNo, "M_PriceList_Version_ID", rs.getInt(5)); } rs.close(); pstmt.close(); } catch (SQLException e) { s_log.error("Order_PriceList", e); return e.getLocalizedMessage(); } return ""; } // Prder_PriceList /** * Order - Defaults for Product. * - UOM * - PriceList, PriceStd, PriceLimit * - Currency */ private static String Order_Product (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { Integer M_Product_ID = (Integer)value; if (M_Product_ID == null || M_Product_ID.intValue() == 0) return ""; calloutActive = true; mTab.setValue("C_Charge_ID", null); // get order date - or default to today's date Timestamp orderDate = (Timestamp)mTab.getValue("DateOrdered"); if (orderDate == null) orderDate = new Timestamp(System.currentTimeMillis()); String SQL = "SELECT BOM_PriceStd(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceStd," // 1 + "BOM_PriceList(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceList," // 2 + "BOM_PriceLimit(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceLimit," // 3 + "p.C_UOM_ID,pv.ValidFrom,pl.C_Currency_ID,pl.EnforcePriceLimit " // 4..7 + "FROM M_Product p, M_ProductPrice pp, M_Pricelist pl, M_PriceList_Version pv " + "WHERE p.M_Product_ID=pp.M_Product_ID" + " AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID" + " AND pv.M_PriceList_ID=pl.M_PriceList_ID" + " AND pv.IsActive='Y'" + " AND p.M_Product_ID=?" // 1 + " AND pv.M_PriceList_Version_ID=?"; // 2 try { boolean noPrice = true; BigDecimal priceActual = Env.ZERO; // Check if Product was selected from Info int M_PriceList_Version_ID = Env.getContextAsInt(ctx, WindowNo, "M_PriceList_Version_ID"); if (M_PriceList_Version_ID != 0) { PreparedStatement pstmt = DB.prepareStatement(SQL); pstmt.setInt(1, M_Product_ID.intValue()); pstmt.setInt(2, M_PriceList_Version_ID); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { s_log.info("Order_Product - Selected PL Version"); noPrice = false; // Price Std priceActual = rs.getBigDecimal(1); // Price List mTab.setValue("PriceList", rs.getBigDecimal(2)); if (rs.wasNull()) mTab.setValue("PriceList", Env.ZERO); // Price Limit mTab.setValue("PriceLimit", rs.getBigDecimal(3)); if (rs.wasNull()) mTab.setValue("PriceLimit", Env.ZERO); // UOM Integer ii = new Integer(rs.getInt(4)); if (!rs.wasNull()) mTab.setValue("C_UOM_ID", ii); // Currency ii = new Integer(rs.getInt(6)); if (!rs.wasNull()) mTab.setValue("C_Currency_ID", ii); // Price Limit Enforce Env.setContext(ctx, WindowNo, "EnforcePriceLimit", rs.getString(7)); } else s_log.info("Order_Product NOT FOUND Selected PL Version - Product_ID=" + M_Product_ID + ", PL_Version_ID=" + M_PriceList_Version_ID); rs.close(); pstmt.close(); // Check/Update Warehouse Setting // int M_Warehouse_ID = Env.getContextAsInt(WindowNo, "M_Warehouse_ID"); // Integer wh = (Integer)mTab.getValue("M_Warehouse_ID"); // if (wh.intValue() != M_Warehouse_ID) // { // mTab.setValue("M_Warehouse_ID", new Integer(M_Warehouse_ID)); // ADias_log.warn(WindowNo, "WarehouseChanged"); // } } // Search Pricelist for current version int M_PriceList_ID = 0; if (noPrice) { M_PriceList_ID = Env.getContextAsInt(ctx, WindowNo, "M_PriceList_ID"); SQL = "SELECT BOM_PriceStd(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceStd," // 1 + "BOM_PriceList(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceList," // 2 + "BOM_PriceLimit(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceLimit," // 3 + "p.C_UOM_ID,pv.ValidFrom,pl.C_Currency_ID,pl.EnforcePriceLimit " // 4..7 + "FROM M_Product p" + " INNER JOIN M_ProductPrice pp ON (p.M_Product_ID=pp.M_Product_ID)" + " INNER JOIN M_PriceList_Version pv ON (pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID)" + " INNER JOIN M_Pricelist pl ON (pv.M_PriceList_ID=pl.M_PriceList_ID) " + "WHERE pv.IsActive='Y'" + " AND p.M_Product_ID=?" // 1 + " AND pl.M_PriceList_ID=?" // 2 + " ORDER BY pv.ValidFrom DESC"; PreparedStatement pstmt = DB.prepareStatement(SQL); pstmt.setInt(1, M_Product_ID.intValue()); pstmt.setInt(2, M_PriceList_ID); ResultSet rs = pstmt.executeQuery(); // while (rs.next() && noPrice) { java.sql.Date plDate = rs.getDate(5); // we have the price list // if order date is after or equal PriceList validFrom if (plDate == null || !orderDate.before(plDate)) { noPrice = false; s_log.info("Order_Product Current PL Version"); // Price Std priceActual = rs.getBigDecimal(1); // Price List mTab.setValue("PriceList", rs.getBigDecimal(2)); if (rs.wasNull()) mTab.setValue("PriceList", Env.ZERO); // Price Limit mTab.setValue("PriceLimit", rs.getBigDecimal(3)); if (rs.wasNull()) mTab.setValue("PriceLimit", Env.ZERO); // UOM Integer ii = new Integer(rs.getInt(4)); if (!rs.wasNull()) mTab.setValue("C_UOM_ID", ii); // Currency ii = new Integer(rs.getInt(6)); if (!rs.wasNull()) mTab.setValue("C_Currency_ID", ii); // Price Limit Enforce Env.setContext(ctx, WindowNo, "EnforcePriceLimit", rs.getString(7)); } } rs.close(); pstmt.close(); } // no prices yet - look base pricelist if (noPrice) { s_log.info("Order_Product NOT FOUND Current PL Version - Product_ID=" + M_Product_ID + ", PL_ID=" + M_PriceList_ID); // Find if via Base Pricelist SQL = "SELECT BOM_PriceStd(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceStd," // 1 + "BOM_PriceList(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceList," // 2 + "BOM_PriceLimit(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceLimit," // 3 + "p.C_UOM_ID,pv.ValidFrom,pl.C_Currency_ID,pl.EnforcePriceLimit " // 4..7 + "FROM M_Product p, M_ProductPrice pp, M_Pricelist pl, M_Pricelist bpl, M_PriceList_Version pv " + "WHERE p.M_Product_ID=pp.M_Product_ID" + " AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID" + " AND pv.M_PriceList_ID=bpl.M_PriceList_ID" + " AND pv.IsActive='Y'" + " AND bpl.M_PriceList_ID=pl.BasePriceList_ID" // Base + " AND p.M_Product_ID=?" // 1 + " AND pl.M_PriceList_ID=?" // 2 + " ORDER BY pv.ValidFrom DESC"; PreparedStatement pstmt = DB.prepareStatement(SQL); pstmt.setInt(1, M_Product_ID.intValue()); pstmt.setInt(2, M_PriceList_ID); ResultSet rs = pstmt.executeQuery(); // while (rs.next() && noPrice) { java.sql.Date plDate = rs.getDate(5); // we have the price list // if order date is after or equal PriceList validFrom if (plDate == null || !orderDate.before(plDate)) { noPrice = false; s_log.info("Order_Product Base Price List"); // Price Std priceActual = rs.getBigDecimal(1); // Price List mTab.setValue("PriceList", rs.getBigDecimal(2)); if (rs.wasNull()) mTab.setValue("PriceList", Env.ZERO); // Price Limit mTab.setValue("PriceLimit", rs.getBigDecimal(3)); if (rs.wasNull()) mTab.setValue("PriceLimit", Env.ZERO); // UOM Integer ii = new Integer(rs.getInt(4)); if (!rs.wasNull()) mTab.setValue("C_UOM_ID", ii); // Currency ii = new Integer(rs.getInt(6)); if (!rs.wasNull()) mTab.setValue("C_Currency_ID", ii); // Price Limit Enforce Env.setContext(ctx, WindowNo, "EnforcePriceLimit", rs.getString(7)); } } rs.close(); pstmt.close(); } // still no price, get UOM if (noPrice) { SQL = "SELECT C_UOM_ID FROM M_Product WHERE M_Product_ID=?"; PreparedStatement pstmt = DB.prepareStatement(SQL); pstmt.setInt(1, M_Product_ID.intValue()); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { s_log.info("Order_Product Only UOM"); Integer ii = new Integer(rs.getInt(1)); if (!rs.wasNull()) mTab.setValue("C_UOM_ID", ii); } rs.close(); pstmt.close(); } // calloutActive = false; // calculate discount, etc. // if (priceActual == null) priceActual = Env.ZERO; mTab.setValue("PriceActual", priceActual); } catch (SQLException e) { s_log.error("Order_Product " + e.getMessage()); calloutActive = false; return e.getLocalizedMessage(); } return ""; } // Order_Product /** * Order - Tax. * - called when tax basis changes (BPartner Loaction, Product) * - sets C_Tax_ID */ private static String Order_Tax (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { String column = mField.getColumnName(); if (value == null) return ""; // Check Product int M_Product_ID = 0; if (column.equals("M_Product_ID")) M_Product_ID = ((Integer)value).intValue(); else M_Product_ID = Env.getContextAsInt(ctx, WindowNo, "M_Product_ID"); int C_Charge_ID = 0; if (column.equals("C_Charge_ID")) C_Charge_ID = ((Integer)value).intValue(); else C_Charge_ID = Env.getContextAsInt(ctx, WindowNo, "C_Charge_ID"); s_log.debug("Product=" + M_Product_ID + ", C_Charge_ID=" + C_Charge_ID); if (M_Product_ID == 0 && C_Charge_ID == 0) return ""; // Check Partner Location int shipC_BPartner_Location_ID = 0; if (column.equals("C_BPartner_Location_ID")) shipC_BPartner_Location_ID = ((Integer)value).intValue(); else shipC_BPartner_Location_ID = Env.getContextAsInt(ctx, WindowNo, "C_BPartner_Location_ID"); if (shipC_BPartner_Location_ID == 0) return ""; s_log.debug("Ship BP_Location=" + shipC_BPartner_Location_ID); // Timestamp billDate = Env.getContextAsDate(ctx, WindowNo, "DateOrdered"); s_log.debug("Bill Date=" + billDate); Timestamp shipDate = Env.getContextAsDate(ctx, WindowNo, "DatePromised"); s_log.debug("Ship Date=" + shipDate); int AD_Org_ID = Env.getContextAsInt(ctx, WindowNo, "AD_Org_ID"); s_log.debug("Org=" + AD_Org_ID); int M_Warehouse_ID = Env.getContextAsInt(ctx, WindowNo, "M_Warehouse_ID"); s_log.debug("Warehouse=" + M_Warehouse_ID); int billC_BPartner_Location_ID = Env.getContextAsInt(ctx, WindowNo, "BillTo_ID"); if (billC_BPartner_Location_ID == 0) billC_BPartner_Location_ID = shipC_BPartner_Location_ID; s_log.debug("Bill BP_Location=" + billC_BPartner_Location_ID); // int C_Tax_ID = Tax.get (ctx, M_Product_ID, C_Charge_ID, billDate, shipDate, AD_Org_ID, M_Warehouse_ID, billC_BPartner_Location_ID, shipC_BPartner_Location_ID, Env.getContext(ctx, WindowNo, "IsSOTrx").equals("Y")); s_log.debug("Tax ID=" + C_Tax_ID); // if (C_Tax_ID == 0) mTab.fireDataStatusEEvent(Log.retrieveError()); else mTab.setValue("C_Tax_ID", new Integer(C_Tax_ID)); return ""; } // Order_Tax /** * Order - Amount. * - called from QtyOrdered, Discount and PriceActual * - calculates LineNetAmt * - enforces PriceLimit */ private static String Order_Amt (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { if (calloutActive) return ""; calloutActive = true; BigDecimal QtyOrdered, PriceActual, PriceLimit, Discount, PriceList; int StdPrecision = Env.getContextAsInt(ctx, WindowNo, "StdPrecision"); // get values QtyOrdered = (BigDecimal)mTab.getValue("QtyOrdered"); PriceActual = ((BigDecimal)mTab.getValue("PriceActual")).setScale(StdPrecision, BigDecimal.ROUND_HALF_UP); Discount = (BigDecimal)mTab.getValue("Discount"); // PriceLimit = ((BigDecimal)mTab.getValue("PriceLimit")).setScale(StdPrecision, BigDecimal.ROUND_HALF_UP); PriceList = (BigDecimal)mTab.getValue("PriceList"); s_log.debug("Ordered=" + QtyOrdered + ", List=" + PriceList + ", Limit=" + PriceLimit + ", Precision=" + StdPrecision); s_log.debug("~ Actual=" + PriceActual + ", Discount=" + Discount); // calculate Discount if (mField.getColumnName().equals("PriceActual") || mField.getColumnName().equals("PriceList")) { if (PriceList.intValue() == 0) Discount = Env.ZERO; else Discount = new BigDecimal ((PriceList.doubleValue() - PriceActual.doubleValue()) / PriceList.doubleValue() * 100.0); if (Discount.scale() > 2) Discount = Discount.setScale(2, BigDecimal.ROUND_HALF_UP); mTab.setValue("Discount", Discount); } // calculate Actual else if (mField.getColumnName().equals("Discount")) { PriceActual = new BigDecimal ((100.0 - Discount.doubleValue()) / 100.0 * PriceList.doubleValue()); if (PriceActual.scale() > StdPrecision) PriceActual = PriceActual.setScale(StdPrecision, BigDecimal.ROUND_HALF_UP); mTab.setValue("PriceActual", PriceActual); } s_log.debug("= Actual=" + PriceActual + ", Discount=" + Discount); // Check PriceLimit if (!mField.getColumnName().equals("QtyOrdered")) { String epl = Env.getContext(ctx, WindowNo, "EnforcePriceLimit"); // Check Price Limit? if (epl != null && epl.equals("Y") && PriceLimit.doubleValue() != 0.0 && PriceActual.compareTo(PriceLimit) < 0) { mTab.setValue ("PriceActual", PriceLimit); mTab.fireDataStatusEEvent ("UnderLimitPrice", ""); PriceActual = PriceLimit; // Repeat Discount calc if (PriceList.intValue() != 0) { Discount = new BigDecimal ((PriceList.doubleValue () - PriceActual.doubleValue ()) / PriceList.doubleValue () * 100.0); if (Discount.scale () > 2) Discount = Discount.setScale (2, BigDecimal.ROUND_HALF_UP); mTab.setValue ("Discount", Discount); } } } // Multiply BigDecimal LineNetAmt = QtyOrdered.multiply(PriceActual); if (LineNetAmt.scale() > StdPrecision) LineNetAmt = LineNetAmt.setScale(StdPrecision, BigDecimal.ROUND_HALF_UP); mTab.setValue("LineNetAmt", LineNetAmt); s_log.debug("Line=" + LineNetAmt); calloutActive = false; return ""; } // Order_Amt /*************************************************************************/ /** * Invoice - DocType. * - sets HasCharges * - sets Approval * - gets DocNo */ private static String Invoice_DocType (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { Integer C_DocType_ID = (Integer)value; if (C_DocType_ID == null || C_DocType_ID.intValue() == 0) return ""; try { String SQL = "SELECT d.HasCharges,d.IsApproved,d.IsDocNoControlled," + "s.CurrentNext, d.DocBaseType " + "FROM C_DocType d, AD_Sequence s " + "WHERE C_DocType_ID=?" // 1 + " AND d.DocNoSequence_ID=s.AD_Sequence_ID(+)"; PreparedStatement pstmt = DB.prepareStatement(SQL); pstmt.setInt(1, C_DocType_ID.intValue()); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { // Charges - Set Context Env.setContext(ctx, WindowNo, "HasCharges", rs.getString(1)); // Approval required? String YN = "Y"; if (rs.getString(2).equals("Y")) YN = "N"; mTab.setValue("IsApproved", YN); Env.setContext(ctx, WindowNo, "IsApproved", YN); // otherwise overwritten by default // DocumentNo if (rs.getString(3).equals("Y")) mTab.setValue("DocumentNo", "<" + rs.getString(4) + ">"); // DocBaseType - Set Context String s = rs.getString(5); Env.setContext(ctx, WindowNo, "DocBaseType", s); // AP Check & AR Credit Memo if (s.startsWith("AP")) mTab.setValue("PaymentRule", "S"); // Check else if (s.endsWith("C")) mTab.setValue("PaymentRule", "P"); // OnCredit } rs.close(); pstmt.close(); } catch (SQLException e) { s_log.error("Invoice_DocType", e); return e.getLocalizedMessage(); } return ""; } // Invoice_DocType /** * Invoice - Defaults for BPartner. * - PriceList * - Currency * - PaymentRule, PaymentTerm * - Location/BillTo * - Contact */ private static String Invoice_BPartner (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { Integer C_BPartner_ID = (Integer)value; if (C_BPartner_ID == null || C_BPartner_ID.intValue() == 0) return ""; String SQL = "SELECT p.AD_Language,p.C_PaymentTerm_ID," + "p.M_PriceList_ID,p.PaymentRule,p.POReference," + "p.SO_Description,p.IsDiscountPrinted," + "p.SO_CreditLimit-p.SO_CreditUsed AS CreditAvailable," + "l.C_BPartner_Location_ID,c.C_BPartner_Contact_ID," + "p.PO_PriceList_ID, p.PaymentRulePO, p.PO_PaymentTerm_ID " + "FROM C_BPartner p, C_BPartner_Location l, C_BPartner_Contact c " + "WHERE p.C_BPartner_ID=l.C_BPartner_ID(+)" + " AND p.C_BPartner_ID=c.C_BPartner_ID(+)" + " AND p.C_BPartner_ID=?"; // 1 boolean IsSOTrx = Env.getContext(ctx, WindowNo, "IsSOTrx").equals("Y"); try { PreparedStatement pstmt = DB.prepareStatement(SQL); pstmt.setInt(1, C_BPartner_ID.intValue()); ResultSet rs = pstmt.executeQuery(); // if (rs.next()) { // PriceList & IsTaxIncluded & Currency Integer ii = new Integer(rs.getInt(IsSOTrx ? "M_PriceList_ID" : "PO_PriceList_ID")); if (!rs.wasNull()) mTab.setValue("M_PriceList_ID", ii); else { // get default PriceList int i = Env.getContextAsInt(ctx, "#M_PriceList_ID"); if (i != 0) mTab.setValue("M_PriceList_ID", new Integer(i)); } // PaymentRule String s = rs.getString(IsSOTrx ? "PaymentRule" : "PaymentRulePO"); if (s != null && s.length() != 0) { if (Env.getContext(ctx, WindowNo, "DocBaseType").endsWith("C")) // Credits are Payment Term s = "P"; else if (IsSOTrx && (s.equals("S") || s.equals("U"))) // No Check/Transfer for SO_Trx s = "P"; // Payment Term mTab.setValue("PaymentRule", s); } // Payment Term ii = new Integer(rs.getInt(IsSOTrx ? "C_PaymentTerm_ID" : "PO_PaymentTerm_ID")); if (!rs.wasNull()) mTab.setValue("C_PaymentTerm_ID", ii); // Location(s) - overwritten by InfoBP selectiion int locID = rs.getInt("C_BPartner_Location_ID"); if (C_BPartner_ID.toString().equals(Env.getContext(ctx, WindowNo, Env.TAB_INFO, "C_BPartner_ID"))) { String loc = Env.getContext(ctx, WindowNo, Env.TAB_INFO, "C_BPartner_Location_ID"); if (loc.length() > 0) locID = Integer.parseInt(loc); } if (locID == 0) mTab.setValue("C_BPartner_Location_ID", null); else mTab.setValue("C_BPartner_Location_ID", new Integer(locID)); // Contact - overwritten by InfoBP selection int contID = rs.getInt("C_BPartner_Contact_ID"); if (C_BPartner_ID.toString().equals(Env.getContext(ctx, WindowNo, Env.TAB_INFO, "C_BPartner_ID"))) { String cont = Env.getContext(ctx, WindowNo, Env.TAB_INFO, "C_BPartner_Contact_ID"); if (cont.length() > 0) contID = Integer.parseInt(cont); } if (contID == 0) mTab.setValue("C_BPartner_Contact_ID", null); else mTab.setValue("C_BPartner_Contact_ID", new Integer(contID)); // CreditAvailable double CreditAvailable = rs.getDouble("CreditAvailable"); if (!rs.wasNull() && CreditAvailable < 0) mTab.fireDataStatusEEvent("CreditLimitOver", DisplayType.getNumberFormat(DisplayType.Amount).format(CreditAvailable)); // PO Reference s = rs.getString("POReference"); if (s != null && s.length() != 0) mTab.setValue("POReference", s); else mTab.setValue("POReference", null); // SO Description s = rs.getString("SO_Description"); if (s != null && s.trim().length() != 0) mTab.setValue("Description", s); // IsDiscountPrinted s = rs.getString("IsDiscountPrinted"); if (s != null && s.length() != 0) mTab.setValue("IsDiscountPrinted", s); else mTab.setValue("IsDiscountPrinted", "N"); } rs.close(); pstmt.close(); } catch (SQLException e) { s_log.error("Invoice_BPartner", e); return e.getLocalizedMessage(); } return ""; } // Invoice_BPartner /************************************************************************** * Invoice Line * - Defaults for Product * - UOM * - PriceList, PriceStd */ private static String Invoice_Product (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { Integer M_Product_ID = (Integer)value; if (M_Product_ID == null || M_Product_ID.intValue() == 0) return ""; calloutActive = true; mTab.setValue("C_Charge_ID", null); // get order date - or default to today's date Timestamp orderDate = Env.getContextAsDate(ctx, WindowNo, "DateInvoiced"); if (orderDate == null) orderDate = new Timestamp(System.currentTimeMillis()); String SQL = "SELECT BOM_PriceStd(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceStd," + "BOM_PriceList(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceList," + "BOM_PriceLimit(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceLimit," + "p.C_UOM_ID,pv.ValidFrom,pl.C_Currency_ID " + "FROM M_Product p, M_ProductPrice pp, M_Pricelist pl, M_PriceList_Version pv " + "WHERE p.M_Product_ID=pp.M_Product_ID" + " AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID" + " AND pv.M_PriceList_ID=pl.M_PriceList_ID" + " AND pv.IsActive='Y'" + " AND p.M_Product_ID=?" // 1 + " AND pv.M_PriceList_Version_ID=?"; // 2 try { boolean noPrice = true; BigDecimal priceActual = Env.ZERO; // Check if Product was selected from Info int M_PriceList_Version_ID = Env.getContextAsInt(ctx, WindowNo, "M_PriceList_Version_ID"); if (M_PriceList_Version_ID != 0) { PreparedStatement pstmt = DB.prepareStatement(SQL); pstmt.setInt(1, M_Product_ID.intValue()); pstmt.setInt(2, M_PriceList_Version_ID); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { noPrice = false; // UOM Integer ii = new Integer(rs.getInt("C_UOM_ID")); if (!rs.wasNull()) mTab.setValue("C_UOM_ID", ii); // Price List mTab.setValue("PriceList", rs.getBigDecimal("PriceList")); if (rs.wasNull()) mTab.setValue("PriceList", Env.ZERO); // Price Std priceActual = rs.getBigDecimal("PriceStd"); // Price Limit mTab.setValue("PriceLimit", rs.getBigDecimal("PriceLimit")); if (rs.wasNull()) mTab.setValue("PriceLimit", Env.ZERO); // Currency // ii = new Integer(rs.getInt("C_Currency_ID")); // if (!rs.wasNull()) // mTab.setValue("C_Currency_ID", ii); } rs.close(); pstmt.close(); } // Search Pricelist for current version if (noPrice) { SQL = "SELECT BOM_PriceStd(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceStd," + "BOM_PriceList(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceList," + "BOM_PriceLimit(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceLimit," + "p.C_UOM_ID,pv.ValidFrom,pl.C_Currency_ID " + "FROM M_Product p, M_ProductPrice pp, M_Pricelist pl, M_PriceList_Version pv " + "WHERE p.M_Product_ID=pp.M_Product_ID" + " AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID" + " AND pv.M_PriceList_ID=pl.M_PriceList_ID" + " AND pv.IsActive='Y'" + " AND p.M_Product_ID=?" // 1 + " AND pl.M_PriceList_ID=?" // 2 + " ORDER BY pv.ValidFrom DESC"; PreparedStatement pstmt = DB.prepareStatement(SQL); pstmt.setInt(1, M_Product_ID.intValue()); pstmt.setInt(2, Env.getContextAsInt(ctx, WindowNo, "M_PriceList_ID")); ResultSet rs = pstmt.executeQuery(); while (rs.next() && noPrice) { java.sql.Date plDate = rs.getDate("ValidFrom"); // we have the price list // if order date is after or equal PriceList validFrom if (plDate == null || !orderDate.before(plDate)) { noPrice = false; // UOM Integer ii = new Integer(rs.getInt("C_UOM_ID")); if (!rs.wasNull()) mTab.setValue("C_UOM_ID", ii); // Price List mTab.setValue("PriceList", rs.getBigDecimal("PriceList")); if (rs.wasNull()) mTab.setValue("PriceList", Env.ZERO); // Price Std priceActual = rs.getBigDecimal("PriceStd"); // Price Limit mTab.setValue("PriceLimit", rs.getBigDecimal("PriceLimit")); if (rs.wasNull()) mTab.setValue("PriceLimit", Env.ZERO); } } rs.close(); pstmt.close(); } // no prices yet - look base pricelist if (noPrice) { // Find if via Base Pricelist SQL = "SELECT BOM_PriceStd(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceStd," + "BOM_PriceList(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceList," + "BOM_PriceLimit(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceLimit," + "p.C_UOM_ID,pv.ValidFrom,pl.C_Currency_ID " + "FROM M_Product p, M_ProductPrice pp, M_Pricelist pl, M_Pricelist bpl, M_PriceList_Version pv " + "WHERE p.M_Product_ID=pp.M_Product_ID" + " AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID" + " AND pv.M_PriceList_ID=bpl.M_PriceList_ID" + " AND pv.IsActive='Y'" + " AND bpl.M_PriceList_ID=pl.BasePriceList_ID" // Base + " AND p.M_Product_ID=?" // 1 + " AND pl.M_PriceList_ID=?" // 2 + " ORDER BY pv.ValidFrom DESC"; PreparedStatement pstmt = DB.prepareStatement(SQL); pstmt.setInt(1, M_Product_ID.intValue()); pstmt.setInt(2, Env.getContextAsInt(ctx, WindowNo, "M_PriceList_ID")); ResultSet rs = pstmt.executeQuery(); while (rs.next() && noPrice) { java.sql.Date plDate = rs.getDate("ValidFrom"); // we have the price list // if order date is after or equal PriceList validFrom if (plDate == null || !orderDate.before(plDate)) { noPrice = false; // UOM Integer ii = new Integer(rs.getInt("C_UOM_ID")); if (!rs.wasNull()) mTab.setValue("C_UOM_ID", ii); // Price List mTab.setValue("PriceList", rs.getBigDecimal("PriceList")); if (rs.wasNull()) mTab.setValue("PriceList", Env.ZERO); // Price Std priceActual = rs.getBigDecimal("PriceStd"); // Price Limit mTab.setValue("PriceLimit", rs.getBigDecimal("PriceLimit")); if (rs.wasNull()) mTab.setValue("PriceLimit", Env.ZERO); } } rs.close(); pstmt.close(); } // still no price, get UOM if (noPrice) { SQL = "SELECT C_UOM_ID FROM M_Product WHERE M_Product_ID=?"; PreparedStatement pstmt = DB.prepareStatement(SQL); pstmt.setInt(1, M_Product_ID.intValue()); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { // UOM Integer ii = new Integer(rs.getInt(1)); if (!rs.wasNull()) mTab.setValue("C_UOM_ID", ii); } rs.close(); pstmt.close(); } // calloutActive = false; // calculate discount, etc. if (priceActual == null) priceActual = Env.ZERO; mTab.setValue("PriceActual", priceActual); } catch (SQLException e) { s_log.error("Invoice_Product", e); calloutActive = false; return e.getLocalizedMessage(); } // calloutActive = true; return ""; } // Invoice_Product /** * Invoice - Tax. * - called when tax basis changes (Product) * - sets C_Tax_ID */ private static String Invoice_Tax (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { String column = mField.getColumnName(); if (value == null) return ""; // Check Product int M_Product_ID = 0; if (column.equals("M_Product_ID")) M_Product_ID = ((Integer)value).intValue(); else M_Product_ID = Env.getContextAsInt(ctx, WindowNo, "M_Product_ID"); int C_Charge_ID = 0; if (column.equals("C_Charge_ID")) C_Charge_ID = ((Integer)value).intValue(); else C_Charge_ID = Env.getContextAsInt(ctx, WindowNo, "C_Charge_ID"); s_log.debug("Product=" + M_Product_ID + ", C_Charge_ID=" + C_Charge_ID); if (M_Product_ID == 0 && C_Charge_ID == 0) return ""; // Check Partner Location int shipC_BPartner_Location_ID = Env.getContextAsInt(ctx, WindowNo, "C_BPartner_Location_ID"); if (shipC_BPartner_Location_ID == 0) return ""; s_log.debug("Ship BP_Location=" + shipC_BPartner_Location_ID); int billC_BPartner_Location_ID = shipC_BPartner_Location_ID; s_log.debug("Bill BP_Location=" + billC_BPartner_Location_ID); // Dates Timestamp billDate = Env.getContextAsDate(ctx, WindowNo, "DateInvoiced"); s_log.debug("Bill Date=" + billDate); Timestamp shipDate = billDate; s_log.debug("Ship Date=" + shipDate); int AD_Org_ID = Env.getContextAsInt(ctx, WindowNo, "AD_Org_ID"); s_log.debug("Org=" + AD_Org_ID); int M_Warehouse_ID = Env.getContextAsInt(ctx, "#M_Warehouse_ID"); s_log.debug("Warehouse=" + M_Warehouse_ID); // int C_Tax_ID = Tax.get(ctx, M_Product_ID, C_Charge_ID, billDate, shipDate, AD_Org_ID, M_Warehouse_ID, billC_BPartner_Location_ID, shipC_BPartner_Location_ID, Env.getContext(ctx, WindowNo, "IsSOTrx").equals("Y")); s_log.debug("Tax ID=" + C_Tax_ID); // if (C_Tax_ID == 0) mTab.fireDataStatusEEvent(Log.retrieveError()); else mTab.setValue("C_Tax_ID", new Integer(C_Tax_ID)); return ""; } // Invoice_Tax /** * Invoice - Amount. * - called from QtyInvoiced, Discount and PriceActual * - calculates LineNetAmt */ private static String Invoice_Amt (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { if (calloutActive) return ""; calloutActive = true; int StdPrecision = Env.getContextAsInt(ctx, WindowNo, "StdPrecision"); // get values BigDecimal QtyInvoiced = (BigDecimal)mTab.getValue("QtyInvoiced"); // BigDecimal PriceActual = ((BigDecimal)mTab.getValue("PriceActual")).setScale(StdPrecision, BigDecimal.ROUND_HALF_UP); // Multiply BigDecimal LineNetAmt = QtyInvoiced.multiply(PriceActual); if (LineNetAmt.scale() > StdPrecision) LineNetAmt = LineNetAmt.setScale(StdPrecision, BigDecimal.ROUND_HALF_UP); mTab.setValue("LineNetAmt", LineNetAmt); s_log.debug("Line=" + LineNetAmt); calloutActive = false; return ""; } // Invoice_Amt /*************************************************************************/ /** * Inventory - Product. * - QtyOnHand */ private static String Inventory_Product (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { if (value == null) return ""; int M_Product_ID = ((Integer)value).intValue(); int M_Locator_ID = Env.getContextAsInt(ctx, WindowNo, "M_Locator_ID"); if (M_Product_ID == 0 || M_Locator_ID == 0) return ""; String SQL = "SELECT QtyOnHand FROM M_Storage " + "WHERE M_Product_ID=?" // 1 + " AND M_Locator_ID=?"; // 2 try { PreparedStatement pstmt = DB.prepareStatement(SQL); pstmt.setInt(1, M_Product_ID); pstmt.setInt(2, M_Locator_ID); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { BigDecimal bd = rs.getBigDecimal(1); if (!rs.wasNull()) mTab.setValue("QtyBook", bd); } rs.close(); pstmt.close(); } catch (SQLException e) { s_log.error("Inventory_Product", e); return e.getLocalizedMessage(); } return ""; } // Inventory_Product /*************************************************************************/ /** * InOut - DocType. * - sets MovementType * - gets DocNo */ private static String InOut_DocType (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { Integer C_DocType_ID = (Integer)value; if (C_DocType_ID == null || C_DocType_ID.intValue() == 0) return ""; try { Env.setContext(ctx, WindowNo, "C_DocTypeTarget_ID", C_DocType_ID.intValue()); String SQL = "SELECT d.DocBaseType, d.IsDocNoControlled, s.CurrentNext " + "FROM C_DocType d, AD_Sequence s " + "WHERE C_DocType_ID=?" // 1 + " AND d.DocNoSequence_ID=s.AD_Sequence_ID(+)"; PreparedStatement pstmt = DB.prepareStatement(SQL); pstmt.setInt(1, C_DocType_ID.intValue()); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { // Set Movement Type String DocBaseType = rs.getString("DocBaseType"); if (DocBaseType.equals("MMS")) // Material Shipments mTab.setValue("MovementType", "C-"); // Customer Shipments else if (DocBaseType.equals("MMR")) // Material Receipts mTab.setValue("MovementType", "V+"); // Vendor Receipts // DocumentNo if (rs.getString("IsDocNoControlled").equals("Y")) mTab.setValue("DocumentNo", "<" + rs.getString("CurrentNext") + ">"); } rs.close(); pstmt.close(); } catch (SQLException e) { s_log.error("InOut_DocType", e); return e.getLocalizedMessage(); } return ""; } // InOut_DocType /** * M_InOut - Defaults for BPartner. * - Location * - Contact */ private static String InOut_BPartner (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { Integer C_BPartner_ID = (Integer)value; if (C_BPartner_ID == null || C_BPartner_ID.intValue() == 0) return ""; String SQL = "SELECT p.AD_Language,p.C_PaymentTerm_ID," + "p.M_PriceList_ID,p.PaymentRule,p.POReference," + "p.SO_Description,p.IsDiscountPrinted," + "p.SO_CreditLimit-p.SO_CreditUsed AS CreditAvailable," + "l.C_BPartner_Location_ID,c.C_BPartner_Contact_ID " + "FROM C_BPartner p, C_BPartner_Location l, C_BPartner_Contact c " + "WHERE p.C_BPartner_ID=l.C_BPartner_ID(+)" + " AND p.C_BPartner_ID=c.C_BPartner_ID(+)" + " AND p.C_BPartner_ID=?"; // 1 try { PreparedStatement pstmt = DB.prepareStatement(SQL); pstmt.setInt(1, C_BPartner_ID.intValue()); ResultSet rs = pstmt.executeQuery(); BigDecimal bd; if (rs.next()) { // Location Integer ii = new Integer(rs.getInt("C_BPartner_Location_ID")); if (rs.wasNull()) mTab.setValue("C_BPartner_Location_ID", null); else mTab.setValue("C_BPartner_Location_ID", ii); // Contact ii = new Integer(rs.getInt("C_BPartner_Contact_ID")); if (rs.wasNull()) mTab.setValue("C_BPartner_Contact_ID", null); else mTab.setValue("C_BPartner_Contact_ID", ii); // CreditAvailable double CreditAvailable = rs.getDouble("CreditAvailable"); if (!rs.wasNull() && CreditAvailable < 0) mTab.fireDataStatusEEvent("CreditLimitOver", DisplayType.getNumberFormat(DisplayType.Amount).format(CreditAvailable)); } rs.close(); pstmt.close(); } catch (SQLException e) { s_log.error("InOut_BPartner", e); return e.getLocalizedMessage(); } return ""; } // InOut_BPartner /*************************************************************************/ /** * BankStmt - Amount. * Calculate ChargeAmt = StmtAmt - TrxAmt - InterestAmt * or id Charge is entered - InterestAmt = StmtAmt - TrxAmt - ChargeAmt */ private static String BankStmt_Amount (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { if (calloutActive) return ""; calloutActive = true; // Get Stmt & Trx BigDecimal stmt = (BigDecimal)mTab.getValue("StmtAmt"); if (stmt == null) stmt = Env.ZERO; BigDecimal trx = (BigDecimal)mTab.getValue("TrxAmt"); if (trx == null) trx = Env.ZERO; BigDecimal bd = stmt.subtract(trx); // Charge - calculate Interest if (mField.getColumnName().equals("ChargeAmt")) { BigDecimal charge = (BigDecimal)value; if (charge == null) charge = Env.ZERO; bd = bd.subtract(charge); // s_log.trace(s_log.l5_DData, "Interest (" + bd + ") = Stmt(" + stmt + ") - Trx(" + trx + ") - Charge(" + charge + ")"); mTab.setValue("InterestAmt", bd); } // Calculate Charge else { BigDecimal interest = (BigDecimal)mTab.getValue("InterestAmt"); if (interest == null) interest = Env.ZERO; bd = bd.subtract(interest); // s_log.trace(s_log.l5_DData, "Charge (" + bd + ") = Stmt(" + stmt + ") - Trx(" + trx + ") - Interest(" + interest + ")"); mTab.setValue("ChargeAmt", bd); } calloutActive = false; return ""; } // BankStmt_Amount /** * BankStmt - Payment. * Update Transaction Amount when payment is selected */ private static String BankStmt_Payment (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { Integer C_Payment_ID = (Integer)value; if (C_Payment_ID == null || C_Payment_ID.intValue() == 0) return ""; String sql = "SELECT PayAmt FROM C_Payment_v WHERE C_Payment_ID=?"; // 1 try { PreparedStatement pstmt = DB.prepareStatement(sql); pstmt.setInt(1, C_Payment_ID.intValue()); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { BigDecimal bd = rs.getBigDecimal(1); mTab.setValue("TrxAmt", bd); } rs.close(); pstmt.close(); } catch (SQLException e) { s_log.error("BankStmt_Payment", e); return e.getLocalizedMessage(); } // Recalculate Amounts BankStmt_Amount (ctx, WindowNo, mTab, mField, value); return ""; } // BankStmt_Payment /*************************************************************************/ /** * Payment_Invoice. * when Invoice selected * - set C_Currency_ID * - InvCurrency_ID/InvTotalAmt (Env), * - C_BPartner_ID * - DiscountAmt = C_Invoice_Discount (ID, DateTrx) * - InvTotalAmt - C_Invoice_Open (ID) * - PayAmt = InvTotalAmt - Discount * - WriteOffAmt = 0 */ private static String Payment_Invoice (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { if (calloutActive) // assuming it is resetting value return ""; calloutActive = true; Integer C_Invoice_ID = (Integer)value; if (C_Invoice_ID == null || C_Invoice_ID.intValue() == 0) { Env.setContext(ctx, WindowNo, "InvCurrency_ID", null); Env.setContext(ctx, WindowNo, "InvTotalAmt", null); calloutActive = false; return ""; } // Date Timestamp ts = (Timestamp)mTab.getValue("DateTrx"); if (ts == null) ts = new Timestamp(System.currentTimeMillis()); boolean isSOTrx = true; // String sql = "SELECT C_BPartner_ID,C_Currency_ID," // 1..2 + " C_Invoice_Open(C_Invoice_ID)," // 3 + " C_Invoice_Discount(C_Invoice_ID,?), IsSOTrx " // 4..5 + "FROM C_Invoice WHERE C_Invoice_ID=?"; try { PreparedStatement pstmt = DB.prepareStatement(sql); pstmt.setTimestamp(1, ts); pstmt.setInt(2, C_Invoice_ID.intValue()); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { mTab.setValue("C_BPartner_ID", new Integer(rs.getInt(1))); int C_Currency_ID = rs.getInt(2); mTab.setValue("C_Currency_ID", new Integer(C_Currency_ID)); Env.setContext(ctx, WindowNo, "InvCurrency_ID", C_Currency_ID); // BigDecimal InvoiceOpen = rs.getBigDecimal(3); if (InvoiceOpen == null) InvoiceOpen = Env.ZERO; Env.setContext(ctx, WindowNo, "InvTotalAmt", InvoiceOpen.toString()); BigDecimal DiscountAmt = rs.getBigDecimal(4); if (DiscountAmt == null) DiscountAmt = Env.ZERO; mTab.setValue("PayAmt", InvoiceOpen.subtract(DiscountAmt)); mTab.setValue("DiscountAmt", DiscountAmt); mTab.setValue("WriteOffAmt", Env.ZERO); // reset as dependent fields get reset Env.setContext(ctx, WindowNo, "C_Invoice_ID", C_Invoice_ID.toString()); mTab.setValue("C_Invoice_ID", C_Invoice_ID); isSOTrx = "Y".equals(rs.getString(5)); } rs.close(); pstmt.close(); } catch (SQLException e) { s_log.error("Payment_Invoice", e); calloutActive = false; return e.getLocalizedMessage(); } calloutActive = false; return Payment_DocType(ctx, WindowNo, mTab, mField, value); } // Payment_Invoice /** * Payment_Document Type. * Verify that Document Type (AP/AR) and Invoice (SO/PO) are in sync */ private static String Payment_DocType (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { int C_Invoice_ID = Env.getContextAsInt(ctx, WindowNo, "C_Invoice_ID"); int C_DocType_ID = Env.getContextAsInt(ctx, WindowNo, "C_DocType_ID"); // if (mField.getColumnName().equals("C_DocType_ID")) // { // } s_log.debug("Payment_DocType - C_Invoice_ID=" + C_Invoice_ID + ", C_DocType_ID=" + C_DocType_ID); if (C_Invoice_ID == 0 || C_DocType_ID == 0) return ""; String sql = "SELECT CASE WHEN (i.IsSOTrx='Y' AND dt.DocBaseType='ARR')" + " OR (i.IsSOTrx='N' AND dt.DocBaseType='APP') THEN 'Y'" + " ELSE 'N' END CASE " + "FROM C_Invoice i, C_DocType dt " + "WHERE i.C_Invoice_ID=? AND dt.C_DocType_ID=?"; String result = null; PreparedStatement pstmt = null; try { pstmt = DB.prepareStatement(sql); pstmt.setInt(1, C_Invoice_ID); pstmt.setInt(2, C_DocType_ID); ResultSet rs = pstmt.executeQuery(); if (rs.next()) result = rs.getString(1); rs.close(); pstmt.close(); pstmt = null; } catch (Exception e) { s_log.error("Payment_DocType", e); } finally { try { if (pstmt != null) pstmt.close (); } catch (Exception e) {} pstmt = null; } if ("N".equals(result)) return "PaymentDocTypeInvoiceInconsistent"; return ""; } // Payment_DocType /** * Payment_No_Verify. * - Verify Credit Card No */ private static String Payment_No_Verify (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { String colName = mField.getColumnName(); // CC if (colName.equals("CreditCardNumber")) { String cc = (String)value; if (cc == null || cc.length() == 0) return ""; return MPayment.validateCreditCardNumber(cc); } if (colName.equals("RoutingNo")) { String rt = (String)value; if (rt == null || rt.length() == 0) return ""; return MPayment.validateBankRoutingNo(rt); } s_log.warn("No Verification available"); return ""; } // Payment_CCNo_Verify /** * Payment_Amounts. * when C_Currency_ID, DiscountAnt, PayAmt, WriteOffAmt, OverUnderAmt, IsOverUnderPayment change * making sure that add up to InvTotalAmt in InvCurrency_ID (created by Payment_Invoice) */ private static String Payment_Amounts (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value, Object oldValue) { if (calloutActive) // assuming it is resetting value return ""; // New Payment if (Env.getContextAsInt(ctx, WindowNo, "C_Payment_ID") == 0 && Env.getContextAsInt(ctx, WindowNo, "C_BPartner_ID") == 0 && Env.getContextAsInt(ctx, WindowNo, "C_Invoice_ID") == 0) return ""; calloutActive = true; // Check, if InvTotalAmt/InvCurrency_ID exists String total = Env.getContext(ctx, WindowNo, "InvTotalAmt"); if (total == null || total.length() == 0) { calloutActive = false; return "InvoiceReSelect"; } BigDecimal InvTotalAmt = new BigDecimal(total); int InvCurrency_ID = Env.getContextAsInt (ctx, WindowNo, "InvCurrency_ID"); // String colName = mField.getColumnName(); if (colName.equals("IsOverUnderPayment")) // Set Over/Under Amt to Zero { mTab.setValue("OverUnderAmt", Env.ZERO); calloutActive = false; return ""; } // BigDecimal PayAmt = (BigDecimal)mTab.getValue("PayAmt"); BigDecimal DiscountAmt = (BigDecimal)mTab.getValue("DiscountAmt"); BigDecimal WriteOffAmt = (BigDecimal)mTab.getValue("WriteOffAmt"); BigDecimal OverUnderAmt = (BigDecimal)mTab.getValue("OverUnderAmt"); int C_Currency_ID = ((Integer)mTab.getValue("C_Currency_ID")).intValue(); Timestamp ConvDate = (Timestamp)mTab.getValue("DateTrx"); s_log.debug("Invoice=" + InvTotalAmt + " in Currency=" + InvCurrency_ID + " - Pay=" + PayAmt + ", Discount=" + DiscountAmt + ", WriteOff=" + WriteOffAmt + ", OverUnderAmt=" + OverUnderAmt + " in Currency=" + C_Currency_ID + " on " + ConvDate); int AD_Client_ID = Env.getContextAsInt(ctx, WindowNo, "AD_Client_ID"); int AD_Org_ID = Env.getContextAsInt(ctx, WindowNo, "AD_Org_ID"); // Currency Changed - convert all if (colName.equals("C_Currency_ID")) { int oldCur = ((Integer)oldValue).intValue(); s_log.debug("FromCurrency=" + oldCur + ", ToCurrency=" + C_Currency_ID + ", Date=" + ConvDate); BigDecimal CurrencyRate = DB.getConvesionRate(oldCur, C_Currency_ID, ConvDate, null, AD_Client_ID, AD_Org_ID); s_log.debug("Rate=" + CurrencyRate); if (CurrencyRate == null || CurrencyRate.compareTo(Env.ZERO) == 0) { mTab.setValue("C_Currency_ID", new Integer(InvCurrency_ID)); calloutActive = false; return "NoCurrencyConversion"; } PayAmt = PayAmt.multiply(CurrencyRate).setScale(2, BigDecimal.ROUND_HALF_UP); mTab.setValue("PayAmt", PayAmt); DiscountAmt = DiscountAmt.multiply(CurrencyRate).setScale(2, BigDecimal.ROUND_HALF_UP); mTab.setValue("DiscountAmt", DiscountAmt); WriteOffAmt = WriteOffAmt.multiply(CurrencyRate).setScale(2, BigDecimal.ROUND_HALF_UP); mTab.setValue("WriteOffAmt", WriteOffAmt); OverUnderAmt = OverUnderAmt.multiply(CurrencyRate).setScale(2, BigDecimal.ROUND_HALF_UP); mTab.setValue("OverUnderAmt", OverUnderAmt); } if (C_Currency_ID != InvCurrency_ID) { InvTotalAmt = DB.getConvertedAmt(InvTotalAmt, InvCurrency_ID, C_Currency_ID, ConvDate, null, AD_Client_ID, AD_Org_ID); if (InvTotalAmt == null || InvTotalAmt.compareTo(Env.ZERO) == 0) { mTab.setValue("C_Currency_ID", new Integer(InvCurrency_ID)); calloutActive = false; return "NoCurrencyConversion"; } s_log.debug("Converted InvTotalAmt=" + InvTotalAmt.toString()); } // PayAmt - calculate write off if (colName.equals("PayAmt")) { WriteOffAmt = InvTotalAmt.subtract(PayAmt).subtract(DiscountAmt).subtract(OverUnderAmt); mTab.setValue("WriteOffAmt", WriteOffAmt); } else // calculate PayAmt { PayAmt = InvTotalAmt.subtract(DiscountAmt).subtract(WriteOffAmt).subtract(OverUnderAmt); mTab.setValue("PayAmt", PayAmt); } calloutActive = false; return ""; } // Payment_Amounts /*************************************************************************/ /** * Cash Journal Line Invoice. * when Invoice selected * - set C_Currency, DiscountAnt, Amount, WriteOffAmt */ private static String CashJournal_Invoice (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { if (calloutActive) // assuming it is resetting value return ""; calloutActive = true; Integer C_Invoice_ID = (Integer)value; if (C_Invoice_ID == null || C_Invoice_ID.intValue() == 0) { calloutActive = false; return ""; } // Date Timestamp ts = Env.getContextAsDate(ctx, WindowNo, "DateAcct"); // from C_Cash if (ts == null) ts = new Timestamp(System.currentTimeMillis()); // String sql = "SELECT C_BPartner_ID, C_Currency_ID, C_Invoice_Open(C_Invoice_ID)," + "C_PaymentTerm_Discount(C_Invoice_Open(C_Invoice_ID),C_PaymentTerm_ID,DateInvoiced,?) " + "FROM C_Invoice WHERE C_Invoice_ID=?"; try { PreparedStatement pstmt = DB.prepareStatement(sql); pstmt.setTimestamp(1, ts); pstmt.setInt(2, C_Invoice_ID.intValue()); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { mTab.setValue("C_Currency_ID", new Integer(rs.getInt(2))); BigDecimal PayAmt = rs.getBigDecimal(3); BigDecimal DiscountAmt = rs.getBigDecimal(4); mTab.setValue("Amount", PayAmt.subtract(DiscountAmt)); mTab.setValue("DiscountAmt", DiscountAmt); mTab.setValue("WriteOffAmt", Env.ZERO); Env.setContext(ctx, WindowNo, "InvTotalAmt", PayAmt.toString()); } rs.close(); pstmt.close(); } catch (SQLException e) { s_log.error("CashJournal_Invoice", e); calloutActive = false; return e.getLocalizedMessage(); } calloutActive = false; return ""; } // CashJournal_Invoice /** * Cash Journal Line Invoice Amounts. * when DiscountAnt, Amount, WriteOffAmt change * making sure that add up to InvTotalAmt (created by CashJournal_Invoice) */ private static String CashJournal_Amounts (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { // Needs to be Invoice if (calloutActive || !"I".equals(mTab.getValue("CashType"))) return ""; // Check, if InvTotalAmt exists String total = Env.getContext(ctx, WindowNo, "InvTotalAmt"); if (total == null || total.length() == 0) return ""; BigDecimal InvTotalAmt = new BigDecimal(total); calloutActive = true; BigDecimal PayAmt = (BigDecimal)mTab.getValue("Amount"); BigDecimal DiscountAmt = (BigDecimal)mTab.getValue("DiscountAmt"); BigDecimal WriteOffAmt = (BigDecimal)mTab.getValue("WriteOffAmt"); String colName = mField.getColumnName(); s_log.debug(colName + " - Invoice=" + InvTotalAmt + " - Amount=" + PayAmt + ", Discount=" + DiscountAmt + ", WriteOff=" + WriteOffAmt); // Amount - calculate write off if (colName.equals("Amount")) { WriteOffAmt = InvTotalAmt.subtract(PayAmt).subtract(DiscountAmt); mTab.setValue("WriteOffAmt", WriteOffAmt); } else // calculate PayAmt { PayAmt = InvTotalAmt.subtract(DiscountAmt).subtract(WriteOffAmt); mTab.setValue("Amount", PayAmt); } calloutActive = false; return ""; } // CashJournal_Amounts /*************************************************************************/ /** * Journal - Period. * Check that selected period is in DateAcct Range or Adjusting Period * Called when C_Period_ID or DateAcct, DateDoc changed */ private static String Journal_Period (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { String colName = mField.getColumnName(); if (value == null || calloutActive) return ""; calloutActive = true; int AD_Client_ID = Env.getContextAsInt(ctx, WindowNo, "AD_Client_ID"); Timestamp DateAcct = null; if (colName.equals("DateAcct")) DateAcct = (Timestamp)value; else DateAcct = (Timestamp)mTab.getValue("DateAcct"); int C_Period_ID = 0; if (colName.equals("C_Period_ID")) C_Period_ID = ((Integer)value).intValue(); // When DateDoc is changed, update DateAcct if (colName.equals("DateDoc")) { mTab.setValue("DateAcct", value); } // When DateAcct is changed, set C_Period_ID else if (colName.equals("DateAcct")) { String sql = "SELECT C_Period_ID " + "FROM C_Period " + "WHERE C_Year_ID IN " + " (SELECT C_Year_ID FROM C_Year WHERE C_Calendar_ID =" + " (SELECT C_Calendar_ID FROM AD_ClientInfo WHERE AD_Client_ID=?))" + " AND ? BETWEEN StartDate AND EndDate" + " AND PeriodType='S'"; try { PreparedStatement pstmt = DB.prepareStatement(sql); pstmt.setInt(1, AD_Client_ID); pstmt.setTimestamp(2, DateAcct); ResultSet rs = pstmt.executeQuery(); if (rs.next()) C_Period_ID = rs.getInt(1); rs.close(); pstmt.close(); } catch (SQLException e) { s_log.error("Journal_Period - DateAcct", e); calloutActive = false; return e.getLocalizedMessage(); } if (C_Period_ID != 0) mTab.setValue("C_Period_ID", new Integer(C_Period_ID)); } // When C_Period_ID is changed, check if in DateAcct range and set to end date if not else { String sql = "SELECT PeriodType, StartDate, EndDate " + "FROM C_Period WHERE C_Period_ID=?"; try { PreparedStatement pstmt = DB.prepareStatement(sql); pstmt.setInt(1, C_Period_ID); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { String PeriodType = rs.getString(1); Timestamp StartDate = rs.getTimestamp(2); Timestamp EndDate = rs.getTimestamp(3); if (PeriodType.equals("S")) // Standard Periods { // out of range - set to last day if (DateAcct.before(StartDate) || DateAcct.after(EndDate)) mTab.setValue("DateAcct", EndDate); } } rs.close(); pstmt.close(); } catch (SQLException e) { s_log.error("Journal_Period - Period", e); calloutActive = false; return e.getLocalizedMessage(); } } calloutActive = false; return ""; } // Journal_Period /** * JournalLine - Amt. * Convert the source amount to accounted amount (AmtAcctDr/Cr) * Called when source amount (AmtSourceCr/Dr) or currency changes * (C_Currency_ID, CurrencyRate, CurrencyRateType, DateAcct) */ private static String JournalLine_Amt (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { String colName = mField.getColumnName(); if (value == null || calloutActive) return ""; calloutActive = true; // Source Currency Integer Currency_ID = (Integer)mTab.getValue("C_Currency_ID"); if (Currency_ID == null) { calloutActive = false; return ""; } int C_Currency_ID = Currency_ID.intValue(); // Get Target Currency & Precision from C_AcctSchema.C_Currency_ID int C_AcctSchema_ID = Env.getContextAsInt(ctx, WindowNo, "C_AcctSchema_ID"); int Target_Currency_ID = 0; int Precision = 2; String sql = "SELECT c.C_Currency_ID, c.StdPrecision FROM C_AcctSchema a, C_Currency c " + "WHERE a.C_Currency_ID=c.C_Currency_ID AND a.C_AcctSchema_ID=?"; try { PreparedStatement pstmt = DB.prepareStatement(sql); pstmt.setInt(1, C_AcctSchema_ID); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { Target_Currency_ID = rs.getInt(1); Precision = rs.getInt(2); } rs.close(); pstmt.close(); } catch (SQLException e) { s_log.error("JournalLine_Amt - Currency", e); calloutActive = false; return e.getLocalizedMessage(); } // Ger variables: DateAcct, CurrencyRate(Type) Timestamp DateAcct = (Timestamp)mTab.getValue("DateAcct"); if (DateAcct == null) DateAcct = new Timestamp(System.currentTimeMillis()); String CurrencyRateType = (String)mTab.getValue("CurrencyRateType"); if (CurrencyRateType == null) { CurrencyRateType = "S"; mTab.setValue("CurrencyRateType", CurrencyRateType); } BigDecimal CurrencyRate = (BigDecimal)mTab.getValue("CurrencyRate"); if (CurrencyRate == null) { CurrencyRate = new BigDecimal(1.0); mTab.setValue("CurrencyRate", CurrencyRate); } // When C_Currency_ID, CurrencyRate(Type) or DateAcct is changed, // look up CurrencyRate // Issue: you are able to overwrite fixed rates if (colName.equals("C_Currency_ID") || colName.equals("CurrencyRateType") || colName.equals("DateAcct")) { s_log.debug("CurrencyConversion From=" + C_Currency_ID + "; To=" + Target_Currency_ID + "; Date=" + DateAcct.toString() + "; Type=" + CurrencyRateType); int AD_Client_ID = Env.getContextAsInt(ctx, WindowNo, "AD_Client_ID"); int AD_Org_ID = Env.getContextAsInt(ctx, WindowNo, "AD_Org_ID"); CurrencyRate = DB.getConvesionRate(C_Currency_ID, Target_Currency_ID, DateAcct, CurrencyRateType, AD_Client_ID, AD_Org_ID); s_log.debug("Rate=" + CurrencyRate.toString()); mTab.setValue("CurrencyRate", CurrencyRate); } // AmtAcct = AmtSource * CurrencyRate ==> Precision BigDecimal AmtSourceDr = (BigDecimal)mTab.getValue("AmtSourceDr"); BigDecimal AmtSourceCr = (BigDecimal)mTab.getValue("AmtSourceCr"); BigDecimal AmtAcctDr = AmtSourceDr.multiply(CurrencyRate); AmtAcctDr = AmtAcctDr.setScale(Precision, BigDecimal.ROUND_HALF_UP); mTab.setValue("AmtAcctDr", AmtAcctDr); BigDecimal AmtAcctCr = AmtSourceCr.multiply(CurrencyRate); AmtAcctCr = AmtAcctCr.setScale(Precision, BigDecimal.ROUND_HALF_UP); mTab.setValue("AmtAcctCr", AmtAcctCr); calloutActive = false; return ""; } // JournalLine_Amt /*************************************************************************/ /** * Expense Report Line * - get ExpenseAmt */ private static String Expense_Product (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { Integer M_Product_ID = (Integer)value; if (M_Product_ID == null || M_Product_ID.intValue() == 0) return ""; calloutActive = true; // get expense date - or default to today's date Timestamp DateExpense = Env.getContextAsDate(ctx, WindowNo, "DateExpense"); if (DateExpense == null) DateExpense = new Timestamp(System.currentTimeMillis()); try { boolean noPrice = true; BigDecimal priceActual = null; // Search Pricelist for current version String sql = "SELECT BOM_PriceStd(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceStd," + "BOM_PriceList(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceList," + "BOM_PriceLimit(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceLimit," + "p.C_UOM_ID,pv.ValidFrom,pl.C_Currency_ID " + "FROM M_Product p, M_ProductPrice pp, M_Pricelist pl, M_PriceList_Version pv " + "WHERE p.M_Product_ID=pp.M_Product_ID" + " AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID" + " AND pv.M_PriceList_ID=pl.M_PriceList_ID" + " AND pv.IsActive='Y'" + " AND p.M_Product_ID=?" // 1 + " AND pl.M_PriceList_ID=?" // 2 + " ORDER BY pv.ValidFrom DESC"; PreparedStatement pstmt = DB.prepareStatement(sql); pstmt.setInt(1, M_Product_ID.intValue()); pstmt.setInt(2, Env.getContextAsInt(ctx, WindowNo, "M_PriceList_ID")); ResultSet rs = pstmt.executeQuery(); while (rs.next() && noPrice) { java.sql.Date plDate = rs.getDate("ValidFrom"); // we have the price list // if order date is after or equal PriceList validFrom if (plDate == null || !DateExpense.before(plDate)) { noPrice = false; // Price priceActual = rs.getBigDecimal("PriceStd"); if (priceActual == null) priceActual = rs.getBigDecimal("PriceList"); if (priceActual == null) priceActual = rs.getBigDecimal("PriceLimit"); // Currency Integer ii = new Integer(rs.getInt("C_Currency_ID")); if (!rs.wasNull()) mTab.setValue("C_Currency_ID", ii); } } rs.close(); pstmt.close(); // no prices yet - look base pricelist if (noPrice) { // Find if via Base Pricelist sql = "SELECT BOM_PriceStd(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceStd," + "BOM_PriceList(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceList," + "BOM_PriceLimit(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceLimit," + "p.C_UOM_ID,pv.ValidFrom,pl.C_Currency_ID " + "FROM M_Product p, M_ProductPrice pp, M_Pricelist pl, M_Pricelist bpl, M_PriceList_Version pv " + "WHERE p.M_Product_ID=pp.M_Product_ID" + " AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID" + " AND pv.M_PriceList_ID=bpl.M_PriceList_ID" + " AND pv.IsActive='Y'" + " AND bpl.M_PriceList_ID=pl.BasePriceList_ID" // Base + " AND p.M_Product_ID=?" // 1 + " AND pl.M_PriceList_ID=?" // 2 + " ORDER BY pv.ValidFrom DESC"; pstmt = DB.prepareStatement(sql); pstmt.setInt(1, M_Product_ID.intValue()); pstmt.setInt(2, Env.getContextAsInt(ctx, WindowNo, "M_PriceList_ID")); rs = pstmt.executeQuery(); while (rs.next() && noPrice) { java.sql.Date plDate = rs.getDate("ValidFrom"); // we have the price list // if order date is after or equal PriceList validFrom if (plDate == null || !DateExpense.before(plDate)) { noPrice = false; // Price priceActual = rs.getBigDecimal("PriceStd"); if (priceActual == null) priceActual = rs.getBigDecimal("PriceList"); if (priceActual == null) priceActual = rs.getBigDecimal("PriceLimit"); // Currency Integer ii = new Integer(rs.getInt("C_Currency_ID")); if (!rs.wasNull()) mTab.setValue("C_Currency_ID", ii); } } rs.close(); pstmt.close(); } // calloutActive = false; // calculate amount if (priceActual == null) priceActual = Env.ZERO; mTab.setValue("ExpenseAmt", priceActual); } catch (SQLException e) { s_log.error("Expense_Product", e); calloutActive = false; return e.getLocalizedMessage(); } // calloutActive = false; return ""; } // Expense_Product /** * Expense - Amount. * - called from Qty, ExpenseAmt, C_Currency_ID * - calculates ConvertedAmt */ private static String Expense_Amount (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { if (calloutActive) return ""; calloutActive = true; // get values BigDecimal Qty = (BigDecimal)mTab.getValue("Qty"); BigDecimal ExpenseAmt = (BigDecimal)mTab.getValue("ExpenseAmt"); Integer C_Currency_ID = (Integer)mTab.getValue("C_Currency_ID"); int C_Currency_To_ID = Env.getContextAsInt(ctx, "$C_Currency_ID"); Timestamp DateExpense = Env.getContextAsDate(ctx, WindowNo, "DateExpense"); // s_log.debug("Qty=" + Qty + ", Amt=" + ExpenseAmt + ", C_Currency_ID=" + C_Currency_ID); // Converted Amount = Unit price BigDecimal ConvertedAmt = ExpenseAmt; // convert if required if (!ConvertedAmt.equals(Env.ZERO) && C_Currency_To_ID != C_Currency_ID.intValue()) { int AD_Client_ID = Env.getContextAsInt (ctx, WindowNo, "AD_Client_ID"); int AD_Org_ID = Env.getContextAsInt (ctx, WindowNo, "AD_Org_ID"); ConvertedAmt = DB.getConvertedAmt (ConvertedAmt, C_Currency_ID.intValue (), C_Currency_To_ID, DateExpense, null, AD_Client_ID, AD_Org_ID); } mTab.setValue("ConvertedAmt", ConvertedAmt); s_log.debug("= ConvertedAmt=" + ConvertedAmt); calloutActive = false; return ""; } // Expense_Amount /** * Assignment_Product. * - called from S_ResourceAssignment_ID * - sets M_Product_ID */ private static String Assignment_Product (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { if (calloutActive || value == null) return ""; // get value int S_ResourceAssignment_ID = ((Integer)value).intValue(); if (S_ResourceAssignment_ID == 0) return ""; calloutActive = true; int M_Product_ID = 0; String Name = null; String Description = null; BigDecimal Qty = null; String sql = "SELECT p.M_Product_ID, ra.Name, ra.Description, ra.Qty " + "FROM S_ResourceAssignment ra" + " INNER JOIN M_Product p ON (p.S_Resource_ID=ra.S_Resource_ID) " + "WHERE ra.S_ResourceAssignment_ID=?"; try { PreparedStatement pstmt = DB.prepareStatement(sql); pstmt.setInt(1, S_ResourceAssignment_ID); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { M_Product_ID = rs.getInt (1); Name = rs.getString(2); Description = rs.getString(3); Qty = rs.getBigDecimal(4); } rs.close(); pstmt.close(); } catch (SQLException e) { s_log.error("Assignment_Product", e); } s_log.debug("S_ResourceAssignment_ID=" + S_ResourceAssignment_ID + " - M_Product_ID=" + M_Product_ID); if (M_Product_ID != 0) { mTab.setValue ("M_Product_ID", new Integer (M_Product_ID)); if (Description != null) Name += " (" + Description + ")"; if (!".".equals(Name)) mTab.setValue("Description", Name); // String variable = "Qty"; if (mTab.getTableName().startsWith("C_Order")) variable = "QtyOrdered"; else if (mTab.getTableName().startsWith("C_Invoice")) variable = "QtyInvoiced"; if (Qty != null) mTab.setValue(variable, Qty); } calloutActive = false; return ""; } // Assignment_Product /*************************************************************************/ /** * Payment Selection - Payment Amount. * - called from C_PaySelectionLine.PayAmt * - update DifferenceAmt */ private static String PaySel_PayAmt (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { if (calloutActive || value == null) return ""; // get value BigDecimal PayAmt = (BigDecimal)value; Integer ii = (Integer)mTab.getValue("C_Invoice_ID"); if (ii == null) return ""; int C_Invoice_ID = ii.intValue(); if (C_Invoice_ID == 0) return ""; calloutActive = true; BigDecimal OpenAmt = Env.ZERO; String sql = "SELECT C_Currency_Convert(C_Invoice_Open(C_Invoice_ID), C_Currency_ID, 100, DateInvoiced, 'S', AD_Client_ID, AD_Org_ID) " + "FROM C_Invoice WHERE C_Invoice_ID=?"; try { PreparedStatement pstmt = DB.prepareStatement(sql); pstmt.setInt(1, C_Invoice_ID); ResultSet rs = pstmt.executeQuery(); if (rs.next()) OpenAmt = rs.getBigDecimal(1); rs.close(); pstmt.close(); } catch (SQLException e) { s_log.error("PaySel_PayAmt", e); } BigDecimal DiffAmt = OpenAmt.subtract(PayAmt); s_log.debug("OpenAmt=" + OpenAmt + ", PayAmt=" + PayAmt + ", DiffAmt=" + DiffAmt); mTab.setValue("DifferenceAmt", DiffAmt); calloutActive = false; return ""; } // PaySel_PayAmt /** * Payment Selection - Invoice. * - called from C_PaySelectionLine.C_Invoice_ID * - update PayAmt & DifferenceAmt */ private static String PaySel_Invoice (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) { if (calloutActive || value == null) return ""; // get value int C_Invoice_ID = ((Integer)value).intValue(); if (C_Invoice_ID == 0) return ""; calloutActive = true; BigDecimal OpenAmt = Env.ZERO; String sql = "SELECT C_Currency_Convert(C_Invoice_Open(C_Invoice_ID), C_Currency_ID, 100, DateInvoiced, 'S', AD_Client_ID, AD_Org_ID) " + "FROM C_Invoice WHERE C_Invoice_ID=?"; try { PreparedStatement pstmt = DB.prepareStatement(sql); pstmt.setInt(1, C_Invoice_ID); ResultSet rs = pstmt.executeQuery(); if (rs.next()) OpenAmt = rs.getBigDecimal(1); rs.close(); pstmt.close(); } catch (SQLException e) { s_log.error("PaySel_Invoice", e); } s_log.debug("OpenAmt=" + OpenAmt); mTab.setValue("PayAmt", OpenAmt); mTab.setValue("DifferenceAmt", Env.ZERO); calloutActive = false; return ""; } // PaySel_Invoice /*************************************************************************/ private static final String DocSubTypeSO_Standard = "SO"; private static final String DocSubTypeSO_Quotation = "OB"; private static final String DocSubTypeSO_Proposal = "ON"; private static final String DocSubTypeSO_Prepay = "PR"; private static final String DocSubTypeSO_POS = "WR"; private static final String DocSubTypeSO_Warehouse = "WP"; private static final String DocSubTypeSO_OnCredit = "WI"; private static final String DocSubTypeSO_RMA = "RM"; /*************************************************************************/ /** * Conversion Rules. * Convert a String * * @param method in notation User_Function * @param value the value * @return converted String or Null if no method found */ public String convert (String method, String value) { String retValue = value.trim(); // if (true) { s_log.error("convert - Unknown Method=" + method); return null; } return retValue; } // convert } // CalloutSystem