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

📄 doc_allocation.java

📁 Java写的ERP系统
💻 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.acct;

import java.math.*;
import java.util.*;
import java.sql.*;

import org.compiere.util.*;
import org.compiere.model.*;

/**
 *  Post Allocation Documents.
 *  <pre>
 *  Table:              C_Allocation (390)
 *  Document Types:     CMA
 *  </pre>
 *  @author Jorg Janke
 *  @version  $Id: Doc_Allocation.java,v 1.15 2003/04/15 05:19:27 jjanke Exp $
 */
public class Doc_Allocation extends Doc
{
	/**
	 *  Constructor
	 *  @param AD_Client_ID AD_Client_ID
	 */
	protected Doc_Allocation(int AD_Client_ID)
	{
		super(AD_Client_ID);
	}   //  Doc_Allocation

	//  References
	private int m_C_Invoice_ID = 0;
	private int m_C_Order_ID = 0;
	private int m_C_Payment_ID = 0;
	private int m_C_CashLine_ID = 0;

	/**
	 *  Return TableName of Document
	 *  @return C_Allocation
	 */
	public String getTableName()
	{
		return "C_Allocation";
	}   //  getTableName

	/**
	 *  Get Table ID
	 *  @return 390
	 */
	public int getAD_Table_ID()
	{
		return 390;
	}   //  getAD_Table_ID

	/**
	 *  Load Specific Document Details
	 *  @param rs result set
	 *  @return true if loadDocumentType was set
	 */
	protected boolean loadDocumentDetails (ResultSet rs)
	{
		p_vo.DocumentType = DocVO.DOCTYPE_Allocation;
		try
		{
			p_vo.DateDoc = rs.getTimestamp("DateTrx");
			p_vo.DocumentNo = "";

			//	Amounts
			p_vo.Amounts[Doc.AMTTYPE_Allocation] = rs.getBigDecimal("Amount");
			if (p_vo.Amounts[Doc.AMTTYPE_Allocation] == null)
				p_vo.Amounts[Doc.AMTTYPE_Allocation] = Env.ZERO;
			p_vo.Amounts[Doc.AMTTYPE_Discount] = rs.getBigDecimal("DiscountAmt");
			if (p_vo.Amounts[Doc.AMTTYPE_Discount] == null)
				p_vo.Amounts[Doc.AMTTYPE_Discount] = Env.ZERO;
			p_vo.Amounts[Doc.AMTTYPE_WriteOff] = rs.getBigDecimal("WriteOffAmt");
			if (p_vo.Amounts[Doc.AMTTYPE_WriteOff] == null)
				p_vo.Amounts[Doc.AMTTYPE_WriteOff] = Env.ZERO;
			//
			p_vo.Amounts[Doc.AMTTYPE_Invoice] = p_vo.Amounts[Doc.AMTTYPE_Allocation]
				.add(p_vo.Amounts[Doc.AMTTYPE_Discount])
				.add(p_vo.Amounts[Doc.AMTTYPE_WriteOff]);

			//  References
			m_C_Invoice_ID = rs.getInt("C_Invoice_ID");
			m_C_Order_ID = rs.getInt("C_Order_ID");
			m_C_Payment_ID = rs.getInt("C_Payment_ID");
			m_C_CashLine_ID = rs.getInt("C_CashLine_ID");
			setReferenceInfo();
		}
		catch (SQLException e)
		{
			log.error("loadDocumentDetails", e);
		}
		return false;
	}   //  loadDocumentDetails

	/**
	 *  Set Reference Details
	 *  - C_CashBook from CashLine
	 *  - C_BankAccount from Payment
	 */
	private void setReferenceInfo()
	{
		//  CashBook Info
		if (m_C_CashLine_ID != 0)
		{
			String sql = "SELECT c.C_CashBook_ID "
				+ "FROM C_Cash c, C_CashLine cl "
				+ "WHERE c.C_Cash_ID=cl.C_Cash_ID AND cl.C_CashLine_ID=?";
			try
			{
				PreparedStatement pstmt = DB.prepareStatement(sql);
				pstmt.setInt(1, m_C_CashLine_ID);
				ResultSet rs = pstmt.executeQuery();
				if (rs.next())
					p_vo.C_CashBook_ID = rs.getInt(1);
				rs.close();
				pstmt.close();
			}
			catch (SQLException e)
			{
				log.error("setReferenceInfo-1", e);
			}
		}   //  CashBook Info

		//  BankAccount
		if (m_C_Payment_ID != 0)
		{
			String sql = "SELECT C_BankAccount_ID "
				+ "FROM C_Payment "
				+ "WHERE C_Payment_ID=?";
			try
			{
				PreparedStatement pstmt = DB.prepareStatement(sql);
				pstmt.setInt(1, m_C_Payment_ID);
				ResultSet rs = pstmt.executeQuery();
				if (rs.next())
					p_vo.C_BankAccount_ID = rs.getInt(1);
				rs.close();
				pstmt.close();
			}
			catch (SQLException e)
			{
				log.error("setReferenceInfo-1", e);
			}
		}   //  BankAccount
	}   //  setReferenceInfo

	/*************************************************************************/

	/**
	 *  Get Source Currency Balance - subtracts line and tax amounts from total - no rounding
	 *  @return positive amount, if total invoice is bigger than lines
	 */
	public BigDecimal getBalance()
	{
		BigDecimal retValue = Env.ZERO;
		return retValue;
	}   //  getBalance

