Fixes issue 36753: Error in Payment Plan after execute a payment
authorAtul Gaware <atul.gaware@openbravo.com>
Wed, 06 Sep 2017 16:51:30 +0530
changeset 32242 594beb85eb49
parent 32241 f3d86373b377
child 32243 a3af497e64de
Fixes issue 36753: Error in Payment Plan after execute a payment

** Use FIN_Utility updatePaymentAmounts method instead of private updatePaymentAmounts
method in FIN_ExecutePayment to avoid wrong updation of fin_payment_schedule
** Use FIN_Utility updateBusinessPartnerCredit
** Remove break statement
** Removed unused private updatePaymentAmounts method from FIN_ExecutePayment
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_ExecutePayment.java
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_ExecutePayment.java	Thu Aug 10 08:49:37 2017 +0200
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_ExecutePayment.java	Wed Sep 06 16:51:30 2017 +0530
@@ -208,11 +208,11 @@
                         return processTransactionError;
                       }
                     }
-                    updatePaymentAmounts(paymentRunPayment.getPayment());
+                    FIN_Utility.updatePaymentAmounts(psd);
                   }
                 }
-                break;
               }
+              FIN_Utility.updateBusinessPartnerCredit(paymentRunPayment.getPayment());
             } finally {
               OBContext.restorePreviousMode();
             }
@@ -263,52 +263,6 @@
         constantParameters.put(parameter.getSearchKey(), parameter.getDefaultTextValue());
   }
 
-  private static void updatePaymentAmounts(FIN_Payment payment) {
-    for (FIN_PaymentDetail pDetail : payment.getFINPaymentDetailList()) {
-      for (FIN_PaymentScheduleDetail psd : pDetail.getFINPaymentScheduleDetailList()) {
-        if (psd.getInvoicePaymentSchedule() != null) {
-          BusinessPartner bPartner = psd.getInvoicePaymentSchedule().getInvoice()
-              .getBusinessPartner();
-          BigDecimal creditUsed = bPartner.getCreditUsed();
-          BigDecimal amountWithSign = psd.getInvoicePaymentSchedule().getInvoice()
-              .isSalesTransaction() ? psd.getAmount() : psd.getAmount().negate();
-          creditUsed = creditUsed.subtract(amountWithSign);
-          bPartner.setCreditUsed(creditUsed);
-          OBDal.getInstance().save(bPartner);
-          FIN_AddPayment.updatePaymentScheduleAmounts(pDetail, psd.getInvoicePaymentSchedule(),
-              psd.getAmount(), psd.getWriteoffAmount());
-        }
-        if (psd.getOrderPaymentSchedule() != null) {
-          FIN_AddPayment.updatePaymentScheduleAmounts(pDetail, psd.getOrderPaymentSchedule(),
-              psd.getAmount(), psd.getWriteoffAmount());
-        }
-        if (pDetail.isPrepayment() && psd.getOrderPaymentSchedule() == null
-            && psd.getInvoicePaymentSchedule() == null) {
-          // This PSD is credit
-          BusinessPartner bPartner = psd.getPaymentDetails().getFinPayment().getBusinessPartner();
-          BigDecimal creditUsed = bPartner.getCreditUsed();
-          BigDecimal amountWithSign = psd.getPaymentDetails().getFinPayment().isReceipt() ? psd
-              .getAmount() : psd.getAmount().negate();
-          creditUsed = creditUsed.subtract(amountWithSign);
-          bPartner.setCreditUsed(creditUsed);
-          OBDal.getInstance().save(bPartner);
-        }
-      }
-    }
-    // When credit is used (consumed) we compensate so_creditused as this amount is already
-    // included in the payment details. Credit consumed should not affect to so_creditused
-    if (payment.getGeneratedCredit().compareTo(BigDecimal.ZERO) == 0
-        && payment.getUsedCredit().compareTo(BigDecimal.ZERO) != 0) {
-      BusinessPartner bp = payment.getBusinessPartner();
-      if (payment.isReceipt()) {
-        bp.setCreditUsed(bp.getCreditUsed().add(payment.getUsedCredit()));
-      } else {
-        bp.setCreditUsed(bp.getCreditUsed().subtract(payment.getUsedCredit()));
-      }
-      OBDal.getInstance().save(bp);
-    }
-  }
-
   /**
    * It calls the Transaction Process for the given transaction and action.
    *