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

📄 doc_order.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 Order Documents.
 *  <pre>
 *  Table:              C_Order (259)
 *  Document Types:     SOO, POO,  POR (Requisition)
 *  </pre>
 *  @author Jorg Janke
 *  @version  $Id: Doc_Order.java,v 1.15 2003/03/19 06:47:32 jjanke Exp $
 */
public class Doc_Order extends Doc
{
	/**
	 *  Constructor
	 *  @param AD_Client_ID client
	 */
	protected Doc_Order(int AD_Client_ID)
	{
		super(AD_Client_ID);
	}

	/** Contained Optional Tax Lines    */
	private DocTax[]        m_taxes = null;

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

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

	/**
	 *  Load Specific Document Details
	 *  @param rs result set
	 *  @return true if loadDocumentType was set
	 */
	protected boolean loadDocumentDetails (ResultSet rs)
	{
		try
		{
			p_vo.DateDoc = rs.getTimestamp("DateOrdered");
			p_vo.TaxIncluded = rs.getString("IsTaxIncluded").equals("Y");

			//	Amounts
			p_vo.Amounts[Doc.AMTTYPE_Gross] = rs.getBigDecimal("GrandTotal");
			if (p_vo.Amounts[Doc.AMTTYPE_Gross] == null)
				p_vo.Amounts[Doc.AMTTYPE_Gross] = Env.ZERO;
			p_vo.Amounts[Doc.AMTTYPE_Net] = rs.getBigDecimal("TotalLines");
			if (p_vo.Amounts[Doc.AMTTYPE_Net] == null)
				p_vo.Amounts[Doc.AMTTYPE_Net] = Env.ZERO;
			p_vo.Amounts[Doc.AMTTYPE_Charge] = rs.getBigDecimal("ChargeAmt");
			if (p_vo.Amounts[Doc.AMTTYPE_Charge] == null)
				p_vo.Amounts[Doc.AMTTYPE_Charge] = Env.ZERO;
		}
		catch (SQLException e)
		{
			log.error("loadDocumentDetails", e);
		}
		loadDocumentType();     //  lines require doc type
		//	Contained Objects
		p_lines = loadLines();
		m_taxes = loadTaxes();
	//	Log.trace(Log.l5_DData, "Lines=" + p_lines.length + ", Taxes=" + m_taxes.length);
		return true;
	}   //  loadDocumentDetails


	/**
	 *	Load Invoice Line
	 *  @return DocLine Array
	 */
	private DocLine[] loadLines()
	{
		ArrayList list = new ArrayList();
		String sql = "SELECT * FROM C_OrderLine WHERE C_Order_ID=? ORDER BY Line";
		try
		{
			PreparedStatement pstmt = DB.prepareStatement(sql);
			pstmt.setInt(1, p_vo.Record_ID);
			ResultSet rs = pstmt.executeQuery();
			//
			while (rs.next())
			{
				int Line_ID = rs.getInt("C_OrderLine_ID");
				DocLine docLine = new DocLine (p_vo.DocumentType, p_vo.Record_ID, Line_ID);
				docLine.loadAttributes(rs, p_vo);
				BigDecimal Qty = rs.getBigDecimal("QtyOrdered");
				docLine.setQty(Qty);
				BigDecimal LineNetAmt = rs.getBigDecimal("LineNetAmt");
			//	BigDecimal PriceList = rs.getBigDecimal("PriceList");
				docLine.setAmount (LineNetAmt);
				list.add(docLine);
			}
			//
			rs.close();
			pstmt.close();
		}
		catch (SQLException e)
		{
			log.error ("loadLines", e);
		}

		//	Return Array
		DocLine[] dl = new DocLine[list.size()];
		list.toArray(dl);
		return dl;
	}	//	loadLines

	/**
	 *	Load Invoice Taxes
	 *  @return DocTax Array
	 */
	private DocTax[] loadTaxes()
	{
		ArrayList list = new ArrayList();
		String sql = "SELECT it.C_Tax_ID, t.Name, t.Rate, it.TaxBaseAmt, it.TaxAmt "
			+ "FROM C_Tax t, C_OrderTax it "
			+ "WHERE t.C_Tax_ID=it.C_Tax_ID AND it.C_Order_ID=?";
		try
		{
			PreparedStatement pstmt = DB.prepareStatement(sql);
			pstmt.setInt(1, p_vo.Record_ID);
			ResultSet rs = pstmt.executeQuery();
			//
			while (rs.next())
			{
				int C_Tax_ID = rs.getInt(1);
				String name = rs.getString(2);
				BigDecimal rate = rs.getBigDecimal(3);
				BigDecimal taxBaseAmt = rs.getBigDecimal(4);
				BigDecimal amount = rs.getBigDecimal(5);
				//
				DocTax taxLine = new DocTax(C_Tax_ID, name, rate, taxBaseAmt, amount);
				list.add(taxLine);
			}
			//
			rs.close();
			pstmt.close();
		}
		catch (SQLException e)
		{
			log.error ("loadTaxes", e);
		}

		//	Return Array
		DocTax[] tl = new DocTax[list.size()];
		list.toArray(tl);
		return tl;
	}	//	loadTaxes

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