	/**
	 *  Create Facts (the accounting logic) for
	 *  CMA.
	 *  <pre>
	 *  AR_Invoice_Payment
	 *      UnAllocatedCash DR
	 *      DiscountExp     DR
	 *      WriteOff        DR
	 *      Receivables             CR
	 *  AR_Invoice_Cash
	 *      CashTransfer    DR
	 *      DiscountExp     DR
	 *      WriteOff        DR
	 *      Receivables             CR
	 *  AP_Invoice_Payment
	 *      Liability       DR
	 *      DiscountRev             CR
	 *      WriteOff                CR
	 *      PaymentSelect           CR
	 *  AP_Invoice_Cash
	 *      Liability       DR
	 *      DiscountRev             CR
	 *      WriteOff                CR
	 *      CashTransfer            CR
	 *  CashBankTransfer
	 *      -
	 *
	 *  </pre>
	 *  Tax needs to be corrected for discount & write-off;
	 *  Currency gain & loss is realized here.
	 *  @param as accounting schema
	 *  @return Fact
	 */
	public Fact createFact (AcctSchema as)
	{
		//  create Fact Header
		Fact fact = new Fact(this, as, Fact.POST_Actual);

		//  CashBankTransfer - all references null and Discount/WriteOff = 0
		if (m_C_Payment_ID != 0 && m_C_Invoice_ID == 0 && m_C_Order_ID == 0
				&& m_C_CashLine_ID == 0 && p_vo.C_BPartner_ID == 0
				&& Env.ZERO.equals(p_vo.Amounts[Doc.AMTTYPE_Discount])
				&& Env.ZERO.equals(p_vo.Amounts[Doc.AMTTYPE_WriteOff]))
			return fact;
		//  We need to have an Invoice - C_Payment_Post created allocations w/o invoices (fixed)
		if (m_C_Invoice_ID == 0)
			return fact;

		//  We need to have a Business Partner
		if (p_vo.C_BPartner_ID == 0)
		{
			//  Transfers
			//  Unidentified Payments ?
			p_vo.Error = "No Business Partner";
			log.error("createFact - " + p_vo.Error);
			return null;
		}

		//  If there is no Payment Source (Payment/Cash) - it is a allocation w/o accounting consequences (??)
		if (m_C_Payment_ID == 0 && m_C_CashLine_ID == 0)
		{
			p_vo.Error = "No Payment/Cash";
			log.error("createFact - " + p_vo.Error);
			return null;
		}
		//
		Boolean sales = isSales();
		if (sales == null)
		{
			p_vo.Error = "Cannot determine SO/PO";
			log.error("createFact - " + p_vo.Error);
			return null;
		}
		else if (sales.booleanValue())	//	Sales
		{
			if (m_C_Payment_ID != 0)
				fact.createLine(null, getAccount(Doc.ACCTTYPE_UnallocatedCash, as),
					p_vo.C_Currency_ID, getAmount(Doc.AMTTYPE_Allocation), null);
			else
				fact.createLine(null, getAccount(Doc.ACCTTYPE_CashTransfer, as),
					p_vo.C_Currency_ID, getAmount(Doc.AMTTYPE_Allocation), null);
			fact.createLine(null, getAccount(Doc.ACCTTYPE_DiscountExp, as),
				p_vo.C_Currency_ID, getAmount(Doc.AMTTYPE_Discount), null);
			fact.createLine(null, getAccount(Doc.ACCTTYPE_WriteOff, as),
				p_vo.C_Currency_ID, getAmount(Doc.AMTTYPE_WriteOff), null);
			fact.createLine(null, getAccount(Doc.ACCTTYPE_C_Receivable, as),
				p_vo.C_Currency_ID, null, getAmount(Doc.AMTTYPE_Invoice));
		}
		else	//	PO
		{
			fact.createLine(null, getAccount(Doc.ACCTTYPE_V_Liability, as),
				p_vo.C_Currency_ID, getAmount(Doc.AMTTYPE_Invoice), null);
			fact.createLine(null, getAccount(Doc.ACCTTYPE_DiscountRev, as),
				p_vo.C_Currency_ID, null, getAmount(Doc.AMTTYPE_Discount));
			fact.createLine(null, getAccount(Doc.ACCTTYPE_WriteOff, as),
						p_vo.C_Currency_ID, null, getAmount(Doc.AMTTYPE_WriteOff));
			if (m_C_Payment_ID != 0)
				fact.createLine(null, getAccount(Doc.ACCTTYPE_PaymentSelect, as),
					p_vo.C_Currency_ID, null, getAmount(Doc.AMTTYPE_Allocation));
			else
				fact.createLine(null, getAccount(Doc.ACCTTYPE_CashTransfer, as),
					p_vo.C_Currency_ID, null, getAmount(Doc.AMTTYPE_Allocation));
		}
		//
		if (as.isDiscountCorrectTax())
			fact.createTaxCorrection();
		//
		fact.createRealizedGainLoss();
		return fact;
	}   //  createFact

	/**
	 *  Is Sales Trx
	 *  @return true if sales tax - false
	 */
	private Boolean isSales()
	{
		Boolean retValue = null;
		String sql = "SELECT IsSOTrx FROM C_Invoice WHERE C_Invoice_ID=?";
		try
		{
			PreparedStatement pstmt = DB.prepareStatement(sql);
			pstmt.setInt(1, m_C_Invoice_ID);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next())
				retValue = new Boolean ("Y".equals(rs.getString(1)));
			rs.close();
			pstmt.close();
		}
		catch (SQLException e)
		{
			log.error("isSales", e);
		}
		return retValue;
	}   //  isSales

}   //  Doc_Allocation

⌨️ 快捷键说明

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