KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > erpCommon > ad_forms > DocBank


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

17 package org.openbravo.erpCommon.ad_forms;
18
19 import org.openbravo.erpCommon.utility.SequenceIdData;
20 import org.openbravo.base.secureApp.VariablesSecureApp;
21 import java.math.*;
22 import java.util.*;
23 import javax.servlet.*;
24 import org.apache.log4j.Logger ;
25 // imports for transactions
26
import org.openbravo.database.ConnectionProvider;
27 import java.sql.Connection JavaDoc;
28 import org.openbravo.data.FieldProvider;
29
30
31
32 public class DocBank extends AcctServer {
33     static Logger log4jDocBank = Logger.getLogger(DocBank.class);
34
35     private String JavaDoc SeqNo = "0";
36
37     /**
38      * Constructor
39      * @param AD_Client_ID AD_Client_ID
40      */

41     public DocBank(String JavaDoc AD_Client_ID){
42         super(AD_Client_ID);
43     }
44
45 public void loadObjectFieldProvider(ConnectionProvider conn, String JavaDoc AD_Client_ID, String JavaDoc Id) throws ServletException{
46     setObjectFieldProvider(DocBankData.selectRegistro(conn, AD_Client_ID, Id));
47 }
48
49 /**
50  * Load Specific Document Details
51  * @param rs result set
52  * @return true if loadDocumentType was set
53  */

54 public boolean loadDocumentDetails (FieldProvider [] data,ConnectionProvider conn){
55     DocumentType = AcctServer.DOCTYPE_BankStatement;
56     DateDoc = data[0].getField("StatementDate");
57     // Amounts
58
Amounts[AcctServer.AMTTYPE_Gross] = data[0].getField("StatementDifference");
59     if (Amounts[AcctServer.AMTTYPE_Gross] == null)
60         Amounts[AcctServer.AMTTYPE_Gross] = ZERO.toString();
61
62     // Set Bank Account Info (Currency)
63
setBankAccountInfo(conn);
64     loadDocumentType(); // lines require doc type
65
String JavaDoc strCount = "";
66     try{
67       strCount = DocLineBankData.selectLinesPeriodClosed(conn, Record_ID);
68     }
69     catch (ServletException e){
70       e.printStackTrace();
71       return false;
72     }
73     if(!strCount.equals("0"))return false;
74     else {
75       // Contained Objects
76
p_lines = loadLines(conn);
77       log4jDocBank.debug("Record_ID = " + Record_ID + " - Lines=" + p_lines.length);
78     }
79     return true;
80 } // loadDocumentDetails
81

82 /**
83  * Set Bank Account Info
84  */

85 private void setBankAccountInfo(ConnectionProvider conn){
86     DocBankData [] data =null;
87     log4jDocBank.debug("C_BankAccount_ID : "+ C_BankAccount_ID);
88     try{
89         data = DocBankData.selectCurrency(conn, C_BankAccount_ID);
90     }catch(ServletException e){
91         log4jDocBank.warn(e);
92     }
93     if (data != null && data.length > 0){
94         C_Currency_ID = data[0].cCurrencyId;
95     }
96 } // setBankAccountInfo
97

98
99 /**
100  * Load Invoice Line.
101  * 4 amounts
102  * AMTTYPE_Payment
103  * AMTTYPE_Statement2
104  * AMTTYPE_Charge
105  * AMTTYPE_Interest
106  * @return DocLine Array
107  */

108 private DocLine[] loadLines(ConnectionProvider conn){
109     ArrayList<Object JavaDoc> list = new ArrayList<Object JavaDoc>();
110     DocLineBankData [] data = null;
111     try{
112         data = DocLineBankData.select(conn, Record_ID);
113     }catch(ServletException e){
114         log4jDocBank.warn(e);
115     }
116         //
117
for (int i=0;data!=null && i<data.length;i++){
118         String JavaDoc Line_ID = data[i].getField("C_BANKSTATEMENTLINE_ID");
119         DocLine_Bank docLine = new DocLine_Bank (DocumentType, Record_ID, Line_ID);
120         docLine.loadAttributes(data[i], this);
121         docLine.m_DateAcct = data[i].getField("DATEACCT");
122         docLine.m_DateDoc = data[i].getField("VALUTADATE");
123         docLine.m_C_Payment_ID = data[i].getField("C_DEBT_PAYMENT_ID");
124         docLine.m_Record_Id2 = data[i].getField("C_DEBT_PAYMENT_ID");
125         docLine.m_IsReversal = data[i].getField("ISREVERSAL");
126         docLine.chargeAmt = data[i].getField("CHARGEAMT");
127         docLine.isManual = data[i].getField("ISMANUAL");
128         docLine.setAmount(data[i].getField("STMTAMT")/*, data[i].getField("INTERESTAMT")*/, data[i].getField("TRXAMT"));
129         docLine.convertChargeAmt = data[i].getField("CONVERTCHARGEAMT");
130         list.add(docLine);
131     }
132
133     // Return Array
134
DocLine[] dl = new DocLine[list.size()];
135     list.toArray(dl);
136     return dl;
137 } // loadLines
138

139 /**
140  * Get Source Currency Balance - subtracts line amounts from total - no rounding
141  * @return positive amount, if total invoice is bigger than lines
142  */

143 public BigDecimal getBalance(){
144     BigDecimal retValue = ZERO;
145     StringBuffer JavaDoc sb = new StringBuffer JavaDoc (" [");
146     // Total
147
retValue = retValue.add(new BigDecimal(getAmount(AcctServer.AMTTYPE_Gross)));
148     sb.append(getAmount(AcctServer.AMTTYPE_Gross));
149     // - Lines
150
for (int i = 0; i < p_lines.length; i++){
151         BigDecimal lineBalance = new BigDecimal(((DocLine_Bank)p_lines[i]).m_StmtAmt);
152         retValue = retValue.subtract(lineBalance);
153         sb.append("-").append(lineBalance);
154     }
155     sb.append("]");
156     //
157
log4jDocBank.debug(" Balance=" + retValue + sb.toString());
158     return retValue;
159 } // getBalance
160

161 /**
162  * Create Facts (the accounting logic) for
163  * CMB.
164  * <pre>
165  * BankAsset DR CR (Statement)
166  * BankInTransit DR CR (Payment)
167  * Charge DR (Charge)
168  * Interest DR CR (Interest)
169  * </pre>
170  * @param as accounting schema
171  * @return Fact
172  */

173 public Fact createFact (AcctSchema as,ConnectionProvider conn,Connection JavaDoc con,VariablesSecureApp vars) throws ServletException{
174     log4jDocBank.debug("createFact - Inicio");
175     // create Fact Header
176
Fact fact = null;
177     String JavaDoc Fact_Acct_Group_ID = SequenceIdData.getSequence(conn, "Fact_Acct_Group", vars.getClient());
178     log4jDocBank.debug("createFact - object created");
179     // Header -- there may be different currency amounts
180
BigDecimal TrxAmt = null;
181     BigDecimal ChargeAmt = null;
182     BigDecimal ConvertChargeAmt = null;
183 // BigDecimal InterestAmt = null;
184
// Lines
185
fact = new Fact(this, as, Fact.POST_Actual);
186     for (int i = 0;p_lines != null && i < p_lines.length; i++){
187         DocLine_Bank line = (DocLine_Bank)p_lines[i];
188         //setC_Period_ID(line.m_DateAcct);
189
// BankAsset DR CR (Statement)
190
TrxAmt = new BigDecimal(line.m_TrxAmt);
191         ChargeAmt = new BigDecimal(line.chargeAmt);
192         ConvertChargeAmt = new BigDecimal(line.convertChargeAmt);
193         log4jDocBank.debug("createFact - p_lines.length = " + p_lines.length + " - i="+i);
194         log4jDocBank.debug("createFact - Record_ID = " + Record_ID);
195         log4jDocBank.debug("createFact - C_BPARTNER_ID = " + line.m_C_BPartner_ID);
196        // log4jDocBank.debug("createFact - BPARTNER_ACCT = " + getAccountBPartner(line.m_C_BPartner_ID, as,conn, true));
197
log4jDocBank.debug("createFact - PAYMENT_ID = " + line.m_C_Payment_ID);
198         log4jDocBank.debug("createFact - isManual = " + line.isManual);
199
200 // InterestAmt = new BigDecimal(line.m_InterestAmt);
201
fact.createLine(line,getAccount(AcctServer.ACCTTYPE_BankAsset, as, conn),line.m_C_Currency_ID, line.m_StmtAmt, Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
202         // BankInTransit DR CR (Payment)
203
/*if (!line.m_C_Payment_ID.equals("") && !line.isManual.equals("Y"))fact.createLine(line,getAccountBPartner(line.m_C_BPartner_ID, as,conn, (TrxAmt.negate().compareTo(new BigDecimal("0"))>0?false:true)),line.m_C_Currency_ID, TrxAmt.negate().toString(), conn);
204         else*/
fact.createLine(line,getAccount(AcctServer.ACCTTYPE_BankInTransitDefault, as, conn),line.m_C_Currency_ID, TrxAmt.negate().toString(), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
205         // Charge DR (Charge)
206
fact.createLine(line,new Account(conn,DocLineBankData.selectChargeAccount(conn, C_BankAccount_ID,as.m_C_AcctSchema_ID)),line.m_C_Currency_ID, ChargeAmt.toString(), "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
207         // Interest DR CR (Interest)
208
/* if (InterestAmt.signum() < 0)
209             fact.createLine(line,getAccount(AcctServer.ACCTTYPE_InterestExp, as, conn), getAccount(AcctServer.ACCTTYPE_InterestExp, as, conn),line.m_C_Currency_ID, InterestAmt.negate().toString(), conn);
210         else
211             fact.createLine(line,getAccount(AcctServer.ACCTTYPE_InterestExp, as, conn), getAccount(AcctServer.ACCTTYPE_InterestRev, as, conn),line.m_C_Currency_ID, InterestAmt.negate().toString(), conn);*/

212         //
213
if (ConvertChargeAmt.signum() > 0) // >0 loss
214
fact.createLine(line,getAccount(AcctServer.ACCTTYPE_ConvertChargeLossAmt, as, conn),line.m_C_Currency_ID, line.convertChargeAmt,"", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
215         else
216           fact.createLine(line,getAccount(AcctServer.ACCTTYPE_ConvertChargeGainAmt, as, conn),line.m_C_Currency_ID, "",ConvertChargeAmt.negate().toString(), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
217         
218         log4jDocBank.debug("createTaxCorrection - (NIY)");
219     }
220     SeqNo = "0";
221     return fact;
222 } // createFact
223

224     /**
225      * Get the account for Accounting Schema
226      * @param AcctType see ACCTTYPE_*
227      * @param as accounting schema
228      * @return Account
229      */

230  /* public final Account getAccountBPartner (String cBPartnerId, AcctSchema as,ConnectionProvider conn, boolean receipt){
231         DocPaymentData [] data=null;
232         try{
233             if (receipt){
234                 data = DocPaymentData.selectBPartnerCustomerAcct(conn, as.getC_AcctSchema_ID(), cBPartnerId);
235             }else data = DocPaymentData.selectBPartnerVendorAcct(conn, as.getC_AcctSchema_ID(), cBPartnerId);
236         }catch(ServletException e){
237             log4jDocBank.warn(e);
238         }
239         // Get Acct
240         String Account_ID = "";
241         if (data != null && data.length!=0){
242             Account_ID = data[0].accountId;
243         }else return null;
244         // No account
245         if (Account_ID.equals("")){
246             log4jDocBank.warn("getAccountBPartner - NO account BPartner="
247                 + cBPartnerId + ", Record=" + Record_ID);
248             return null;
249         }
250         // Return Account
251         Account acct = null;
252         try{
253             acct = Account.getAccount(conn, Account_ID);
254         }catch(ServletException e){
255             log4jDocBank.warn(e);
256         }
257         return acct;
258     } */
// getAccount
259

260     public String JavaDoc nextSeqNo(String JavaDoc oldSeqNo){
261       log4jDocBank.debug("DocBank - oldSeqNo = " + oldSeqNo);
262       BigDecimal seqNo = new BigDecimal(oldSeqNo);
263       SeqNo = (seqNo.add(new BigDecimal("10"))).toString();
264       log4jDocBank.debug("DocBank - nextSeqNo = " + SeqNo);
265       return SeqNo;
266     }
267
268   /**
269    * Get Document Confirmation
270    * @not used
271    */

272   public boolean getDocumentConfirmation(ConnectionProvider conn, String JavaDoc strRecordId) {
273     String JavaDoc strCount = "";
274     try {
275       strCount = DocLineBankData.selectLinesPeriodClosed(conn, strRecordId);
276     }catch (ServletException e){
277       e.printStackTrace();
278       return false;
279     }
280     //System.out.println("***************************************** - " + strCount);
281
if(!strCount.equals("0")) return false;
282     else return true;
283   }
284
285     public String JavaDoc getServletInfo() {
286     return "Servlet for the accounting";
287   } // end of getServletInfo() method
288
}
289
Popular Tags