1 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 import org.openbravo.database.ConnectionProvider; 27 import java.sql.Connection ; 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 SeqNo = "0"; 37 private String SettlementType = ""; 38 public final BigDecimal ZERO = new BigDecimal("0"); 39 43 public DocPayment(String AD_Client_ID){ 44 super(AD_Client_ID); 45 } 46 47 public void loadObjectFieldProvider(ConnectionProvider conn, String AD_Client_ID, String Id) throws ServletException{ 48 setObjectFieldProvider(DocPaymentData.selectRegistro(conn, AD_Client_ID, Id)); 49 } 50 51 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 p_lines = loadLines(conn); 61 if (log4j.isDebugEnabled()) log4j.debug("DocPayment - loadDocumentDetails - Lines=" + p_lines.length); 62 return false; 63 } 65 69 private DocLine[] loadLines(ConnectionProvider conn){ 70 ArrayList<Object > list = new ArrayList<Object >(); 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 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 DocLine[] dl = new DocLine[list.size()]; 105 list.toArray(dl); 106 return dl; 107 } 109 110 111 115 public BigDecimal getBalance(){ 116 BigDecimal retValue = ZERO; 117 return retValue; 118 } 120 140 public Fact createFact (AcctSchema as,ConnectionProvider conn,Connection 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 Fact_Acct_Group_ID = SequenceIdData.getSequence(conn, "Fact_Acct_Group", vars.getClient()); 144 145 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 String 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")) { if (!line.C_Settlement_Generate_ID.equals(Record_ID)) { 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 { 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 { if (log4j.isDebugEnabled()) log4j.debug("Manual DP - DirectPosting: "+line.IsDirectPosting+" - SettType:"+SettlementType); 173 if (line.IsDirectPosting.equals("Y")) { 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 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 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 amountdebit = getConvertedAmt (data[j].amountdebit, line.C_Currency_ID_From, C_Currency_ID, DateAcct, "", AD_Client_ID,AD_Org_ID,conn); 201 String 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 } 208 if (line.C_Settlement_Cancel_ID.equals(Record_ID)) { 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 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 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 } SeqNo = "0"; 229 if (log4j.isDebugEnabled()) log4j.debug("DocPayment - createFact - finish"); 230 return fact; 231 } 232 233 public String convertAmount(String Amount,boolean isReceipt, String DateAcct, String conversionDate, String C_Currency_ID_From, String C_Currency_ID, DocLine line, AcctSchema as, Fact fact, String 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 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 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 266 public final Account getAccountBPartner(String cBPartnerId, AcctSchema as,boolean isReceipt, String 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 String Account_ID = ""; 280 if (data != null && data.length!=0){ 281 Account_ID = data[0].accountId; 282 }else return null; 283 if (Account_ID.equals("")){ 285 log4j.warn("DocPayment - getAccountBPartner - NO account BPartner=" 286 + cBPartnerId + ", Record=" + Record_ID+", status "+dpStatus); 287 return null; 288 } 289 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 } 299 public String nextSeqNo(String 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 311 public boolean getDocumentConfirmation(ConnectionProvider conn, String strRecordId) { 312 return true; 313 } 314 315 public String getServletInfo() { 316 return "Servlet for the accounting"; 317 } } 319 | Popular Tags |