Fixes issue 33407: Error in Costing Background due to manual costing records
authorDavid Miguelez <david.miguelez@openbravo.com>
Fri, 01 Jul 2016 09:51:44 +0200
changeset 29841 f2a3f94930f2
parent 29840 3f6e4a7da362
child 29842 be7ed445aa54
Fixes issue 33407: Error in Costing Background due to manual costing records

Do not take into account manually inserted Costing Entries when calculating the current Stock or the current valued Stock.
Also, centralize conditions into one boolean variable.
src/org/openbravo/costing/CostAdjustmentUtils.java
src/org/openbravo/costing/CostingUtils.java
--- a/src/org/openbravo/costing/CostAdjustmentUtils.java	Thu Jun 30 18:04:33 2016 +0200
+++ b/src/org/openbravo/costing/CostAdjustmentUtils.java	Fri Jul 01 09:51:44 2016 +0200
@@ -174,8 +174,8 @@
     where.append("   and trx." + MaterialTransaction.PROPERTY_MOVEMENTQUANTITY + " >= :trxqty");
     where.append(" ))");
 
-    where.append(
-        "   and trunc(trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + ") > :movementDate");
+    where.append("   and trunc(trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE
+        + ") > :movementDate");
     where.append("   and trx." + MaterialTransaction.PROPERTY_PRODUCT + ".id = :productId");
     where.append("   and trx." + MaterialTransaction.PROPERTY_ORGANIZATION + ".id in (:orgs)");
 
@@ -390,6 +390,12 @@
     CostingRule costingRule = CostingUtils.getCostDimensionRule(costorg,
         trx.getTransactionProcessDate());
 
+    boolean existsCumulatedStockOnTrxDate = costing != null
+        && costing.getTotalMovementQuantity() != null
+        && costing.getInventoryTransaction() != null
+        && (!costingRule.isBackdatedTransactionsFixed() || trx.getMovementDate().after(
+            costing.getInventoryTransaction().getMovementDate()));
+
     StringBuffer select = new StringBuffer();
     select
         .append(" select sum(trx." + MaterialTransaction.PROPERTY_MOVEMENTQUANTITY + ") as stock");
@@ -404,10 +410,7 @@
     select.append("   and trx." + MaterialTransaction.PROPERTY_PRODUCT + " = :product");
     // Include only transactions that have its cost calculated. Should be all.
     select.append("   and trx." + MaterialTransaction.PROPERTY_ISCOSTCALCULATED + " = true");
