Fixes issue 33550: Error voiding an invoice if bp doesn't have default account
authorMark <markmm82@gmail.com>
Thu, 04 Aug 2016 09:52:25 -0400
changeset 30066 ee738640c404
parent 30065 57f68b22dd06
child 30067 ae1076c7e9ea
Fixes issue 33550: Error voiding an invoice if bp doesn't have default account

Method used to get the default financial account (getFinancialAccountPaymentMethod) was not taking into account the natural tree of the organization of document being processed, always was ordering by "default" account and when the query returns more than one results, if the first record's account does not belong to the natural tree of the entity's organization (in this case invoice's organization) validation was failing and an error message was shown.

To avoid it, was overwritten the getFinancialAccountPaymentMethod method of the FIN_Utility class to also filtering by financial accounts belonging to the natural tree of the entity's organization. Was necessary to adapt other classes using this method to work correctly.
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/ad_actionbutton/ProcessInvoice.java
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/filterexpression/AddPaymentDefaultValuesHandler.java
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/utility/FIN_Utility.java
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/ad_actionbutton/ProcessInvoice.java	Fri Aug 05 09:14:45 2016 +0200
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/ad_actionbutton/ProcessInvoice.java	Thu Aug 04 09:52:25 2016 -0400
@@ -227,24 +227,22 @@
                 && invoice.getBusinessPartner().getAccount() != null
                 && FIN_Utility.getFinancialAccountPaymentMethod(invoice.getPaymentMethod().getId(),
                     invoice.getBusinessPartner().getAccount().getId(), isSOTrx, invoice
-                        .getCurrency().getId()) != null
-                && osp.isInNaturalTree(invoice.getBusinessPartner().getAccount().getOrganization(),
-                    invoice.getOrganization())) {
+                        .getCurrency().getId(), invoice.getBusinessPartner().getAccount()
+                        .getOrganization().getId(), invoice.getOrganization().getId()) != null) {
               bpFinAccount = invoice.getBusinessPartner().getAccount();
             } else if (!isSOTrx
                 && invoice.getBusinessPartner().getPOFinancialAccount() != null
                 && FIN_Utility.getFinancialAccountPaymentMethod(invoice.getPaymentMethod().getId(),
                     invoice.getBusinessPartner().getPOFinancialAccount().getId(), isSOTrx, invoice
-                        .getCurrency().getId()) != null
-                && osp.isInNaturalTree(invoice.getBusinessPartner().getPOFinancialAccount()
-                    .getOrganization(), invoice.getOrganization())) {
+                        .getCurrency().getId(), invoice.getBusinessPartner()
+                        .getPOFinancialAccount().getOrganization().getId(), invoice
+                        .getOrganization().getId()) != null) {
               bpFinAccount = invoice.getBusinessPartner().getPOFinancialAccount();
             } else {
               FinAccPaymentMethod fpm = FIN_Utility.getFinancialAccountPaymentMethod(invoice
-                  .getPaymentMethod().getId(), null, isSOTrx, invoice.getCurrency().getId());
-              if (fpm != null
-                  && osp.isInNaturalTree(fpm.getAccount().getOrganization(),
-                      invoice.getOrganization())) {
+                  .getPaymentMethod().getId(), null, isSOTrx, invoice.getCurrency().getId(), null,
+                  invoice.getOrganization().getId());
+              if (fpm != null) {
                 bpFinAccount = fpm.getAccount();
               }
             }
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/filterexpression/AddPaymentDefaultValuesHandler.java	Fri Aug 05 09:14:45 2016 +0200
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/filterexpression/AddPaymentDefaultValuesHandler.java	Thu Aug 04 09:52:25 2016 -0400
@@ -11,7 +11,7 @@
  * under the License.
  * The Original Code is Openbravo ERP.
  * The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2014-2015 Openbravo SLU
