Fixes bug 29892: Total Movement qty fixed in costing tab with backdated trx
authorUnai Martirena <unai.martirena@openbravo.com>
Thu, 14 May 2015 17:08:37 +0200
changeset 26677 ac82e4df4fb9
parent 26676 24da1cff74ce
child 26678 166dd0705d05
Fixes bug 29892: Total Movement qty fixed in costing tab with backdated trx

While working with cost adjustments, on certain cases the existing Costing records changes. This can happen because the cost has been recalculated due to backdated transactions, price adjustments, manual cost corrections, etc. In all this cases the 'Total Movement Quantity' field was not being correctly updated.
This field has to store the current stock of the product on that moment. So, each time the costing record is updated it is being checked if this value changes, and if it has changed the current stock is set.
src/org/openbravo/costing/AverageCostAdjustment.java
--- a/src/org/openbravo/costing/AverageCostAdjustment.java	Wed May 13 18:06:30 2015 +0200
+++ b/src/org/openbravo/costing/AverageCostAdjustment.java	Thu May 14 17:08:37 2015 +0200
@@ -198,22 +198,26 @@
         OBDal.getInstance().flush();
       } else {
         Costing curCosting = basetrx.getMaterialMgmtCostingList().get(0);
-        if (curCosting.getCost().compareTo(cost) != 0) {
-          // Update existing costing
+
+        if (curCosting.getCost().compareTo(cost) != 0
+            || curCosting.getTotalMovementQuantity().compareTo(currentStock) != 0) {
           curCosting.setPermanent(Boolean.FALSE);
           OBDal.getInstance().save(curCosting);
           OBDal.getInstance().flush();
-          if (curCosting.getOriginalCost() == null) {
-            curCosting.setOriginalCost(curCosting.getCost());
+          // Update existing costing
+          if (curCosting.getCost().compareTo(cost) != 0) {
+            if (curCosting.getOriginalCost() == null) {
+              curCosting.setOriginalCost(curCosting.getCost());
+            }
+            curCosting.setCost(cost);
+            curCosting.setPrice(trxPrice);
           }
-          curCosting.setCost(cost);
-          curCosting.setPrice(trxPrice);
+          curCosting.setTotalMovementQuantity(currentStock);
           curCosting.setPermanent(Boolean.TRUE);
           OBDal.getInstance().flush();
           OBDal.getInstance().save(curCosting);
         }
       }
-
     }
 
     // Modify isManufacturingProduct flag in case it has changed at some point.
@@ -328,8 +332,10 @@
                 negCorrAmt.toPlainString(), cost.toPlainString());
           }
 
-          if (curCosting.getCost().compareTo(cost) == 0 && StringUtils.isEmpty(bdCostingId)) {
-            // new cost hasn't changed, following transactions will have the same cost, so no more
+          if (curCosting.getCost().compareTo(cost) == 0 && StringUtils.isEmpty(bdCostingId)
+              && curCosting.getTotalMovementQuantity().compareTo(currentStock) == 0) {
+            // new cost hasn't changed and total movement qty is equal to current stock, following
+            // transactions will have the same cost, so no more
             // related transactions are needed to include.
             // If bdCosting is not empty it is needed to loop through the next related transaction
             // to set the new time ringe of the costing.
@@ -340,11 +346,14 @@
             curCosting.setPermanent(Boolean.FALSE);
             OBDal.getInstance().save(curCosting);
             OBDal.getInstance().flush();
-            if (curCosting.getOriginalCost() == null) {
-              curCosting.setOriginalCost(curCosting.getCost());
+            if (curCosting.getCost().compareTo(cost) != 0) {
+              if (curCosting.getOriginalCost() == null) {
+                curCosting.setOriginalCost(curCosting.getCost());
+              }
+              curCosting.setPrice(trxPrice);
+              curCosting.setCost(cost);
             }
-            curCosting.setPrice(trxPrice);
-            curCosting.setCost(cost);
+            curCosting.setTotalMovementQuantity(currentStock);
             curCosting.setPermanent(Boolean.TRUE);
             OBDal.getInstance().save(curCosting);
           }
@@ -386,15 +395,19 @@
               trxPrice = trxUnitCost.add(trxUnitCostAdjAmt).divide(trx.getMovementQuantity().abs(),
                   costCurPrecission, RoundingMode.HALF_UP);
             }
-            if (curCosting.getCost().compareTo(cost) != 0) {
+            if (curCosting.getCost().compareTo(cost) != 0
+                || curCosting.getTotalMovementQuantity().compareTo(currentStock) != 0) {
               curCosting.setPermanent(Boolean.FALSE);
               OBDal.getInstance().save(curCosting);
               OBDal.getInstance().flush();
-              if (curCosting.getOriginalCost() == null) {
-                curCosting.setOriginalCost(curCosting.getCost());
+              if (curCosting.getCost().compareTo(cost) != 0) {
+                if (curCosting.getOriginalCost() == null) {
+                  curCosting.setOriginalCost(curCosting.getCost());
+                }
+                curCosting.setPrice(trxPrice);
+                curCosting.setCost(cost);
               }
-              curCosting.setPrice(trxPrice);
-              curCosting.setCost(cost);
+              curCosting.setTotalMovementQuantity(currentStock);
               curCosting.setPermanent(Boolean.TRUE);
               OBDal.getInstance().save(curCosting);
             }