⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calloutpayment.java

📁 大家共享愉快, 共享愉快, 共享愉快, 共享愉快,共享愉快
💻 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 Smart Business Solution. The Initial
 * Developer of the Original Code is Jorg Janke. Portions created by Jorg Janke
 * are Copyright (C) 1999-2005 Jorg Janke.
 * All parts are Copyright (C) 1999-2005 ComPiere, Inc.  All Rights Reserved.
 * Contributor(s): ______________________________________.
 *****************************************************************************/
package org.compiere.model;

import java.math.*;
import java.sql.*;
import java.util.*;
import java.util.logging.*;
import org.compiere.util.*;

/**
 *	Payment Callouts.
 *	org.compiere.model.CalloutPayment.*
 *	
 *  @author Jorg Janke
 *  @version $Id: CalloutPayment.java,v 1.17 2005/11/06 01:17:27 jjanke Exp $
 */
public class CalloutPayment extends CalloutEngine
{
	/**
	 *  Payment_Invoice.
	 *  when Invoice selected
	 *  - set C_Currency_ID
	 * 		- C_BPartner_ID
	 *  	- DiscountAmt = C_Invoice_Discount (ID, DateTrx)
	 *   	- PayAmt = invoiceOpen (ID) - Discount
	 * 		- WriteOffAmt = 0
	 */
	public String invoice (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value)
	{
		Integer C_Invoice_ID = (Integer)value;
		if (isCalloutActive()		//	assuming it is resetting value
			|| C_Invoice_ID == null || C_Invoice_ID.intValue() == 0)
			return "";
		setCalloutActive(true);
		mTab.setValue("C_Order_ID", null);
		mTab.setValue("C_Charge_ID", null);
		mTab.setValue("IsPrepayment", Boolean.FALSE);
		//
		mTab.setValue("DiscountAmt", Env.ZERO);
		mTab.setValue("WriteOffAmt", Env.ZERO);
		mTab.setValue("IsOverUnderPayment", Boolean.FALSE);
		mTab.setValue("OverUnderAmt", Env.ZERO);

		int C_InvoicePaySchedule_ID = 0;
		if (Env.getContextAsInt(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "C_Invoice_ID") == C_Invoice_ID.intValue()
			&& Env.getContextAsInt(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "C_InvoicePaySchedule_ID") != 0)
			C_InvoicePaySchedule_ID = Env.getContextAsInt(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "C_InvoicePaySchedule_ID");

		//  Payment Date
		Timestamp ts = (Timestamp)mTab.getValue("DateTrx");
		if (ts == null)
			ts = new Timestamp(System.currentTimeMillis());
		//
		String sql = "SELECT C_BPartner_ID,C_Currency_ID,"		//	1..2
			+ " invoiceOpen(C_Invoice_ID, ?),"					//	3		#1
			+ " invoiceDiscount(C_Invoice_ID,?,?), IsSOTrx "	//	4..5	#2/3
			+ "FROM C_Invoice WHERE C_Invoice_ID=?";			//			#4
		try
		{
			PreparedStatement pstmt = DB.prepareStatement(sql, null);
			pstmt.setInt(1, C_InvoicePaySchedule_ID);
			pstmt.setTimestamp(2, ts);
			pstmt.setInt(3, C_InvoicePaySchedule_ID);
			pstmt.setInt(4, 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);					//	Set Invoice Currency
				mTab.setValue("C_Currency_ID", new Integer(C_Currency_ID));
				//
				BigDecimal InvoiceOpen = rs.getBigDecimal(3);		//	Set Invoice OPen Amount
				if (InvoiceOpen == null)
					InvoiceOpen = Env.ZERO;
				BigDecimal DiscountAmt = rs.getBigDecimal(4);		//	Set Discount Amt
				if (DiscountAmt == null)
					DiscountAmt = Env.ZERO;
				mTab.setValue("PayAmt", InvoiceOpen.subtract(DiscountAmt));
				mTab.setValue("DiscountAmt", DiscountAmt);
				//  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);
			}
			rs.close();
			pstmt.close();
		}
		catch (SQLException e)
		{
			log.log(Level.SEVERE, sql, e);
			setCalloutActive(false);
			return e.getLocalizedMessage();
		}

