details:   https://code.openbravo.com/erp/devel/pi/rev/b852c03d58e7
changeset: 25224:b852c03d58e7
user:      Unai Martirena <unai.martirena <at> openbravo.com>
date:      Wed Nov 12 11:56:25 2014 +0100
summary:   Related to Issue 28113: Add some missing currency roundings

details:   https://code.openbravo.com/erp/devel/pi/rev/3b8d1e5bf534
changeset: 25225:3b8d1e5bf534
user:      Unai Martirena <unai.martirena <at> openbravo.com>
date:      Wed Nov 12 12:19:40 2014 +0100
summary:   Fixes Bug 28115: Is Cost Permanent transactions added as input in 
Costing tab

diffstat:

 src/org/openbravo/costing/AverageCostAdjustment.java |  140 +++++++++++++-----
 1 files changed, 97 insertions(+), 43 deletions(-)

diffs (197 lines):

diff -r 9f21ea10365f -r 3b8d1e5bf534 
src/org/openbravo/costing/AverageCostAdjustment.java
--- a/src/org/openbravo/costing/AverageCostAdjustment.java      Wed Nov 12 
11:24:27 2014 +0100
+++ b/src/org/openbravo/costing/AverageCostAdjustment.java      Wed Nov 12 
12:19:40 2014 +0100
@@ -127,12 +127,13 @@
           RoundingMode.HALF_UP);
     }
     log.debug("Starting average cost {}", cost == null ? "not cost" : 
cost.toPlainString());
-    if (AverageAlgorithm.modifiesAverage(trxType) && cost != null) {
+    if (cost != null && (AverageAlgorithm.modifiesAverage(trxType) || 
!baseCAL.isBackdatedTrx())) {
       BigDecimal trxCost = CostAdjustmentUtils.getTrxCost(basetrx, false, 
getCostCurrency());
       BigDecimal trxPrice = trxCost.add(adjustmentBalance).divide(
           basetrx.getMovementQuantity().abs(), costCurPrecission, 
RoundingMode.HALF_UP);
       if (checkNegativeStockCorrection && 
currentStock.compareTo(basetrx.getMovementQuantity()) < 0
-          && cost.compareTo(trxPrice) != 0 && 
!baseCAL.isNegativeStockCorrection()) {
+          && cost.compareTo(trxPrice) != 0 && 
!baseCAL.isNegativeStockCorrection()
+          && AverageAlgorithm.modifiesAverage(trxType)) {
         // stock was negative and cost different than trx price then Negative 
Stock Correction
         // is added
         BigDecimal trxSignMultiplier = new 
BigDecimal(basetrx.getMovementQuantity().signum());
@@ -150,22 +151,54 @@
         log.debug("Negative stock correction. Amount: {}, new cost {}", 
negCorrAmt.toPlainString(),
             cost.toPlainString());
       }
+      if (basetrx.getMaterialMgmtCostingList().size() == 0) {
+        Date newDate = new Date();
+        Date dateTo = costing.getEndingDate();
+        costing.setEndingDate(newDate);
+        OBDal.getInstance().save(costing);
+        Costing newCosting = OBProvider.getInstance().get(Costing.class);
+        newCosting.setCost(cost);
+        newCosting.setCurrency((Currency) 
OBDal.getInstance().getProxy(Currency.ENTITY_NAME,
+            strCostCurrencyId));
+        newCosting.setStartingDate(newDate);
+        newCosting.setEndingDate(dateTo);
+        newCosting.setInventoryTransaction(basetrx);
+        newCosting.setProduct(basetrx.getProduct());
+        if (isManufacturingProduct) {
+          newCosting.setOrganization((Organization) 
OBDal.getInstance().getProxy(
+              Organization.ENTITY_NAME, "0"));
+        } else {
+          newCosting.setOrganization((Organization) 
OBDal.getInstance().getProxy(
+              Organization.ENTITY_NAME, strCostOrgId));
+        }
+        newCosting.setQuantity(basetrx.getMovementQuantity());
+        newCosting.setTotalMovementQuantity(currentStock);
+        newCosting.setPrice(cost);
+        newCosting.setCostType("AVA");
+        newCosting.setManual(false);
+        newCosting.setPermanent(true);
+        newCosting.setProduction(trxType == TrxType.ManufacturingProduced);
+        newCosting.setWarehouse((Warehouse) 
getCostDimensions().get(CostDimension.Warehouse));
+        OBDal.getInstance().save(newCosting);
+        OBDal.getInstance().flush();
+      } else {
+        Costing curCosting = basetrx.getMaterialMgmtCostingList().get(0);
+        if (curCosting.getCost().compareTo(cost) != 0) {
+          // Update existing costing
+          curCosting.setPermanent(Boolean.FALSE);
+          OBDal.getInstance().save(curCosting);
+          OBDal.getInstance().flush();
+          if (curCosting.getOriginalCost() == null) {
+            curCosting.setOriginalCost(curCosting.getCost());
+          }
+          curCosting.setCost(cost);
+          curCosting.setPrice(cost);
+          curCosting.setPermanent(Boolean.TRUE);
+          OBDal.getInstance().flush();
+          OBDal.getInstance().save(curCosting);
+        }
+      }
 
-      Costing curCosting = basetrx.getMaterialMgmtCostingList().get(0);
-      if (curCosting.getCost().compareTo(cost) != 0) {
-        // Update existing costing
-        curCosting.setPermanent(Boolean.FALSE);
-        OBDal.getInstance().save(curCosting);
-        OBDal.getInstance().flush();
-        if (curCosting.getOriginalCost() == null) {
-          curCosting.setOriginalCost(curCosting.getCost());
-        }
-        curCosting.setCost(cost);
-        curCosting.setPrice(cost);
-        curCosting.setPermanent(Boolean.TRUE);
-        OBDal.getInstance().flush();
-        OBDal.getInstance().save(curCosting);
-      }
     }
 
     // Modify isManufacturingProduct flag in case it has changed at some point.
@@ -248,7 +281,8 @@
           log.debug("New average cost: {}", cost.toPlainString());
           Costing curCosting = trx.getMaterialMgmtCostingList().get(0);
           BigDecimal trxPrice = 
curCosting.getPrice().multiply(trx.getMovementQuantity().abs())
-              .add(trxAdjAmt).divide(trx.getMovementQuantity().abs());
+              .add(trxAdjAmt)
+              .divide(trx.getMovementQuantity().abs(), costCurPrecission, 
RoundingMode.HALF_UP);
 
           if (checkNegativeStockCorrection && 
currentStock.compareTo(trx.getMovementQuantity()) < 0
               && cost.compareTo(trxPrice) != 0) {
@@ -290,25 +324,47 @@
             curCosting.setPermanent(Boolean.TRUE);
             OBDal.getInstance().save(curCosting);
           }
-        } else if (!trx.isCostPermanent() && cost != null && !isVoidedTrx(trx, 
currentTrxType)) {
-          // Check current trx unit cost matches new expected cost
-          BigDecimal expectedCost = 
cost.multiply(trx.getMovementQuantity().abs());
-          BigDecimal unitCost = CostAdjustmentUtils.getTrxCost(trx, true,
-              OBDal.getInstance().get(Currency.class, strCurrentCurId));
-          unitCost = unitCost.add(trxAdjAmt);
-          log.debug("Is adjustment needed? Expected {} vs Current {}",
-              expectedCost.toPlainString(), unitCost.toPlainString());
-          if (expectedCost.compareTo(unitCost) != 0) {
-            trxAdjAmt = 
trxAdjAmt.add(expectedCost.subtract(unitCost).multiply(trxSignMultiplier));
-            adjustmentBalance = 
adjustmentBalance.add(expectedCost.subtract(unitCost).multiply(
-                trxSignMultiplier));
-            // If there is a difference insert a cost adjustment line.
-            CostAdjustmentLine newCAL = insertCostAdjustmentLine(trx,
-                expectedCost.subtract(unitCost), null);
-            newCAL.setRelatedTransactionAdjusted(true);
-            OBDal.getInstance().save(newCAL);
-            log.debug("Adjustment added. Amount {}.", 
expectedCost.subtract(unitCost)
-                .toPlainString());
+        } else if (cost != null && !isVoidedTrx(trx, currentTrxType)) {
+          if (!trx.isCostPermanent()) {
+            // Check current trx unit cost matches new expected cost
+            BigDecimal expectedCost = 
cost.multiply(trx.getMovementQuantity().abs()
+                .setScale(costCurPrecission, RoundingMode.HALF_UP));
+            BigDecimal unitCost = CostAdjustmentUtils.getTrxCost(trx, true, 
OBDal.getInstance()
+                .get(Currency.class, strCurrentCurId));
+            unitCost = unitCost.add(trxAdjAmt);
+            log.debug("Is adjustment needed? Expected {} vs Current {}",
+                expectedCost.toPlainString(), unitCost.toPlainString());
+            if (expectedCost.compareTo(unitCost) != 0) {
+              trxAdjAmt = trxAdjAmt
+                  
.add(expectedCost.subtract(unitCost).multiply(trxSignMultiplier));
+              adjustmentBalance = 
adjustmentBalance.add(expectedCost.subtract(unitCost).multiply(
+                  trxSignMultiplier));
+              // If there is a difference insert a cost adjustment line.
+              CostAdjustmentLine newCAL = insertCostAdjustmentLine(trx,
+                  expectedCost.subtract(unitCost), null);
+              newCAL.setRelatedTransactionAdjusted(true);
+              OBDal.getInstance().save(newCAL);
+              log.debug("Adjustment added. Amount {}.", 
expectedCost.subtract(unitCost)
+                  .toPlainString());
+            }
+          }
+          if (trx.getMaterialMgmtCostingList().size() != 0) {
+            Costing curCosting = trx.getMaterialMgmtCostingList().get(0);
+            if (currentStock.signum() != 0) {
+              cost = 
currentValueAmt.add(adjustmentBalance).divide(currentStock, costCurPrecission,
+                  RoundingMode.HALF_UP);
+            }
+            if (curCosting.getCost().compareTo(cost) != 0) {
+              curCosting.setPermanent(Boolean.FALSE);
+              OBDal.getInstance().save(curCosting);
+              OBDal.getInstance().flush();
+              if (curCosting.getOriginalCost() == null) {
+                curCosting.setOriginalCost(curCosting.getCost());
+              }
+              curCosting.setPrice(cost);
+              curCosting.setCost(cost);
+              OBDal.getInstance().save(curCosting);
+            }
           }
         }
 
@@ -323,10 +379,6 @@
       // This is the current costing rule. Check if current average cost needs 
to be updated.
       Costing currentCosting = AverageAlgorithm.getProductCost(new Date(), 
basetrx.getProduct(),
           getCostDimensions(), getCostOrg());
-      // if (currentStock.signum() != 0) {
-      // cost = currentValueAmt.add(adjustmentBalance).divide(currentStock, 
costCurPrecission,
-      // RoundingMode.HALF_UP);
-      // }
       if (currentCosting.getCost().compareTo(cost) != 0) {
         basetrx = getTransaction();
         Date newDate = new Date();
@@ -407,7 +459,8 @@
           costCurrency, trx.getTransactionProcessDate(), getCostOrg(),
           FinancialUtils.PRECISION_COSTING);
     }
