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

📄 doc_invoice.java

📁 Java写的ERP系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************
 * 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 Invoice Documents.
 *  <pre>
 *  Table:              C_Invoice (318)
 *  Document Types:     ARI, ARC, ARF, API, APC
 *  </pre>
 *  @author Jorg Janke
 *  @version  $Id: Doc_Invoice.java,v 1.18 2003/03/19 06:47:32 jjanke Exp $
 */
public class Doc_Invoice extends Doc
{
	/** AD_Table_ID             */
	public static final int C_Invoice_TABLE_ID = 318;

	/**
	 *  Constructor
	 *  @param AD_Client_ID AD_Client_ID
	 */
	protected Doc_Invoice(int AD_Client_ID)
	{
		super(AD_Client_ID);
	}

	/** Contained Optional Tax Lines    */
	private DocTax[]        m_taxes = null;
	/** Loaction                        */
	private int             C_BPartner_Location_ID = 0;

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

	/**
	 *  Get Table ID
	 *  @return 318
	 */
	public int getAD_Table_ID()
	{
		return C_Invoice_TABLE_ID;
	}   //  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("DateInvoiced");
			p_vo.TaxIncluded = rs.getString("IsTaxIncluded").equals("Y");
			C_BPartner_Location_ID = rs.getInt("C_BPartner_Location_ID");

			//	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.debug("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_InvoiceLine WHERE C_Invoice_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_InvoiceLine_ID");
				DocLine_Invoice docLine = new DocLine_Invoice (p_vo.DocumentType, p_vo.Record_ID, Line_ID);
				docLine.loadAttributes(rs, p_vo);
				BigDecimal Qty = rs.getBigDecimal("QtyInvoiced");
				docLine.setQty(Qty);
				BigDecimal LineNetAmt = rs.getBigDecimal("LineNetAmt");
				BigDecimal PriceList = rs.getBigDecimal("PriceList");
				docLine.setAmount (LineNetAmt, PriceList, Qty);
				//
				log.debug(docLine.toString());
				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_InvoiceTax it "
			+ "WHERE t.C_Tax_ID=it.C_Tax_ID AND it.C_Invoice_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);
				log.debug(taxLine.toString());
				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 = Env.ZERO;
		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
		for (int i = 0; i < m_taxes.length; i++)
		{
			retValue = retValue.subtract(m_taxes[i].getAmount());
			sb.append("-").append(m_taxes[i].getAmount());
		}
		//  - Lines
		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
	 *  ARI, ARC, ARF, API, APC.
	 *  <pre>
	 *  ARI, ARF

⌨️ 快捷键说明

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