KickJava   Java API By Example, From Geeks To Geeks.

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


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_BankStatement (392)
27  * Document Types: CMB
28  * </pre>
29  * @author Jorg Janke
30  * @version $Id: Doc_Bank.java,v 1.14 2003/03/19 06:47:32 jjanke Exp $
31  */

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

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

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

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

56     public int getAD_Table_ID()
57     {
58         return 392;
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_BankStatement;
69         try
70         {
71             p_vo.DateDoc = rs.getTimestamp("StatementDate");
72             // Amounts
73
p_vo.Amounts[Doc.AMTTYPE_Gross] = rs.getBigDecimal("StatementDifference");
74             if (p_vo.Amounts[Doc.AMTTYPE_Gross] == null)
75                 p_vo.Amounts[Doc.AMTTYPE_Gross] = Env.ZERO;
76         }
77         catch (SQLException e)
78         {
79             log.error("loadDocumentDetails", e);
80         }
81
82         // Set Bank Account Info (Currency)
83
setBankAccountInfo();
84         loadDocumentType(); // lines require doc type
85

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

92     /**
93      * Set Bank Account Info
94      */

95     private void setBankAccountInfo()
96     {
97         String JavaDoc sql = "SELECT C_Currency_ID FROM C_BankAccount WHERE C_BankAccount_ID=?";
98         try
99         {
100             PreparedStatement pstmt = DB.prepareStatement(sql);
101             pstmt.setInt(1, p_vo.C_BankAccount_ID);
102             ResultSet rs = pstmt.executeQuery();
103             if (rs.next())
104                 p_vo.C_Currency_ID = rs.getInt(1);
105             rs.close();
106             pstmt.close();
107         }
108         catch (SQLException e)
109         {
110             log.error("setBankAccountInfo", e);
111         }
112     } // setBankAccountInfo
113

114     /**
115      * Load Invoice Line.
116      * 4 amounts
117      * AMTTYPE_Payment
118      * AMTTYPE_Statement2
119      * AMTTYPE_Charge
120      * AMTTYPE_Interest
121      * @return DocLine Array
122      */

123     private DocLine[] loadLines()
124     {
125         ArrayList list = new ArrayList();
126         String JavaDoc sql = "SELECT * FROM C_BankStatementLine WHERE C_BankStatement_ID=? ORDER BY Line";
127         try
128         {
129             PreparedStatement pstmt = DB.prepareStatement(sql);
130             pstmt.setInt(1, p_vo.Record_ID);
131             ResultSet rs = pstmt.executeQuery();
132             //
133
while (rs.next())
134             {
135                 int Line_ID = rs.getInt("C_BankStatementLine_ID");
136                 DocLine_Bank docLine = new DocLine_Bank (p_vo.DocumentType, p_vo.Record_ID, Line_ID);
137                 docLine.loadAttributes(rs, p_vo);
138                 docLine.setDateDoc(rs.getTimestamp("ValutaDate"));
139                 docLine.setC_Payment_ID(rs.getInt("C_Payment_ID"));
140                 docLine.setIsReversal(rs.getString("IsReversal"));
141                 docLine.setAmount(rs.getBigDecimal("StmtAmt"), rs.getBigDecimal("InterestAmt"), rs.getBigDecimal("TrxAmt"));
142                 list.add(docLine);
143             }
144             //
145
rs.close();
146             pstmt.close();
147         }
148         catch (SQLException e)
149         {
150             log.error ("loadLines", e);
151         }
152
153         // Return Array
154
DocLine[] dl = new DocLine[list.size()];
155         list.toArray(dl);
156         return dl;
157     } // loadLines
158

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

165     public BigDecimal getBalance()
166     {
167         BigDecimal retValue = Env.ZERO;
168         StringBuffer JavaDoc sb = new StringBuffer JavaDoc (" [");
169         // Total
170
retValue = retValue.add(getAmount(Doc.AMTTYPE_Gross));
171         sb.append(getAmount(Doc.AMTTYPE_Gross));
172         // - Lines
173
for (int i = 0; i < p_lines.length; i++)
174         {
175             BigDecimal lineBalance = ((DocLine_Bank)p_lines[i]).getStmtAmt();
176             retValue = retValue.subtract(lineBalance);
177             sb.append("-").append(lineBalance);
178         }
179         sb.append("]");
180         //
181
log.debug(toString() + " Balance=" + retValue + sb.toString());
182         return retValue;
183     } // getBalance
184

185     /**
186      * Create Facts (the accounting logic) for
187      * CMB.
188      * <pre>
189      * BankAsset DR CR (Statement)
190      * BankInTransit DR CR (Payment)
191      * Charge DR (Charge)
192      * Interest DR CR (Interest)
193      * </pre>
194      * @param as accounting schema
195      * @return Fact
196      */

197     public Fact createFact (AcctSchema as)
198     {
199         // create Fact Header
200
Fact fact = new Fact(this, as, Fact.POST_Actual);
201
202         // Header -- there may be different currency amounts
203

204         // Lines
205
for (int i = 0; i < p_lines.length; i++)
206         {
207             DocLine_Bank line = (DocLine_Bank)p_lines[i];
208             // BankAsset DR CR (Statement)
209
fact.createLine(line,
210                 getAccount(Doc.ACCTTYPE_BankAsset, as),
211                 line.getC_Currency_ID(), line.getStmtAmt());
212             // BankInTransit DR CR (Payment)
213
fact.createLine(line,
214                 getAccount(Doc.ACCTTYPE_BankInTransit, as),
215                 line.getC_Currency_ID(), line.getTrxAmt().negate());
216             // Charge DR (Charge)
217
fact.createLine(line,
218                 line.getChargeAccount(as, line.getChargeAmt().negate()),
219                 line.getC_Currency_ID(), line.getChargeAmt().negate(), null);
220             // Interest DR CR (Interest)
221
if (line.getInterestAmt().signum() < 0)
222                 fact.createLine(line,
223                     getAccount(Doc.ACCTTYPE_InterestExp, as), getAccount(Doc.ACCTTYPE_InterestExp, as),
224                     line.getC_Currency_ID(), line.getInterestAmt().negate());
225             else
226                 fact.createLine(line,
227                     getAccount(Doc.ACCTTYPE_InterestExp, as), getAccount(Doc.ACCTTYPE_InterestRev, as),
228                     line.getC_Currency_ID(), line.getInterestAmt().negate());
229             //
230
fact.createTaxCorrection();
231         }
232         return fact;
233     } // createFact
234

235 } // Doc_Bank
236
Popular Tags