	/**
	 *  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 = new BigDecimal(0.0);
		StringBuffer sb = new StringBuffer (" [");
		//  Total
		retValue = retValue.add(getAmount(Doc.AMTTYPE_Gross));
		sb.append(getAmount(Doc.AMTTYPE_Gross));
		//  - Charge
		retValue = retValue.subtract(getAmount(Doc.AMTTYPE_Charge));
		sb.append("-").append(getAmount(Doc.AMTTYPE_Charge));
		//  - Tax
		if (m_taxes != null)
		{
			for (int i = 0; i < m_taxes.length; i++)
			{
				retValue = retValue.subtract(m_taxes[i].getAmount());
				sb.append("-").append(m_taxes[i].getAmount());
			}
		}
		//  - Lines
		if (p_lines != null)
		{
			for (int i = 0; i < p_lines.length; i++)
			{
				retValue = retValue.subtract(p_lines[i].getAmount());
				sb.append("-").append(p_lines[i].getAmount());
			}
			sb.append("]");
		}
		//
		log.debug(toString() + " Balance=" + retValue + sb.toString());
		return retValue;
	}   //  getBalance

	/**
	 *  Create Facts (the accounting logic) for
	 *  SOO, POO, POR.
	 *  <pre>
	 *  </pre>
	 *  @param as accounting schema
	 *  @return Fact
	 */
	public Fact createFact (AcctSchema as)
	{
		//  Purchase Order
		if (p_vo.DocumentType.equals(DocVO.DOCTYPE_POrder))
			updateProductInfo(as.getC_AcctSchema_ID());

		//  create Fact Header
		Fact fact = new Fact(this, as, Fact.POST_Actual);
		return fact;
	}   //  createFact

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

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

		/** @todo Last.. would need to compare document/last updated date
		 *  would need to maintain LastPriceUpdateDate on _PO and _Costing */

		//  update Product PO info
		//  should only be once, but here for every AcctSchema
		//  ignores multiple lines with same product - just uses first
		StringBuffer sql = new StringBuffer (
			"UPDATE M_Product_PO po "
			+ "SET PriceLastPO = (SELECT C_Currency_Convert(ol.PriceActual,ol.C_Currency_ID,po.C_Currency_ID,o.DateOrdered,null,o.AD_Client_ID,o.AD_Org_ID) "
			+ "FROM C_Order o, C_OrderLine ol "
			+ "WHERE o.C_Order_ID=ol.C_Order_ID"
			+ " AND po.M_Product_ID=ol.M_Product_ID AND po.C_BPartner_ID=o.C_BPartner_ID"
			+ " AND ROWNUM=1 AND o.C_Order_ID=").append(p_vo.Record_ID).append(") ")
			.append("WHERE EXISTS (SELECT * "
			+ "FROM C_Order o, C_OrderLine ol "
			+ "WHERE o.C_Order_ID=ol.C_Order_ID"
			+ " AND po.M_Product_ID=ol.M_Product_ID AND po.C_BPartner_ID=o.C_BPartner_ID"
			+ " AND o.C_Order_ID=").append(p_vo.Record_ID).append(")");
		int no = DB.executeUpdate(sql.toString());
		log.debug("M_Product_PO - Updated=" + no);

		//  update Product Costing
		//  requires existence of currency conversion !!
		//  if there are multiple lines of the same product last price uses first
		sql = new StringBuffer (
			"UPDATE M_Product_Costing pc "
			+ "SET PriceLastPO = "
			+ "(SELECT C_Currency_Convert(ol.PriceActual,ol.C_Currency_ID,a.C_Currency_ID,o.DateOrdered,null,o.AD_Client_ID,o.AD_Org_ID) "
			+ "FROM C_Order o, C_OrderLine ol, C_AcctSchema a "
			+ "WHERE o.C_Order_ID=ol.C_Order_ID"
			+ " AND pc.M_Product_ID=ol.M_Product_ID AND pc.C_AcctSchema_ID=a.C_AcctSchema_ID"
			+ " AND ROWNUM=1"
			+ " AND pc.C_AcctSchema_ID=").append(C_AcctSchema_ID).append(" AND o.C_Order_ID=").append(p_vo.Record_ID).append(") ")
			.append("WHERE EXISTS (SELECT * "
			+ "FROM C_Order o, C_OrderLine ol, C_AcctSchema a "
			+ "WHERE o.C_Order_ID=ol.C_Order_ID"
			+ " AND pc.M_Product_ID=ol.M_Product_ID AND pc.C_AcctSchema_ID=a.C_AcctSchema_ID"
			+ " AND pc.C_AcctSchema_ID=").append(C_AcctSchema_ID).append(" AND o.C_Order_ID=").append(p_vo.Record_ID).append(")");
		no = DB.executeUpdate(sql.toString());
		log.debug("M_Product_Costing - Updated=" + no);
	}   //  updateProductInfo

}   //  Doc_Order

⌨️ 快捷键说明

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