KickJava   Java API By Example, From Geeks To Geeks.

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


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 Invoice Documents.
25  * <pre>
26  * Table: C_Cash (407)
27  * Document Types: CMC
28  * </pre>
29  * @author Jorg Janke
30  * @version $Id: Doc_Cash.java,v 1.12 2003/03/19 06:47:32 jjanke Exp $
31  */

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

38     protected Doc_Cash(int AD_Client_ID)
39     {
40         super(AD_Client_ID);
41     }
42
43     /**
44      * Return TableName of Document
45      * @return C_Cash
46      */

47     public String JavaDoc getTableName()
48     {
49         return "C_Cash";
50     } // getTableName
51

52     /**
53      * Get Table ID
54      * @return 407
55      */

56     public int getAD_Table_ID()
57     {
58         return 407;
59     } // getAD_Table_ID
60

61     /**
62      * Load Specific Document Details
63      * @param rs result set
64      * @return true if loadDocumentType was set
65      */

66     protected boolean loadDocumentDetails (ResultSet rs)
67     {
68         p_vo.DocumentType = DocVO.DOCTYPE_CashJournal;
69         try
70         {
71             p_vo.DateDoc = rs.getTimestamp("StatementDate");
72
73             // Amounts
74
p_vo.Amounts[Doc.AMTTYPE_Gross] = rs.getBigDecimal("StatementDifference");
75             if (p_vo.Amounts[Doc.AMTTYPE_Gross] == null)
76                 p_vo.Amounts[Doc.AMTTYPE_Gross] = Env.ZERO;
77         }
78         catch (SQLException e)
79         {
80             log.error("loadDocumentDetails", e);
81         }
82
83         // Set CashBook Org & Currency
84
setCashBookInfo();
85         loadDocumentType(); // lines require doc type
86

87         // Contained Objects
88
p_lines = loadLines();
89         log.debug("Lines=" + p_lines.length);
90         return true;
91     } // loadDocumentDetails
92

93     /**
94      * Set CashBook info.
95      * - CashBook_ID
96      * - Organization
97      * - Currency
98      */

99     private void setCashBookInfo()
100     {
101         int retValue = 0;
102         String JavaDoc sql = "SELECT cb.C_CashBook_ID, cb.AD_Org_ID, cb.C_Currency_ID "
103             + "FROM C_Cash c, C_CashBook cb "
104             + "WHERE c.C_CashBook_ID=cb.C_CashBook_ID AND c.C_Cash_ID=?";
105         try
106         {
107             PreparedStatement pstmt = DB.prepareStatement(sql);
108             pstmt.setInt(1, p_vo.Record_ID);
109             ResultSet rs = pstmt.executeQuery();
110             if (rs.next())
111             {
112                 p_vo.C_CashBook_ID = rs.getInt(1);
113                 p_vo.AD_Org_ID = rs.getInt(2);
114                 p_vo.C_Currency_ID = rs.getInt(3);
115             }
116             rs.close();
117             pstmt.close();
118         }
119         catch (SQLException e)
120         {
121             log.error("setCashBookInfo", e);
122         }
123     } // setCashBookInfo
124

125     /**
126      * Load Cash Line
127      * @return DocLine Array
128      */

129     private DocLine[] loadLines()
130     {
131         ArrayList list = new ArrayList();
132         String JavaDoc sql = "SELECT * FROM C_CashLine WHERE C_Cash_ID=? ORDER BY Line";
133         try
134         {
135             PreparedStatement pstmt = DB.prepareStatement(sql);
136             pstmt.setInt(1, p_vo.Record_ID);
137             ResultSet rs = pstmt.executeQuery();
138             //
139
while (rs.next())
140             {
141                 int Line_ID = rs.getInt("C_CashLine_ID");
142                 DocLine_Cash docLine = new DocLine_Cash (p_vo.DocumentType, p_vo.Record_ID, Line_ID);
143                 docLine.loadAttributes(rs, p_vo);
144                 docLine.setCashType(rs.getString("CashType"));
145                 docLine.setReference(rs.getInt("C_BankAccount_ID"), rs.getInt("C_Invoice_ID"));
146                 docLine.setAmount (rs.getBigDecimal("Amount"),
147                     rs.getBigDecimal("DiscountAmt"), rs.getBigDecimal("WriteOffAmt"));
148                 list.add(docLine);
149             }
150             //
151
rs.close();
152             pstmt.close();
153         }
154         catch (SQLException e)
155         {
156             log.error ("loadLines", e);
157         }
158
159         // Return Array
160
DocLine[] dl = new DocLine[list.size()];
161         list.toArray(dl);
162         return dl;
163     } // loadLines
164

165     /*************************************************************************/
166
167     /**
168      * Get Source Currency Balance - subtracts line amounts from total - no rounding
169      * @return positive amount, if total invoice is bigger than lines
170      */

171     public BigDecimal getBalance()
172     {
173         BigDecimal retValue = Env.ZERO;
174         StringBuffer JavaDoc sb = new StringBuffer JavaDoc (" [");
175         // Total
176
retValue = retValue.add(getAmount(Doc.AMTTYPE_Gross));
177         sb.append(getAmount(Doc.AMTTYPE_Gross));
178         // - Lines
179
for (int i = 0; i < p_lines.length; i++)
180         {
181             retValue = retValue.subtract(p_lines[i].getAmount());
182             sb.append("-").append(p_lines[i].getAmount());
183         }
184         sb.append("]");
185         //
186
log.debug(toString() + " Balance=" + retValue + sb.toString());
187     // return retValue;
188
return Env.ZERO; // Lines are balanced
189
} // getBalance
190

