Fixes issue 32914: Fix properly issue 31457
authorAlvaro Ferraz <alvaro.ferraz@openbravo.com>
Thu, 19 May 2016 15:15:02 +0200
changeset 29513 036fbe5f521c
parent 29512 98ac1279b215
child 29514 ce8a80cbda32
Fixes issue 32914: Fix properly issue 31457

Change getOrderedPaymentScheduleDetails method in AddPaymentActionHandler class to order by absolute value of FIN_PaymentScheduleDetail.PROPERTY_AMOUNT property.
Change also the manage negative amounts check to recalculate remainingAmount if it is lower than outstandingAmount in case remainingAmount is negative.
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/actionHandler/AddPaymentActionHandler.java
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/actionHandler/AddPaymentActionHandler.java	Fri May 20 10:08:11 2016 +0200
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/actionHandler/AddPaymentActionHandler.java	Thu May 19 15:15:02 2016 +0200
@@ -21,6 +21,7 @@
 import java.math.BigDecimal;
 import java.sql.SQLException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
@@ -49,6 +50,7 @@
 import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.dal.service.OBDao;
+import org.openbravo.dal.service.OBQuery;
 import org.openbravo.database.ConnectionProvider;
 import org.openbravo.erpCommon.utility.OBDateUtils;
 import org.openbravo.erpCommon.utility.OBError;
@@ -310,7 +312,7 @@
 
       boolean isWriteOff = psdRow.getBoolean("writeoff");
       // psdIds can be grouped
-      String[] psdIds = strPSDIds.replaceAll(" ", "").split(",");
+      List<String> psdIds = Arrays.asList(strPSDIds.replaceAll(" ", "").split(","));
       List<FIN_PaymentScheduleDetail> psds = getOrderedPaymentScheduleDetails(psdIds);
       BigDecimal outstandingAmount = BigDecimal.ZERO;
       BigDecimal remainingAmount = paidAmount;
@@ -332,8 +334,7 @@
         }
         // Manage negative amounts
         if ((remainingAmount.signum() > 0 && remainingAmount.compareTo(outstandingAmount) >= 0)
-            || ((remainingAmount.signum() < 0 && outstandingAmount.signum() < 0) && (remainingAmount
-                .compareTo(outstandingAmount) >= 0))) {
+            || (remainingAmount.signum() < 0 && remainingAmount.compareTo(outstandingAmount) <= 0)) {
           assignAmount = outstandingAmount;
           remainingAmount = remainingAmount.subtract(outstandingAmount);
         } else {
@@ -611,12 +612,15 @@
     return message;
   }
 
-  private List<FIN_PaymentScheduleDetail> getOrderedPaymentScheduleDetails(String[] psdSet) {
-    OBCriteria<FIN_PaymentScheduleDetail> orderedPSDs = OBDal.getInstance().createCriteria(
-        FIN_PaymentScheduleDetail.class);
-    orderedPSDs.add(Restrictions.in(FIN_PaymentScheduleDetail.PROPERTY_ID, psdSet));
-    orderedPSDs.addOrderBy(FIN_PaymentScheduleDetail.PROPERTY_PAYMENTDETAILS, true);
-    orderedPSDs.addOrderBy(FIN_PaymentScheduleDetail.PROPERTY_AMOUNT, true);
+  private List<FIN_PaymentScheduleDetail> getOrderedPaymentScheduleDetails(List<String> psdSet) {
+    StringBuffer where = new StringBuffer();
+    where.append(" as psd");
+    where.append(" where psd." + FIN_PaymentScheduleDetail.PROPERTY_ID + " in (:psdSet)");
+    where.append(" order by psd." + FIN_PaymentScheduleDetail.PROPERTY_PAYMENTDETAILS);
+    where.append(", abs(psd." + FIN_PaymentScheduleDetail.PROPERTY_AMOUNT + ")");
+    OBQuery<FIN_PaymentScheduleDetail> orderedPSDs = OBDal.getInstance().createQuery(
+        FIN_PaymentScheduleDetail.class, where.toString());
+    orderedPSDs.setNamedParameter("psdSet", psdSet);
     return orderedPSDs.list();
   }