KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

55 public boolean loadDocumentDetails (FieldProvider [] data,ConnectionProvider conn){
56     DateDoc = data[0].getField("DateTrx");
57     ChargeAmt = data[0].getField("ChargedAmt");
58     SettlementType = data[0].getField("settlementtype");
59     // Contained Objects
60
p_lines = loadLines(conn);
61     if (log4j.isDebugEnabled()) log4j.debug("DocPayment - loadDocumentDetails - Lines=" + p_lines.length);
62     return false;
63 } // loadDocumentDetails
64

65 /**
66  * Load Payment Line. Settlement Cancel
67  * @return DocLine Array
68  */

69 private DocLine[] loadLines(ConnectionProvider conn){
70     ArrayList<Object JavaDoc> list = new ArrayList<Object JavaDoc>();
71     DocLinePaymentData[] data = null;
72     try{
73         data = DocLinePaymentData.select(this, Record_ID);
74     }
75     catch (ServletException e){
76         log4j.warn(e);
77     }
78     for (int i=0;i<data.length;i++){
79         String JavaDoc Line_ID = data[i].cDebtPaymentId;
80         DocLine_Payment docLine = new DocLine_Payment (DocumentType, Record_ID, Line_ID);
81         docLine.Amount = data[i].getField("amount");
82         docLine.WriteOffAmt = data[i].getField("writeoffamt");
83         docLine.isReceipt = data[i].getField("isreceipt");
84         docLine.isManual = data[i].getField("ismanual");
85         docLine.isPaid = data[i].getField("ispaid");
86         docLine.loadAttributes(data[i], this);
87         docLine.m_Record_Id2 = data[i].cDebtPaymentId;
88         docLine.C_Settlement_Generate_ID = data[i].getField("cSettlementGenerateId");
89         docLine.C_Settlement_Cancel_ID = data[i].getField("cSettlementCancelId");
90         docLine.C_GLItem_ID = data[i].getField("cGlitemId");
91         docLine.IsDirectPosting = data[i].getField("isdirectposting");
92         docLine.C_Currency_ID_From = data[i].getField("cCurrencyId");
93         docLine.conversionDate = data[i].getField("conversiondate");
94         try{
95           docLine.dpStatus = DocLinePaymentData.getDPStatus(this, Record_ID, data[i].getField("cDebtPaymentId"));
96         } catch(ServletException e) {
97           log4j.error(e);
98           docLine.dpStatus = "";
99         }
100         if (log4j.isDebugEnabled()) log4j.debug("DocPayment - loadLines - docLine.IsDirectPosting - " + docLine.IsDirectPosting);
101         list.add(docLine);
102     }
103     // Return Array
104
DocLine[] dl = new DocLine[list.size()];
105     list.toArray(dl);
106     return dl;
107 } // loadLines
108

109
110
111 /**
112  * Get Source Currency Balance - always zero
113  * @return Zero (always balanced)
114  */

115 public BigDecimal getBalance(){
116     BigDecimal retValue = ZERO;
117     return retValue;
118 } // getBalance
119

120 /**
121  * Create Facts (the accounting logic) for
122  * STT, APP.
123  * <pre>
124  *
125  * Flow:
126  * 1. Currency conversion variations
127  * 2. Non manual DPs in settlement
128  * 2.1 Cancelled
129  * 2.2 Generated
130  * 3. Manual DPs in settlement
131  * 3.1 Transitory account
132  * 4. Conceptos contables (manual sett and cancelation DP)
133  * 5. Writeoff
134  * 6. Bank in transit
135  *
136  * </pre>
137  * @param as accounting schema
138  * @return Fact
139  */

