KickJava   Java API By Example, From Geeks To Geeks.

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


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 DocCash extends AcctServer {
33     static Logger log4jDocCash = Logger.getLogger(DocCash.class);
34
35     private DocTax[] m_taxes = null;
36     private String JavaDoc SeqNo = "0";
37
38 /**
39  * Constructor
40  * @param AD_Client_ID AD_Client_ID
41  */

42 public DocCash(String JavaDoc AD_Client_ID){
43     super(AD_Client_ID);
44 }
45
46 public void loadObjectFieldProvider(ConnectionProvider conn, String JavaDoc AD_Client_ID, String JavaDoc Id) throws ServletException{
47     log4jDocCash.debug("************************** DocCash - loadObjectFieldProvider - ID - " + Id);
48     setObjectFieldProvider(DocCashData.selectRegistro(conn, AD_Client_ID, Id));
49 }
50
51 /**
52  * Load Specific Document Details
53  * @param rs result set
54  * @return true if loadDocumentType was set
55  */

56 public boolean loadDocumentDetails (FieldProvider [] data,ConnectionProvider conn){
57     DocumentType = DOCTYPE_CashJournal;
58     log4jDocCash.debug("data.length = " + data.length + " - DocumentType = " + DocumentType);
59     DateDoc = data[0].getField("StatementDate");
60
61     // Amounts
62
Amounts[AcctServer.AMTTYPE_Gross] = data[0].getField("StatementDifference");
63     if (Amounts[AcctServer.AMTTYPE_Gross] == null)
64         Amounts[AcctServer.AMTTYPE_Gross] = ZERO.toString();
65
66     // Set CashBook Org & Currency
67
setCashBookInfo();
68     loadDocumentType(); // lines require doc type
69

70     // Contained Objects
71
p_lines = loadLines(conn);
72     log4jDocCash.debug("Lines=" + p_lines.length);
73     return true;
74 } // loadDocumentDetails
75

76
77 /**
78  * Set CashBook info.
79  * - CashBook_ID
80  * - Organization
81  * - Currency
82  */

83 private void setCashBookInfo(){
84     DocCashData [] data = null;
85     try{
86         data = DocCashData.select(this, Record_ID);
87     }
88     catch (ServletException e){
89         log4jDocCash.warn(e);
90     }
91     if (data!=null && data.length!=0){
92         C_CashBook_ID = data[0].cCashbookId;
93         if (AD_Org_ID==null || AD_Org_ID.equals("")) AD_Org_ID = data[0].adOrgId;
94         C_Currency_ID = data[0].cCurrencyId;
95     }
96     log4jDocCash.debug("setCashBookInfo - C_Currency_ID = " + C_Currency_ID + " - AD_Org_ID = " + AD_Org_ID + " - C_CashBook_ID = " + C_CashBook_ID);
97 } // setCashBookInfo
98

99 /**
100  * Load Cash Line
101  * @return DocLine Array
102  */

103 private DocLine[] loadLines(ConnectionProvider conn){
104     ArrayList<Object JavaDoc> list = new ArrayList<Object JavaDoc>();
105     DocLineCashData[] data = null;
106     try{
107         data = DocLineCashData.select(this, Record_ID);
108         for (int i=0;data!=null && i<data.length;i++){
109             String JavaDoc t_Line_ID = data[i].cCashlineId;
110             DocLine_Cash docLine = new DocLine_Cash (DocumentType, Record_ID, t_Line_ID);
111             docLine.Line_ID = t_Line_ID;
112             docLine.loadAttributes(data[i], this);
113             docLine.setCashType(data[i].cashtype);
114             docLine.m_C_Order_Id = data[i].cOrderId;
115             docLine.m_C_Debt_Payment_Id = data[i].cDebtPaymentId;
116             docLine.m_Record_Id2 = data[i].cDebtPaymentId;
117             docLine.m_C_BPartner_ID = DocLineCashData.selectDebtBPartner(this, docLine.m_C_Debt_Payment_Id);
118             docLine.setReference(data[i].cOrderId, data[i].cDebtPaymentId, conn);
119             docLine.setAmount (data[i].amount,data[i].discountamt, data[i].writeoffamt);
120             list.add(docLine);
121         }
122     }
123     catch (ServletException e){
124         log4jDocCash.warn(e);
125     }
126     // Return Array
127
DocLine[] dl = new DocLine[list.size()];
128     list.toArray(dl);
129     return dl;
130 } // loadLines
131

132     /**
133      * Get Source Currency Balance - subtracts line amounts from total - no rounding
134      * @return positive amount, if total invoice is bigger than lines
135      */

136     public BigDecimal getBalance(){
137         BigDecimal retValue = ZERO;
138         StringBuffer JavaDoc sb = new StringBuffer JavaDoc (" [");
139         // Total
140
retValue = retValue.add(new BigDecimal(getAmount(AcctServer.AMTTYPE_Gross)));
141         sb.append(getAmount(AcctServer.AMTTYPE_Gross));
142         // - Lines
143
for (int i = 0; i < p_lines.length; i++){
144             retValue = retValue.subtract(new BigDecimal(p_lines[i].getAmount()));
145             sb.append("-").append(p_lines[i].getAmount());
146         }
147         sb.append("]");
148         //
149
log4jDocCash.debug(" Balance=" + retValue + sb.toString());
150     // return retValue;
151
return ZERO; // Lines are balanced
152
} // getBalance
153

154     /**
155      * Create Facts (the accounting logic) for
156      * CMC.
157      * <pre>
158      * Expense
159      * CashExpense DR
160      * CashAsset CR
161      * Receipt
162      * CashAsset DR
163      * CashReceipt CR
164      * Charge
165      * Charge DR
166      * CashAsset CR
167      * Difference
168      * CashDifference DR
169      * CashAsset CR
170      * Invoice
171      * CashAsset DR
172      * CashTransfer CR
173      * Transfer
174      * BankInTransit DR
175      * CashAsset CR
176      * </pre>
177      * @param as account schema
178      * @return Fact
179      */

180     public Fact createFact (AcctSchema as,ConnectionProvider conn,Connection JavaDoc con,VariablesSecureApp vars) throws ServletException{
181         // Need to have CashBook
182
if (C_CashBook_ID.equals("")){
183             log4jDocCash.warn("createFact - C_CashBook_ID not set");
184             return null;
185         }
186
187         // create Fact Header
188
Fact fact = new Fact(this, as, Fact.POST_Actual);
189         String JavaDoc Fact_Acct_Group_ID = SequenceIdData.getSequence(conn, "Fact_Acct_Group", vars.getClient());
190         // Header posting amt as Invoices and Transfer could be differenet currency
191
// CashAsset Total
192
BigDecimal assetAmt = ZERO;
193         // Lines
194
for (int i = 0;p_lines!=null && i < p_lines.length; i++){
195             DocLine_Cash line = (DocLine_Cash)p_lines[i];
196             String JavaDoc CashType = line.m_CashType;
197             BigDecimal amount = new BigDecimal(line.getAmount());
198             log4jDocCash.debug("antes del creteline, line.getAmount(): " + line.getAmount() + " - CashType: " + CashType);
199             if (CashType.equals(DocLine_Cash.CASHTYPE_EXPENSE)){
200                 // amount is negative
201
// CashExpense DR
202
// CashAsset CR
203
log4jDocCash.debug("********** DocCash - factAcct - account - " + getAccount(AcctServer.ACCTTYPE_CashExpense, as, conn).C_ValidCombination_ID);
204                 fact.createLine(line, getAccount(AcctServer.ACCTTYPE_CashExpense, as, conn),C_Currency_ID, amount.negate().toString(), "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
205             // fact.createLine(line, getAccount(AcctServer.ACCTTYPE_CashAsset, as),
206
// p_vo.C_Currency_ID, null, line.getAmount().negate());
207
assetAmt = assetAmt.subtract(amount.negate());
208             }else if (CashType.equals(DocLine_Cash.CASHTYPE_RECEIPT)){
209                 // amount is positive
210
// CashAsset DR
211
// CashReceipt CR
212
// fact.createLine(line, getAccount(Doc.ACCTTYPE_CashAsset, as),
213
// p_vo.C_Currency_ID, line.getAmount(), null);
214
assetAmt = assetAmt.add(amount);
215                 log4jDocCash.debug("********** DocCash - factAcct - account - " + getAccount(AcctServer.ACCTTYPE_CashReceipt, as, conn).C_ValidCombination_ID);
216                 fact.createLine(line, getAccount(AcctServer.ACCTTYPE_CashReceipt, as, conn),C_Currency_ID, "", amount.toString(), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
217             }else if (CashType.equals(DocLine_Cash.CASHTYPE_CHARGE)){
218                 // amount is negative
219
// Charge DR
220
// CashAsset CR
221
log4jDocCash.debug("********** DocCash - factAcct - account - " + line.getChargeAccount(as,new BigDecimal(getAmount()), conn).C_ValidCombination_ID);
222                 fact.createLine(line, line.getChargeAccount(as,new BigDecimal(getAmount()), conn),C_Currency_ID, amount.negate().toString(), "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
223             // fact.createLine(line, getAccount(Doc.ACCTTYPE_CashAsset, as),
224
// p_vo.C_Currency_ID, null, line.getAmount().negate());
225
assetAmt = assetAmt.subtract(amount.negate());
226             }else if (CashType.equals(DocLine_Cash.CASHTYPE_DIFFERENCE)){
227                 // amount is pos/neg
228
// CashDifference DR
229
// CashAsset CR
230
log4jDocCash.debug("********** DocCash - factAcct - account - " + getAccount(AcctServer.ACCTTYPE_CashDifference, as, conn).C_ValidCombination_ID);
231                 fact.createLine(line, getAccount(AcctServer.ACCTTYPE_CashDifference, as, conn),C_Currency_ID, amount.negate().toString(), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
232             // fact.createLine(line, getAccount(AcctServer.ACCTTYPE_CashAsset, as),
233
// p_vo.C_Currency_ID, line.getAmount());
234
assetAmt = assetAmt.add(amount);
235             }else if (CashType.equals(DocLine_Cash.CASHTYPE_INVOICE)){
236                 // amount is pos/neg
237
// CashAsset DR dr -- Invoice is in Invoice Currency !
238
// CashTransfer cr CR
239
log4jDocCash.debug("********** DocCash - factAcct - account - " + getAccount(AcctServer.ACCTTYPE_CashAsset, as, conn).C_ValidCombination_ID);
240                 if (line.getC_Currency_ID(conn) == C_Currency_ID) assetAmt = assetAmt.add (amount);
241                 else fact.createLine(line,getAccount(AcctServer.ACCTTYPE_CashAsset, as, conn),line.getC_Currency_ID(conn), amount.toString(), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
242                 fact.createLine(line,getAccount(AcctServer.ACCTTYPE_CashTransfer, as, conn),line.getC_Currency_ID(conn), amount.negate().toString(), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
243             }else if (CashType.equals(DocLine_Cash.CASHTYPE_DEBTPAYMENT) || CashType.equals(DocLine_Cash.CASHTYPE_ORDER)){
244                 if (amount.signum() == 1){
245                     log4jDocCash.debug("********** DocCash - factAcct - amount - " + amount.toString() + " - debit");
246                     log4jDocCash.debug("********** DocCash - factAcct - account - " + getAccount(AcctServer.ACCTTYPE_BankInTransitDefault, as, conn).C_ValidCombination_ID);
247                     fact.createLine(line,getAccount(AcctServer.ACCTTYPE_BankInTransitDefault, as, conn),line.getC_Currency_ID(conn), "", amount.toString(), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
248                     fact.createLine(line, getAccount(AcctServer.ACCTTYPE_CashAsset, as, conn),C_Currency_ID, amount.toString(), "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
249                     //fact.createLine(line,getAccount(AcctServer.ACCTTYPE_CashReceipt, as, conn),line.getC_Currency_ID(conn), "", amount.negate().toString(), conn);
250
//assetAmt = assetAmt.add(amount);
251
}else{
252                     log4jDocCash.debug("********** DocCash - factAcct - amount - " + amount.toString() + " - credit");
253                     //fact.createLine(line,getAccount(AcctServer.ACCTTYPE_CashExpense, as, conn),line.getC_Currency_ID(conn), "", amount.toString(), conn);
254
fact.createLine(line,getAccount(AcctServer.ACCTTYPE_BankInTransitDefault, as, conn),line.getC_Currency_ID(conn), amount.negate().toString(), "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
255                     fact.createLine(line, getAccount(AcctServer.ACCTTYPE_CashAsset, as, conn),C_Currency_ID, "", amount.negate().toString(), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
256                     //assetAmt = assetAmt.subtract(amount.negate());
257
}
258             }/*else if (CashType.equals(DocLine_Cash.CASHTYPE_ORDER)){
259                 log4jDocCash.debug("********************* pasa por aqui " + CashType);
260                 String BPartner = "";
261                 String isSOTrx = "";
262                 DocCashData [] data = null;
263                 try{
264                     data = DocCashData.selectBPartner(conn, line.Line_ID);
265                 }
266                 catch (ServletException e){
267                     log4jDocCash.warn(e);
268                 }
269                 if (data!=null && data.length > 0){
270                   BPartner = data[0].cBpartnerId;
271                   isSOTrx = data[0].issotrx;
272                 }
273                 log4jDocCash.debug("DocCash CASHTYPE_ORDER - C_CURRENCY_ID = " + line.getC_Currency_ID(conn));
274                 if (isSOTrx.equals("Y")){
275                     fact.createLine(line,getAccountBPartner(true,BPartner, as, conn),line.getC_Currency_ID(conn), "", amount.toString(), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
276                     //fact.createLine(line,getAccount(AcctServer.ACCTTYPE_CashReceipt, as, conn),line.getC_Currency_ID(conn), "", amount.negate().toString(), conn);
277                     assetAmt = assetAmt.add(amount);
278                 }else{
279                     //fact.createLine(line,getAccount(AcctServer.ACCTTYPE_CashExpense, as, conn),line.getC_Currency_ID(conn), "", amount.toString(), conn);
280                 log4jDocCash.debug("********** DocCash - factAcct - account - " + getAccountBPartner(false,BPartner, as, conn).C_ValidCombination_ID);
281                     fact.createLine(line,getAccountBPartner(false,BPartner, as, conn),line.getC_Currency_ID(conn), amount.negate().toString(), "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
282                     assetAmt = assetAmt.subtract(amount.negate());
283                 }
284             }*/
else if (CashType.equals(DocLine_Cash.CASHTYPE_TRANSFER)){
285                 // amount is pos/neg
286
// BankInTransit DR dr -- Transfer is in Bank Account Currency
287
// CashAsset dr CR
288
String JavaDoc temp = C_BankAccount_ID;
289                 C_BankAccount_ID = line.m_C_BankAccount_ID;
290                 log4jDocCash.debug("********** DocCash - factAcct - account - " + getAccount(AcctServer.ACCTTYPE_BankInTransit, as, conn).C_ValidCombination_ID);
291                 fact.createLine(line,getAccount(AcctServer.ACCTTYPE_BankInTransit, as, conn),line.getC_Currency_ID(conn), amount.negate().toString(), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
292                 C_BankAccount_ID = temp;
293                 if (line.getC_Currency_ID(conn) == C_Currency_ID) assetAmt = assetAmt.add (amount);
294                 else fact.createLine(line,getAccount(AcctServer.ACCTTYPE_CashAsset, as, conn),line.getC_Currency_ID(conn), amount.toString(), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,conn);
295             }
296         } // lines
297

298         // Cash Asset
299
log4jDocCash.debug("********** DocCash - factAcct - account2 - " + getAccount(AcctServer.ACCTTYPE_CashAsset, as, conn).C_ValidCombination_ID);
300         if(!assetAmt.toString().equals("0"))fact.createLine(null, getAccount(AcctServer.ACCTTYPE_CashAsset, as, conn),C_Currency_ID, assetAmt.toString(), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
301         SeqNo = "0";
302         return fact;
303     } // createFact
304

305     /**
306      * Get the account for Accounting Schema
307      * @param AcctType see ACCTTYPE_*
308      * @param as accounting schema
309      * @return Account
310      */

311    /* public final Account getAccountBPartner (boolean receipt, String cBPartnerId, AcctSchema as,ConnectionProvider conn){
312         DocPaymentData [] data=null;
313         try{
314             if (receipt){
315                 data = DocPaymentData.selectBPartnerCustomerAcct(conn, as.getC_AcctSchema_ID(), cBPartnerId);
316             }else {
317                 data = DocPaymentData.selectBPartnerVendorAcct(conn, as.getC_AcctSchema_ID(), cBPartnerId);
318             }
319         }catch(ServletException e){
320             log4jDocCash.warn(e);
321         }
322         // Get Acct
323         String Account_ID = "";
324         if (data != null && data.length!=0){
325             Account_ID = data[0].accountId;
326         }else return null;
327         // No account
328         if (Account_ID.equals("")){
329             log4jDocCash.warn("getAccountBPartner - NO account BPartner="
330                 + cBPartnerId + ", Record=" + Record_ID);
331             return null;
332         }
333         // Return Account
334         Account acct = null;
335         try{
336             acct = Account.getAccount(conn, Account_ID);
337         }catch(ServletException e){
338             log4jDocCash.warn(e);
339         }
340         return acct;
341     } */
// getAccount
342

343     public String JavaDoc nextSeqNo(String JavaDoc oldSeqNo){
344       log4jDocCash.debug("DocCash - oldSeqNo = " + oldSeqNo);
345       BigDecimal seqNo = new BigDecimal(oldSeqNo);
346       SeqNo = (seqNo.add(new BigDecimal("10"))).toString();
347       log4jDocCash.debug("DocCash - nextSeqNo = " + SeqNo);
348       return SeqNo;
349     }
350
351   /**
352    * Get Document Confirmation
353    * @not used
354    */

355   public boolean getDocumentConfirmation(ConnectionProvider conn, String JavaDoc strRecordId) {
356     return true;
357   }
358
359     public String JavaDoc getServletInfo() {
360     return "Servlet for the accounting";
361   } // end of getServletInfo() method
362
}
363
Popular Tags