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

📄 doc_matchpo.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 MatchPO Documents.
 *  <pre>
 *  Table:              C_MatchPO (473)
 *  Document Types:     MXP
 *  </pre>
 *  @author Jorg Janke
 *  @version  $Id: Doc_MatchPO.java,v 1.9 2003/03/19 06:47:32 jjanke Exp $
 */
public class Doc_MatchPO extends Doc
{
	/**
	 *  Constructor
	 *  @param AD_Client_ID client
	 */
	protected Doc_MatchPO(int AD_Client_ID)
	{
		super(AD_Client_ID);
	}   //  Doc_MatchPO

	private int         m_C_OrderLine_ID = 0;
	private int         m_M_InOutLine_ID = 0;
	private int         m_C_InvoiceLine_ID = 0;
	private ProductInfo m_pi;

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

	/**
	 *  Get Table ID
	 *  @return 473
	 */
	public int getAD_Table_ID()
	{
		return 473;
	}   //  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_MatMatchPO;
		try
		{
			p_vo.DateDoc = rs.getTimestamp("DateTrx");
			p_vo.M_Product_ID = rs.getInt("M_Product_ID");
			p_vo.Qty = rs.getBigDecimal("Qty");
			m_C_OrderLine_ID = rs.getInt("C_OrderLine_ID");
			m_M_InOutLine_ID = rs.getInt("M_InOutLine_ID");
			m_C_InvoiceLine_ID = rs.getInt("C_InvoiceLine_ID");
			//
			m_pi = new ProductInfo (p_vo.M_Product_ID);
			m_pi.setQty(p_vo.Qty);
		}
		catch (SQLException e)
		{
			log.error("loadDocumentDetails", e);
		}
		p_vo.C_Currency_ID = Doc.NO_CURRENCY;
		return false;
	}   //  loadDocumentDetails

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

	/**
	 *  Get Source Currency Balance - subtracts line and tax amounts from total - no rounding
	 *  @return Zero - always balanced
	 */
	public BigDecimal getBalance()
	{
		return Env.ZERO;
	}   //  getBalance

	/**
	 *  Create Facts (the accounting logic) for
	 *  MXP.
	 *  <pre>
	 *      Product PPV     <difference>
	 *      PPV_Offset                  <difference>
	 *  </pre>
	 *  @param as accounting schema
	 *  @return Fact
	 */
	public Fact createFact (AcctSchema as)
	{
		//  create Fact Header
		Fact fact = new Fact(this, as, Fact.POST_Actual);
		//  Nothing to do if no Product
		if (p_vo.M_Product_ID == 0)
			return fact;
		//  No posting if not matched to Shipment
		if (m_M_InOutLine_ID == 0)
			return fact;
		//
		p_vo.C_Currency_ID = as.getC_Currency_ID();
		BigDecimal difference = loadInfo(as);   //  PPV difference
		if (difference == null)
		{
			p_vo.Error = "No PPV Difference";
			log.error("createFact - " + p_vo.Error);
			return null;
		}

		//  Product PPV
		FactLine cr = fact.createLine(null,
			m_pi.getAccount(ProductInfo.ACCTTYPE_P_PPV, as),
			as.getC_Currency_ID(), difference);
		//  PPV Offset
		FactLine dr = fact.createLine(null,
			getAccount(Doc.ACCTTYPE_PPVOffset, as),
			as.getC_Currency_ID(), difference.negate());

		return fact;
	}   //  createFact

	/**
	 *  Load Info and return difference.
	 *  Difference between Receipt (cost) and Purchase Order amount
	 * 	@param as accounting schema
	 *  @return difference
	 */
	private BigDecimal loadInfo(AcctSchema as)
	{
		BigDecimal difference = null;
		//  get PO Amount (probably no Acct_Fact)
		String sql = "SELECT PriceActual from C_OrderLine WHERE C_OrderLine_ID=?";
		try
		{
			PreparedStatement pstmt = DB.prepareStatement(sql);
			pstmt.setInt(1, m_C_OrderLine_ID);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next())
			{
				difference = rs.getBigDecimal(1);
			}
			rs.close();
			pstmt.close();
		}
		catch (SQLException e)
		{
			log.error("loadInfo", e);
		}
		if (difference == null)
			return null;

		//  subtract Standard Cost (should get data from Receipt account entry) ??
		//  is "more" correct if Average Costing - but issue if std price has changed in the meantime
		difference.subtract(m_pi.getProductItemCost(as, AcctSchema.COSTING_STANDARD));

		return difference.multiply(p_vo.Qty);
	}   //  loadInfo

	/**
	 *  Update Product Info.
	 *  - Costing (CostStandardPOQty, CostStandardPOAmt)
	 *  @param C_AcctSchema_ID accounting schema
	 */
	private void updateProductInfo (int C_AcctSchema_ID)
	{
		log.debug("updateProductInfo - M_MatchPO_ID=" + p_vo.Record_ID);

		//  update Product Costing
		//  requires existence of currency conversion !!
		StringBuffer sql = new StringBuffer (
			"UPDATE M_Product_Costing pc "
			+ "SET (CostStandardPOQty,CostStandardPOAmt) = "
			+ "(SELECT CostStandardPOQty + m.Qty,"
			+ " CostStandardPOAmt + C_Currency_Convert(ol.PriceActual,ol.C_Currency_ID,a.C_Currency_ID,ol.DateOrdered,null,ol.AD_Client_ID,ol.AD_Org_ID)*m.Qty "
			+ "FROM M_MatchPO m, C_OrderLine ol, C_AcctSchema a "
			+ "WHERE m.C_OrderLine_ID=ol.C_OrderLine_ID"
			+ " AND pc.M_Product_ID=ol.M_Product_ID"
			+ " AND pc.C_AcctSchema_ID=a.C_AcctSchema_ID"
			+ "AND m.M_MatchPO_ID=").append(p_vo.Record_ID).append(") ")
			.append("WHERE pc.C_AcctSchema_ID=").append(C_AcctSchema_ID)
			.append(" AND pc.M_Product_ID=").append(p_vo.M_Product_ID);
		int no = DB.executeUpdate(sql.toString());
		log.debug("M_Product_Costing - Updated=" + no);
	}   //  updateProductInfo

}   //  Doc_MatchPO

⌨️ 快捷键说明

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