www.pudn.com > code_source_compiere_erp_crm_logiciel_java.zip > MAllocation.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.model; 
 
import java.sql.*; 
import java.util.*; 
import java.math.*; 
 
import org.compiere.util.*; 
import java.io.*; 
 
/** 
 *  Payment Allocation Model 
 * 
 *  @author 	Jorg Janke 
 *  @version 	$Id: MAllocation.java,v 1.8 2002/09/03 05:10:35 jjanke Exp $ 
 */ 
public final class MAllocation implements Serializable 
{ 
	/** 
	 *  Create Allocation. 
	 *  - load Allocation starting from TYPE_ 
	 *  @param type type of allocation TYPE_ 
	 *  @param Record_ID record id 
	 */ 
	public MAllocation (int type, int Record_ID) 
	{ 
		String sql = "SELECT * FROM C_Allocation WHERE "; 
		if (type == TYPE_Invoice) 
			sql += "C_Invoice_ID=?"; 
		else  if (type == TYPE_Order) 
			sql += "C_Order_ID=?"; 
		else  if (type == TYPE_Payment) 
			sql += "C_Payment_ID=?"; 
		else  if (type == TYPE_CashLine) 
			sql += "C_CashLine_ID=?"; 
		else 
			return; 
 
		//  Retrieve from DB 
		try 
		{ 
			PreparedStatement pstmt = DB.prepareStatement(sql.toString()); 
			pstmt.setInt(1, Record_ID); 
			ResultSet rs = pstmt.executeQuery(); 
			while (rs.next()) 
			{ 
				int C_Invoice_ID = rs.getInt("C_Invoice_ID"); 
				if (!rs.wasNull()) 
					m_listInvoices.add(new Integer(C_Invoice_ID)); 
				// 
				int C_Order_ID = rs.getInt("C_Order_ID"); 
				if (!rs.wasNull()) 
					m_listOrders.add(new Integer(C_Order_ID)); 
				// 
				int C_Payment_ID = rs.getInt("C_Payment_ID"); 
				if (!rs.wasNull()) 
					m_listPayments.add(new Integer(C_Payment_ID)); 
				// 
				int C_CashLine_ID = rs.getInt("C_CashLine_ID"); 
				if (!rs.wasNull()) 
					m_listCashLines.add(new Integer(C_CashLine_ID)); 
				//  Amounts 
				BigDecimal temp = rs.getBigDecimal("Amount"); 
				if (temp != null) 
					m_Amount = m_Amount.add(temp); 
				temp = rs.getBigDecimal("DiscountAmt"); 
				if (temp != null) 
					m_DiscountAmt = m_DiscountAmt.add(temp); 
				temp = rs.getBigDecimal("WriteOffAmt"); 
				if (temp != null) 
					m_WriteOffAmt = m_WriteOffAmt.add(temp); 
			} 
			rs.close(); 
			pstmt.close(); 
		} 
		catch (SQLException e) 
		{ 
			Log.error ("MAllocation.Constructor", e); 
		} 
	}   //  MAllocation 
 
	/** Associated Invoices     */ 
	private ArrayList m_listInvoices = new ArrayList(); 
	/** Associated Orders       */ 
	private ArrayList m_listOrders = new ArrayList(); 
	/** Associated Payments     */ 
	private ArrayList m_listPayments = new ArrayList(); 
	/** Associated CashLines    */ 
	private ArrayList m_listCashLines = new ArrayList(); 
 
	BigDecimal m_Amount = new BigDecimal(0.0); 
	BigDecimal m_DiscountAmt = new BigDecimal(0.0); 
	BigDecimal m_WriteOffAmt = new BigDecimal(0.0); 
 
	/** */ 
	public final static int TYPE_Invoice    = 1; 
	public final static int TYPE_Order      = 2; 
	public final static int TYPE_Payment    = 3; 
	public final static int TYPE_CashLine   = 4; 
 
	/** 
	 *  Dispose 
	 */ 
	public void dispose() 
	{ 
		if (m_listInvoices != null) 
			m_listInvoices.clear(); 
		m_listInvoices = null; 
		if (m_listOrders != null) 
			m_listOrders.clear(); 
		m_listOrders = null; 
		if (m_listPayments != null) 
			m_listPayments.clear(); 
		m_listPayments = null; 
		if (m_listCashLines != null) 
			m_listCashLines.clear(); 
		m_listCashLines = null; 
	}   //  dispose 
 
	/** 
	 *  Get InvoiceIDs 
	 *  @return invoice IDs 
	 */ 
	public ArrayList getInvoiceIDs() 
	{ 
		return m_listInvoices; 
	}   //  getInvoiceIDs 
 
	/** 
	 *  Get OrderIDs 
	 *  @return order IDs 
	 */ 
	public ArrayList getOrderIDs() 
	{ 
		return m_listOrders; 
	}   //  getOrdersIDs 
 
	/** 
	 *  Get PaymentIDs 
	 *  @return payment IDs 
	 */ 
	public ArrayList getPaymentIDs() 
	{ 
		return m_listPayments; 
	}   //  getPaymentIDs 
 
	/** 
	 *  Get CashLineIDs 
	 *  @return cash line IDs 
	 */ 
	public ArrayList getCashLineIDs() 
	{ 
		return m_listCashLines; 
	}   //  getCashLineIDs 
 
