# HG changeset patch
# User Atul Gaware <atul.gaware@openbravo.com>
# Date 1580200029 -19800
#      Tue Jan 28 13:57:09 2020 +0530
# Node ID d01f3672bfea599d497996f2e30435ef7b588db6
# Parent  99d3b6153ad217b575382ed842b78eff821b6c7a
Fixes BUG-0043003: Validating a new costing rule is trying to
create physical inventories in organizations where transactions
are not allowed

**Use Organization from the Parent list of Warehouse Organization
whose organization type has transaction allowed flag set as Yes,
if no such organization is found, the use Organization from the
Inventory Line
* Used final modifier to variables and parameters
* Defined separated methods to improve readability

diff -r 99d3b6153ad2 -r d01f3672bfea src/org/openbravo/costing/CostingRuleProcess.java
--- a/src/org/openbravo/costing/CostingRuleProcess.java	Mon Jan 20 11:37:25 2020 +0100
+++ b/src/org/openbravo/costing/CostingRuleProcess.java	Tue Jan 28 13:57:09 2020 +0530
@@ -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) 2012-2018 Openbravo SLU
+ * All portions are Copyright (C) 2012-2020 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  *************************************************************************
@@ -504,11 +504,15 @@
     }
     String clientId = rule.getClient().getId();
     String orgId = rule.getOrganization().getId();
+    Warehouse warehouse = (Warehouse) OBDal.getInstance()
+            .getProxy(Warehouse.ENTITY_NAME, warehouseId);
+    final Organization invOrg = CostingUtils.getOrganizationForCloseAndOpenInventories(orgId,
+            warehouse);
     CostingRuleInit cri = OBProvider.getInstance().get(CostingRuleInit.class);
     cri.setClient((Client) OBDal.getInstance().getProxy(Client.ENTITY_NAME, clientId));
     cri.setOrganization(
         (Organization) OBDal.getInstance().getProxy(Organization.ENTITY_NAME, orgId));
-    cri.setWarehouse((Warehouse) OBDal.getInstance().getProxy(Warehouse.ENTITY_NAME, warehouseId));
+    cri.setWarehouse(warehouse);
     cri.setCostingRule(rule);
     List<CostingRuleInit> criList = rule.getCostingRuleInitList();
     criList.add(cri);
@@ -516,22 +520,18 @@
 
     InventoryCount closeInv = OBProvider.getInstance().get(InventoryCount.class);
     closeInv.setClient((Client) OBDal.getInstance().getProxy(Client.ENTITY_NAME, clientId));
-    closeInv.setOrganization(
-        (Organization) OBDal.getInstance().getProxy(Organization.ENTITY_NAME, orgId));
+    closeInv.setOrganization(invOrg);
     closeInv.setName(OBMessageUtils.messageBD("CostCloseInventory"));
-    closeInv
-        .setWarehouse((Warehouse) OBDal.getInstance().getProxy(Warehouse.ENTITY_NAME, warehouseId));
+    closeInv.setWarehouse(warehouse);
     closeInv.setMovementDate(localDate);
     closeInv.setInventoryType("C");
     cri.setCloseInventory(closeInv);
 
     InventoryCount initInv = OBProvider.getInstance().get(InventoryCount.class);
     initInv.setClient((Client) OBDal.getInstance().getProxy(Client.ENTITY_NAME, clientId));
-    initInv.setOrganization(
-        (Organization) OBDal.getInstance().getProxy(Organization.ENTITY_NAME, orgId));
+    initInv.setOrganization(invOrg);
     initInv.setName(OBMessageUtils.messageBD("CostInitInventory"));
-    initInv
-        .setWarehouse((Warehouse) OBDal.getInstance().getProxy(Warehouse.ENTITY_NAME, warehouseId));
+    initInv.setWarehouse(warehouse);
     initInv.setMovementDate(localDate);
     initInv.setInventoryType("O");
     cri.setInitInventory(initInv);