		setCalloutActive(false);
		return docType(ctx, WindowNo, mTab, mField, value);
	}	//	invoice

	/**
	 *  Payment_Order.
	 *  when Waiting Payment Order selected
	 *  - set C_Currency_ID
	 * 		- C_BPartner_ID
	 *  	- DiscountAmt = C_Invoice_Discount (ID, DateTrx)
	 *   	- PayAmt = invoiceOpen (ID) - Discount
	 * 		- WriteOffAmt = 0
	 */
	public String order (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value)
	{
		Integer C_Order_ID = (Integer)value;
		if (isCalloutActive()		//	assuming it is resetting value
			|| C_Order_ID == null || C_Order_ID.intValue() == 0)
			return "";
		setCalloutActive(true);
		mTab.setValue("C_Invoice_ID", null);
		mTab.setValue("C_Charge_ID", null);
		mTab.setValue("IsPrepayment", Boolean.TRUE);
		//
		mTab.setValue("DiscountAmt", Env.ZERO);
		mTab.setValue("WriteOffAmt", Env.ZERO);
		mTab.setValue("IsOverUnderPayment", Boolean.FALSE);
		mTab.setValue("OverUnderAmt", Env.ZERO);

		//  Payment Date
		Timestamp ts = (Timestamp)mTab.getValue("DateTrx");
		if (ts == null)
			ts = new Timestamp(System.currentTimeMillis());
		//
		String sql = "SELECT C_BPartner_ID,C_Currency_ID, GrandTotal "
			+ "FROM C_Order WHERE C_Order_ID=?"; 	// #1
		try
		{
			PreparedStatement pstmt = DB.prepareStatement(sql, null);
			pstmt.setInt(1, C_Order_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);					//	Set Order Currency
				mTab.setValue("C_Currency_ID", new Integer(C_Currency_ID));
				//
				BigDecimal GrandTotal = rs.getBigDecimal(3);		//	Set Pay Amount
				if (GrandTotal == null)
					GrandTotal = Env.ZERO;
				mTab.setValue("PayAmt", GrandTotal);
			}
			rs.close();
			pstmt.close();
		}
		catch (SQLException e)
		{
			log.log(Level.SEVERE, sql, e);
			setCalloutActive(false);
			return e.getLocalizedMessage();
		}

		setCalloutActive(false);
		return docType(ctx, WindowNo, mTab, mField, value);
	}	//	order

	/**
	 *  Payment_Project.
	 *  - reset - C_BPartner_ID, Invoice, Order, Project,
	 *  	Discount, WriteOff
	 */
	public String project (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value)
	{
		Integer C_Project_ID = (Integer)value;
		if (isCalloutActive()		//	assuming it is resetting value
			|| C_Project_ID == null || C_Project_ID.intValue() == 0)
			return "";
		setCalloutActive(true);
		mTab.setValue("C_Charge_ID", null);
		setCalloutActive(false);
		return "";
	}	//	project

	/**
	 *  Payment_Charge.
	 *  - reset - C_BPartner_ID, Invoice, Order, Project,
	 *  	Discount, WriteOff
	 */
	public String charge (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value)
	{
		Integer C_Charge_ID = (Integer)value;
		if (isCalloutActive()		//	assuming it is resetting value
			|| C_Charge_ID == null || C_Charge_ID.intValue() == 0)
			return "";
		setCalloutActive(true);
		mTab.setValue("C_Invoice_ID", null);
		mTab.setValue("C_Order_ID", null);
		mTab.setValue("C_Project_ID", null);
		mTab.setValue("IsPrepayment", Boolean.FALSE);
		//
		mTab.setValue("DiscountAmt", Env.ZERO);
		mTab.setValue("WriteOffAmt", Env.ZERO);
		mTab.setValue("IsOverUnderPayment", Boolean.FALSE);
		mTab.setValue("OverUnderAmt", Env.ZERO);
		setCalloutActive(false);
		return "";
	}	//	charge

	/**
	 *  Payment_Document Type.
	 * 	Verify that Document Type (AP/AR) and Invoice (SO/PO) are in sync
	 */
	public String docType (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value)
	{
		int C_Invoice_ID = Env.getContextAsInt(ctx, WindowNo, "C_Invoice_ID");
		int C_Order_ID = Env.getContextAsInt(ctx, WindowNo, "C_Order_ID");
		int C_DocType_ID = Env.getContextAsInt(ctx, WindowNo, "C_DocType_ID");
		log.fine("Payment_DocType - C_Invoice_ID=" + C_Invoice_ID + ", C_DocType_ID=" + C_DocType_ID);
		MDocType dt = null;
		if (C_DocType_ID != 0)
		{
			dt = MDocType.get(ctx, C_DocType_ID);
			Env.setContext(ctx, WindowNo, "IsSOTrx", dt.isSOTrx() ? "Y" : "N");
		}
		//	Invoice
		if (C_Invoice_ID != 0)
		{
			MInvoice inv = new MInvoice (ctx, C_Invoice_ID, null);
			if (dt != null)
			{
				if (inv.isSOTrx() != dt.isSOTrx())
					return "PaymentDocTypeInvoiceInconsistent";
			}
		}
		//	Order Waiting Payment (can only be SO)
		if (C_Order_ID != 0 && !dt.isSOTrx())
			return "PaymentDocTypeInvoiceInconsistent";
		
		return "";
	}	//	docType


	/**
	 *  Payment_Amounts.
	 *	Change of:
	 *		- IsOverUnderPayment -> set OverUnderAmt to 0
	 *		- C_Currency_ID, C_ConvesionRate_ID -> convert all
	 *		- PayAmt, DiscountAmt, WriteOffAmt, OverUnderAmt -> PayAmt
	 *			make sure that add up to InvoiceOpenAmt
	 */
	public String amounts (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value, Object oldValue)
	{
		if (isCalloutActive())		//	assuming it is resetting value
			return "";
		int C_Invoice_ID = Env.getContextAsInt(ctx, WindowNo, "C_Invoice_ID");
		//	New Payment
		if (Env.getContextAsInt(ctx, WindowNo, "C_Payment_ID") == 0
			&& Env.getContextAsInt(ctx, WindowNo, "C_BPartner_ID") == 0
			&& C_Invoice_ID == 0)
			return "";
		setCalloutActive(true);

		//	Changed Column
		String colName = mField.getColumnName();
		if (colName.equals("IsOverUnderPayment")	//	Set Over/Under Amt to Zero
			|| !"Y".equals(Env.getContext(ctx, WindowNo, "IsOverUnderPayment")))
			mTab.setValue("OverUnderAmt", Env.ZERO);

		int C_InvoicePaySchedule_ID = 0;
		if (Env.getContextAsInt(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "C_Invoice_ID") == C_Invoice_ID
			&& Env.getContextAsInt(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "C_InvoicePaySchedule_ID") != 0)
			C_InvoicePaySchedule_ID = Env.getContextAsInt(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "C_InvoicePaySchedule_ID");

		//	Get Open Amount & Invoice Currency
		BigDecimal InvoiceOpenAmt = Env.ZERO;
		int C_Currency_Invoice_ID = 0;
		if (C_Invoice_ID != 0)
		{
			Timestamp ts = (Timestamp)mTab.getValue("DateTrx");
			if (ts == null)
				ts = new Timestamp(System.currentTimeMillis());
			String sql = "SELECT C_BPartner_ID,C_Currency_ID,"		//	1..2
				+ " invoiceOpen(C_Invoice_ID,?),"					//	3		#1
				+ " invoiceDiscount(C_Invoice_ID,?,?), IsSOTrx "	//	4..5	#2/3
				+ "FROM C_Invoice WHERE C_Invoice_ID=?";			//			#4
			try
			{
				PreparedStatement pstmt = DB.prepareStatement(sql, null);
				pstmt.setInt(1, C_InvoicePaySchedule_ID);
				pstmt.setTimestamp(2, ts);
				pstmt.setInt(3, C_InvoicePaySchedule_ID);
				pstmt.setInt(4, C_Invoice_ID);
				ResultSet rs = pstmt.executeQuery();
				if (rs.next())
				{
					C_Currency_Invoice_ID= rs.getInt(2);
					InvoiceOpenAmt = rs.getBigDecimal(3);		//	Set Invoice Open Amount
					if (InvoiceOpenAmt == null)
						InvoiceOpenAmt = Env.ZERO;
				}
				rs.close();
				pstmt.close();
			}
			catch (SQLException e)
			{
				log.log(Level.SEVERE, sql, e);
				setCalloutActive(false);
				return e.getLocalizedMessage();
			}
		}	//	get Invoice Info
		log.fine("Open=" + InvoiceOpenAmt + ", C_Invoice_ID=" + C_Invoice_ID 
			+ ", C_Currency_ID=" + C_Currency_Invoice_ID);

		//	Get Info from Tab
		BigDecimal PayAmt = (BigDecimal)mTab.getValue("PayAmt");
		BigDecimal DiscountAmt = (BigDecimal)mTab.getValue("DiscountAmt");
		BigDecimal WriteOffAmt = (BigDecimal)mTab.getValue("WriteOffAmt");
		BigDecimal OverUnderAmt = (BigDecimal)mTab.getValue("OverUnderAmt");
		log.fine("Pay=" + PayAmt + ", Discount=" + DiscountAmt
			+ ", WriteOff=" + WriteOffAmt + ", OverUnderAmt=" + OverUnderAmt);
		//	Get Currency Info
		int C_Currency_ID = ((Integer)mTab.getValue("C_Currency_ID")).intValue();
		MCurrency currency = MCurrency.get(ctx, C_Currency_ID);
		Timestamp ConvDate = (Timestamp)mTab.getValue("DateTrx");
		int C_ConversionType_ID = 0;
		Integer ii = (Integer)mTab.getValue("C_ConversionType_ID");
		if (ii != null)
			C_ConversionType_ID = ii.intValue();
		int AD_Client_ID = Env.getContextAsInt(ctx, WindowNo, "AD_Client_ID");
		int AD_Org_ID = Env.getContextAsInt(ctx, WindowNo, "AD_Org_ID");
		//	Get Currency Rate
		BigDecimal CurrencyRate = Env.ONE;
		if ((C_Currency_ID > 0 && C_Currency_Invoice_ID > 0 &&
			C_Currency_ID != C_Currency_Invoice_ID)
			|| colName.equals("C_Currency_ID") || colName.equals("C_ConversionType_ID"))
		{
			log.fine("InvCurrency=" + C_Currency_Invoice_ID 
				+ ", PayCurrency=" + C_Currency_ID 
				+ ", Date=" + ConvDate + ", Type=" + C_ConversionType_ID);
			CurrencyRate = MConversionRate.getRate (C_Currency_Invoice_ID, C_Currency_ID, 
				ConvDate, C_ConversionType_ID, AD_Client_ID, AD_Org_ID);
			if (CurrencyRate == null || CurrencyRate.compareTo(Env.ZERO) == 0)
			{
			//	mTab.setValue("C_Currency_ID", new Integer(C_Currency_Invoice_ID));	//	does not work
				setCalloutActive(false);
				if (C_Currency_Invoice_ID == 0)
					return "";		//	no error message when no invoice is selected
				return "NoCurrencyConversion";
			}
			//
			InvoiceOpenAmt = InvoiceOpenAmt.multiply(CurrencyRate)
				.setScale(currency.getStdPrecision(), BigDecimal.ROUND_HALF_UP);
			log.fine("Rate=" + CurrencyRate + ", InvoiceOpenAmt=" + InvoiceOpenAmt);
		}

		//	Currency Changed - convert all
		if (colName.equals("C_Currency_ID") || colName.equals("C_ConversionType_ID"))
		{
			PayAmt = PayAmt.multiply(CurrencyRate)
				.setScale(currency.getStdPrecision(), BigDecimal.ROUND_HALF_UP);
			mTab.setValue("PayAmt", PayAmt);
			DiscountAmt = DiscountAmt.multiply(CurrencyRate)
				.setScale(currency.getStdPrecision(), BigDecimal.ROUND_HALF_UP);
			mTab.setValue("DiscountAmt", DiscountAmt);
			WriteOffAmt = WriteOffAmt.multiply(CurrencyRate)
				.setScale(currency.getStdPrecision(), BigDecimal.ROUND_HALF_UP);
			mTab.setValue("WriteOffAmt", WriteOffAmt);
			OverUnderAmt = OverUnderAmt.multiply(CurrencyRate)
				.setScale(currency.getStdPrecision(), BigDecimal.ROUND_HALF_UP);
			mTab.setValue("OverUnderAmt", OverUnderAmt);
		}

		//	No Invoice - Set Discount, Witeoff, Under/Over to 0
		else if (C_Invoice_ID == 0)
		{
			if (Env.ZERO.compareTo(DiscountAmt) != 0)
				mTab.setValue("DiscountAmt", Env.ZERO);
			if (Env.ZERO.compareTo(WriteOffAmt) != 0)
				mTab.setValue("WriteOffAmt", Env.ZERO);
			if (Env.ZERO.compareTo(OverUnderAmt) != 0)
				mTab.setValue("OverUnderAmt", Env.ZERO);				
		}
		//  PayAmt - calculate write off
		else if (colName.equals("PayAmt"))
		{
			WriteOffAmt = InvoiceOpenAmt.subtract(PayAmt).subtract(DiscountAmt).subtract(OverUnderAmt);
			mTab.setValue("WriteOffAmt", WriteOffAmt);
		}
		else    //  calculate PayAmt
		{
			PayAmt = InvoiceOpenAmt.subtract(DiscountAmt).subtract(WriteOffAmt).subtract(OverUnderAmt);
			mTab.setValue("PayAmt", PayAmt);
		}

		setCalloutActive(false);
		return "";
	}	//	amounts

}	//	CalloutPayment

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -