Fixes issue 30954: NPE in SE_Payment_BPartner callout
authorAlvaro Ferraz <alvaro.ferraz@openbravo.com>
Tue, 29 Sep 2015 12:00:47 +0200
changeset 28142 e7bf20e738da
parent 28131 8942b6596f0b
child 28143 d85f65dd5246
Fixes issue 30954: NPE in SE_Payment_BPartner callout

A NPE was raised in SE_Payment_BPartner callout, when selecting a business partner in Payment In or Payment Out window with no default payment method or financial account.
src/org/openbravo/erpCommon/ad_callouts/SE_Payment_BPartner.java
--- a/src/org/openbravo/erpCommon/ad_callouts/SE_Payment_BPartner.java	Thu Dec 10 15:10:00 2015 +0530
+++ b/src/org/openbravo/erpCommon/ad_callouts/SE_Payment_BPartner.java	Tue Sep 29 12:00:47 2015 +0200
@@ -56,29 +56,40 @@
       paymentMethod = bpartner.getPOPaymentMethod();
       financialAccount = bpartner.getPOFinancialAccount();
     }
-    final OBCriteria<FinAccPaymentMethod> apmCriteria = OBDal.getInstance().createCriteria(FinAccPaymentMethod.class);
-    apmCriteria.add(Restrictions.eq(FinAccPaymentMethod.PROPERTY_PAYMENTMETHOD, paymentMethod));
-    apmCriteria.add(Restrictions.eq(FinAccPaymentMethod.PROPERTY_ACCOUNT, financialAccount));
-    apmCriteria.setFilterOnActive(false);
-    FinAccPaymentMethod accPaymentMethod = (FinAccPaymentMethod) apmCriteria.uniqueResult();
-    if (financialAccount.isActive() && accPaymentMethod.isActive()) {
-      try {
-        info.addResult("inpfinPaymentmethodId", isReceipt ? bpartner.getPaymentMethod().getId()
-            : bpartner.getPOPaymentMethod().getId());
-        info.addResult("inpfinFinancialAccountId", isReceipt ? bpartner.getAccount().getId()
-            : bpartner.getPOFinancialAccount().getId());
-      } catch (Exception e) {
+
+    if (paymentMethod != null && financialAccount != null) {
+      final OBCriteria<FinAccPaymentMethod> apmCriteria = OBDal.getInstance().createCriteria(
+          FinAccPaymentMethod.class);
+      apmCriteria.add(Restrictions.eq(FinAccPaymentMethod.PROPERTY_PAYMENTMETHOD, paymentMethod));
+      apmCriteria.add(Restrictions.eq(FinAccPaymentMethod.PROPERTY_ACCOUNT, financialAccount));
+      apmCriteria.setFilterOnActive(false);
+      FinAccPaymentMethod accPaymentMethod = (FinAccPaymentMethod) apmCriteria.uniqueResult();
+      if (accPaymentMethod != null) {
+        if (financialAccount.isActive() && accPaymentMethod.isActive()) {
+          info.addResult("inpfinPaymentmethodId", paymentMethod.getId());
+          info.addResult("inpfinFinancialAccountId", financialAccount.getId());
+        } else if (!financialAccount.isActive() && !accPaymentMethod.isActive()) {
+          info.addResult(
+              "WARNING",
+              String.format(
+                  Utility.messageBD(new DalConnectionProvider(), "finnac_paymet_inact",
+                      vars.getLanguage()), financialAccount.getIdentifier(),
+                  paymentMethod.getIdentifier()));
+        } else if (!financialAccount.isActive()) {
+          info.addResult(
+              "WARNING",
+              String.format(Utility.messageBD(new DalConnectionProvider(), "finnac_inact",
+                  vars.getLanguage()), financialAccount.getIdentifier()));
+        } else if (!accPaymentMethod.isActive()) {
+          info.addResult("WARNING", String.format(
+              Utility.messageBD(new DalConnectionProvider(), "paymet_inact", vars.getLanguage()),
+              paymentMethod.getIdentifier(), financialAccount.getIdentifier()));
+        }
+      } else {
         log4j.info("No default info for the selected business partner");
       }
-    } else if (!financialAccount.isActive() && !accPaymentMethod.isActive()) {
-      info.addResult("WARNING", String.format(
-          Utility.messageBD(new DalConnectionProvider(), "finnac_paymet_inact", vars.getLanguage()), financialAccount.getIdentifier(), paymentMethod.getIdentifier()));
-    } else if (!financialAccount.isActive()) {
-      info.addResult("WARNING", String.format(
-          Utility.messageBD(new DalConnectionProvider(), "finnac_inact", vars.getLanguage()), financialAccount.getIdentifier()));
-    } else if (!accPaymentMethod.isActive()) {
-      info.addResult("WARNING", String.format(
-          Utility.messageBD(new DalConnectionProvider(), "paymet_inact", vars.getLanguage()), paymentMethod.getIdentifier(), financialAccount.getIdentifier()));
+    } else {
+      log4j.info("No default info for the selected business partner");
     }
     if ((!strcBpartnerId.equals(""))
         && (FIN_Utility.isBlockedBusinessPartner(strcBpartnerId, "Y".equals(strisreceipt), 4))) {