details:   https://code.openbravo.com/erp/devel/pi/rev/ffedbefe6e2f
changeset: 29725:ffedbefe6e2f
user:      David Miguelez <david.miguelez <at> openbravo.com>
date:      Fri Jul 01 09:51:44 2016 +0200
summary:   Fixes issue 33391: 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.

diffstat:

 src/org/openbravo/costing/CostAdjustmentUtils.java |  46 +++++++++------------
 src/org/openbravo/costing/CostingUtils.java        |  22 ++++++---
 2 files changed, 34 insertions(+), 34 deletions(-)

diffs (204 lines):

diff -r 291e57012457 -r ffedbefe6e2f 
src/org/openbravo/costing/CostAdjustmentUtils.java
--- a/src/org/openbravo/costing/CostAdjustmentUtils.java        Fri Jul 01 
13:06:12 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,
diff -r 291e57012457 -r ffedbefe6e2f src/org/openbravo/costing/CostingUtils.java
--- a/src/org/openbravo/costing/CostingUtils.java       Fri Jul 01 13:06:12 
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,

------------------------------------------------------------------------------
Attend Shape: An AT&T Tech Expo July 15-16. Meet us at AT&T Park in San
Francisco, CA to explore cutting-edge tech and listen to tech luminaries
present their vision of the future. This family event has something for
everyone, including kids. Get more information and register today.
http://sdm.link/attshape
_______________________________________________
Openbravo-commits mailing list
Openbravo-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to