Fixes issue 30714: Payment Method not filled in Add Payment
authorAlvaro Ferraz <alvaro.ferraz@openbravo.com>
Wed, 02 Sep 2015 19:55:07 +0200
changeset 27770 a5c3132bbcd2
parent 27769 ba4ab8980e96
child 27771 e5863950c81d
Fixes issue 30714: Payment Method not filled in Add Payment

Payment Method was not filled when running Add Payment and business partner has no default Financial Account.
When calling from getDefaultPaymentMethod to getFinancialAccountPaymentMethod in AddPaymentDefaultValuesHandler class with a null financialAccountId (in case of opening Add Payment from windows without Financial Account information like Purchase/Sales Order/Invoice and Business Partner does not have a default Financial Account setted), a NPE was raised and Payment Method was not filled in Add Payment.
Now, in Purchase/Sales Order/Invoice windows, where is no financial account information in the context, document payment method will be taken if it is active for any financial account, if not, business partner default payment method will be taken instead (if it is active for any financial account and document organization has access to it).
In those windows, Financial Account selector will show Business Partner default account if selected Payment Method belongs to it, or any Financial Account which Payment Method belongs to instead.
In case of opening Add Payment from Payment In/Out windows, where exists Financial Account information in the context, Payment Method and Financial Account will be taken from the document.
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/filterexpression/AddPaymentDefaultValuesHandler.java
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/filterexpression/AddPaymentDefaultValuesHandler.java	Fri Sep 04 10:26:47 2015 +0200
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/filterexpression/AddPaymentDefaultValuesHandler.java	Wed Sep 02 19:55:07 2015 +0200
@@ -282,32 +282,64 @@
         && StringUtils.isNotEmpty(context.getString("Fin_Financial_Account_ID"))) {
       return context.getString("Fin_Financial_Account_ID");
     }
-    String strBPartnerId = getDefaultReceivedFrom(requestMap);
 
