KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > model > MAllocation


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.model;
15
16 import java.sql.*;
17 import java.util.*;
18 import java.math.*;
19
20 import org.compiere.util.*;
21 import java.io.*;
22
23 /**
24  * Payment Allocation Model
25  *
26  * @author Jorg Janke
27  * @version $Id: MAllocation.java,v 1.8 2002/09/03 05:10:35 jjanke Exp $
28  */

29 public final class MAllocation implements Serializable
30 {
31     /**
32      * Create Allocation.
33      * - load Allocation starting from TYPE_
34      * @param type type of allocation TYPE_
35      * @param Record_ID record id
36      */

37     public MAllocation (int type, int Record_ID)
38     {
39         String JavaDoc sql = "SELECT * FROM C_Allocation WHERE ";
40         if (type == TYPE_Invoice)
41             sql += "C_Invoice_ID=?";
42         else if (type == TYPE_Order)
43             sql += "C_Order_ID=?";
44         else if (type == TYPE_Payment)
45             sql += "C_Payment_ID=?";
46         else if (type == TYPE_CashLine)
47             sql += "C_CashLine_ID=?";
48         else
49             return;
50
51         // Retrieve from DB
52
try
53         {
54             PreparedStatement pstmt = DB.prepareStatement(sql.toString());
55             pstmt.setInt(1, Record_ID);
56             ResultSet rs = pstmt.executeQuery();
57             while (rs.next())
58             {
59                 int C_Invoice_ID = rs.getInt("C_Invoice_ID");
60                 if (!rs.wasNull())
61                     m_listInvoices.add(new Integer JavaDoc(C_Invoice_ID));
62                 //
63
int C_Order_ID = rs.getInt("C_Order_ID");
64                 if (!rs.wasNull())
65                     m_listOrders.add(new Integer JavaDoc(C_Order_ID));
66                 //
67
int C_Payment_ID = rs.getInt("C_Payment_ID");
68                 if (!rs.wasNull())
69                     m_listPayments.add(new Integer JavaDoc(C_Payment_ID));
70                 //
71
int C_CashLine_ID = rs.getInt("C_CashLine_ID");
72                 if (!rs.wasNull())
73                     m_listCashLines.add(new Integer JavaDoc(C_CashLine_ID));
74                 // Amounts
75
BigDecimal temp = rs.getBigDecimal("Amount");
76                 if (temp != null)
77                     m_Amount = m_Amount.add(temp);
78                 temp = rs.getBigDecimal("DiscountAmt");
79                 if (temp != null)
80                     m_DiscountAmt = m_DiscountAmt.add(temp);
81                 temp = rs.getBigDecimal("WriteOffAmt");
82                 if (temp != null)
83                     m_WriteOffAmt = m_WriteOffAmt.add(temp);
84             }
85             rs.close();
86             pstmt.close();
87         }
88         catch (SQLException e)
89         {
90             Log.error ("MAllocation.Constructor", e);
91         }
92     } // MAllocation
93

94     /** Associated Invoices */
95     private ArrayList m_listInvoices = new ArrayList();
96     /** Associated Orders */
97     private ArrayList m_listOrders = new ArrayList();
98     /** Associated Payments */
99     private ArrayList m_listPayments = new ArrayList();
100     /** Associated CashLines */
101     private ArrayList m_listCashLines = new ArrayList();
102
103     BigDecimal m_Amount = new BigDecimal(0.0);
104     BigDecimal m_DiscountAmt = new BigDecimal(0.0);
105     BigDecimal m_WriteOffAmt = new BigDecimal(0.0);
106
107     /** */
108     public final static int TYPE_Invoice = 1;
109     public final static int TYPE_Order = 2;
110     public final static int TYPE_Payment = 3;
111     public final static int TYPE_CashLine = 4;
112
113     /**
114      * Dispose
115      */

116     public void dispose()
117     {
118         if (m_listInvoices != null)
119             m_listInvoices.clear();
120         m_listInvoices = null;
121         if (m_listOrders != null)
122             m_listOrders.clear();
123         m_listOrders = null;
124         if (m_listPayments != null)
125             m_listPayments.clear();
126         m_listPayments = null;
127         if (m_listCashLines != null)
128             m_listCashLines.clear();
129         m_listCashLines = null;
130     } // dispose
131

132     /**
133      * Get InvoiceIDs
134      * @return invoice IDs
135      */

136     public ArrayList getInvoiceIDs()
137     {
138         return m_listInvoices;
139     } // getInvoiceIDs
140

141     /**
142      * Get OrderIDs
143      * @return order IDs
144      */

145     public ArrayList getOrderIDs()
146     {
147         return m_listOrders;
148     } // getOrdersIDs
149

150     /**
151      * Get PaymentIDs
152      * @return payment IDs
153      */

154     public ArrayList getPaymentIDs()
155     {
156         return m_listPayments;
157     } // getPaymentIDs
158

159     /**
160      * Get CashLineIDs
161      * @return cash line IDs
162      */

163     public ArrayList getCashLineIDs()
164     {
165         return m_listCashLines;
166     } // getCashLineIDs
167

168     /**
169      * Get Total Amount
170      * @return total amount
171      */

172     public BigDecimal getAmount()
173     {
174         return m_Amount;
175     } // getAmount
176

177     /**
178      * Get Total DiscountAmt
179      * @return discount amount
180      */

181     public BigDecimal getDiscountAmt()
182     {
183         return m_DiscountAmt;
184     } // getDiscountAmt
185

186     /**
187      * Get Total WriteOffAmt
188      * @return write-off amount
189      */

190     public BigDecimal getWriteOffAmt()
191     {
192         return m_WriteOffAmt;
193     } // getWriteOffAmt
194

195     /*************************************************************************/
196
197     /**
198      * Create new Allocation
199      *
200      * @param ctx context
201      * @param WindowNo window no
202      * @param AD_Client_ID client
203      * @param AD_Org_ID org
204      * @param C_BPartner_ID business partner
205      * @param C_Order_ID order
206      * @param C_Invoice_ID invoice
207      * @param C_Payment_ID payment
208      * @param C_CashLine_ID cash line
209      * @param AllocationNo allocation no
210      * @param C_Currency_ID currency
211      * @param DateTrx trx date
212      * @param IsManual manual trx
213      * @param Amount amount
214      * @param DiscountAmt discount amount
215      * @param WriteOffAmt write-off amount
216      * @return C_Allocation_ID or zero if failed
217      */

218     public static int createAllocation (Properties ctx, int WindowNo,
219         int AD_Client_ID, int AD_Org_ID,
220         int C_BPartner_ID, int C_Order_ID, int C_Invoice_ID, int C_Payment_ID, int C_CashLine_ID,
221         int AllocationNo, int C_Currency_ID, Timestamp DateTrx, boolean IsManual,
222         BigDecimal Amount, BigDecimal DiscountAmt, BigDecimal WriteOffAmt)
223     {
224         StringBuffer JavaDoc sql = new StringBuffer JavaDoc("INSERT INTO C_Allocation "
225             + "(C_Allocation_ID,AD_Client_ID,AD_Org_ID, "
226             + "IsActive,Created,CreatedBy,Updated,UpdatedBy, "
227             + "AllocationNo,C_Currency_ID,DateTrx,IsManual, "
228             + "C_BPartner_ID,C_Invoice_ID,C_Order_ID,C_Payment_ID,C_CashLine_ID, "
229             + "Amount,DiscountAmt,WriteOffAmt, "
230             + "Processed,Posted) VALUES (");
231         //
232
Env.setContext(ctx, WindowNo, "AD_Client_ID", AD_Client_ID); // make sure
233
int C_Allocation_ID = DB.getKeyNextNo(ctx, WindowNo, "C_Allocation");
234         int user = Env.getContextAsInt(ctx, "#AD_User_ID");
235         //
236
sql.append(C_Allocation_ID).append(",");
237         sql.append(AD_Client_ID).append(",").append(AD_Org_ID);
238         sql.append(",'Y',SysDate,").append(user).append(",SysDate,").append(user).append(",");
239         // AllocationNo,C_Currency_ID,DateTrx,IsManual,
240
sql.append(AllocationNo).append(",");
241         sql.append(C_Currency_ID).append(",");
242         sql.append(DB.TO_DATE(DateTrx, true)).append(",");
243         sql.append(IsManual ? "'Y'," : "'N',");
244         // C_BPartner_ID,C_Invoice_ID,C_Order_ID,C_Payment_ID,C_CashLine_ID,
245
if (C_BPartner_ID == 0)
246             sql.append("NULL,");
247         else
248             sql.append(C_BPartner_ID).append(",");
249         if (C_Invoice_ID == 0)
250             sql.append("NULL,");
251         else
252             sql.append(C_Invoice_ID).append(",");
253         if (C_Order_ID == 0)
254             sql.append("NULL,");
255         else
256             sql.append(C_Order_ID).append(",");
257         if (C_Payment_ID == 0)
258             sql.append("NULL,");
259         else
260             sql.append(C_Payment_ID).append(",");
261         if (C_CashLine_ID == 0)
262             sql.append("NULL,");
263         else
264             sql.append(C_CashLine_ID).append(",");
265         // Amount,DiscountAmt,WriteOffAmt,
266
if (Amount == null)
267             sql.append("0,");
268         else
269             sql.append(Amount).append(",");
270         if (DiscountAmt == null)
271             sql.append("0,");
272         else
273             sql.append(DiscountAmt).append(",");
274         if (WriteOffAmt == null)
275             sql.append("0,");
276         else
277             sql.append(WriteOffAmt).append(",");
278         // Processed,Posted
279
sql.append("'Y','N')");
280         //
281
int no = DB.executeUpdate(sql.toString());
282         //
283
if (no == 1)
284             Log.trace(Log.l3_Util, "MAllocation.createAllocation - " + C_Allocation_ID);
285         else
286         {
287             Log.error("MAllocation.createAllocation - Not Inserted - " + C_Allocation_ID);
288             C_Allocation_ID = 0;
289         }
290         return C_Allocation_ID;
291     } // create Allocation
292

293 } // MAllocation
294
Popular Tags