140   public Fact createFact (AcctSchema as,ConnectionProvider conn,Connection JavaDoc con,VariablesSecureApp vars) throws ServletException{
141     if (log4j.isDebugEnabled()) log4j.debug("DocPayment - createFact - p_lines.length - " + p_lines.length);
142     Fact fact = new Fact(this, as, Fact.POST_Actual);
143     String JavaDoc Fact_Acct_Group_ID = SequenceIdData.getSequence(conn, "Fact_Acct_Group", vars.getClient());
144
145     //Loop to cover C_Debt_Payment in settlement (SttType != 'I' || directPosting=Y)
146
for (int i = 0;p_lines!=null && i < p_lines.length; i++) {
147       DocLine_Payment line = (DocLine_Payment)p_lines[i];
148       
149       if (log4j.isDebugEnabled()) log4j.debug("DocPayment - createFact - line.conversionDate - " + line.conversionDate);
150       //1* Amount is calculated and if there is currency conversion variations between dates this change is accounted
151
String JavaDoc convertedAmt = convertAmount(line.Amount,line.isReceipt.equals("Y"),DateAcct, line.conversionDate, line.C_Currency_ID_From, C_Currency_ID, line, as, fact, Fact_Acct_Group_ID, conn);
152            
153       if(line.isManual.equals("N")) { //2* Normal debt-payments
154
if (!line.C_Settlement_Generate_ID.equals(Record_ID)) { //2.1* Cancelled DP
155
fact.createLine(line, getAccountBPartner(line.m_C_BPartner_ID, as, line.isReceipt.equals("Y"), line.dpStatus, conn),C_Currency_ID,
156                                    (line.isReceipt.equals("Y")?"":convertedAmt),
157                                    (line.isReceipt.equals("Y")?convertedAmt:""),
158                                    Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,conn);
159         } else { //2.2* Generated DP
160
if (log4j.isDebugEnabled()) log4j.debug("Genenarted DP");
161           if(!line.isPaid.equals("Y") || !(line.C_Settlement_Cancel_ID==null || line.C_Settlement_Cancel_ID.equals(""))) {
162             if (log4j.isDebugEnabled()) log4j.debug("Not paid");
163             fact.createLine(line, getAccountBPartner(line.m_C_BPartner_ID, as, line.isReceipt.equals("Y"), line.dpStatus, conn),C_Currency_ID,
164                             (line.isReceipt.equals("Y")?convertedAmt:""),
165                             (line.isReceipt.equals("Y")?"":convertedAmt),
166                             Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,conn);
167           }
168         }
169         
170         if (log4j.isDebugEnabled()) log4j.debug("DocPayment - createFact - No manual - isReceipt: " + line.isReceipt);
171       } else {//3* MANUAL debt-payments (generated in a Manual stt)
172
if (log4j.isDebugEnabled()) log4j.debug("Manual DP - DirectPosting: "+line.IsDirectPosting+" - SettType:"+SettlementType);
173         if (line.IsDirectPosting.equals("Y")) { //Direct posting: transitory Account
174
BigDecimal amount = ZERO;
175           DocPaymentData [] data = DocPaymentData.selectDirectManual(conn, as.m_C_AcctSchema_ID, line.Line_ID);
176           if (log4j.isDebugEnabled()) log4j.debug("data[0].amount:"+data[0].amount+" - convertedAmt:"+convertedAmt);
177           
178           if(convertedAmt!=null && !convertedAmt.equals("")) amount = new BigDecimal(convertedAmt);
179           boolean changeGenerate = (!SettlementType.equals("I"));
180           if(changeGenerate) amount = amount.negate();
181           BigDecimal transitoryAmount = new BigDecimal(convertedAmt);
182           if (log4j.isDebugEnabled()) log4j.debug("Manual DP - amount:"+amount+" - transitoryAmount:"+transitoryAmount+" - Receipt:"+line.isReceipt);
183           //Depending on the stt type and the signum of DP it will be posted on credit or debit
184
if(amount.signum() == 1) {
185             fact.createLine(line, new Account(conn, (line.isReceipt.equals("Y")?data[0].creditAcct:data[0].debitAcct)), C_Currency_ID,
186                             (line.isReceipt.equals("Y")?transitoryAmount.abs().toString():"0"),
187                             (line.isReceipt.equals("Y")?"0":transitoryAmount.abs().toString()), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
188             if ((!changeGenerate && line.isReceipt.equals("N")) || (changeGenerate && line.isReceipt.equals("Y"))) amount = amount.negate();
189           } else {
190             fact.createLine(line, new Account(conn, (line.isReceipt.equals("Y")?data[0].creditAcct:data[0].debitAcct)), C_Currency_ID,
191                             (line.isReceipt.equals("Y")?"0":transitoryAmount.abs().toString()),
192                             (line.isReceipt.equals("Y")?transitoryAmount.abs().toString():"0"), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
193             if ((!changeGenerate && line.isReceipt.equals("Y")) || (changeGenerate && line.isReceipt.equals("N"))) amount = amount.negate();
194           }
195         }
196         //4 Manual Sett + Cancelation Sett (no direct posting) conceptos contables
197
if (SettlementType.equals("I") || line.IsDirectPosting.equals("N")) {
198           DocPaymentData [] data = DocPaymentData.selectManual(conn, as.m_C_AcctSchema_ID, line.Line_ID);
199           for(int j = 0;data!=null && j < data.length;j++){
200             String JavaDoc amountdebit = getConvertedAmt (data[j].amountdebit, line.C_Currency_ID_From, C_Currency_ID, DateAcct, "", AD_Client_ID,AD_Org_ID,conn);
201             String JavaDoc amountcredit = getConvertedAmt (data[j].amountcredit, line.C_Currency_ID_From, C_Currency_ID, DateAcct, "", AD_Client_ID,AD_Org_ID,conn);
202             if (log4j.isDebugEnabled()) log4j.debug("DocPayment - createFact - Conceptos - AmountDebit: " + amountdebit + " - AmountCredit: " + amountcredit);
203             fact.createLine(line, new Account(conn, (line.isReceipt.equals("Y")?data[j].creditAcct:data[j].debitAcct)), C_Currency_ID, (amountdebit.equals("0")?"":amountdebit), (amountcredit.equals("0")?"":amountcredit), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
204           }
205         }
206       } //END debt-payment conditions
207

208       //5* WRITEOFF calculations
209
if (line.C_Settlement_Cancel_ID.equals(Record_ID)) { //Cancelled debt-payments
210
if (line.WriteOffAmt!=null && !line.WriteOffAmt.equals("") && !line.WriteOffAmt.equals("0")) {
211           fact.createLine(line, getAccount(AcctServer.ACCTTYPE_WriteOffDefault, as,conn),C_Currency_ID,
212                           (line.isReceipt.equals("Y")?line.WriteOffAmt:""),
213                           (line.isReceipt.equals("Y")?"":line.WriteOffAmt), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,conn);
214         }
215       }
216
217       //6* PPA - Bank in transit default, paid DPs, (non manual and manual non direct posting)
218
if(line.isPaid.equals("Y")&&((line.C_Settlement_Cancel_ID == null || line.C_Settlement_Cancel_ID.equals(""))||(line.C_Settlement_Cancel_ID.equals(Record_ID)))){
219         BigDecimal finalLineAmt = new BigDecimal(line.Amount);
220         if (line.WriteOffAmt!=null && !line.WriteOffAmt.equals("") && !line.WriteOffAmt.equals("0")) finalLineAmt = finalLineAmt.subtract(new BigDecimal(line.WriteOffAmt));
221         String JavaDoc finalAmtTo = getConvertedAmt (finalLineAmt.toString(), line.C_Currency_ID_From, C_Currency_ID, DateAcct, "", AD_Client_ID,AD_Org_ID,conn);
222         finalLineAmt = new BigDecimal(finalAmtTo);
223         if(finalLineAmt.compareTo(new BigDecimal("0.00"))!=0) fact.createLine(line, getAccount(AcctServer.ACCTTYPE_BankInTransitDefault, as,conn),C_Currency_ID,
224                           (line.isReceipt.equals("Y")?finalAmtTo:""),
225                           (line.isReceipt.equals("Y")?"":finalAmtTo), Fact_Acct_Group_ID, "999999", DocumentType,conn);
226       }
227     } //END of the C_Debt_Payment loop
228
SeqNo = "0";
229     if (log4j.isDebugEnabled()) log4j.debug("DocPayment - createFact - finish");
230     return fact;
231   }
232
233   public String JavaDoc convertAmount(String JavaDoc Amount,boolean isReceipt, String JavaDoc DateAcct, String JavaDoc conversionDate, String JavaDoc C_Currency_ID_From, String JavaDoc C_Currency_ID, DocLine line, AcctSchema as, Fact fact, String JavaDoc Fact_Acct_Group_ID, ConnectionProvider conn) throws ServletException{
234     if (log4j.isDebugEnabled()) log4j.debug("Amount:"+Amount+" curr from:"+C_Currency_ID_From+" Curr to:"+C_Currency_ID+" convDate:"+conversionDate+" DateAcct:"+DateAcct);
235     
236     String JavaDoc Amt = getConvertedAmt (Amount, C_Currency_ID_From, C_Currency_ID,conversionDate, "", AD_Client_ID,AD_Org_ID,conn);
237     if (log4j.isDebugEnabled()) log4j.debug("Amt:"+Amt);
238     
239     String JavaDoc AmtTo = getConvertedAmt (Amount, C_Currency_ID_From, C_Currency_ID, DateAcct, "", AD_Client_ID,AD_Org_ID,conn);
240     if (log4j.isDebugEnabled()) log4j.debug("AmtTo:"+AmtTo);
241     
242     BigDecimal AmtDiff = (new BigDecimal(AmtTo)).subtract(new BigDecimal(Amt));
243     if (log4j.isDebugEnabled()) log4j.debug("AmtDiff:"+AmtDiff);
244     
245     if (log4j.isDebugEnabled()){
246       log4j.debug("curr from:"+C_Currency_ID_From+" Curr to:"+C_Currency_ID+" convDate:"+conversionDate+" DateAcct:"+DateAcct);
247       log4j.debug("Amt:"+Amt+" AmtTo:"+AmtTo+" Diff:"+AmtDiff.toString());
248     }
249
250     if ((isReceipt && AmtDiff.compareTo(new BigDecimal("0.00"))==1) || (!isReceipt && AmtDiff.compareTo(new BigDecimal("0.00"))==-1)) {
251       fact.createLine(line,getAccount(AcctServer.ACCTTYPE_ConvertGainDefaultAmt, as, conn),C_Currency_ID,"", AmtDiff.abs().toString(), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
252     } else {
253       fact.createLine(line,getAccount(AcctServer.ACCTTYPE_ConvertChargeDefaultAmt, as, conn),C_Currency_ID, AmtDiff.abs().toString(),"", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
254     }
255     
256     return Amt;
257   }
258
259
260     /**
261      * Get the account for Accounting Schema
262      * @param AcctType see ACCTTYPE_*
263      * @param as accounting schema
264      * @return Account
265      */

266     public final Account getAccountBPartner(String JavaDoc cBPartnerId, AcctSchema as,boolean isReceipt, String JavaDoc dpStatus, ConnectionProvider conn){
267         DocPaymentData [] data=null;
268         try{
269             if (log4j.isDebugEnabled()) log4j.debug("DocPayment - getAccountBPartner - DocumentType = " + DocumentType);
270             if (isReceipt){
271                 data = DocPaymentData.selectBPartnerCustomerAcct(conn, cBPartnerId, as.getC_AcctSchema_ID(), dpStatus);
272             }else{
273                 data = DocPaymentData.selectBPartnerVendorAcct(conn, cBPartnerId, as.getC_AcctSchema_ID(), dpStatus);
274             }
275         }catch(ServletException e){
276             log4j.warn(e);
277         }
278         // Get Acct
279
String JavaDoc Account_ID = "";
280         if (data != null && data.length!=0){
281             Account_ID = data[0].accountId;
282         }else return null;
283         // No account
284
if (Account_ID.equals("")){
285             log4j.warn("DocPayment - getAccountBPartner - NO account BPartner="
286                 + cBPartnerId + ", Record=" + Record_ID+", status "+dpStatus);
287             return null;
288         }
289         // Return Account
290
Account acct = null;
291         try{
292             acct = Account.getAccount(conn, Account_ID);
293         }catch(ServletException e){
294             log4j.warn(e);
295         }
296         return acct;
297     } // getAccount
298

299     public String JavaDoc nextSeqNo(String JavaDoc oldSeqNo){
300       if (log4j.isDebugEnabled()) log4j.debug("DocPayment - oldSeqNo = " + oldSeqNo);
301       BigDecimal seqNo = new BigDecimal(oldSeqNo);
302       SeqNo = (seqNo.add(new BigDecimal("10"))).toString();
303       if (log4j.isDebugEnabled()) log4j.debug("DocPayment - nextSeqNo = " + SeqNo);
304       return SeqNo;
305     }
306
307   /**
308    * Get Document Confirmation
309    * @not used
310    */

311   public boolean getDocumentConfirmation(ConnectionProvider conn, String JavaDoc strRecordId) {
312     return true;
313   }
314
315     public String JavaDoc getServletInfo() {
316     return "Servlet for the accounting";
317   } // end of getServletInfo() method
318
}
319
Popular Tags