-    if (StringUtils.isNotEmpty(strBPartnerId)) {
-      BusinessPartner businessPartner = OBDal.getInstance().get(BusinessPartner.class,
-          strBPartnerId);
-      boolean isSOTrx = "Y".equals(getDefaultIsSOTrx(requestMap));
-      if (context.has("inpadClientId") && context.has("inpadOrgId")) {
-        final OrganizationStructureProvider osp = OBContext.getOBContext()
-            .getOrganizationStructureProvider(context.getString("inpadClientId"));
-        if (isSOTrx && businessPartner.getAccount() != null) {
-          if (osp.isInNaturalTree(businessPartner.getAccount().getOrganization(), OBDal
-              .getInstance().get(Organization.class, context.getString("inpadOrgId")))) {
-            return businessPartner.getAccount().getId();
-          } else {
-            return "";
-          }
+    String paymentMethodId = getDefaultPaymentMethod(requestMap);
+    if (StringUtils.isNotEmpty(paymentMethodId) && context.has("inpadClientId")
+        && context.has("inpadOrgId")) {
 
-        } else if (!isSOTrx && businessPartner.getPOFinancialAccount() != null) {
-          if (osp.isInNaturalTree(businessPartner.getPOFinancialAccount().getOrganization(), OBDal
-              .getInstance().get(Organization.class, context.getString("inpadOrgId")))) {
-            return businessPartner.getPOFinancialAccount().getId();
-          } else {
-            return "";
-          }
+      final OrganizationStructureProvider osp = OBContext.getOBContext()
+          .getOrganizationStructureProvider(context.getString("inpadClientId"));
+      String strBPartnerId = getDefaultReceivedFrom(requestMap);
+      if (StringUtils.isNotEmpty(strBPartnerId)) {
+
+        BusinessPartner businessPartner = OBDal.getInstance().get(BusinessPartner.class,
+            strBPartnerId);
+        boolean isSOTrx = "Y".equals(getDefaultIsSOTrx(requestMap));
+
+        if (isSOTrx
+            && businessPartner.getAccount() != null
+            && getFinancialAccountPaymentMethod(paymentMethodId, businessPartner.getAccount()
+                .getId()) != null
+            && osp.isInNaturalTree(businessPartner.getAccount().getOrganization(), OBDal
+                .getInstance().get(Organization.class, context.getString("inpadOrgId")))) {
+          return businessPartner.getAccount().getId();
+        }
+
+        else if (!isSOTrx
+            && businessPartner.getPOFinancialAccount() != null
+            && getFinancialAccountPaymentMethod(paymentMethodId, businessPartner
+                .getPOFinancialAccount().getId()) != null
+            && osp.isInNaturalTree(businessPartner.getPOFinancialAccount().getOrganization(), OBDal
+                .getInstance().get(Organization.class, context.getString("inpadOrgId")))) {
+          return businessPartner.getPOFinancialAccount().getId();
         }
       }
+
+      FinAccPaymentMethod fpm = getFinancialAccountPaymentMethod(paymentMethodId, null);
+      if (fpm != null
+          && osp.isInNaturalTree(fpm.getAccount().getOrganization(),
+              OBDal.getInstance().get(Organization.class, context.getString("inpadOrgId")))) {
+        return fpm.getAccount().getId();
+      }
+    }
+
+    return null;
+  }
+
+  private String getContextFinancialAccount(Map<String, String> requestMap) throws JSONException {
+    String strContext = requestMap.get("context");
+    if (strContext == null) {
+      return null;
+    }
+    JSONObject context = new JSONObject(strContext);
+    if (context.has("inpfinFinancialAccountId")
+        && context.get("inpfinFinancialAccountId") != JSONObject.NULL
+        && StringUtils.isNotEmpty(context.getString("inpfinFinancialAccountId"))) {
+      return context.getString("inpfinFinancialAccountId");
+    } else if (context.has("Fin_Financial_Account_ID")
+        && context.get("Fin_Financial_Account_ID") != JSONObject.NULL
+        && StringUtils.isNotEmpty(context.getString("Fin_Financial_Account_ID"))) {
+      return context.getString("Fin_Financial_Account_ID");
     }
     return null;
   }
@@ -331,26 +363,38 @@
         && StringUtils.isNotEmpty(context.getString("inpfinPaymentmethodId"))) {
       strFinPaymentMethodId = context.getString("inpfinPaymentmethodId");
     }
-    strFinancialAccountId = getDefaultFinancialAccount(requestMap);
-    if (strFinPaymentMethodId != null && strFinancialAccountId != null) {
-      if (getFinancialAccountPaymentMethod(strFinPaymentMethodId, strFinancialAccountId) != null) {
-        return strFinPaymentMethodId;
-      }
+
+    strFinancialAccountId = getContextFinancialAccount(requestMap);
+    if (strFinPaymentMethodId != null
+        && getFinancialAccountPaymentMethod(strFinPaymentMethodId, strFinancialAccountId) != null) {
+      return strFinPaymentMethodId;
     }
+
     String strBPartnerId = getDefaultReceivedFrom(requestMap);
-    if (StringUtils.isNotEmpty(strBPartnerId)) {
+    if (StringUtils.isNotEmpty(strBPartnerId) && context.has("inpadClientId")
+        && context.has("inpadOrgId")) {
+
+      final OrganizationStructureProvider osp = OBContext.getOBContext()
+          .getOrganizationStructureProvider(context.getString("inpadClientId"));
       BusinessPartner businessPartner = OBDal.getInstance().get(BusinessPartner.class,
           strBPartnerId);
       boolean isSOTrx = "Y".equals(getDefaultIsSOTrx(requestMap));
+
       if (isSOTrx
           && businessPartner.getPaymentMethod() != null
           && getFinancialAccountPaymentMethod(businessPartner.getPaymentMethod().getId(),
-              strFinancialAccountId) != null) {
+              strFinancialAccountId) != null
+          && osp.isInNaturalTree(businessPartner.getPaymentMethod().getOrganization(), OBDal
+              .getInstance().get(Organization.class, context.getString("inpadOrgId")))) {
         return businessPartner.getPaymentMethod().getId();
-      } else if (!isSOTrx
+      }
+
+      else if (!isSOTrx
           && businessPartner.getPOPaymentMethod() != null
           && getFinancialAccountPaymentMethod(businessPartner.getPOPaymentMethod().getId(),
-              strFinancialAccountId) != null) {
+              strFinancialAccountId) != null
+          && osp.isInNaturalTree(businessPartner.getPOPaymentMethod().getOrganization(), OBDal
+              .getInstance().get(Organization.class, context.getString("inpadOrgId")))) {
         return businessPartner.getPOPaymentMethod().getId();
       }
     }
@@ -428,8 +472,10 @@
         FinAccPaymentMethod.class);
     obc.setFilterOnReadableOrganization(false);
     obc.setMaxResults(1);
-    obc.add(Restrictions.eq(FinAccPaymentMethod.PROPERTY_ACCOUNT,
-        OBDal.getInstance().get(FIN_FinancialAccount.class, financialAccountId)));
+    if (!StringUtils.isEmpty(financialAccountId)) {
+      obc.add(Restrictions.eq(FinAccPaymentMethod.PROPERTY_ACCOUNT,
+          OBDal.getInstance().get(FIN_FinancialAccount.class, financialAccountId)));
+    }
     obc.add(Restrictions.eq(FinAccPaymentMethod.PROPERTY_PAYMENTMETHOD,
         OBDal.getInstance().get(FIN_PaymentMethod.class, paymentMethodId)));
     return (FinAccPaymentMethod) obc.uniqueResult();