[Doubtful Debt] Fixes and formats accounting
authorEduardo Argal Guibert <eduardo.argal@openbravo.com>
Tue, 15 Jan 2013 19:01:35 +0100
changeset 19570 75194f603f33
parent 19569 a2580c51ed45
child 19571 28d35fd3cae2
[Doubtful Debt] Fixes and formats accounting
src/org/openbravo/erpCommon/ad_forms/DocFINFinAccTransaction.java
src/org/openbravo/erpCommon/ad_forms/DocFINPayment.java
src/org/openbravo/erpCommon/ad_forms/DocFINReconciliation.java
--- a/src/org/openbravo/erpCommon/ad_forms/DocFINFinAccTransaction.java	Tue Jan 15 18:28:43 2013 +0100
+++ b/src/org/openbravo/erpCommon/ad_forms/DocFINFinAccTransaction.java	Tue Jan 15 19:01:35 2013 +0100
@@ -434,12 +434,16 @@
     FIN_FinaccTransaction transaction = OBDal.getInstance().get(FIN_FinaccTransaction.class,
         Record_ID);
     String Fact_Acct_Group_ID = SequenceIdData.getUUID();
+    String Fact_Acct_Group_ID2 = SequenceIdData.getUUID();
+    String Fact_Acct_Group_ID3 = SequenceIdData.getUUID();
     boolean isReceipt = transaction.getFinPayment().isReceipt();
     Currency paymentCurrency = transaction.getFinPayment().getCurrency();
     if (!getDocumentPaymentConfirmation(transaction.getFinPayment())) {
       for (int i = 0; p_lines != null && i < p_lines.length; i++) {
         DocLine_FINFinAccTransaction line = (DocLine_FINFinAccTransaction) p_lines[i];
         boolean isPrepayment = "Y".equals(line.getIsPrepayment());
+        String bpartnerId = (line.m_C_BPartner_ID == null || line.m_C_BPartner_ID.equals("")) ? this.C_BPartner_ID
+            : line.m_C_BPartner_ID;
         BigDecimal bpamount = new BigDecimal(line.getAmount());
         if (!"".equals(line.getWriteOffAmt())
             && ZERO.compareTo(new BigDecimal(line.getWriteOffAmt())) != 0) {
@@ -490,28 +494,20 @@
             BigDecimal doubtFulDebtAmount = convertAmount(line.getDoubtFulDebtAmount(), isReceipt,
                 DateAcct, TABLEID_Invoice, invoice.getId(), C_Currency_ID, as.m_C_Currency_ID,
                 line, as, fact, Fact_Acct_Group_ID, nextSeqNo(SeqNo), conn, false);
-            fact.createLine(
-                line,
-                getAccountBPartner(
-                    (line.m_C_BPartner_ID == null || line.m_C_BPartner_ID.equals("")) ? this.C_BPartner_ID
-                        : line.m_C_BPartner_ID, as, true, false, true, conn), C_Currency_ID, "",
-                doubtFulDebtAmount.toString(), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,
-                conn);
+            fact.createLine(line, getAccountBPartner(bpartnerId, as, true, false, true, conn),
+                C_Currency_ID, "", doubtFulDebtAmount.toString(), Fact_Acct_Group_ID,
+                nextSeqNo(SeqNo), DocumentType, conn);
             bpAmountConverted = bpAmountConverted.subtract(doubtFulDebtAmount);
-            String Fact_Acct_Group_ID2 = SequenceIdData.getUUID();
-            fact.createLine(line,
-                getAccountBPartnerAllowanceForDoubtfulDebt(C_BPartner_ID, as, conn),
+            fact.createLine(line, getAccountBPartnerAllowanceForDoubtfulDebt(bpartnerId, as, conn),
                 this.C_Currency_ID, doubtFulDebtAmount.toString(), "", Fact_Acct_Group_ID2,
                 nextSeqNo(SeqNo), DocumentType, conn);
-            fact.createLine(line, getAccountBPartnerBadDebt(C_BPartner_ID, false, as, conn),
+            fact.createLine(line, getAccountBPartnerBadDebt(bpartnerId, false, as, conn),
                 this.C_Currency_ID, "", doubtFulDebtAmount.toString(), Fact_Acct_Group_ID2,
                 nextSeqNo(SeqNo), DocumentType, conn);
           }
           fact.createLine(
               line,
-              getAccountBPartner(
-                  (line.m_C_BPartner_ID == null || line.m_C_BPartner_ID.equals("")) ? this.C_BPartner_ID
-                      : line.m_C_BPartner_ID, as, isReceipt,
+              getAccountBPartner(bpartnerId, as, isReceipt,
                   (isPrepayment || line.isPrepaymentAgainstInvoice) ? true : false, conn),
               paymentCurrency.getId(), (isReceipt ? "" : bpAmountConverted.toString()),
               (isReceipt ? bpAmountConverted.toString() : ""), Fact_Acct_Group_ID,
@@ -519,25 +515,16 @@
           // If payment date is prior to invoice date book invoice as a pre-payment not as a regular
           // Receivable/Payable
           if (line.isPrepaymentAgainstInvoice()) {
-            String Fact_Acct_Group_ID2 = SequenceIdData.getUUID();
             DocLine line2 = new DocLine(DocumentType, Record_ID, line.m_TrxLine_ID);
             line2.copyInfo(line);
             line2.m_DateAcct = OBDateUtils.formatDate(invoice.getAccountingDate());
-            fact.createLine(
-                line2,
-                getAccountBPartner((line2.m_C_BPartner_ID == null || line2.m_C_BPartner_ID
-                    .equals("")) ? this.C_BPartner_ID : line2.m_C_BPartner_ID, as, isReceipt,
-                    false, conn), paymentCurrency.getId(),
-                (isReceipt ? "" : bpAmountConverted.toString()),
-                (isReceipt ? bpAmountConverted.toString() : ""), Fact_Acct_Group_ID2,
+            fact.createLine(line2, getAccountBPartner(bpartnerId, as, isReceipt, false, conn),
+                paymentCurrency.getId(), (isReceipt ? "" : bpAmountConverted.toString()),
+                (isReceipt ? bpAmountConverted.toString() : ""), Fact_Acct_Group_ID3,
                 nextSeqNo(SeqNo), DocumentType, conn);
-            fact.createLine(
-                line2,
-                getAccountBPartner((line2.m_C_BPartner_ID == null || line2.m_C_BPartner_ID
-                    .equals("")) ? this.C_BPartner_ID : line2.m_C_BPartner_ID, as, isReceipt, true,
-                    conn), paymentCurrency.getId(),
-                (!isReceipt ? "" : bpAmountConverted.toString()),
-                (!isReceipt ? bpAmountConverted.toString() : ""), Fact_Acct_Group_ID2,
+            fact.createLine(line2, getAccountBPartner(bpartnerId, as, isReceipt, true, conn),
+                paymentCurrency.getId(), (!isReceipt ? "" : bpAmountConverted.toString()),
+                (!isReceipt ? bpAmountConverted.toString() : ""), Fact_Acct_Group_ID3,
                 nextSeqNo(SeqNo), DocumentType, conn);
           }
 
--- a/src/org/openbravo/erpCommon/ad_forms/DocFINPayment.java	Tue Jan 15 18:28:43 2013 +0100
+++ b/src/org/openbravo/erpCommon/ad_forms/DocFINPayment.java	Tue Jan 15 19:01:35 2013 +0100
@@ -249,6 +249,9 @@
     final StringBuilder whereClause = new StringBuilder();
     Fact fact = new Fact(this, as, Fact.POST_Actual);
     String Fact_Acct_Group_ID = SequenceIdData.getUUID();
+    String Fact_Acct_Group_ID2 = SequenceIdData.getUUID();
+    String Fact_Acct_Group_ID3 = SequenceIdData.getUUID();
+
     OBContext.setAdminMode();
     try {
       whereClause.append(" as astdt ");
@@ -293,6 +296,8 @@
 
         boolean isReceipt = DocumentType.equals("ARR");
         boolean isPrepayment = line.getIsPrepayment().equals("Y");
+        String bpartnerId = (line.m_C_BPartner_ID == null || line.m_C_BPartner_ID.equals("")) ? this.C_BPartner_ID
+            : line.m_C_BPartner_ID;
 
         String bpAmount = line.getAmount();
         if (line.WriteOffAmt != null && !line.WriteOffAmt.equals("")
@@ -323,51 +328,39 @@
                   isReceipt, DateAcct, TABLEID_Invoice, invoice.getId(), C_Currency_ID,
                   as.m_C_Currency_ID, line, as, fact, Fact_Acct_Group_ID, nextSeqNo(SeqNo), conn,
                   false);
+              fact.createLine(line, getAccountBPartner(bpartnerId, as, true, false, true, conn),
+                  strcCurrencyId, "", doubtFulDebtAmount.toString(), Fact_Acct_Group_ID,
+                  nextSeqNo(SeqNo), DocumentType, conn);
+              bpAmountConverted = new BigDecimal(bpAmountConverted).subtract(doubtFulDebtAmount)
+                  .toString();
+              fact.createLine(line,
+                  getAccountBPartnerAllowanceForDoubtfulDebt(bpartnerId, as, conn),
+                  this.C_Currency_ID, doubtFulDebtAmount.toString(), "", Fact_Acct_Group_ID2,
+                  nextSeqNo(SeqNo), DocumentType, conn);
               fact.createLine(
                   line,
-                  getAccountBPartner((line.m_C_BPartner_ID == null || line.m_C_BPartner_ID
-                      .equals("")) ? this.C_BPartner_ID : line.m_C_BPartner_ID, as, true, false,
-                      true, conn), strcCurrencyId, "", doubtFulDebtAmount.toString(),
-                  Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
-              bpAmountConverted = new BigDecimal(bpAmountConverted).subtract(doubtFulDebtAmount)
-                  .toString();
-              String Fact_Acct_Group_ID2 = SequenceIdData.getUUID();
-              fact.createLine(line,
-                  getAccountBPartnerAllowanceForDoubtfulDebt(C_BPartner_ID, as, conn),
-                  this.C_Currency_ID, doubtFulDebtAmount.toString(), "", Fact_Acct_Group_ID2,
-                  nextSeqNo(SeqNo), DocumentType, conn);
-              fact.createLine(line, getAccountBPartnerBadDebt(C_BPartner_ID, false, as, conn),
+                  getAccountBPartnerBadDebt((line.m_C_BPartner_ID == null || line.m_C_BPartner_ID
+                      .equals("")) ? this.C_BPartner_ID : line.m_C_BPartner_ID, false, as, conn),
                   this.C_Currency_ID, "", doubtFulDebtAmount.toString(), Fact_Acct_Group_ID2,
                   nextSeqNo(SeqNo), DocumentType, conn);
             }
           }
-          fact.createLine(
-              line,
-              getAccountBPartner(
-                  (line.m_C_BPartner_ID == null || line.m_C_BPartner_ID.equals("")) ? this.C_BPartner_ID
-                      : line.m_C_BPartner_ID, as, isReceipt, isPrepayment, conn), strcCurrencyId,
-              (isReceipt ? "" : bpAmountConverted), (isReceipt ? bpAmountConverted : ""),
-              Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
+          fact.createLine(line, getAccountBPartner(bpartnerId, as, isReceipt, isPrepayment, conn),
+              strcCurrencyId, (isReceipt ? "" : bpAmountConverted), (isReceipt ? bpAmountConverted
+                  : ""), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
           // If payment date is prior to invoice date book invoice as a pre-payment not as a regular
           // Receivable/Payable
           if (line.isPrepaymentAgainstInvoice()) {
-            String Fact_Acct_Group_ID2 = SequenceIdData.getUUID();
             DocLine line2 = new DocLine(DocumentType, Record_ID, line.m_TrxLine_ID);
             line2.copyInfo(line);
             line2.m_DateAcct = OBDateUtils.formatDate(invoice.getAccountingDate());
-            fact.createLine(
-                line2,
-                getAccountBPartner((line2.m_C_BPartner_ID == null || line2.m_C_BPartner_ID
-                    .equals("")) ? this.C_BPartner_ID : line2.m_C_BPartner_ID, as, isReceipt,
-                    false, conn), strcCurrencyId, (isReceipt ? "" : bpAmountConverted),
-                (isReceipt ? bpAmountConverted : ""), Fact_Acct_Group_ID2, nextSeqNo(SeqNo),
+            fact.createLine(line2, getAccountBPartner(bpartnerId, as, isReceipt, false, conn),
+                strcCurrencyId, (isReceipt ? "" : bpAmountConverted),
+                (isReceipt ? bpAmountConverted : ""), Fact_Acct_Group_ID3, nextSeqNo(SeqNo),
                 DocumentType, conn);
-            fact.createLine(
-                line2,
-                getAccountBPartner((line2.m_C_BPartner_ID == null || line2.m_C_BPartner_ID
-                    .equals("")) ? this.C_BPartner_ID : line2.m_C_BPartner_ID, as, isReceipt, true,
-                    conn), strcCurrencyId, (!isReceipt ? "" : bpAmountConverted),
-                (!isReceipt ? bpAmountConverted : ""), Fact_Acct_Group_ID2, nextSeqNo(SeqNo),
+            fact.createLine(line2, getAccountBPartner(bpartnerId, as, isReceipt, true, conn),
+                strcCurrencyId, (!isReceipt ? "" : bpAmountConverted),
+                (!isReceipt ? bpAmountConverted : ""), Fact_Acct_Group_ID3, nextSeqNo(SeqNo),
                 DocumentType, conn);
           }
         } else {
--- a/src/org/openbravo/erpCommon/ad_forms/DocFINReconciliation.java	Tue Jan 15 18:28:43 2013 +0100
+++ b/src/org/openbravo/erpCommon/ad_forms/DocFINReconciliation.java	Tue Jan 15 19:01:35 2013 +0100
@@ -552,6 +552,7 @@
         }
       }
       String Fact_Acct_Group_ID = SequenceIdData.getUUID();
+      String Fact_Acct_Group_ID2 = SequenceIdData.getUUID();
       for (int i = 0; p_lines != null && i < p_lines.length; i++) {
         DocLine_FINReconciliation line = (DocLine_FINReconciliation) p_lines[i];
         FIN_FinaccTransaction transaction = OBDal.getInstance().get(FIN_FinaccTransaction.class,
@@ -560,7 +561,7 @@
         if (TRXTYPE_BankFee.equals(transaction.getTransactionType())) {
           fact = createFactFee(line, as, conn, fact, Fact_Acct_Group_ID);
         } else if (!"".equals(line.getFinPaymentId())) {
-          fact = createFactPayment(line, as, conn, fact, Fact_Acct_Group_ID);
+          fact = createFactPayment(line, as, conn, fact, Fact_Acct_Group_ID, Fact_Acct_Group_ID2);
         } else {
           fact = createFactGLItem(line, as, conn, fact, Fact_Acct_Group_ID);
         }
@@ -610,7 +611,8 @@
   }
 
   public Fact createFactPayment(DocLine_FINReconciliation line, AcctSchema as,
-      ConnectionProvider conn, Fact fact, String Fact_Acct_Group_ID) throws ServletException {
+      ConnectionProvider conn, Fact fact, String Fact_Acct_Group_ID, String Fact_Acct_Group_ID2)
+      throws ServletException {
     FIN_Payment payment = OBDal.getInstance().get(FIN_Payment.class, line.getFinPaymentId());
     FIN_FinaccTransaction transaction = OBDal.getInstance().get(FIN_FinaccTransaction.class,
         line.getFinFinAccTransactionId());
@@ -654,7 +656,8 @@
         // Cambiar line to reflect BPs
         FIN_PaymentDetail paymentDetail = OBDal.getInstance().get(FIN_PaymentDetail.class,
             data[i].getField("FIN_Payment_Detail_ID"));
-        fact = createFactPaymentDetails(detail, paymentDetail, as, conn, fact, Fact_Acct_Group_ID);
+        fact = createFactPaymentDetails(detail, paymentDetail, as, conn, fact, Fact_Acct_Group_ID,
+            Fact_Acct_Group_ID2);
       }
     } else {
       BigDecimal paymentAmount = new BigDecimal(line.getAmount());
@@ -758,13 +761,15 @@
 
   public Fact createFactPaymentDetails(DocLine_FINReconciliation line,
       FIN_PaymentDetail paymentDetail, AcctSchema as, ConnectionProvider conn, Fact fact,
-      String Fact_Acct_Group_ID) throws ServletException {
+      String Fact_Acct_Group_ID, String Fact_Acct_Group_ID2) throws ServletException {
     boolean isPrepayment = paymentDetail.isPrepayment();
     boolean isPaymentDatePriorToInvoiceDate = isPaymentDatePriorToInvoiceDate(paymentDetail)
         && !paymentDetail.isPrepayment();
     boolean isReceipt = paymentDetail.getFinPayment().isReceipt();
     BigDecimal bpAmount = paymentDetail.getAmount();
     Currency paymentCurrency = paymentDetail.getFinPayment().getCurrency();
+    String bpartnerId = (line.m_C_BPartner_ID == null || line.m_C_BPartner_ID.equals("")) ? this.C_BPartner_ID
+        : line.m_C_BPartner_ID;
     if (paymentDetail.getWriteoffAmount() != null
         && paymentDetail.getWriteoffAmount().compareTo(BigDecimal.ZERO) != 0) {
       Account account = isReceipt ? getAccountWriteOffBPartner(AcctServer.ACCTTYPE_WriteOff,
@@ -785,8 +790,6 @@
           DocumentType, line.m_DateAcct, null, conn);
       bpAmount = bpAmount.add(paymentDetail.getWriteoffAmount());
     }
-    String bpartnerId = (line.m_C_BPartner_ID == null || line.m_C_BPartner_ID.equals("")) ? this.C_BPartner_ID
-        : line.m_C_BPartner_ID;
     if (bpartnerId == null || bpartnerId.equals("")) {
       bpartnerId = paymentDetail.getFINPaymentScheduleDetailList().get(0)
           .getInvoicePaymentSchedule() != null ? paymentDetail.getFINPaymentScheduleDetailList()
@@ -826,18 +829,14 @@
         BigDecimal doubtFulDebtAmount = convertAmount(line.getDoubtFulDebtAmount(), isReceipt,
             DateAcct, TABLEID_Invoice, invoice.getId(), C_Currency_ID, as.m_C_Currency_ID, line,
             as, fact, Fact_Acct_Group_ID, nextSeqNo(SeqNo), conn, false);
-        fact.createLine(
-            line,
-            getAccountBPartner(
-                (line.m_C_BPartner_ID == null || line.m_C_BPartner_ID.equals("")) ? this.C_BPartner_ID
-                    : line.m_C_BPartner_ID, as, true, false, true, conn), C_Currency_ID, "",
-            doubtFulDebtAmount.toString(), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
+        fact.createLine(line, getAccountBPartner(bpartnerId, as, true, false, true, conn),
+            C_Currency_ID, "", doubtFulDebtAmount.toString(), Fact_Acct_Group_ID, nextSeqNo(SeqNo),
+            DocumentType, conn);
         bpAmountConverted = bpAmountConverted.subtract(doubtFulDebtAmount);
-        String Fact_Acct_Group_ID2 = SequenceIdData.getUUID();
-        fact.createLine(line, getAccountBPartnerAllowanceForDoubtfulDebt(C_BPartner_ID, as, conn),
+        fact.createLine(line, getAccountBPartnerAllowanceForDoubtfulDebt(bpartnerId, as, conn),
             this.C_Currency_ID, doubtFulDebtAmount.toString(), "", Fact_Acct_Group_ID2,
             nextSeqNo(SeqNo), DocumentType, conn);
-        fact.createLine(line, getAccountBPartnerBadDebt(C_BPartner_ID, false, as, conn),
+        fact.createLine(line, getAccountBPartnerBadDebt(bpartnerId, false, as, conn),
             this.C_Currency_ID, "", doubtFulDebtAmount.toString(), Fact_Acct_Group_ID2,
             nextSeqNo(SeqNo), DocumentType, conn);
       }
@@ -850,7 +849,6 @@
       // If payment date is prior to invoice date book invoice as a pre-payment not as a regular
       // Receivable/Payable
       if (isPaymentDatePriorToInvoiceDate) {
-        String Fact_Acct_Group_ID2 = SequenceIdData.getUUID();
         DocLine line2 = new DocLine(DocumentType, Record_ID, line.m_TrxLine_ID);
         line2.copyInfo(line);
         line2.m_DateAcct = OBDateUtils.formatDate(invoice.getAccountingDate());