diff -r 99d3b6153ad2 -r d01f3672bfea src/org/openbravo/costing/CostingUtils.java
--- a/src/org/openbravo/costing/CostingUtils.java	Mon Jan 20 11:37:25 2020 +0100
+++ b/src/org/openbravo/costing/CostingUtils.java	Tue Jan 28 13:57:09 2020 +0530
@@ -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) 2012-2018 Openbravo SLU
+ * All portions are Copyright (C) 2012-2020 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  *************************************************************************
@@ -929,4 +929,32 @@
     qry.setMaxResults(1);
     return !qry.list().isEmpty();
   }
+
+  public static Organization getOrganizationForCloseAndOpenInventories(final String OrgId,
+      final Warehouse warehouse) {
+    Organization invOrg = getTransactionAllowedOrg(warehouse.getOrganization());
+    if (invOrg == null) {
+      return (Organization) OBDal.getInstance().getProxy(Organization.ENTITY_NAME, OrgId);
+    }
+    return invOrg;
+  }
+
+  private static Organization getTransactionAllowedOrg(final Organization org) {
+    if (org.getOrganizationType().isTransactionsAllowed()) {
+      return org;
+    } else {
+      final Organization parentOrg = OBContext.getOBContext()
+          .getOrganizationStructureProvider()
+          .getParentOrg(org);
+      if (parentOrg != null && !isStarOrganization(parentOrg)) {
+        return getTransactionAllowedOrg(parentOrg);
+      } else {
+        return null;
+      }
+    }
+  }
+
+  private static boolean isStarOrganization(final Organization parentOrg) {
+    return StringUtils.equals(parentOrg.getId(), "0");
+  }
 }
diff -r 99d3b6153ad2 -r d01f3672bfea src/org/openbravo/costing/InventoryAmountUpdateProcess.java
--- a/src/org/openbravo/costing/InventoryAmountUpdateProcess.java	Mon Jan 20 11:37:25 2020 +0100
+++ b/src/org/openbravo/costing/InventoryAmountUpdateProcess.java	Tue Jan 28 13:57:09 2020 +0530
@@ -26,7 +26,6 @@
 import java.util.Map;
 import java.util.Set;
 
-import org.apache.commons.lang.StringUtils;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.codehaus.jettison.json.JSONObject;
@@ -358,7 +357,8 @@
     invLine.setInventoryAmountUpdateLineInventoriesList(invList);
 
     final InventoryCount closeInv = OBProvider.getInstance().get(InventoryCount.class);
-    final Organization invOrg = getOrganizationForCloseAndOpenInventories(orgId, warehouse);
+    final Organization invOrg = CostingUtils.getOrganizationForCloseAndOpenInventories(orgId,
+        warehouse);
     closeInv.setClient(client);
     closeInv.setName(OBMessageUtils.messageBD("InvAmtUpdCloseInventory"));
     closeInv.setWarehouse(warehouse);
@@ -417,33 +417,4 @@
     OBDal.getInstance().flush();
     return icl;
   }
-
-  private Organization getOrganizationForCloseAndOpenInventories(final String inventoryLineOrgId,
-      final Warehouse warehouse) {
-    Organization invOrg = getTransactionAllowedOrg(warehouse.getOrganization());
-    if (invOrg == null) {
-      return (Organization) OBDal.getInstance()
-          .getProxy(Organization.ENTITY_NAME, inventoryLineOrgId);
-    }
-    return invOrg;
-  }
-
-  private Organization getTransactionAllowedOrg(final Organization org) {
-    if (org.getOrganizationType().isTransactionsAllowed()) {
-      return org;
-    } else {
-      final Organization parentOrg = OBContext.getOBContext()
-          .getOrganizationStructureProvider()
-          .getParentOrg(org);
-      if (parentOrg != null && !isStarOrganization(parentOrg)) {
-        return getTransactionAllowedOrg(parentOrg);
-      } else {
-        return null;
-      }
-    }
-  }
-
-  private boolean isStarOrganization(final Organization parentOrg) {
-    return StringUtils.equals(parentOrg.getId(), "0");
-  }
 }
