KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > acct > Doc_Allocation


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.acct;
15
16 import java.math.*;
17 import java.util.*;
18 import java.sql.*;
19
20 import org.compiere.util.*;
21 import org.compiere.model.*;
22
23 /**
24  * Post Allocation Documents.
25  * <pre>
26  * Table: C_Allocation (390)
27  * Document Types: CMA
28  * </pre>
29  * @author Jorg Janke
30  * @version $Id: Doc_Allocation.java,v 1.15 2003/04/15 05:19:27 jjanke Exp $
31  */

32 public class Doc_Allocation extends Doc
33 {
34     /**
35      * Constructor
36      * @param AD_Client_ID AD_Client_ID
37      */

38     protected Doc_Allocation(int AD_Client_ID)
39     {
40         super(AD_Client_ID);
41     } // Doc_Allocation
42

43     // References
44
private int m_C_Invoice_ID = 0;
45     private int m_C_Order_ID = 0;
46     private int m_C_Payment_ID = 0;
47     private int m_C_CashLine_ID = 0;
48
49     /**
50      * Return TableName of Document
51      * @return C_Allocation
52      */

53     public String JavaDoc getTableName()
54     {
55         return "C_Allocation";
56     } // getTableName
57

58     /**
59      * Get Table ID
60      * @return 390
61      */

62     public int getAD_Table_ID()
63     {
64         return 390;
65     } // getAD_Table_ID
66

67     /**
68      * Load Specific Document Details
69      * @param rs result set
70      * @return true if loadDocumentType was set
71      */

72     protected boolean loadDocumentDetails (ResultSet rs)
73     {
74         p_vo.DocumentType = DocVO.DOCTYPE_Allocation;
75         try
76         {
77             p_vo.DateDoc = rs.getTimestamp("DateTrx");
78             p_vo.DocumentNo = "";
79
80             // Amounts
81
p_vo.Amounts[Doc.AMTTYPE_Allocation] = rs.getBigDecimal("Amount");
82             if (p_vo.Amounts[Doc.AMTTYPE_Allocation] == null)
83                 p_vo.Amounts[Doc.AMTTYPE_Allocation] = Env.ZERO;
84             p_vo.Amounts[Doc.AMTTYPE_Discount] = rs.getBigDecimal("DiscountAmt");
85             if (p_vo.Amounts[Doc.AMTTYPE_Discount] == null)
86                 p_vo.Amounts[Doc.AMTTYPE_Discount] = Env.ZERO;
87             p_vo.Amounts[Doc.AMTTYPE_WriteOff] = rs.getBigDecimal("WriteOffAmt");
88             if (p_vo.Amounts[Doc.AMTTYPE_WriteOff] == null)
89                 p_vo.Amounts[Doc.AMTTYPE_WriteOff] = Env.ZERO;
90             //
91
p_vo.Amounts[Doc.AMTTYPE_Invoice] = p_vo.Amounts[Doc.AMTTYPE_Allocation]
92                 .add(p_vo.Amounts[Doc.AMTTYPE_Discount])
93                 .add(p_vo.Amounts[Doc.AMTTYPE_WriteOff]);
94
95             // References
96
m_C_Invoice_ID = rs.getInt("C_Invoice_ID");
97             m_C_Order_ID = rs.getInt("C_Order_ID");
98             m_C_Payment_ID = rs.getInt("C_Payment_ID");
99             m_C_CashLine_ID = rs.getInt("C_CashLine_ID");
100             setReferenceInfo();
101         }
102         catch (SQLException e)
103         {
104             log.error("loadDocumentDetails", e);
105         }
106         return false;
107     } // loadDocumentDetails
108

109     /**
110      * Set Reference Details
111      * - C_CashBook from CashLine
112      * - C_BankAccount from Payment
113      */

114     private void setReferenceInfo()
115     {
116         // CashBook Info
117
if (m_C_CashLine_ID != 0)
118         {
119             String JavaDoc sql = "SELECT c.C_CashBook_ID "
120                 + "FROM C_Cash c, C_CashLine cl "
121                 + "WHERE c.C_Cash_ID=cl.C_Cash_ID AND cl.C_CashLine_ID=?";
122             try
123             {
124                 PreparedStatement pstmt = DB.prepareStatement(sql);
125                 pstmt.setInt(1, m_C_CashLine_ID);
126                 ResultSet rs = pstmt.executeQuery();
127                 if (rs.next())
128                     p_vo.C_CashBook_ID = rs.getInt(1);
129                 rs.close();
130                 pstmt.close();
131             }
132             catch (SQLException e)
133             {
134                 log.error("setReferenceInfo-1", e);
135             }
136         } // CashBook Info
137

138         // BankAccount
139
if (m_C_Payment_ID != 0)
140         {
141             String JavaDoc sql = "SELECT C_BankAccount_ID "
142                 + "FROM C_Payment "
143                 + "WHERE C_Payment_ID=?";
144             try
145             {
146                 PreparedStatement pstmt = DB.prepareStatement(sql);
147                 pstmt.setInt(1, m_C_Payment_ID);
148                 ResultSet rs = pstmt.executeQuery();
149                 if (rs.next())
150                     p_vo.C_BankAccount_ID = rs.getInt(1);
151                 rs.close();
152                 pstmt.close();
153             }
154             catch (SQLException e)
155             {
156                 log.error("setReferenceInfo-1", e);
157             }
158         } // BankAccount
159
} // setReferenceInfo
160

161     /*************************************************************************/
162
163     /**
164      * Get Source Currency Balance - subtracts line and tax amounts from total - no rounding
165      * @return positive amount, if total invoice is bigger than lines
166      */

167     public BigDecimal getBalance()
168     {
169         BigDecimal retValue = Env.ZERO;
170         return retValue;
171     } // getBalance
172

173     /**
174      * Create Facts (the accounting logic) for
175      * CMA.
176      * <pre>
177      * AR_Invoice_Payment
178      * UnAllocatedCash DR
179      * DiscountExp DR
180      * WriteOff DR
181      * Receivables CR
182      * AR_Invoice_Cash
183      * CashTransfer DR
184      * DiscountExp DR
185      * WriteOff DR
186      * Receivables CR
187      * AP_Invoice_Payment
188      * Liability DR
189      * DiscountRev CR
190      * WriteOff CR
191      * PaymentSelect CR
192      * AP_Invoice_Cash
193      * Liability DR
194      * DiscountRev CR
195      * WriteOff CR
196      * CashTransfer CR
197      * CashBankTransfer
198      * -
199      *
200      * </pre>
201      * Tax needs to be corrected for discount & write-off;
202      * Currency gain & loss is realized here.
203      * @param as accounting schema
204      * @return Fact
205      */

206     public Fact createFact (AcctSchema as)
207     {
208         // create Fact Header
209
Fact fact = new Fact(this, as, Fact.POST_Actual);
210
211         // CashBankTransfer - all references null and Discount/WriteOff = 0
212
if (m_C_Payment_ID != 0 && m_C_Invoice_ID == 0 && m_C_Order_ID == 0
213                 && m_C_CashLine_ID == 0 && p_vo.C_BPartner_ID == 0
214                 && Env.ZERO.equals(p_vo.Amounts[Doc.AMTTYPE_Discount])
215                 && Env.ZERO.equals(p_vo.Amounts[Doc.AMTTYPE_WriteOff]))
216             return fact;
217         // We need to have an Invoice - C_Payment_Post created allocations w/o invoices (fixed)
218
if (m_C_Invoice_ID == 0)
219             return fact;
220
221         // We need to have a Business Partner
222
if (p_vo.C_BPartner_ID == 0)
223         {
224             // Transfers
225
// Unidentified Payments ?
226
p_vo.Error = "No Business Partner";
227             log.error("createFact - " + p_vo.Error);
228             return null;
229         }
230
231         // If there is no Payment Source (Payment/Cash) - it is a allocation w/o accounting consequences (??)
232
if (m_C_Payment_ID == 0 && m_C_CashLine_ID == 0)
233         {
234             p_vo.Error = "No Payment/Cash";
235             log.error("createFact - " + p_vo.Error);
236             return null;
237         }
238         //
239
Boolean JavaDoc sales = isSales();
240         if (sales == null)
241         {
242             p_vo.Error = "Cannot determine SO/PO";
243             log.error("createFact - " + p_vo.Error);
244             return null;
245         }
246         else if (sales.booleanValue()) // Sales
247
{
248             if (m_C_Payment_ID != 0)
249                 fact.createLine(null, getAccount(Doc.ACCTTYPE_UnallocatedCash, as),
250                     p_vo.C_Currency_ID, getAmount(Doc.AMTTYPE_Allocation), null);
251             else
252                 fact.createLine(null, getAccount(Doc.ACCTTYPE_CashTransfer, as),
253                     p_vo.C_Currency_ID, getAmount(Doc.AMTTYPE_Allocation), null);
254             fact.createLine(null, getAccount(Doc.ACCTTYPE_DiscountExp, as),
255                 p_vo.C_Currency_ID, getAmount(Doc.AMTTYPE_Discount), null);
256             fact.createLine(null, getAccount(Doc.ACCTTYPE_WriteOff, as),
257                 p_vo.C_Currency_ID, getAmount(Doc.AMTTYPE_WriteOff), null);
258             fact.createLine(null, getAccount(Doc.ACCTTYPE_C_Receivable, as),
259                 p_vo.C_Currency_ID, null, getAmount(Doc.AMTTYPE_Invoice));
260         }
261         else // PO
262
{
263             fact.createLine(null, getAccount(Doc.ACCTTYPE_V_Liability, as),
264                 p_vo.C_Currency_ID, getAmount(Doc.AMTTYPE_Invoice), null);
265             fact.createLine(null, getAccount(Doc.ACCTTYPE_DiscountRev, as),
266                 p_vo.C_Currency_ID, null, getAmount(Doc.AMTTYPE_Discount));
267             fact.createLine(null, getAccount(Doc.ACCTTYPE_WriteOff, as),
268                         p_vo.C_Currency_ID, null, getAmount(Doc.AMTTYPE_WriteOff));
269             if (m_C_Payment_ID != 0)
270                 fact.createLine(null, getAccount(Doc.ACCTTYPE_PaymentSelect, as),
271                     p_vo.C_Currency_ID, null, getAmount(Doc.AMTTYPE_Allocation));
272             else
273                 fact.createLine(null, getAccount(Doc.ACCTTYPE_CashTransfer, as),
274                     p_vo.C_Currency_ID, null, getAmount(Doc.AMTTYPE_Allocation));
275         }
276         //
277
if (as.isDiscountCorrectTax())
278             fact.createTaxCorrection();
279         //
280
fact.createRealizedGainLoss();
281         return fact;
282     } // createFact
283

284     /**
285      * Is Sales Trx
286      * @return true if sales tax - false
287      */

288     private Boolean JavaDoc isSales()
289     {
290         Boolean JavaDoc retValue = null;
291         String JavaDoc sql = "SELECT IsSOTrx FROM C_Invoice WHERE C_Invoice_ID=?";
292         try
293         {
294             PreparedStatement pstmt = DB.prepareStatement(sql);
295             pstmt.setInt(1, m_C_Invoice_ID);
296             ResultSet rs = pstmt.executeQuery();
297             if (rs.next())
298                 retValue = new Boolean JavaDoc ("Y".equals(rs.getString(1)));
299             rs.close();
300             pstmt.close();
301         }
302         catch (SQLException e)
303         {
304             log.error("isSales", e);
305         }
306         return retValue;
307     } // isSales
308

309 } // Doc_Allocation
310
Popular Tags