+ * All portions are Copyright (C) 2014-2016 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -301,25 +301,26 @@
         if (isSOTrx
             && businessPartner.getAccount() != null
             && FIN_Utility.getFinancialAccountPaymentMethod(paymentMethodId, businessPartner
-                .getAccount().getId(), isSOTrx, currencyId) != null
-            && osp.isInNaturalTree(businessPartner.getAccount().getOrganization(), OBDal
-                .getInstance().get(Organization.class, context.getString("inpadOrgId")))) {
+                .getAccount().getId(), isSOTrx, currencyId, businessPartner.getAccount()
+                .getOrganization().getId(),
+                OBDal.getInstance().get(Organization.class, context.getString("inpadOrgId"))
+                    .getId()) != null) {
           return businessPartner.getAccount().getId();
         } else if (!isSOTrx
             && businessPartner.getPOFinancialAccount() != null
             && FIN_Utility.getFinancialAccountPaymentMethod(paymentMethodId, businessPartner
-                .getPOFinancialAccount().getId(), isSOTrx, currencyId) != null
-            && osp.isInNaturalTree(businessPartner.getPOFinancialAccount().getOrganization(), OBDal
-                .getInstance().get(Organization.class, context.getString("inpadOrgId")))) {
+                .getPOFinancialAccount().getId(), isSOTrx, currencyId, businessPartner
+                .getPOFinancialAccount().getOrganization().getId(),
+                OBDal.getInstance().get(Organization.class, context.getString("inpadOrgId"))
+                    .getId()) != null) {
           return businessPartner.getPOFinancialAccount().getId();
         }
       }
 
       FinAccPaymentMethod fpm = FIN_Utility.getFinancialAccountPaymentMethod(paymentMethodId, null,
-          isSOTrx, currencyId);
-      if (fpm != null
-          && osp.isInNaturalTree(fpm.getAccount().getOrganization(),
-              OBDal.getInstance().get(Organization.class, context.getString("inpadOrgId")))) {
+          isSOTrx, currencyId, null,
+          OBDal.getInstance().get(Organization.class, context.getString("inpadOrgId")).getId());
+      if (fpm != null) {
         return fpm.getAccount().getId();
       }
     }
@@ -385,18 +386,16 @@
       if (isSOTrx
           && businessPartner.getPaymentMethod() != null
           && FIN_Utility.getFinancialAccountPaymentMethod(businessPartner.getPaymentMethod()
-              .getId(), strFinancialAccountId, isSOTrx, null) != null
-          && osp.isInNaturalTree(businessPartner.getPaymentMethod().getOrganization(), OBDal
-              .getInstance().get(Organization.class, context.getString("inpadOrgId")))) {
+              .getId(), strFinancialAccountId, isSOTrx, null, businessPartner.getPaymentMethod()
+              .getOrganization().getId(),
+              OBDal.getInstance().get(Organization.class, context.getString("inpadOrgId")).getId()) != null) {
         return businessPartner.getPaymentMethod().getId();
-      }
-
-      else if (!isSOTrx
+      } else if (!isSOTrx
           && businessPartner.getPOPaymentMethod() != null
           && FIN_Utility.getFinancialAccountPaymentMethod(businessPartner.getPOPaymentMethod()
-              .getId(), strFinancialAccountId, isSOTrx, null) != null
-          && osp.isInNaturalTree(businessPartner.getPOPaymentMethod().getOrganization(), OBDal
-              .getInstance().get(Organization.class, context.getString("inpadOrgId")))) {
+              .getId(), strFinancialAccountId, isSOTrx, null, businessPartner.getPOPaymentMethod()
+              .getOrganization().getId(),
+              OBDal.getInstance().get(Organization.class, context.getString("inpadOrgId")).getId()) != null) {
         return businessPartner.getPOPaymentMethod().getId();
       }
     }
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/utility/FIN_Utility.java	Fri Aug 05 09:14:45 2016 +0200
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/utility/FIN_Utility.java	Thu Aug 04 09:52:25 2016 -0400
@@ -1084,8 +1084,7 @@
       invoiceDocNo = invoice.getDocumentNo();
 
       final String paymentDescription = OBDal.getInstance()
