Fixes BUG-42994:Validating a new costing rule is trying to
authorAtul Gaware <atul.gaware@openbravo.com>
Mon, 27 Jan 2020 08:11:56 +0530
changeset 37001 82b3930f899d
parent 37000 c5ff57ca6732
child 37002 3788eb72814b
Fixes BUG 42994:Validating a new costing rule is trying to
create physical inventories in organizations where transactions
are not allowed


** Moved method from InventoryAmountUpdateProcess to CostingUtils
to achieve reusability that uses Organization from the Parent
list of Warehouse Organization whose organization type has
transaction allowed flag set as Yes, if no such organization
is found, then use Organization from the Inventory Line when
validating costing rule process which creates Closing and
Opening Inventories.
src/org/openbravo/costing/CostingRuleProcess.java
src/org/openbravo/costing/CostingUtils.java
src/org/openbravo/costing/InventoryAmountUpdateProcess.java
--- a/src/org/openbravo/costing/CostingRuleProcess.java	Fri Jan 24 08:55:59 2020 +0100
+++ b/src/org/openbravo/costing/CostingRuleProcess.java	Mon Jan 27 08:11:56 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-2019 Openbravo SLU
+ * All portions are Copyright (C) 2012-2020 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  *************************************************************************
@@ -510,7 +510,9 @@
 
     InventoryCount closeInv = OBProvider.getInstance().get(InventoryCount.class);
     closeInv.setClient((Client) OBDal.getInstance().getProxy(Client.ENTITY_NAME, clientId));
-    closeInv.setOrganization(warehouse.getOrganization());
+    final Organization invOrg = CostingUtils.getOrganizationForCloseAndOpenInventories(orgId,
+        warehouse);
+    closeInv.setOrganization(invOrg);
     closeInv.setName(OBMessageUtils.messageBD("CostCloseInventory"));
     closeInv.setWarehouse(warehouse);
     closeInv.setMovementDate(localDate);
@@ -519,7 +521,7 @@
 
     InventoryCount initInv = OBProvider.getInstance().get(InventoryCount.class);
     initInv.setClient((Client) OBDal.getInstance().getProxy(Client.ENTITY_NAME, clientId));
-    initInv.setOrganization(warehouse.getOrganization());
+    initInv.setOrganization(invOrg);
     initInv.setName(OBMessageUtils.messageBD("CostInitInventory"));
     initInv.setWarehouse(warehouse);
     initInv.setMovementDate(localDate);
--- a/src/org/openbravo/costing/CostingUtils.java	Fri Jan 24 08:55:59 2020 +0100
+++ b/src/org/openbravo/costing/CostingUtils.java	Mon Jan 27 08:11:56 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");
+  }
 }
--- a/src/org/openbravo/costing/InventoryAmountUpdateProcess.java	Fri Jan 24 08:55:59 2020 +0100
+++ b/src/org/openbravo/costing/InventoryAmountUpdateProcess.java	Mon Jan 27 08:11:56 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");
-  }
 }