Fixes issue 41793:Fix backdated transaction is not fixing all backdated trx
authorArmaignac <collazoandy4@gmail.com>
Mon, 09 Sep 2019 12:07:53 -0400
changeset 36562 b75877805a1a
parent 36561 db9908961a58
child 36563 45f5100585a8
Fixes issue 41793:Fix backdated transaction is not fixing all backdated trx

When the fix backdated transaction process is executed in a costing rule to enable
backdated corrections the process is not adjusting all existing backdated transactions
because the method isNeededBackdatedCostAdjustment from CostAdjustmentUtils is
not returning value when an old transaction already calculated is passed as parameter if was
processed after last transaction and the old transaction movement date is before

Now the process create an ajustment if the transaction movement date is before previous
adjusted transaction movement date
src/org/openbravo/costing/AverageCostAdjustment.java
src/org/openbravo/costing/FixBackdatedTransactionsProcess.java
--- a/src/org/openbravo/costing/AverageCostAdjustment.java	Wed Sep 04 18:31:06 2019 -0400
+++ b/src/org/openbravo/costing/AverageCostAdjustment.java	Mon Sep 09 12:07:53 2019 -0400
@@ -95,7 +95,7 @@
     CostAdjustmentLine baseCAL = getCostAdjLine();
     for (CostAdjustmentLine costAdjLine : getTrxAdjustmentLines(basetrx)) {
       BigDecimal adjustmentAmt = costAdjLine.getAdjustmentAmount();
-      if (!strCostCurrencyId.equals(costAdjLine.getCurrency().getId())) {
+      if (!strCostCurrencyId.equals(costAdjLine.getCurrency().getId()) && adjustmentAmt != null) {
         adjustmentAmt = FinancialUtils.getConvertedAmount(adjustmentAmt, costAdjLine.getCurrency(),
             getCostCurrency(), costAdjLine.getAccountingDate(), getCostOrg(),
             FinancialUtils.PRECISION_STANDARD);
@@ -105,6 +105,14 @@
         if (costAdjLine.isSource() && !costAdjLine.isRelatedTransactionAdjusted()
             && !costAdjLine.getId().equals(strCostAdjLineId)) {
           searchRelatedTransactionCosts(costAdjLine);
+          if (adjustmentAmt == null) {
+            adjustmentAmt = costAdjLine.getAdjustmentAmount();
+            if (!strCostCurrencyId.equals(costAdjLine.getCurrency().getId())) {
+              adjustmentAmt = FinancialUtils.getConvertedAmount(adjustmentAmt,
+                  costAdjLine.getCurrency(), getCostCurrency(), costAdjLine.getAccountingDate(),
+                  getCostOrg(), FinancialUtils.PRECISION_STANDARD);
+            }
+          }
         }
 
         costAdjLine.setRelatedTransactionAdjusted(Boolean.TRUE);
@@ -260,7 +268,7 @@
         List<CostAdjustmentLine> existingAdjLines = getTrxAdjustmentLines(trx);
         for (CostAdjustmentLine existingCAL : existingAdjLines) {
           BigDecimal adjustmentAmt = existingCAL.getAdjustmentAmount();
-          if (!strCurrentCurId.equals(existingCAL.getCurrency().getId())) {
+          if (!strCurrentCurId.equals(existingCAL.getCurrency().getId()) && adjustmentAmt != null) {
             Currency curCurrency = OBDal.getInstance().get(Currency.class, strCurrentCurId);
             adjustmentAmt = FinancialUtils.getConvertedAmount(adjustmentAmt,
                 existingCAL.getCurrency(), curCurrency, existingCAL.getAccountingDate(),
@@ -269,6 +277,15 @@
           if (isFromThisCostAdjustment(existingCAL)) {
             if (existingCAL.isSource() && !existingCAL.isRelatedTransactionAdjusted()) {
               searchRelatedTransactionCosts(existingCAL);
+              if (adjustmentAmt == null) {
+                adjustmentAmt = existingCAL.getAdjustmentAmount();
+                if (!strCurrentCurId.equals(existingCAL.getCurrency().getId())) {
+                  Currency curCurrency = OBDal.getInstance().get(Currency.class, strCurrentCurId);
+                  adjustmentAmt = FinancialUtils.getConvertedAmount(adjustmentAmt,
+                      existingCAL.getCurrency(), curCurrency, existingCAL.getAccountingDate(),
+                      getCostOrg(), FinancialUtils.PRECISION_STANDARD);
+                }
+              }
             }
             if (existingCAL.getTransactionCostList().isEmpty()) {
               trxAdjAmt = trxAdjAmt.add(adjustmentAmt);
--- a/src/org/openbravo/costing/FixBackdatedTransactionsProcess.java	Wed Sep 04 18:31:06 2019 -0400
+++ b/src/org/openbravo/costing/FixBackdatedTransactionsProcess.java	Mon Sep 09 12:07:53 2019 -0400
@@ -19,6 +19,7 @@
 package org.openbravo.costing;
 
 import java.text.ParseException;
+import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.Map;
 import java.util.Set;
@@ -95,10 +96,11 @@
             rule.getEndingDate());
         int i = 0;
         try {
+          SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
+          Date lastMovementDate = dateFormat.parse("01-01-1900");
           while (transactions.next()) {
             MaterialTransaction trx = (MaterialTransaction) transactions.get()[0];
-            if (CostAdjustmentUtils.isNeededBackdatedCostAdjustment(trx,
-                rule.isWarehouseDimension(), CostingUtils.getCostingRuleStartingDate(rule))) {
+            if (trx.getMovementDate().compareTo(lastMovementDate) < 0) {
               createCostAdjustmenHeader(rule.getOrganization());
               final CostAdjustmentLineParameters lineParameters = new CostAdjustmentLineParameters(
                   trx, null, costAdjHeader);
@@ -112,6 +114,8 @@
                 // Reload rule after clear session.
                 rule = OBDal.getInstance().get(CostingRule.class, ruleId);
               }
+            } else {
+              lastMovementDate = trx.getMovementDate();
             }
           }
         } finally {
@@ -192,7 +196,7 @@
       select.append(
           " and trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE + " < (:endDate)");
     }
-    select.append(" order by trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE);
+    select.append(" order by trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE);
 
     Query<MaterialTransaction> stockLinesQry = OBDal.getInstance()
         .getSession()