-          .get(OrganizationInformation.class, (organization.getId()))
-          .getAPRMPaymentDescription();
+          .get(OrganizationInformation.class, (organization.getId())).getAPRMPaymentDescription();
       // In case of a purchase invoice and the Supplier Reference is selected use
       // Reference
       if (paymentDescription.equals("Supplier Reference") && !invoice.isSalesTransaction()) {
@@ -1641,6 +1640,69 @@
   }
 
   /**
+   * Get an active FinAccPaymentMethod related to paymentMethodId FIN_PaymentMethod and
+   * financialAccountId FIN_FinancialAccount, if exists. If paymentMethodId is null it will retrieve
+   * any FinAccPaymentMethod related to paymentMethodId FIN_PaymentMethod ordered by default field.
+   * FinAccPaymentMethod must have pay in/out active and must be compatible with currencyId Currency
+   * if currencyId is not null. Also must validate the condition that orgId1 is in natural tree of
+   * the orgId2 to avoid get records related to financial accounts that does not belong to the
+   * natural tree of the document's organization, if the orgId1 @param is null then it will use as
+   * organization id the organization id of the account related to the financial account payment
+   * method. These validations guarantees that only is returned a valid FinAccPaymentMethod.
+   */
+  public static FinAccPaymentMethod getFinancialAccountPaymentMethod(String paymentMethodId,
+      String financialAccountId, boolean issotrx, String currencyId, String orgId1, String orgId2) {
+    StringBuffer where = new StringBuffer();
+    where.append(" as fapm");
+    where.append(" join fapm." + FinAccPaymentMethod.PROPERTY_ACCOUNT + " as fa");
+    where.append(" where fapm." + FinAccPaymentMethod.PROPERTY_PAYMENTMETHOD + " = :paymentMethod");
+    where.append(" and fa." + FIN_FinancialAccount.PROPERTY_ACTIVE + " = true");
+    if (issotrx) {
+      where.append(" and fapm." + FinAccPaymentMethod.PROPERTY_PAYINALLOW + " = true");
+    } else {
+      where.append(" and fapm." + FinAccPaymentMethod.PROPERTY_PAYOUTALLOW + " = true");
+    }
+    if (!StringUtils.isEmpty(financialAccountId)) {
+      where.append(" and fapm." + FinAccPaymentMethod.PROPERTY_ACCOUNT + " = :financialAccount");
+    }
+    if (!StringUtils.isEmpty(currencyId)) {
+      where.append(" and (fa." + FIN_FinancialAccount.PROPERTY_CURRENCY + " = :currency");
+      if (issotrx) {
+        where.append(" or fapm." + FinAccPaymentMethod.PROPERTY_PAYINISMULTICURRENCY + " = true)");
+      } else {
+        where.append(" or fapm." + FinAccPaymentMethod.PROPERTY_PAYOUTISMULTICURRENCY + " = true)");
+      }
+    }
+    if (!StringUtils.isEmpty(orgId2)) {
+      final String fapmAccOrg = "fapm." + FinAccPaymentMethod.PROPERTY_ACCOUNT + "."
+          + FIN_FinancialAccount.PROPERTY_ORGANIZATION;
+      where
+          .append(" and ad_org_isinnaturaltree("
+              + (orgId1 == null ? ("CASE WHEN " + fapmAccOrg + " IS NOT NULL THEN " + fapmAccOrg + ".id ELSE '-1' END")
+                  : "'" + orgId1 + "'") + ",'" + orgId2 + "','"
+              + OBContext.getOBContext().getCurrentClient().getId() + "') = 'Y'");
+    }
+    where.append(" order by fapm." + FinAccPaymentMethod.PROPERTY_DEFAULT + " desc");
+
+    OBQuery<FinAccPaymentMethod> qry = OBDal.getInstance().createQuery(FinAccPaymentMethod.class,
+        where.toString());
+    qry.setFilterOnReadableOrganization(false);
+    qry.setMaxResult(1);
+
+    qry.setNamedParameter("paymentMethod",
+        OBDal.getInstance().get(FIN_PaymentMethod.class, paymentMethodId));
+    if (!StringUtils.isEmpty(financialAccountId)) {
+      qry.setNamedParameter("financialAccount",
+          OBDal.getInstance().get(FIN_FinancialAccount.class, financialAccountId));
+    }
+    if (!StringUtils.isEmpty(currencyId)) {
+      qry.setNamedParameter("currency", OBDal.getInstance().get(Currency.class, currencyId));
+    }
+
+    return qry.uniqueResult();
+  }
+
+  /**
    * Appends existing Financial Account Transaction description with either GL Item Description or
    * Payment description in a new line
    */