📄 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.
* <p>
* 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
* <p>
* 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -