Related to issue 30661: Reload payment method only if it is active
authorAlvaro Ferraz <alvaro.ferraz@openbravo.com>
Fri, 04 Sep 2015 10:26:47 +0200
changeset 27769 ba4ab8980e96
parent 27768 725f39b6fa93
child 27770 a5c3132bbcd2
Related to issue 30661: Reload payment method only if it is active

Reload payment method when changing business partner in the selector only if business partner default payment method is active for the current financial account.
Also ReceivedFromPaymentMethodActionHandler performance has been improved.
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/actionHandler/ReceivedFromPaymentMethodActionHandler.java
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/actionHandler/ReceivedFromPaymentMethodActionHandler.java	Tue Aug 25 14:00:35 2015 +0200
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/actionHandler/ReceivedFromPaymentMethodActionHandler.java	Fri Sep 04 10:26:47 2015 +0200
@@ -21,10 +21,11 @@
 
 import java.util.Map;
 
-import org.apache.commons.lang.StringUtils;
 import org.codehaus.jettison.json.JSONObject;
+import org.hibernate.criterion.Restrictions;
 import org.openbravo.base.exception.OBException;
 import org.openbravo.client.kernel.BaseActionHandler;
+import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.model.common.businesspartner.BusinessPartner;
 import org.openbravo.model.financialmgmt.payment.FIN_FinancialAccount;
@@ -40,9 +41,9 @@
       JSONObject result = new JSONObject();
       FIN_FinancialAccount financialAccount = OBDal.getInstance().get(FIN_FinancialAccount.class,
           jsonData.getString("financialAccount"));
-      boolean contains = false;
-      String paymentMethod = null;
-      String paymentMethodName = null;
+      FIN_PaymentMethod paymentMethod = null;
+      String paymentMethodId = "";
+      String paymentMethodName = "";
 
       if (financialAccount != null) {
         if (jsonData.has("receivedFrom") && jsonData.get("receivedFrom") != JSONObject.NULL) {
@@ -50,35 +51,31 @@
           BusinessPartner businessPartner = OBDal.getInstance().get(BusinessPartner.class,
               receivedFrom);
           if (jsonData.getString("isSOTrx").toString().equals("true")) {
-            paymentMethod = businessPartner.getPaymentMethod() != null ? businessPartner
-                .getPaymentMethod().getId() : "";
+            paymentMethod = businessPartner.getPaymentMethod();
           } else {
-            paymentMethod = businessPartner.getPOPaymentMethod() != null ? businessPartner
-                .getPOPaymentMethod().getId() : "";
+            paymentMethod = businessPartner.getPOPaymentMethod();
           }
-          for (FinAccPaymentMethod finAccPaymentMethod : financialAccount
-              .getFinancialMgmtFinAccPaymentMethodList()) {
-            if (StringUtils.equals(finAccPaymentMethod.getPaymentMethod().getId(), paymentMethod)) {
-              contains = true;
-              break;
-            } else {
-              contains = false;
+
+          if (paymentMethod != null) {
+            OBCriteria<FinAccPaymentMethod> criteria = OBDal.getInstance().createCriteria(
+                FinAccPaymentMethod.class);
+            criteria.add(Restrictions.eq(FinAccPaymentMethod.PROPERTY_ACCOUNT, financialAccount));
+            criteria
+                .add(Restrictions.eq(FinAccPaymentMethod.PROPERTY_PAYMENTMETHOD, paymentMethod));
+            criteria.setFilterOnReadableOrganization(false);
+            criteria.setFilterOnActive(true);
+            criteria.setMaxResults(1);
+            FinAccPaymentMethod finAccPaymentMethod = (FinAccPaymentMethod) criteria.uniqueResult();
+
+            if (finAccPaymentMethod != null) {
+              paymentMethodId = finAccPaymentMethod.getPaymentMethod().getId();
+              paymentMethodName = finAccPaymentMethod.getPaymentMethod().getName();
             }
           }
         }
-      } else {
-        contains = false;
       }
 
-      if (!contains) {
-        paymentMethod = "";
-        paymentMethodName = "";
-      } else {
-        paymentMethodName = OBDal.getInstance().get(FIN_PaymentMethod.class, paymentMethod)
-            .getName();
-      }
-
-      result.put("paymentMethodId", paymentMethod);
+      result.put("paymentMethodId", paymentMethodId);
       result.put("paymentMethodName", paymentMethodName);
       return result;
     } catch (Exception e) {