-    if (costing != null
-        && costing.getTotalMovementQuantity() != null
-        && (!costingRule.isBackdatedTransactionsFixed() || trx.getMovementDate().after(
-            costing.getInventoryTransaction().getMovementDate()))) {
+    if (existsCumulatedStockOnTrxDate) {
       select.append(" and (trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE
           + " > :dateFrom");
       select.append(" or (trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE
@@ -484,10 +487,7 @@
     trxQry.setParameter("trxqty", trx.getMovementQuantity());
     trxQry.setParameter("trxid", trx.getId());
 
-    if (costing != null
-        && costing.getTotalMovementQuantity() != null
-        && (!costingRule.isBackdatedTransactionsFixed() || trx.getMovementDate().after(
-            costing.getInventoryTransaction().getMovementDate()))) {
+    if (existsCumulatedStockOnTrxDate) {
       trxQry.setParameter("dateFrom", costing.getStartingDate());
     }
 
@@ -504,10 +504,7 @@
     if (stock == null) {
       stock = BigDecimal.ZERO;
     }
-    if (costing != null
-        && costing.getTotalMovementQuantity() != null
-        && (!costingRule.isBackdatedTransactionsFixed() || trx.getMovementDate().after(
-            costing.getInventoryTransaction().getMovementDate()))) {
+    if (existsCumulatedStockOnTrxDate) {
       stock = stock.add(costing.getTotalMovementQuantity());
     }
     return stock;
@@ -700,6 +697,12 @@
     CostingRule costingRule = CostingUtils.getCostDimensionRule(costorg,
         trx.getTransactionProcessDate());
 
+    boolean existsCumulatedValuationOnTrxDate = costing != null
+        && costing.getTotalStockValuation() != null
+        && costing.getInventoryTransaction() != null
+        && (!costingRule.isBackdatedTransactionsFixed() || trx.getMovementDate().after(
+            costing.getInventoryTransaction().getMovementDate()));
+
     StringBuffer select = new StringBuffer();
     select.append(" select sum(case");
     select.append("     when trx." + MaterialTransaction.PROPERTY_MOVEMENTQUANTITY
@@ -722,10 +725,7 @@
     select.append("  and trx." + MaterialTransaction.PROPERTY_PRODUCT + " = :product");
     // Include only transactions that have its cost calculated
     select.append("  and trx." + MaterialTransaction.PROPERTY_ISCOSTCALCULATED + " = true");
-    if (costing != null
-        && costing.getTotalStockValuation() != null
-        && (!costingRule.isBackdatedTransactionsFixed() || trx.getMovementDate().after(
-            costing.getInventoryTransaction().getMovementDate()))) {
+    if (existsCumulatedValuationOnTrxDate) {
       select.append(" and (trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE
           + " > :dateFrom");
       select.append(" or (trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE
@@ -799,10 +799,7 @@
     Query trxQry = OBDal.getInstance().getSession().createQuery(select.toString());
     trxQry.setParameter("refid", MovementTypeRefID);
     trxQry.setParameter("product", trx.getProduct());
-    if (costing != null
-        && costing.getTotalStockValuation() != null
-        && (!costingRule.isBackdatedTransactionsFixed() || trx.getMovementDate().after(
-            costing.getInventoryTransaction().getMovementDate()))) {
+    if (existsCumulatedValuationOnTrxDate) {
       trxQry.setParameter("dateFrom", costing.getStartingDate());
     }
     if (costDimensions.get(CostDimension.Warehouse) != null) {
@@ -839,10 +836,7 @@
       scroll.close();
     }
 
-    if (costing != null
-        && costing.getTotalStockValuation() != null
-        && (!costingRule.isBackdatedTransactionsFixed() || trx.getMovementDate().after(
-            costing.getInventoryTransaction().getMovementDate()))) {
+    if (existsCumulatedValuationOnTrxDate) {
       BigDecimal costingValuedStock = costing.getTotalStockValuation();
       if (!StringUtils.equals(costing.getCurrency().getId(), currency.getId())) {
         costingValuedStock = FinancialUtils.getConvertedAmount(costingValuedStock,
--- a/src/org/openbravo/costing/CostingUtils.java	Thu Jun 30 18:04:33 2016 +0200
+++ b/src/org/openbravo/costing/CostingUtils.java	Fri Jul 01 09:51:44 2016 +0200
@@ -365,6 +365,9 @@
     Set<String> orgs = OBContext.getOBContext().getOrganizationStructureProvider()
         .getChildTree(costorg.getId(), true);
 
+    boolean existsCumulatedStock = costing != null && costing.getInventoryTransaction() != null
+        && costing.getTotalMovementQuantity() != null;
+
     StringBuffer select = new StringBuffer();
     select
         .append(" select sum(trx." + MaterialTransaction.PROPERTY_MOVEMENTQUANTITY + ") as stock");
@@ -372,13 +375,13 @@
     if (costDimensions.get(CostDimension.Warehouse) != null) {
       select.append(" join trx." + MaterialTransaction.PROPERTY_STORAGEBIN + " as locator");
     }
-    if (costing != null && costing.getTotalMovementQuantity() != null) {
+    if (existsCumulatedStock) {
       select.append(", " + org.openbravo.model.ad.domain.List.ENTITY_NAME + " as trxtype");
     }
     select.append(" where trx." + MaterialTransaction.PROPERTY_PRODUCT + ".id = :product");
     select
         .append(" and trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE + " <= :dateTo");
-    if (costing != null && costing.getTotalMovementQuantity() != null) {
+    if (existsCumulatedStock) {
       select.append(" and trxtype." + CostAdjustmentUtils.propADListValue + " = trx."
           + MaterialTransaction.PROPERTY_MOVEMENTTYPE);
       select.append(" and trxtype." + CostAdjustmentUtils.propADListReference + ".id = :refid");
@@ -402,7 +405,7 @@
     Query trxQry = OBDal.getInstance().getSession().createQuery(select.toString());
     trxQry.setParameter("product", product.getId());
     trxQry.setParameter("dateTo", dateTo);
-    if (costing != null && costing.getTotalMovementQuantity() != null) {
+    if (existsCumulatedStock) {
       trxQry.setParameter("refid", CostAdjustmentUtils.MovementTypeRefID);
       trxQry.setParameter("dateFrom", costing.getStartingDate());
       trxQry.setParameter("trxTypePrio",
@@ -418,7 +421,7 @@
     if (stock == null) {
       stock = BigDecimal.ZERO;
     }
-    if (costing != null && costing.getTotalMovementQuantity() != null) {
+    if (existsCumulatedStock) {
       stock = stock.add(costing.getTotalMovementQuantity());
     }
     return stock;
@@ -447,6 +450,9 @@
     Set<String> orgs = OBContext.getOBContext().getOrganizationStructureProvider()
         .getChildTree(costorg.getId(), true);
 
+    boolean existsCumulatedValuation = costing != null && costing.getInventoryTransaction() != null
+        && costing.getTotalStockValuation() != null;
+
     StringBuffer select = new StringBuffer();
     select.append(" select sum(case");
     select.append(" when trx." + MaterialTransaction.PROPERTY_MOVEMENTQUANTITY + " < 0 then -tc."
@@ -460,14 +466,14 @@
     if (costDimensions.get(CostDimension.Warehouse) != null) {
       select.append(" join trx." + MaterialTransaction.PROPERTY_STORAGEBIN + " as locator");
     }
-    if (costing != null && costing.getTotalStockValuation() != null) {
+    if (existsCumulatedValuation) {
       select.append(", " + org.openbravo.model.ad.domain.List.ENTITY_NAME + " as trxtype");
     }
 
     select.append(" where trx." + MaterialTransaction.PROPERTY_PRODUCT + ".id = :product");
     select
         .append(" and trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE + " <= :dateTo");
-    if (costing != null && costing.getTotalStockValuation() != null) {
+    if (existsCumulatedValuation) {
       select.append(" and trxtype." + CostAdjustmentUtils.propADListValue + " = trx."
           + MaterialTransaction.PROPERTY_MOVEMENTTYPE);
       select.append(" and trxtype." + CostAdjustmentUtils.propADListReference + ".id = :refid");
@@ -494,7 +500,7 @@
     Query trxQry = OBDal.getInstance().getSession().createQuery(select.toString());
     trxQry.setParameter("product", product.getId());
     trxQry.setParameter("dateTo", dateTo);
-    if (costing != null && costing.getTotalStockValuation() != null) {
+    if (existsCumulatedValuation) {
       trxQry.setParameter("refid", CostAdjustmentUtils.MovementTypeRefID);
       trxQry.setParameter("dateFrom", costing.getStartingDate());
       trxQry.setParameter("trxTypePrio",
@@ -528,7 +534,7 @@
       scroll.close();
     }
 
-    if (costing != null && costing.getTotalStockValuation() != null) {
+    if (existsCumulatedValuation) {
       BigDecimal costingValuedStock = costing.getTotalStockValuation();
       if (!StringUtils.equals(costing.getCurrency().getId(), currency.getId())) {
         costingValuedStock = FinancialUtils.getConvertedAmount(costingValuedStock,