-    BigDecimal expectedCostAmt = 
trx.getMovementQuantity().abs().multiply(cost);
+    BigDecimal expectedCostAmt = trx.getMovementQuantity().abs().multiply(cost)
+        .setScale(costCurPrecission, RoundingMode.HALF_UP);
     BigDecimal currentCost = trx.getTransactionCost();
     return expectedCostAmt.subtract(currentCost);
   }
@@ -555,7 +608,8 @@
 
     Costing curCosting = basetrx.getMaterialMgmtCostingList().get(0);
     BigDecimal trxPrice = curCosting.getPrice();
-    BigDecimal adjustAmt = 
currentStock.multiply(trxPrice).subtract(currentValueAmt);
+    BigDecimal adjustAmt = currentStock.multiply(trxPrice)
+        .setScale(stdCurPrecission, 
RoundingMode.HALF_UP).subtract(currentValueAmt);
 
     costAdjLine.setCurrency((Currency) 
OBDal.getInstance().getProxy(Currency.ENTITY_NAME,
         strCostCurrencyId));

------------------------------------------------------------------------------
Comprehensive Server Monitoring with Site24x7.
Monitor 10 servers for $9/Month.
Get alerted through email, SMS, voice calls or mobile push notifications.
Take corrective actions from your mobile device.
http://pubads.g.doubleclick.net/gampad/clk?id=154624111&iu=/4140/ostg.clktrk
_______________________________________________
Openbravo-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to