	/** 
	 *  Get Total Amount 
	 *  @return total amount 
	 */ 
	public BigDecimal getAmount() 
	{ 
		return m_Amount; 
	}   //  getAmount 
 
	/** 
	 *  Get Total DiscountAmt 
	 *  @return discount amount 
	 */ 
	public BigDecimal getDiscountAmt() 
	{ 
		return m_DiscountAmt; 
	}   //  getDiscountAmt 
 
	/** 
	 *  Get Total WriteOffAmt 
	 *  @return write-off amount 
	 */ 
	public BigDecimal getWriteOffAmt() 
	{ 
		return m_WriteOffAmt; 
	}   //  getWriteOffAmt 
 
	/*************************************************************************/ 
 
	/** 
	 *  Create new Allocation 
	 * 
	 * 	@param ctx context 
	 * 	@param WindowNo window no 
	 * 	@param AD_Client_ID client 
	 * 	@param AD_Org_ID org 
	 * 	@param C_BPartner_ID business partner 
	 * 	@param C_Order_ID order 
	 * 	@param C_Invoice_ID invoice 
	 * 	@param C_Payment_ID payment 
	 * 	@param C_CashLine_ID cash line 
	 * 	@param AllocationNo allocation no 
	 * 	@param C_Currency_ID currency 
	 * 	@param DateTrx trx date 
	 * 	@param IsManual manual trx 
	 * 	@param Amount amount 
	 * 	@param DiscountAmt discount amount 
	 * 	@param WriteOffAmt write-off amount 
	 *  @return C_Allocation_ID or zero if failed 
	 */ 
	public static int createAllocation (Properties ctx, int WindowNo, 
		int AD_Client_ID, int AD_Org_ID, 
		int C_BPartner_ID, int C_Order_ID, int C_Invoice_ID, int C_Payment_ID, int C_CashLine_ID, 
		int AllocationNo, int C_Currency_ID, Timestamp DateTrx, boolean IsManual, 
		BigDecimal Amount, BigDecimal DiscountAmt, BigDecimal WriteOffAmt) 
	{ 
		StringBuffer sql = new StringBuffer("INSERT INTO C_Allocation " 
			+ "(C_Allocation_ID,AD_Client_ID,AD_Org_ID, " 
			+ "IsActive,Created,CreatedBy,Updated,UpdatedBy, " 
			+ "AllocationNo,C_Currency_ID,DateTrx,IsManual, " 
			+ "C_BPartner_ID,C_Invoice_ID,C_Order_ID,C_Payment_ID,C_CashLine_ID, " 
			+ "Amount,DiscountAmt,WriteOffAmt, " 
			+ "Processed,Posted) VALUES ("); 
		// 
		Env.setContext(ctx, WindowNo, "AD_Client_ID", AD_Client_ID);    //  make sure 
		int C_Allocation_ID = DB.getKeyNextNo(ctx, WindowNo, "C_Allocation"); 
		int user = Env.getContextAsInt(ctx, "#AD_User_ID"); 
		// 
		sql.append(C_Allocation_ID).append(","); 
		sql.append(AD_Client_ID).append(",").append(AD_Org_ID); 
		sql.append(",'Y',SysDate,").append(user).append(",SysDate,").append(user).append(","); 
		//  AllocationNo,C_Currency_ID,DateTrx,IsManual, 
		sql.append(AllocationNo).append(","); 
		sql.append(C_Currency_ID).append(","); 
		sql.append(DB.TO_DATE(DateTrx, true)).append(","); 
		sql.append(IsManual ? "'Y'," : "'N',"); 
		//  C_BPartner_ID,C_Invoice_ID,C_Order_ID,C_Payment_ID,C_CashLine_ID, 
		if (C_BPartner_ID == 0) 
			sql.append("NULL,"); 
		else 
			sql.append(C_BPartner_ID).append(","); 
		if (C_Invoice_ID == 0) 
			sql.append("NULL,"); 
		else 
			sql.append(C_Invoice_ID).append(","); 
		if (C_Order_ID == 0) 
			sql.append("NULL,"); 
		else 
			sql.append(C_Order_ID).append(","); 
		if (C_Payment_ID == 0) 
			sql.append("NULL,"); 
		else 
			sql.append(C_Payment_ID).append(","); 
		if (C_CashLine_ID == 0) 
			sql.append("NULL,"); 
		else 
			sql.append(C_CashLine_ID).append(","); 
		//  Amount,DiscountAmt,WriteOffAmt, 
		if (Amount == null) 
			sql.append("0,"); 
		else 
			sql.append(Amount).append(","); 
		if (DiscountAmt == null) 
			sql.append("0,"); 
		else 
			sql.append(DiscountAmt).append(","); 
		if (WriteOffAmt == null) 
			sql.append("0,"); 
		else 
			sql.append(WriteOffAmt).append(","); 
		//  Processed,Posted 
		sql.append("'Y','N')"); 
		// 
		int no = DB.executeUpdate(sql.toString()); 
		// 
		if (no == 1) 
			Log.trace(Log.l3_Util, "MAllocation.createAllocation - " + C_Allocation_ID); 
		else 
		{ 
			Log.error("MAllocation.createAllocation - Not Inserted - " + C_Allocation_ID); 
			C_Allocation_ID = 0; 
		} 
		return C_Allocation_ID; 
	}      //  create Allocation 
 
}   //  MAllocation