# HG changeset patch
# User Atul Gaware <atul.gaware@openbravo.com>
# Date 1580199912 -19800
#      Tue Jan 28 13:55:12 2020 +0530
# Node ID c22eda008409f7dbe5a9168a2ae89cc409ede241
# Parent  9bb45c7a8667b06c65b69d4be7e10effd5cfda91
Fixes BUG-0043004: 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 9bb45c7a8667 -r c22eda008409 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:55:12 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 9bb45c7a8667 -r c22eda008409 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:55:12 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 9bb45c7a8667 -r c22eda008409 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:55:12 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");
-  }
 }
