Fixed bug 28729 select the proper credit when a reverse payment is made
authorSandra Huguet <sandra.huguet@openbravo.com>
Wed, 04 Mar 2015 18:22:34 +0100
changeset 26124 268b0e438f83
parent 26123 f06d7018a52b
child 26125 86a6b64eb747
Fixed bug 28729 select the proper credit when a reverse payment is made

When reverse payment process is run for a credit payment the credit
generated in that specific payment should be consumed
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_PaymentProcess.java
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_PaymentProcess.java	Tue Mar 03 13:57:53 2015 +0100
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_PaymentProcess.java	Wed Mar 04 18:22:34 2015 +0100
@@ -1154,7 +1154,21 @@
       final boolean isReceipt = newPayment.isReceipt();
       final Organization Org = newPayment.getOrganization();
 
-      List<FIN_Payment> creditPayments = dao.getCustomerPaymentsWithCredit(Org, bp, isReceipt);
+      final OBCriteria<FIN_Payment> reversepayment = OBDal.getInstance().createCriteria(
+          FIN_Payment.class);
+      reversepayment.add(Restrictions.eq(FIN_Payment.PROPERTY_REVERSEDPAYMENT, newPayment));
+      final FIN_Payment reversepaymnt = (FIN_Payment) reversepayment.uniqueResult();
+
+      List<FIN_Payment> creditPayments;
+      if (reversepaymnt == null) {
+        // Normal scenario
+        creditPayments = dao.getCustomerPaymentsWithCredit(Org, bp, isReceipt);
+      } else {
+        // If it is a reverse payment use its original payment
+        creditPayments = new ArrayList<FIN_Payment>(1);
+        creditPayments.add(reversepaymnt);
+      }
+
       BigDecimal pendingToAllocateAmount = usedAmount;
       for (FIN_Payment creditPayment : creditPayments) {
         BigDecimal availableAmount = creditPayment.getGeneratedCredit().subtract(