Fixes bug 29076: Division by zero error managed in Costing Background.
authorUnai Martirena <unai.martirena@openbravo.com>
Thu, 26 Feb 2015 15:29:20 +0100
changeset 26086 67607f6caaee
parent 26085 ecf72f76195b
child 26087 4b1565029827
Fixes bug 29076: Division by zero error managed in Costing Background.

Under certain circumnstance a division by zero error was happening in Cost Adjustments process inside Costing Background (when calculating backdated adjustments of a transaction of movementqty zero). This problem has been managed by being sure that this division will not happen again. If the movementqty of the transaction is zero, the cost of the full transaction will be zero as well.
src/org/openbravo/costing/AverageCostAdjustment.java
--- a/src/org/openbravo/costing/AverageCostAdjustment.java	Thu Feb 26 15:23:25 2015 +0100
+++ b/src/org/openbravo/costing/AverageCostAdjustment.java	Thu Feb 26 15:29:20 2015 +0100
@@ -135,8 +135,13 @@
     log.debug("Starting average cost {}", cost == null ? "not cost" : cost.toPlainString());
     if (cost != null && (AverageAlgorithm.modifiesAverage(trxType) || !baseCAL.isBackdatedTrx())) {
       BigDecimal trxCost = CostAdjustmentUtils.getTrxCost(basetrx, false, getCostCurrency());
-      BigDecimal trxPrice = trxCost.add(adjustmentBalance.multiply(signMultiplier)).divide(
-          basetrx.getMovementQuantity().abs(), costCurPrecission, RoundingMode.HALF_UP);
+      BigDecimal trxPrice = null;
+      if (basetrx.getMovementQuantity().signum() == 0) {
+        trxPrice = BigDecimal.ZERO;
+      } else {
+        trxPrice = trxCost.add(adjustmentBalance.multiply(signMultiplier)).divide(
+            basetrx.getMovementQuantity().abs(), costCurPrecission, RoundingMode.HALF_UP);
+      }
       if (checkNegativeStockCorrection && currentStock.compareTo(basetrx.getMovementQuantity()) < 0
           && cost.compareTo(trxPrice) != 0 && !baseCAL.isNegativeStockCorrection()
           && AverageAlgorithm.modifiesAverage(trxType)) {
@@ -286,9 +291,14 @@
           }
           log.debug("New average cost: {}", cost.toPlainString());
           Costing curCosting = trx.getMaterialMgmtCostingList().get(0);
-          BigDecimal trxPrice = curCosting.getPrice().multiply(trx.getMovementQuantity().abs())
-              .add(trxAdjAmt.multiply(trxSignMultiplier))
-              .divide(trx.getMovementQuantity().abs(), costCurPrecission, RoundingMode.HALF_UP);
+          BigDecimal trxPrice = null;
+          if (trx.getMovementQuantity().signum() == 0) {
+            trxPrice = BigDecimal.ZERO;
+          } else {
+            trxPrice = curCosting.getPrice().multiply(trx.getMovementQuantity().abs())
+                .add(trxAdjAmt.multiply(trxSignMultiplier))
+                .divide(trx.getMovementQuantity().abs(), costCurPrecission, RoundingMode.HALF_UP);
+          }
 
           if (checkNegativeStockCorrection && currentStock.compareTo(trx.getMovementQuantity()) < 0
               && cost.compareTo(trxPrice) != 0) {
@@ -360,9 +370,14 @@
               cost = currentValueAmt.add(adjustmentBalance).divide(currentStock, costCurPrecission,
                   RoundingMode.HALF_UP);
             }
-            BigDecimal trxPrice = curCosting.getPrice().multiply(trx.getMovementQuantity().abs())
-                .add(trxAdjAmt.multiply(trxSignMultiplier))
-                .divide(trx.getMovementQuantity().abs(), costCurPrecission, RoundingMode.HALF_UP);
+            BigDecimal trxPrice = null;
+            if (trx.getMovementQuantity().signum() == 0) {
+              trxPrice = BigDecimal.ZERO;
+            } else {
+              trxPrice = curCosting.getPrice().multiply(trx.getMovementQuantity().abs())
+                  .add(trxAdjAmt.multiply(trxSignMultiplier))
+                  .divide(trx.getMovementQuantity().abs(), costCurPrecission, RoundingMode.HALF_UP);
+            }
             if (curCosting.getCost().compareTo(cost) != 0) {
               curCosting.setPermanent(Boolean.FALSE);
               OBDal.getInstance().save(curCosting);