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

📄 minvoice.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 Smart Business Solution
 * The Initial Developer of the Original Code is Jorg Janke  and ComPiere, Inc.
 * Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
 * created by ComPiere are Copyright (C) ComPiere, Inc.;   All Rights Reserved.
 * Contributor(s): ______________________________________.
 *****************************************************************************/
package org.compiere.model;

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

import org.compiere.util.*;
import org.compiere.print.*;


/**
 *	Invoice Model
 *
 *  @author Jorg Janke
 *  @version $Id: MInvoice.java,v 1.6 2003/04/28 04:18:58 jjanke Exp $
 */
public class MInvoice extends PO
{
	/**
	 * 	Invoice Constructor
	 * 	@param ctx context
	 * 	@param C_Invoice_ID invoice or 0 for new
	 */
	public MInvoice (Properties ctx, int C_Invoice_ID)
	{
		super (ctx, C_Invoice_ID);
		if (C_Invoice_ID == 0)
		{
			setDocStatus ("DR");		//	Draft
			setDocAction ("CO");
			setPaymentRule("P");		//	Payment Terms

			setDateInvoiced (new Timestamp (System.currentTimeMillis ()));
			setDateAcct (new Timestamp (System.currentTimeMillis ()));
			//
			setChargeAmt (Env.ZERO);
			setTotalLines (Env.ZERO);
			setGrandTotal (Env.ZERO);
			//
			setIsSOTrx (true);
			setIsTaxIncluded (false);
			setIsApproved (false);
			setIsDiscountPrinted (false);
			setIsPaid (false);
			setSendEMail (false);
			setIsPrinted (false);
			setIsTransferred (false);
			setPosted(false);
			setProcessed (false);
		}
	}	//	MInvoice

	/**
	 *  Load Constructor
	 *  @param ctx context
	 *  @param rs result set record
	 */
	public MInvoice (Properties ctx, ResultSet rs)
	{
		super (ctx, rs);
	}	//	MOrder

	public static final String		DocBaseType_ARI = "ARI";
	public static final String		DocBaseType_API = "API";

	/**	Open Amount				*/
	private BigDecimal 		m_openAmt = null;

	/**	Invoice Lines			*/
	private MInvoiceLine[]	m_lines;


	/**
	 *  Initialize and return PO_Info
	 *  @param ctx context
	 *  @return POInfo
	 */
	protected POInfo initPO (Properties ctx)
	{
		int AD_Table_ID = 318;
		POInfo poi = POInfo.getPOInfo (ctx, AD_Table_ID);
		return poi;
	}	//	initPO

	/**
	 * 	Set Business Partner Defaults & Details
	 * 	@param bp business partner
	 */
	public void setBPartner (MBPartner bp)
	{
		if (bp == null)
			return;

		setC_BPartner_ID(bp.getC_BPartner_ID());
		//	Set Defaults
		int ii = 0;
		if (isSOTrx())
			ii = bp.getC_PaymentTerm_ID();
		else
			ii = bp.getPO_PaymentTerm_ID();
		if (ii != 0)
			setC_PaymentTerm_ID(ii);
		//
		if (isSOTrx())
			ii = bp.getM_PriceList_ID();
		else
			ii = bp.getPO_PriceList_ID();
		if (ii != 0)
			setM_PriceList_ID(ii);
		//
		String ss = bp.getPaymentRule();
		if (ss != null)
			setPaymentRule(ss);


		//	Set Locations
		MBPartner_Location[] locs = bp.getLocations();
		if (locs != null)
		{
			for (int i = 0; i < locs.length; i++)
			{
				if ((locs[i].isBillTo() && isSOTrx()) || (locs[i].isPayFrom() && !isSOTrx()))
					setC_BPartner_Location_ID(locs[i].getC_BPartner_Location_ID());
			}
			//	set to first
			if (getC_BPartner_Location_ID() == 0 && locs.length > 0)
				setC_BPartner_Location_ID(locs[0].getC_BPartner_Location_ID());
		}
		if (getC_BPartner_Location_ID() == 0)
			log.error("setBPartner - Has no To Address: " + bp);

		//	Set Contact
		MBPartner_Contact[] contacts = bp.getContacts();
		if (contacts != null && contacts.length == 1)
			setC_BPartner_Contact_ID(contacts[0].getC_BPartner_Contact_ID());
	}	//	setBPartner