191     /**
192      * Create Facts (the accounting logic) for
193      * CMC.
194      * <pre>
195      * Expense
196      * CashExpense DR
197      * CashAsset CR
198      * Receipt
199      * CashAsset DR
200      * CashReceipt CR
201      * Charge
202      * Charge DR
203      * CashAsset CR
204      * Difference
205      * CashDifference DR
206      * CashAsset CR
207      * Invoice
208      * CashAsset DR
209      * CashTransfer CR
210      * Transfer
211      * BankInTransit DR
212      * CashAsset CR
213      * </pre>
214      * @param as account schema
215      * @return Fact
216      */

217     public Fact createFact (AcctSchema as)
218     {
219         // Need to have CashBook
220
if (p_vo.C_CashBook_ID == 0)
221         {
222             p_vo.Error = "C_CashBook_ID not set";
223             log.error("createFact - " + p_vo.Error);
224             return null;
225         }
226
227         // create Fact Header
228
Fact fact = new Fact(this, as, Fact.POST_Actual);
229
230         // Header posting amt as Invoices and Transfer could be differenet currency
231
// CashAsset Total
232
BigDecimal assetAmt = Env.ZERO;
233
234         // Lines
235
for (int i = 0; i < p_lines.length; i++)
236         {
237             DocLine_Cash line = (DocLine_Cash)p_lines[i];
238             String JavaDoc CashType = line.getCashType();
239
240             if (CashType.equals(DocLine_Cash.CASHTYPE_EXPENSE))
241             { // amount is negative
242
// CashExpense DR
243
// CashAsset CR
244
fact.createLine(line, getAccount(Doc.ACCTTYPE_CashExpense, as),
245                     p_vo.C_Currency_ID, line.getAmount().negate(), null);
246             // fact.createLine(line, getAccount(Doc.ACCTTYPE_CashAsset, as),
247
// p_vo.C_Currency_ID, null, line.getAmount().negate());
248
assetAmt = assetAmt.subtract(line.getAmount().negate());
249             }
250             else if (CashType.equals(DocLine_Cash.CASHTYPE_RECEIPT))
251             { // amount is positive
252
// CashAsset DR
253
// CashReceipt CR
254
// fact.createLine(line, getAccount(Doc.ACCTTYPE_CashAsset, as),
255
// p_vo.C_Currency_ID, line.getAmount(), null);
256
assetAmt = assetAmt.add(line.getAmount());
257                 fact.createLine(line, getAccount(Doc.ACCTTYPE_CashReceipt, as),
258                     p_vo.C_Currency_ID, null, line.getAmount());
259             }
260             else if (CashType.equals(DocLine_Cash.CASHTYPE_CHARGE))
261             { // amount is negative
262
// Charge DR
263
// CashAsset CR
264
fact.createLine(line, line.getChargeAccount(as, getAmount()),
265                     p_vo.C_Currency_ID, line.getAmount().negate(), null);
266             // fact.createLine(line, getAccount(Doc.ACCTTYPE_CashAsset, as),
267
// p_vo.C_Currency_ID, null, line.getAmount().negate());
268
assetAmt = assetAmt.subtract(line.getAmount().negate());
269             }
270             else if (CashType.equals(DocLine_Cash.CASHTYPE_DIFFERENCE))
271             { // amount is pos/neg
272
// CashDifference DR
273
// CashAsset CR
274
fact.createLine(line, getAccount(Doc.ACCTTYPE_CashDifference, as),
275                     p_vo.C_Currency_ID, line.getAmount().negate());
276             // fact.createLine(line, getAccount(Doc.ACCTTYPE_CashAsset, as),
277
// p_vo.C_Currency_ID, line.getAmount());
278
assetAmt = assetAmt.add(line.getAmount());
279             }
280             else if (CashType.equals(DocLine_Cash.CASHTYPE_INVOICE))
281             { // amount is pos/neg
282
// CashAsset DR dr -- Invoice is in Invoice Currency !
283
// CashTransfer cr CR
284
if (line.getC_Currency_ID() == p_vo.C_Currency_ID)
285                     assetAmt = assetAmt.add (line.getAmount());
286                 else
287                     fact.createLine(line,
288                         getAccount(Doc.ACCTTYPE_CashAsset, as),
289                         line.getC_Currency_ID(), line.getAmount());
290                 fact.createLine(line,
291                     getAccount(Doc.ACCTTYPE_CashTransfer, as),
292                     line.getC_Currency_ID(), line.getAmount().negate());
293             }
294             else if (CashType.equals(DocLine_Cash.CASHTYPE_TRANSFER))
295             { // amount is pos/neg
296
// BankInTransit DR dr -- Transfer is in Bank Account Currency
297
// CashAsset dr CR
298
int temp = p_vo.C_BankAccount_ID;
299                 p_vo.C_BankAccount_ID = line.getC_BankAccount_ID();
300                 fact.createLine(line,
301                     getAccount(Doc.ACCTTYPE_BankInTransit, as),
302                     line.getC_Currency_ID(), line.getAmount().negate());
303                 p_vo.C_BankAccount_ID = temp;
304                 if (line.getC_Currency_ID() == p_vo.C_Currency_ID)
305                     assetAmt = assetAmt.add (line.getAmount());
306                 else
307                     fact.createLine(line,
308                         getAccount(Doc.ACCTTYPE_CashAsset, as),
309                         line.getC_Currency_ID(), line.getAmount());
310             }
311         } // lines
312

313         // Cash Asset
314
fact.createLine(null, getAccount(Doc.ACCTTYPE_CashAsset, as),
315             p_vo.C_Currency_ID, assetAmt);
316
317         return fact;
318     } // createFact
319

320 } // Doc_Cash
321
Popular Tags