From 7136116f9367fbc9a37f35d463e507b0312cc5a5 Mon Sep 17 00:00:00 2001
From: Alejandro Matos <alekosmp86@gmail.com>
Date: Mon, 28 Dec 2020 10:36:40 -0500
Subject: [PATCH] Do not retrieve the financial account of the new store to be
 added using its name, but a map object which relates the original financial
 account with the new one

---
 .../DigitalCouponsPostProcessHandler.java     | 32 ++++---------------
 .../DigitalCouponsPropertiesHandler.java      | 14 ++------
 2 files changed, 9 insertions(+), 37 deletions(-)

diff --git a/src/org/openbravo/retail/copystore/digitalcoupons/DigitalCouponsPostProcessHandler.java b/src/org/openbravo/retail/copystore/digitalcoupons/DigitalCouponsPostProcessHandler.java
index 6461752..5555d87 100644
--- a/src/org/openbravo/retail/copystore/digitalcoupons/DigitalCouponsPostProcessHandler.java
+++ b/src/org/openbravo/retail/copystore/digitalcoupons/DigitalCouponsPostProcessHandler.java
@@ -49,15 +49,12 @@ public class DigitalCouponsPostProcessHandler extends PostProcessHandler {
    *          The original financial account
    */
   private void processFinancialAccount(CopyStoreProcess process, IssuingCompanyAccount account) {
-    String newFinAcctName = process.copiedFinAccountNamelist
-        .get(account.getFinancialAccount().getName().toString());
-    if (newFinAcctName != null) {
-      FIN_FinancialAccount finAccount = getFinancialAccount(process.newStore, newFinAcctName);
-      if (finAccount != null && process.isInOrgTree(account.getIssuingCompany())) {
-        IssuingCompanyAccount newICAccount = OBProvider.getInstance()
-            .get(IssuingCompanyAccount.class);
-        process.cloneObject(account, newICAccount, true);
-      }
+    FIN_FinancialAccount finAccount = (FIN_FinancialAccount) process.newObjects
+        .get(FIN_FinancialAccount.ENTITY_NAME + '-' + account.getFinancialAccount().getId());
+    if (finAccount != null && process.isInOrgTree(account.getIssuingCompany())) {
+      IssuingCompanyAccount newICAccount = OBProvider.getInstance()
+          .get(IssuingCompanyAccount.class);
+      process.cloneObject(account, newICAccount, true);
     }
   }
 
@@ -74,21 +71,4 @@ public class DigitalCouponsPostProcessHandler extends PostProcessHandler {
     criICAccounts.add(Restrictions.eq(IssuingCompanyAccount.PROPERTY_ORGANIZATION, originalStore));
     return criICAccounts.list();
   }
-
-  /**
-   * Gets the financial account related to the new store with the given name
-   * 
-   * @param newStore
-   *          The new store
-   * @param finAccountName
-   *          Financial account name
-   * @return A financial account
-   */
-  private FIN_FinancialAccount getFinancialAccount(Organization newStore, String finAccountName) {
-    OBCriteria<FIN_FinancialAccount> criFinAccount = OBDal.getInstance()
-        .createCriteria(FIN_FinancialAccount.class);
-    criFinAccount.add(Restrictions.eq(FIN_FinancialAccount.PROPERTY_ORGANIZATION, newStore));
-    criFinAccount.add(Restrictions.eq(FIN_FinancialAccount.PROPERTY_NAME, finAccountName));
-    return (FIN_FinancialAccount) criFinAccount.uniqueResult();
-  }
 }
diff --git a/src/org/openbravo/retail/copystore/digitalcoupons/DigitalCouponsPropertiesHandler.java b/src/org/openbravo/retail/copystore/digitalcoupons/DigitalCouponsPropertiesHandler.java
index 3e74eff..f4617d0 100644
--- a/src/org/openbravo/retail/copystore/digitalcoupons/DigitalCouponsPropertiesHandler.java
+++ b/src/org/openbravo/retail/copystore/digitalcoupons/DigitalCouponsPropertiesHandler.java
@@ -9,11 +9,8 @@
 
 package org.openbravo.retail.copystore.digitalcoupons;
 
-import org.hibernate.criterion.Restrictions;
 import org.openbravo.base.structure.BaseOBObject;
 import org.openbravo.client.kernel.ComponentProvider.Qualifier;
-import org.openbravo.dal.service.OBCriteria;
-import org.openbravo.dal.service.OBDal;
 import org.openbravo.model.financialmgmt.payment.FIN_FinancialAccount;
 import org.openbravo.retail.copystore.process.CopyStoreProcess;
 import org.openbravo.retail.copystore.process.FKPropertyHandler;
@@ -55,14 +52,9 @@ public class DigitalCouponsPropertiesHandler {
      */
     private FIN_FinancialAccount getFinancialAccount(IssuingCompanyAccount originalObject,
         CopyStoreProcess process) {
-      String newFinAcctName = process.copiedFinAccountNamelist
-          .get(originalObject.getFinancialAccount().getName().toString());
-      OBCriteria<FIN_FinancialAccount> criteriaAccounts = OBDal.getInstance()
-          .createCriteria(FIN_FinancialAccount.class);
-      criteriaAccounts
-          .add(Restrictions.eq(FIN_FinancialAccount.PROPERTY_ORGANIZATION, process.newStore));
-      criteriaAccounts.add(Restrictions.eq(FIN_FinancialAccount.PROPERTY_NAME, newFinAcctName));
-      return (FIN_FinancialAccount) criteriaAccounts.uniqueResult();
+      FIN_FinancialAccount finAccount = (FIN_FinancialAccount) process.newObjects.get(
+          FIN_FinancialAccount.ENTITY_NAME + '-' + originalObject.getFinancialAccount().getId());
+      return finAccount;
     }
   }
 }
-- 
2.28.0.windows.1