	/**
	 * 	Set Target Document Type
	 * 	@param DocBaseType doc type
	 */
	public void setC_DocTypeTarget_ID (String DocBaseType)
	{
		int C_DocType_ID = 0;
		//
		String sql = "SELECT C_DocType_ID FROM C_DocType WHERE AD_Client_ID=? AND DocBaseType=? ORDER BY IsDefault DESC";
		PreparedStatement pstmt = null;
		try
		{
			pstmt = DB.prepareStatement(sql);
			pstmt.setInt(1, getAD_Org_ID());
			pstmt.setString(2, DocBaseType);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next())
				C_DocType_ID = rs.getInt(1);
			rs.close();
			pstmt.close();
			pstmt = null;
		}
		catch (Exception e)
		{
			log.error("setC_DocTypeTarget_ID", e);
		}
		finally
		{
			try
			{
				if (pstmt != null)
					pstmt.close ();
			}
			catch (Exception e)
			{}
			pstmt = null;
		}
		if (C_DocType_ID == 0)
			log.error("setC_DocTypeTarget_ID - Not found for AC_Client_ID=" + getAD_Client_ID() + " - " + DocBaseType);
		else
		{
			log.debug ("setC_DocTypeTarget_ID - " + DocBaseType);
			setC_DocTypeTarget_ID (C_DocType_ID);
			setIsSOTrx (DocBaseType_ARI.equals(DocBaseType));
		}
	}	//	setC_DocTypeTarget_ID

	/**
	 * 	Set Defaults for mandatory values where not set yet
	 */
	private void setDefaults ()
	{
		log.debug("setDefaults");
		int AD_Client_ID = getAD_Client_ID();
		//	No Partner Info - set Template
		if (getC_BPartner_ID() == 0)
			setBPartner(MBPartner.getTemplate(getCtx(), AD_Client_ID));
		if (getC_BPartner_Location_ID() == 0)
			setBPartner(new MBPartner(getCtx(), getC_BPartner_ID()));

		//	Price List
		if (getM_PriceList_ID() == 0)
		{
			int ii = Env.getContextAsInt(getCtx(), "#M_PriceList_ID");
			if (ii != 0)
				setM_PriceList_ID(ii);
			else
			{
				String sql = "SLECT M_PriceList_ID FROM M_PriceList WHERE AD_Client_ID=? AND IsDefault='Y'";
				ii = DB.getSQLValue (sql, AD_Client_ID);
				if (ii != 0)
					setM_PriceList_ID (ii);
			}
		}
		//	Currency
		if (getC_Currency_ID() == 0)
		{
			String sql = "SLECT C_Currency_ID FROM M_PriceList WHERE M_PriceList_ID=?";
			int ii = DB.getSQLValue (sql, getM_PriceList_ID());
			if (ii != 0)
				setC_Currency_ID (ii);
			else
				setC_Currency_ID(Env.getContextAsInt(getCtx(), "#C_Currency_ID"));
		}

		//	Sales Rep
		if (getSalesRep_ID() == 0)
		{
			int ii = Env.getContextAsInt(getCtx(), "#SalesRep_ID");
			if (ii != 0)
				setSalesRep_ID (ii);
		}

		//	Document Type
		if (getC_DocType_ID() == 0)
			setC_DocType_ID (0);	//	make sure it's set to 0
		if (getC_DocTypeTarget_ID() == 0)
			setC_DocTypeTarget_ID(isSOTrx() ? DocBaseType_ARI : DocBaseType_API);

		//	Payment Term
		if (getC_PaymentTerm_ID() == 0)
		{
			int ii = Env.getContextAsInt(getCtx(), "#C_PaymentTerm_ID");
			if (ii != 0)
				setC_PaymentTerm_ID (ii);
			else
			{
				String sql = "SELECT C_PaymentTerm_ID FROM C_PaymentTerm WHERE AD_Client_ID=? AND IsDefault='Y'";
				ii = DB.getSQLValue(sql, AD_Client_ID);
				if (ii != 0)
					setC_PaymentTerm_ID (ii);
			}
		}
	}	//	setDefaults


	/**
	 * 	Get Invoice Lines
	 * 	@return lines
	 */
	public MInvoiceLine[] getLines()
	{
		if (m_lines == null || m_lines.length == 0)
			;
		else
			return m_lines;
		//
		ArrayList list = new ArrayList();
		String sql = "SELECT * FROM C_InvoiceLine WHERE C_Invoice_ID=? ORDER BY Line";
		PreparedStatement pstmt = null;
		try
		{
			pstmt = DB.prepareStatement(sql);
			pstmt.setInt(1, getC_Invoice_ID());
			ResultSet rs = pstmt.executeQuery();
			while (rs.next())
				list.add(new MInvoiceLine(getCtx(), rs));
			rs.close();
			pstmt.close();
			pstmt = null;
		}
		catch (Exception e)
		{
			log.error("getLines", e);
		}
		finally
		{
			try
			{
				if (pstmt != null)
					pstmt.close ();
			}
			catch (Exception e)
			{}
			pstmt = null;
		}

		//
		m_lines = new MInvoiceLine[list.size()];
		list.toArray(m_lines);
		return m_lines;
	}	//	getLines



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

	/**
	 * 	Save Invoice
	 * 	@return true if saved
	 */
	public boolean save ()
	{
		log.debug ("save");
		setDefaults();
		if (getDocumentNo() == null)
		{
			String DocumentNo = DB.getDocumentNo (getAD_Client_ID (), getC_DocTypeTarget_ID ());
			setDocumentNo (DocumentNo);
		}
		return super.save ();
	}	//	save


	public String toString ()
	{
		StringBuffer sb = new StringBuffer ("MInvoice[")
			.append(getID())
			.append ("]");
		return sb.toString ();
	}



	public void setC_Activity_ID (int C_Activity_ID)
	{
		setValue ("C_Activity_ID", new Integer (C_Activity_ID));
	}

	public int getC_Activity_ID ()
	{
		Integer ii = (Integer)getValue ("C_Activity_ID");
		if (ii == null)
			return 0;
		return ii.intValue ();
	}

	public void setC_BPartner_Location_ID (int C_BPartner_Location_ID)
	{
		setValue ("C_BPartner_Location_ID", new Integer (C_BPartner_Location_ID));
	}

	public int getC_BPartner_Location_ID ()
	{
		Integer ii = (Integer)getValue ("C_BPartner_Location_ID");
		if (ii == null)
			return 0;
		return ii.intValue ();
	}

	public int getC_Invoice_ID ()
	{
		return getID();
	}

	public void setC_BPartner_Contact_ID (int C_BPartner_Contact_ID)
	{
		setValue ("C_BPartner_Contact_ID", new Integer (C_BPartner_Contact_ID));
	}

	public int getC_BPartner_Contact_ID ()
	{
		Integer ii = (Integer)getValue ("C_BPartner_Contact_ID");
		if (ii == null)
			return 0;
		return ii.intValue ();
	}

	public void setDateAcct (Timestamp DateAcct)
	{
		if (DateAcct == null)
			throw new IllegalArgumentException ("DateAcct is mandatory");
		setValue ("DateAcct", DateAcct);
	}

	public Timestamp getDateAcct ()
	{
		return (Timestamp)getValue ("DateAcct");
	}

	void setDateOrdered (Timestamp DateOrdered)
	{
		setValueNoCheck ("DateOrdered", DateOrdered);
	}

	public Timestamp getDateOrdered ()
	{
		return (Timestamp)getValue ("DateOrdered");
	}

	/**
	 * 	Set Price List (and Currency) when valid
	 * 	@param M_PriceList_ID price list
	 */
	public void setM_PriceList_ID (int M_PriceList_ID)
	{
		String sql = "SELECT M_PriceList_ID, C_Currency_ID "
			+ "FROM M_PriceList WHERE M_PriceList_ID=?";
		PreparedStatement pstmt = null;
		try
		{
			pstmt = DB.prepareStatement(sql);
			pstmt.setInt(1, M_PriceList_ID);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next())
			{
				setValue ("M_PriceList_ID", new Integer(rs.getInt(1)));
				setC_Currency_ID (rs.getInt(2));
			}
			rs.close();
			pstmt.close();
			pstmt = null;
		}
		catch (Exception e)
		{
			log.error("setM_PriceList_ID", e);
		}
		finally
		{
			try
			{
				if (pstmt != null)
					pstmt.close ();
			}
			catch (Exception e)
			{}
			pstmt = null;
		}
	}

	public int getM_PriceList_ID ()
	{
		Integer ii = (Integer)getValue ("M_PriceList_ID");
		if (ii == null)
			return 0;
		return ii.intValue ();
	}

	void setC_Order_ID (int C_Order_ID)
	{
		setValueNoCheck ("C_Order_ID", new Integer (C_Order_ID));
	}

	public int getC_Order_ID ()
	{
		Integer ii = (Integer)getValue ("C_Order_ID");
		if (ii == null)
			return 0;
		return ii.intValue ();
	}

	public void setC_Campaign_ID (int C_Campaign_ID)
	{
		setValue ("C_Campaign_ID", new Integer (C_Campaign_ID));
	}

	public int getC_Campaign_ID ()
	{
		Integer ii = (Integer)getValue ("C_Campaign_ID");
		if (ii == null)
			return 0;
		return ii.intValue ();
	}

	void setDocStatus (String DocStatus)
	{
		if (DocStatus.equals ("??") || DocStatus.equals ("AP")
		  || DocStatus.equals ("CH") || DocStatus.equals ("CL")
		  || DocStatus.equals ("CO") || DocStatus.equals ("DR")
		  || DocStatus.equals ("IN") || DocStatus.equals ("IP")
		  || DocStatus.equals ("NA") || DocStatus.equals ("PE")
		  || DocStatus.equals ("PO") || DocStatus.equals ("PR")
		  || DocStatus.equals ("RE") || DocStatus.equals ("TE")
		  || DocStatus.equals ("TR") || DocStatus.equals ("VO")
		  || DocStatus.equals ("WP") || DocStatus.equals ("XX"))
			;
		else
			throw new IllegalArgumentException ("DocStatus Invalid value - Reference_ID=131 - ?? - AP - CH - CL - CO - DR - IN - IP - NA - PE - PO - PR - RE - TE - TR - VO - WP - XX");
		if (DocStatus == null)
			throw new IllegalArgumentException ("DocStatus is mandatory");
		setValueNoCheck ("DocStatus", DocStatus);
	}

	public String getDocStatus ()
	{
		return (String)getValue ("DocStatus");
	}

	void setDocAction (String DocAction)
	{
		if (DocAction.equals ("--") || DocAction.equals ("AP")
		  || DocAction.equals ("CL") || DocAction.equals ("CO")
		  || DocAction.equals ("PO") || DocAction.equals ("PR")
		  || DocAction.equals ("RA") || DocAction.equals ("RC")
		  || DocAction.equals ("RE") || DocAction.equals ("RJ")
		  || DocAction.equals ("TR") || DocAction.equals ("VO")
		  || DocAction.equals ("XL"))
			;
		else
			throw new IllegalArgumentException ("DocAction Invalid value - Reference_ID=135 - __ - AP - CL - CO - PO - PR - RA - RC - TE - RJ - VO - XL");
		if (DocAction == null)
			throw new IllegalArgumentException ("DocAction is mandatory");
		setValueNoCheck ("DocAction", DocAction);
	}

	public String getDocAction ()
	{
		return (String)getValue ("DocAction");
	}

	void setGrandTotal (BigDecimal GrandTotal)

⌨️ 快捷键说明

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