Fixes Issue 39822. Fixes issues when merging new cost adjustment lines
authorDavid Miguelez <david.miguelez@openbravo.com>
Mon, 17 Dec 2018 17:50:52 +0100
changeset 35314 11dbadbe4ba4
parent 35313 b4c3e3431595
child 35315 4b812cac3744
Fixes Issue 39822. Fixes issues when merging new cost adjustment lines
with existing ones.

To avoid creating more Adjusment Lines than needed, the process
tries to find existing cost adjustment lines and it adds the cost
adjusted to an existing line if any.
But when looking for existing lines it was not taking into account
the unit cost, negative correction or backdated flags.

To do so, it is necessary to send the parameters to the methods
that creates the lines. But adding more parameters to this
functions was too complex, as they already had many parameters.

What has been done is to create a new class to store the line information.
This class is initialized with just three parameters and the
rest are set to the default values.

Then, the developer can change any parameter with the setter methods.
This makes the code more clear and clean, instead of having a
function with more than 10 parameters, being 4 of them booleans,
now it creates a new object with three parameters and then it
sets the boolean values with methods that have a name with more
meaning.

After this change, all the information of the line is set through this
new object, so there is no need to change the line object afterwards,
since all the different parameters are set to the LineParameter object.

Changes:

Database:
* Change default value of needsposting column to use the same
one as in AD and avoid confusion

CostAdjustmentLineParameters:
* New class to store lines information
* Does not allow to create adjustment lines with empty cost adjusment
or empty Transaction
* Does not create an adjustment that is both negative and unit cost.
This was not controlled before

CostAdjustmentUtils:
* Deprecated old methods that does not send the new flags
* Created new API to use the new LineParams object
* Modify query to take into account new flags
* Modify insert method to set all the parameters for the new
Cost Adjusment Line

CostingAlgorithmAdjustmentImp:
* Modify inser line method to use new API

Adapted all the calls to old methods to use new API
src-db/database/model/tables/M_COSTADJUSTMENTLINE.xml
src-db/database/sourcedata/AD_MESSAGE.xml
src/org/openbravo/costing/AverageCostAdjustment.java
src/org/openbravo/costing/CostAdjustmentLineParameters.java
src/org/openbravo/costing/CostAdjustmentUtils.java
src/org/openbravo/costing/CostingAlgorithmAdjustmentImp.java
src/org/openbravo/costing/CostingServer.java
src/org/openbravo/costing/FixBackdatedTransactionsProcess.java
src/org/openbravo/costing/LCMatchingProcess.java
src/org/openbravo/costing/LandedCostProcess.java
src/org/openbravo/costing/ManualCostAdjustmentProcessHandler.java
src/org/openbravo/costing/PriceDifferenceProcess.java
src/org/openbravo/costing/StandardCostAdjustment.java
--- a/src-db/database/model/tables/M_COSTADJUSTMENTLINE.xml	Mon Jan 14 16:03:40 2019 +0100
+++ b/src-db/database/model/tables/M_COSTADJUSTMENTLINE.xml	Mon Dec 17 17:50:52 2018 +0100
@@ -50,7 +50,7 @@
         <onCreateDefault/>
       </column>
       <column name="NEEDSPOSTING" primaryKey="false" required="true" type="CHAR" size="1" autoIncrement="false">
-        <default><![CDATA[N]]></default>
+        <default><![CDATA[Y]]></default>
         <onCreateDefault/>
       </column>
       <column name="DATEACCT" primaryKey="false" required="false" type="TIMESTAMP" size="7" autoIncrement="false">
--- a/src-db/database/sourcedata/AD_MESSAGE.xml	Mon Jan 14 16:03:40 2019 +0100
+++ b/src-db/database/sourcedata/AD_MESSAGE.xml	Mon Dec 17 17:50:52 2018 +0100
@@ -26873,6 +26873,18 @@
 <!--D84035A620344FF3A01D117939F38E8A-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
 <!--D84035A620344FF3A01D117939F38E8A--></AD_MESSAGE>
 
+<!--D84471E258424AA294671DD85D553F85--><AD_MESSAGE>
+<!--D84471E258424AA294671DD85D553F85-->  <AD_MESSAGE_ID><![CDATA[D84471E258424AA294671DD85D553F85]]></AD_MESSAGE_ID>
+<!--D84471E258424AA294671DD85D553F85-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--D84471E258424AA294671DD85D553F85-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--D84471E258424AA294671DD85D553F85-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--D84471E258424AA294671DD85D553F85-->  <VALUE><![CDATA[CostAdjsutmentCalculationError]]></VALUE>
+<!--D84471E258424AA294671DD85D553F85-->  <MSGTEXT><![CDATA[There has been a problem while calculating the Cost Adjustments, this process can not be completed.]]></MSGTEXT>
+<!--D84471E258424AA294671DD85D553F85-->  <MSGTYPE><![CDATA[E]]></MSGTYPE>
+<!--D84471E258424AA294671DD85D553F85-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--D84471E258424AA294671DD85D553F85-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
+<!--D84471E258424AA294671DD85D553F85--></AD_MESSAGE>
+
 <!--D84A9308B4674840AB3D78817A6A884D--><AD_MESSAGE>
 <!--D84A9308B4674840AB3D78817A6A884D-->  <AD_MESSAGE_ID><![CDATA[D84A9308B4674840AB3D78817A6A884D]]></AD_MESSAGE_ID>
 <!--D84A9308B4674840AB3D78817A6A884D-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
--- a/src/org/openbravo/costing/AverageCostAdjustment.java	Mon Jan 14 16:03:40 2019 +0100
+++ b/src/org/openbravo/costing/AverageCostAdjustment.java	Mon Dec 17 17:50:52 2018 +0100
@@ -26,6 +26,8 @@
 import java.util.Set;
 
 import org.apache.commons.lang.StringUtils;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 import org.hibernate.ScrollMode;
 import org.hibernate.ScrollableResults;
 import org.hibernate.criterion.Restrictions;
@@ -55,8 +57,6 @@
 import org.openbravo.model.materialmgmt.cost.CostingRule;
 import org.openbravo.model.materialmgmt.cost.TransactionCost;
 import org.openbravo.model.materialmgmt.transaction.MaterialTransaction;
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.LogManager;
 
 @ComponentProvider.Qualifier("org.openbravo.costing.AverageAlgorithm")
 public class AverageCostAdjustment extends CostingAlgorithmAdjustmentImp {
@@ -168,11 +168,11 @@
             .subtract(adjustmentBalance);
         adjustmentBalance = adjustmentBalance.add(negCorrAmt.multiply(trxSignMultiplier));
         // If there is a difference insert a cost adjustment line.
-        CostAdjustmentLine newCAL = insertCostAdjustmentLine(basetrx, negCorrAmt, null);
-        newCAL.setNegativeStockCorrection(Boolean.TRUE);
-        newCAL.setRelatedTransactionAdjusted(Boolean.TRUE);
-        newCAL.setUnitCost(Boolean.FALSE);
-        OBDal.getInstance().save(newCAL);
+        final CostAdjustmentLineParameters lineParameters = new CostAdjustmentLineParameters(
+            basetrx, negCorrAmt, getCostAdj());
+        lineParameters.setNegativeCorrection(true);
+        lineParameters.setRelatedTransactionAdjusted(true);
+        insertCostAdjustmentLine(lineParameters);
         cost = trxPrice;
         log.debug("Negative stock correction. Amount: {}, new cost {}", negCorrAmt.toPlainString(),
             cost.toPlainString());
@@ -319,9 +319,10 @@
                 adjustmentBalance = adjustmentBalance.add(unitCostDifference
                     .multiply(trxSignMultiplier));
                 // If there is a difference insert a cost adjustment line.
-                CostAdjustmentLine newCAL = insertCostAdjustmentLine(trx, unitCostDifference, null);
-                newCAL.setRelatedTransactionAdjusted(Boolean.TRUE);
-                OBDal.getInstance().save(newCAL);
+                final CostAdjustmentLineParameters lineParameters = new CostAdjustmentLineParameters(
+                    trx, unitCostDifference, getCostAdj());
+                lineParameters.setRelatedTransactionAdjusted(true);
+                insertCostAdjustmentLine(lineParameters);
                 log.debug("Adjustment added. Amount {}.", unitCostDifference.toPlainString());
               }
 
@@ -348,11 +349,11 @@
                 .subtract(adjustmentBalance);
             adjustmentBalance = adjustmentBalance.add(negCorrAmt.multiply(trxSignMultiplier));
             // If there is a difference insert a cost adjustment line.
-            CostAdjustmentLine newCAL = insertCostAdjustmentLine(trx, negCorrAmt, null);
-            newCAL.setNegativeStockCorrection(Boolean.TRUE);
-            newCAL.setRelatedTransactionAdjusted(Boolean.TRUE);
-            newCAL.setUnitCost(Boolean.FALSE);
-            OBDal.getInstance().save(newCAL);
+            final CostAdjustmentLineParameters lineParameters = new CostAdjustmentLineParameters(
+                trx, negCorrAmt, getCostAdj());
+            lineParameters.setNegativeCorrection(true);
+            lineParameters.setRelatedTransactionAdjusted(true);
+            insertCostAdjustmentLine(lineParameters);
             cost = trxPrice;
             log.debug("Negative stock correction. Amount: {}, new cost {}",
                 negCorrAmt.toPlainString(), cost.toPlainString());
@@ -367,12 +368,11 @@
               }
               adjustmentBalance = adjustmentBalance.add(revertedNegativeAdjustment);
               // If there is a difference insert a cost adjustment line.
-              CostAdjustmentLine newCAL = insertCostAdjustmentLine(trx, revertedNegativeAdjustment,
-                  null);
-              newCAL.setNegativeStockCorrection(Boolean.TRUE);
-              newCAL.setRelatedTransactionAdjusted(Boolean.TRUE);
-              newCAL.setUnitCost(Boolean.FALSE);
-              OBDal.getInstance().save(newCAL);
+              final CostAdjustmentLineParameters lineParameters = new CostAdjustmentLineParameters(
+                  trx, revertedNegativeAdjustment, getCostAdj());
+              lineParameters.setNegativeCorrection(true);
+              lineParameters.setRelatedTransactionAdjusted(true);
+              insertCostAdjustmentLine(lineParameters);
               cost = currentValueAmt.add(adjustmentBalance).divide(currentStock, costCurPrecission,
                   RoundingMode.HALF_UP);
               log.debug("Revert Negative stock correction. Amount: {}, new cost {}",
@@ -412,9 +412,10 @@
               trxUnitCostAdjAmt = trxUnitCostAdjAmt.add(newAdjAmt);
               adjustmentBalance = adjustmentBalance.add(newAdjAmt.multiply(trxSignMultiplier));
               // If there is a difference insert a cost adjustment line.
-              CostAdjustmentLine newCAL = insertCostAdjustmentLine(trx, newAdjAmt, null);
-              newCAL.setRelatedTransactionAdjusted(Boolean.TRUE);
-              OBDal.getInstance().save(newCAL);
+              final CostAdjustmentLineParameters lineParameters = new CostAdjustmentLineParameters(
+                  trx, newAdjAmt, getCostAdj());
+              lineParameters.setRelatedTransactionAdjusted(true);
+              insertCostAdjustmentLine(lineParameters);
               log.debug("Adjustment added. Amount {}.", newAdjAmt.toPlainString());
             }
           }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/openbravo/costing/CostAdjustmentLineParameters.java	Mon Dec 17 17:50:52 2018 +0100
@@ -0,0 +1,233 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License.
+ * The Original Code is Openbravo ERP.
+ * The Initial Developer of the Original Code is Openbravo SLU
+ * All portions are Copyright (C) 2014-2018 Openbravo SLU
+ * All Rights Reserved.
+ * Contributor(s):  ______________________________________.
+ *************************************************************************
+ */
+
+package org.openbravo.costing;
+
+import java.math.BigDecimal;
+
+import org.openbravo.base.exception.OBException;
+import org.openbravo.erpCommon.utility.OBMessageUtils;
+import org.openbravo.model.materialmgmt.cost.CostAdjustment;
+import org.openbravo.model.materialmgmt.transaction.MaterialTransaction;
+
+/**
+ * This class contains the necessary information to create a Cost Adjustment Line. Based on the
+ * information stored in the class, a process will be able to create a Cost Adjustment Line
+ * afterwards.
+ * 
+ * It controls that it is not possible to create a Cost Adjustment Line with an empty Transaction or
+ * an empty Cost Adjustment Header.
+ * 
+ * Also, it is not possible to create an adjustment that is both Unit and Negative Correction. When
+ * the Negative Correction flag is true, the Unit Cost flag is set to false automatically.
+ *
+ */
+public class CostAdjustmentLineParameters {
+  private MaterialTransaction transaction;
+  private CostAdjustment costAdjustmentHeader;
+  private BigDecimal costadjusted;
+  private boolean isSource;
+  private boolean isUnitCost;
+  private boolean isBackdatedTransaction;
+  private boolean isNegativeCorrection;
+  private boolean isNeedPosting;
+  private boolean isRelatedTransactionAdjusted;
+
+  /**
+   * It creates a new object based on the given parameters. It also defaults the values of some
+   * other variables:
+   * <ol>
+   * <li>isSource: false</il>
+   * <li>isUnitCost: true></il>
+   * <li>isBackdatedTransaction: false</il>
+   * <li>isNegativeCorrection: false</il>
+   * <li>isNeedsPosting: true</il>
+   * <li>isRelatedTransactionAdjusted: false</il>
+   * </ol>
+   * 
+   * @param transaction
+   *          The Transaction for which the adjustment is going to be made
+   * @param costAdjusted
+   *          The amount that will be adjusted against the Transaction
+   * @param costAdjustmentHeader
+   *          The Cost Adjustment Document that will contain the adjustment line
+   */
+  public CostAdjustmentLineParameters(final MaterialTransaction transaction,
+      final BigDecimal costAdjusted, final CostAdjustment costAdjustmentHeader) {
+    if (transaction == null || costAdjustmentHeader == null) {
+      throw new OBException(OBMessageUtils.messageBD("CostAdjsutmentCalculationError"));
+    }
+
+    this.transaction = transaction;
+    this.costAdjustmentHeader = costAdjustmentHeader;
+    this.costadjusted = costAdjusted;
+    this.isSource = false;
+    this.isUnitCost = true;
+    this.isBackdatedTransaction = false;
+    this.isNegativeCorrection = false;
+    this.isNeedPosting = true;
+    this.isRelatedTransactionAdjusted = false;
+  }
+
+  /**
+   * 
+   * @return the value of the is Source flag. An adjustment that is source can create several other
+   *         adjustments in cascade.
+   */
+  public boolean isSource() {
+    return isSource;
+  }
+
+  /**
+   * Set the is Source flag value. An adjustment that is source can create several other adjustments
+   * in cascade.
+   */
+  public void setSource(final boolean isSource) {
+    this.isSource = isSource;
+  }
+
+  /**
+   * 
+   * @return the value of the is Unit Cost flag. An adjustment that is Unit Cost will modify the
+   *         unitary cost of the Transaction (the intrinsic cost, not taking additional costs like
+   *         Landed Costs into account) A Unit Cost can not be a Negative Correction at the same
+   *         time.
+   */
+  public boolean isUnitCost() {
+    return isUnitCost;
+  }
+
+  /**
+   * Sets the value of the Unit Cost flag. An adjustment that is Unit Cost will modify the unitary
+   * cost of the Transaction (the intrinsic cost, not taking additional costs like Landed Costs into
+   * account) A Unit Cost can not be a Negative Correction at the same time.
+   * 
+   * If the value of the Unit Cost is true, then the value of the Negative Correction is set to
+   * false automatically.
+   */
+  public void setUnitCost(final boolean isUnitCost) {
+    this.isUnitCost = isUnitCost;
+    if (isUnitCost) {
+      this.isNegativeCorrection = false;
+    }
+  }
+
+  /**
+   * 
+   * @return the value of the is Backdated Transaction flag. A Backdated Transaction adjustment
+   *         means that it will take place in the past, and it will affect transactions between the
+   *         past and the present.
+   */
+  public boolean isBackdatedTransaction() {
+    return isBackdatedTransaction;
+  }
+
+  /**
+   * Sets the value of the Backdated Transaction flag. A Backdated Transaction adjustment means that
+   * it will take place in the past, and it will affect transactions between the past and the
+   * present.
+   */
+  public void setBackdatedTransaction(final boolean isBackdatedTransaction) {
+    this.isBackdatedTransaction = isBackdatedTransaction;
+  }
+
+  /**
+   * 
+   * @return the value of the Negative Correction flag. A Negative Correction Adjustment happens
+   *         when there is negative stock and the related transaction is going to increase the stock
+   *         quantity. A Negative Correction can not be a Unit Cost adjustment at the same time.
+   */
+  public boolean isNegativeCorrection() {
+    return isNegativeCorrection;
+  }
+
+  /**
+   * Sets the value of the Negative Correction flag. A Negative Correction Adjustment happens when
+   * there is negative stock and the related transaction is going to increase the stock quantity. A
+   * Negative Correction can not be a Unit Cost adjustment at the same time.
+   * 
+   * If the value of the Negative Correction is true, then the value of the Unit Cost is set to
+   * false automatically.
+   */
+  public void setNegativeCorrection(final boolean isNegativeCorrection) {
+    this.isNegativeCorrection = isNegativeCorrection;
+    if (isNegativeCorrection) {
+      this.isUnitCost = false;
+    }
+  }
+
+  /**
+   * 
+   * @return the value of the is Need Posting flag. It is used to differentiate the adjustments that
+   *         needs to be posted to the ledger to the ones that are not needed
+   */
+  public boolean isNeedPosting() {
+    return isNeedPosting;
+  }
+
+  /**
+   * Sets the value of the is Need Posting flag. It is used to differentiate the adjustments that
+   * needs to be posted to the ledger to the ones that are not needed
+   */
+  public void setNeedPosting(boolean isNeedPosting) {
+    this.isNeedPosting = isNeedPosting;
+  }
+
+  /**
+   * 
+   * @return the value of the is Related Transaction Adjusted flag. It is true when the related
+   *         transaction has been taken into account already in the adjustment process
+   */
+  public boolean isRelatedTransactionAdjusted() {
+    return isRelatedTransactionAdjusted;
+  }
+
+  /**
+   * Sets the is Related Transaction Adjusted flag. It is true when the related transaction has been
+   * taken into account already in the adjustment process
+   */
+  public void setRelatedTransactionAdjusted(boolean isRelatedTransactionAdjusted) {
+    this.isRelatedTransactionAdjusted = isRelatedTransactionAdjusted;
+  }
+
+  /**
+   * 
+   * @return The transaction that is being adjusted
+   */
+  public MaterialTransaction getTransaction() {
+    return transaction;
+  }
+
+  /**
+   * 
+   * @return The Cost Adjustment document that is going to contain the cost adjustment line
+   */
+  public CostAdjustment getCostAdjustmentHeader() {
+    return costAdjustmentHeader;
+  }
+
+  /**
+   * 
+   * @return The amount that is going to be adjusted to the transaction
+   */
+  public BigDecimal getCostadjusted() {
+    return costadjusted;
+  }
+
+}
--- a/src/org/openbravo/costing/CostAdjustmentUtils.java	Mon Jan 14 16:03:40 2019 +0100
+++ b/src/org/openbravo/costing/CostAdjustmentUtils.java	Mon Dec 17 17:50:52 2018 +0100
@@ -27,6 +27,8 @@
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.time.DateUtils;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 import org.hibernate.ScrollMode;
 import org.hibernate.ScrollableResults;
 import org.hibernate.criterion.Restrictions;
@@ -58,8 +60,6 @@
 import org.openbravo.model.materialmgmt.transaction.InventoryCountLine;
 import org.openbravo.model.materialmgmt.transaction.MaterialTransaction;
 import org.openbravo.model.materialmgmt.transaction.TransactionLast;
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.LogManager;
 
 public class CostAdjustmentUtils {
   private static final Logger log4j = LogManager.getLogger();
@@ -117,6 +117,7 @@
    * 
    * @param isSource
    */
+  @Deprecated
   public static CostAdjustmentLine insertCostAdjustmentLine(MaterialTransaction transaction,
       CostAdjustment costAdjustmentHeader, BigDecimal costAdjusted, boolean isSource,
       Date accountingDate) {
@@ -125,6 +126,7 @@
         accountingDate, lineNo);
   }
 
+  @Deprecated
   public static CostAdjustmentLine insertCostAdjustmentLine(MaterialTransaction transaction,
       CostAdjustment costAdjustmentHeader, BigDecimal costAdjusted, boolean isSource,
       Date accountingDate, Currency currency) {
@@ -133,6 +135,7 @@
         accountingDate, lineNo, currency);
   }
 
+  @Deprecated
   public static CostAdjustmentLine insertCostAdjustmentLine(MaterialTransaction transaction,
       CostAdjustment costAdjustmentHeader, BigDecimal costAdjusted, boolean isSource,
       Date accountingDate, Long lineNo) {
@@ -140,34 +143,98 @@
         accountingDate, lineNo, null);
   }
 
+  @Deprecated
   public static CostAdjustmentLine insertCostAdjustmentLine(MaterialTransaction transaction,
       CostAdjustment costAdjustmentHeader, BigDecimal costAdjusted, boolean isSource,
       Date accountingDate, Long lineNo, Currency currency) {
-    Long stdPrecission = transaction.getCurrency().getStandardPrecision();
+    final CostAdjustmentLineParameters lineParameters = new CostAdjustmentLineParameters(
+        transaction, costAdjusted, costAdjustmentHeader);
+    lineParameters.setSource(isSource);
+    return insertCostAdjustmentLine(lineParameters, accountingDate, lineNo, currency);
+  }
+
+  /**
+   * Creates a new Adjustment Line and returns it
+   * 
+   * @param lineParameters
+   *          Object that contains most of the information needed to created the Adjustment Line
+   * @param accountingDate
+   *          The Date for which this document will be posted
+   * @return An Adjustment Line created based on the given parameters
+   */
+  public static CostAdjustmentLine insertCostAdjustmentLine(
+      final CostAdjustmentLineParameters lineParameters, final Date accountingDate) {
+    final Long lineNo = getNewLineNo(lineParameters.getCostAdjustmentHeader());
+    return insertCostAdjustmentLine(lineParameters, accountingDate, lineNo, null);
+  }
+
+  /**
+   * Creates a new Adjustment Line and returns it
+   * 
+   * @param lineParameters
+   *          Object that contains most of the information needed to created the Adjustment Line
+   * @param accountingDate
+   *          The Date for which this document will be posted
+   * @param currency
+   *          The Currency to which the Adjustment amount must be converted
+   * @return An Adjustment Line created based on the given parameters
+   */
+  public static CostAdjustmentLine insertCostAdjustmentLine(
+      final CostAdjustmentLineParameters lineParameters, final Date accountingDate,
+      final Currency currency) {
+    final Long lineNo = getNewLineNo(lineParameters.getCostAdjustmentHeader());
+    return insertCostAdjustmentLine(lineParameters, accountingDate, lineNo, currency);
+  }
+
+  /**
+   * 
+   * Creates a new Adjustment Line and returns it
+   * 
+   * @param lineParameters
+   *          Object that contains most of the information needed to created the Adjustment Line
+   * @param accountingDate
+   *          The Date for which this document will be posted
+   * @param lineNo
+   *          Number to position the line within the Cost Adjustment Document
+   * @param currency
+   *          The Currency to which the Adjustment amount must be converted
+   * @return An Adjustment Line created based on the given parameters
+   */
+  public static CostAdjustmentLine insertCostAdjustmentLine(
+      final CostAdjustmentLineParameters lineParameters, final Date accountingDate,
+      final Long lineNo, final Currency currency) {
+    final Long stdPrecission = lineParameters.getTransaction().getCurrency().getStandardPrecision();
     Currency adjustmentCurrency = currency;
     if (adjustmentCurrency == null) {
-      adjustmentCurrency = transaction.getCurrency();
+      adjustmentCurrency = lineParameters.getTransaction().getCurrency();
     }
 
-    CostAdjustmentLine costAdjustmentLine = getExistingCostAdjustmentLine(transaction,
-        costAdjustmentHeader, isSource, accountingDate, adjustmentCurrency);
+    CostAdjustmentLine costAdjustmentLine = getExistingCostAdjustmentLine(lineParameters,
+        accountingDate, adjustmentCurrency);
     if (costAdjustmentLine == null) {
       costAdjustmentLine = OBProvider.getInstance().get(CostAdjustmentLine.class);
-      costAdjustmentLine.setOrganization(costAdjustmentHeader.getOrganization());
-      costAdjustmentLine.setCostAdjustment(costAdjustmentHeader);
+      costAdjustmentLine
+          .setOrganization(lineParameters.getCostAdjustmentHeader().getOrganization());
+      costAdjustmentLine.setCostAdjustment(lineParameters.getCostAdjustmentHeader());
       costAdjustmentLine.setCurrency(adjustmentCurrency);
-      costAdjustmentLine.setInventoryTransaction(transaction);
-      costAdjustmentLine.setSource(isSource);
+      costAdjustmentLine.setInventoryTransaction(lineParameters.getTransaction());
+      costAdjustmentLine.setSource(lineParameters.isSource());
       costAdjustmentLine.setAccountingDate(accountingDate);
       costAdjustmentLine.setLineNo(lineNo);
+      costAdjustmentLine.setUnitCost(lineParameters.isUnitCost());
+      costAdjustmentLine.setNegativeStockCorrection(lineParameters.isNegativeCorrection());
+      costAdjustmentLine.setBackdatedTrx(lineParameters.isBackdatedTransaction());
+      costAdjustmentLine.setNeedsPosting(lineParameters.isNeedPosting());
+      costAdjustmentLine.setRelatedTransactionAdjusted(lineParameters
+          .isRelatedTransactionAdjusted());
     }
-    if (costAdjusted == null) {
+    if (lineParameters.getCostadjusted() == null) {
       costAdjustmentLine.setAdjustmentAmount(null);
     } else {
       BigDecimal previouslyAdjustedAmount = costAdjustmentLine.getAdjustmentAmount() == null ? BigDecimal.ZERO
           : costAdjustmentLine.getAdjustmentAmount();
-      costAdjustmentLine.setAdjustmentAmount(costAdjusted.add(previouslyAdjustedAmount).setScale(
-          stdPrecission.intValue(), RoundingMode.HALF_UP));
+      costAdjustmentLine.setAdjustmentAmount(lineParameters.getCostadjusted()
+          .add(previouslyAdjustedAmount).setScale(stdPrecission.intValue(), RoundingMode.HALF_UP));
     }
 
     OBDal.getInstance().save(costAdjustmentLine);
@@ -175,8 +242,8 @@
     return costAdjustmentLine;
   }
 
-  private static CostAdjustmentLine getExistingCostAdjustmentLine(MaterialTransaction transaction,
-      CostAdjustment costAdjustmentHeader, boolean isSource, Date accountingDate, Currency currency) {
+  private static CostAdjustmentLine getExistingCostAdjustmentLine(
+      CostAdjustmentLineParameters lineParameters, Date accountingDate, Currency currency) {
     StringBuilder hql = new StringBuilder("");
     hql.append(" costAdjustment.id = :costAdjustmentId ");
     hql.append(" and inventoryTransaction.id = :transactionId ");
@@ -184,14 +251,20 @@
     hql.append(" and currency.id = :currencyId ");
     hql.append(" and isSource = :isSource ");
     hql.append(" and accountingDate = :accountingDate");
+    hql.append(" and unitCost = :isUnitCost ");
+    hql.append(" and isBackdatedTrx = :isBackdatedTrx");
+    hql.append(" and isNegativeStockCorrection = :isNegativeCorrection");
 
     OBQuery<CostAdjustmentLine> obc = OBDal.getInstance().createQuery(CostAdjustmentLine.class,
         hql.toString());
-    obc.setNamedParameter("costAdjustmentId", costAdjustmentHeader.getId());
-    obc.setNamedParameter("transactionId", transaction.getId());
+    obc.setNamedParameter("costAdjustmentId", lineParameters.getCostAdjustmentHeader().getId());
+    obc.setNamedParameter("transactionId", lineParameters.getTransaction().getId());
     obc.setNamedParameter("currencyId", currency.getId());
-    obc.setNamedParameter("isSource", isSource);
+    obc.setNamedParameter("isSource", lineParameters.isSource());
     obc.setNamedParameter("accountingDate", accountingDate);
+    obc.setNamedParameter("isUnitCost", lineParameters.isUnitCost());
+    obc.setNamedParameter("isBackdatedTrx", lineParameters.isBackdatedTransaction());
+    obc.setNamedParameter("isNegativeCorrection", lineParameters.isNegativeCorrection());
 
     obc.setMaxResult(1);
 
--- a/src/org/openbravo/costing/CostingAlgorithmAdjustmentImp.java	Mon Jan 14 16:03:40 2019 +0100
+++ b/src/org/openbravo/costing/CostingAlgorithmAdjustmentImp.java	Mon Dec 17 17:50:52 2018 +0100
@@ -26,6 +26,8 @@
 
 import javax.enterprise.context.Dependent;
 
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 import org.hibernate.ScrollMode;
 import org.hibernate.ScrollableResults;
 import org.hibernate.criterion.Restrictions;
@@ -54,8 +56,6 @@
 import org.openbravo.model.materialmgmt.transaction.ProductionLine;
 import org.openbravo.model.materialmgmt.transaction.ShipmentInOut;
 import org.openbravo.model.materialmgmt.transaction.ShipmentInOutLine;
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.LogManager;
 
 @Dependent
 public abstract class CostingAlgorithmAdjustmentImp {
@@ -199,9 +199,13 @@
     }
   }
 
+  protected CostAdjustmentLine insertCostAdjustmentLine(CostAdjustmentLineParameters lineParameters) {
+    return insertCostAdjustmentLine(lineParameters, null);
+  }
+
   /**
    * Inserts a new cost adjustment line
-   * 
+   *
    * @param trx
    *          Material transaction
    * @param adjustmentamt
@@ -210,9 +214,9 @@
    *          Cost Adjustment Line
    * 
    */
-  protected CostAdjustmentLine insertCostAdjustmentLine(MaterialTransaction trx,
-      BigDecimal adjustmentamt, CostAdjustmentLine _parentLine) {
-    Date dateAcct = trx.getMovementDate();
+  protected CostAdjustmentLine insertCostAdjustmentLine(
+      CostAdjustmentLineParameters lineParameters, CostAdjustmentLine _parentLine) {
+    Date dateAcct = lineParameters.getTransaction().getMovementDate();
 
     CostAdjustmentLine parentLine;
     if (_parentLine == null) {
@@ -229,12 +233,11 @@
       dateAcct = parentAcctDate;
     }
 
-    CostAdjustmentLine newCAL = CostAdjustmentUtils.insertCostAdjustmentLine(trx,
-        (CostAdjustment) OBDal.getInstance().getProxy(CostAdjustment.ENTITY_NAME, strCostAdjId),
-        adjustmentamt, false, dateAcct, getNextLineNo());
-    newCAL.setRelatedTransactionAdjusted(false);
+    CostAdjustmentLine newCAL = CostAdjustmentUtils.insertCostAdjustmentLine(lineParameters,
+        dateAcct, getNextLineNo(), null);
     if (!newCAL.getId().equals(parentLine.getId())) {
       newCAL.setParentCostAdjustmentLine(parentLine);
+      OBDal.getInstance().save(newCAL);
     }
 
     addCostDependingTrx(newCAL);
@@ -279,7 +282,10 @@
     if (!deptrx.isCostCalculated() || deptrx.isCostPermanent()) {
       return;
     }
-    insertCostAdjustmentLine(deptrx, costAdjLine.getAdjustmentAmount(), _costAdjLine);
+
+    final CostAdjustmentLineParameters lineParameters = new CostAdjustmentLineParameters(deptrx,
+        costAdjLine.getAdjustmentAmount(), getCostAdj());
+    insertCostAdjustmentLine(lineParameters, _costAdjLine);
   }
 
   protected void searchManufacturingProduced(CostAdjustmentLine _costAdjLine) {
@@ -317,7 +323,10 @@
       if (!prodtrx.isCostCalculated() || prodtrx.isCostPermanent()) {
         continue;
       }
-      CostAdjustmentLine newCAL = insertCostAdjustmentLine(prodtrx, adjAmt, _costAdjLine);
+
+      final CostAdjustmentLineParameters lineParameters = new CostAdjustmentLineParameters(prodtrx,
+          adjAmt, getCostAdj());
+      CostAdjustmentLine newCAL = insertCostAdjustmentLine(lineParameters, _costAdjLine);
 
       lastAdjLine = newCAL;
     }
@@ -352,7 +361,10 @@
       if (!prodtrx.isCostCalculated() || prodtrx.isCostPermanent()) {
         continue;
       }
-      insertCostAdjustmentLine(prodtrx, costAdjLine.getAdjustmentAmount(), _costAdjLine);
+      final CostAdjustmentLineParameters lineParameters = new CostAdjustmentLineParameters(prodtrx,
+          costAdjLine.getAdjustmentAmount(), getCostAdj());
+      lineParameters.setUnitCost(true);
+      insertCostAdjustmentLine(lineParameters, _costAdjLine);
     }
   }
 
@@ -376,7 +388,9 @@
     if (!voidedTrx.isCostCalculated() || voidedTrx.isCostPermanent()) {
       return;
     }
-    insertCostAdjustmentLine(voidedTrx, costAdjLine.getAdjustmentAmount(), _costAdjLine);
+    final CostAdjustmentLineParameters lineParameters = new CostAdjustmentLineParameters(voidedTrx,
+        costAdjLine.getAdjustmentAmount(), getCostAdj());
+    insertCostAdjustmentLine(lineParameters, _costAdjLine);
   }
 
   protected void searchIntMovementTo(CostAdjustmentLine _costAdjLine) {
@@ -395,7 +409,9 @@
       if (!movementTransaction.isCostCalculated() || movementTransaction.isCostPermanent()) {
         continue;
       }
-      insertCostAdjustmentLine(movementTransaction, costAdjLine.getAdjustmentAmount(), _costAdjLine);
+      final CostAdjustmentLineParameters lineParameters = new CostAdjustmentLineParameters(
+          movementTransaction, costAdjLine.getAdjustmentAmount(), getCostAdj());
+      insertCostAdjustmentLine(lineParameters, _costAdjLine);
     }
   }
 
@@ -415,7 +431,9 @@
       if (!trx.isCostCalculated() || trx.isCostPermanent()) {
         continue;
       }
-      insertCostAdjustmentLine(trx, costAdjLine.getAdjustmentAmount(), _costAdjLine);
+      final CostAdjustmentLineParameters lineParameters = new CostAdjustmentLineParameters(trx,
+          costAdjLine.getAdjustmentAmount(), getCostAdj());
+      insertCostAdjustmentLine(lineParameters, _costAdjLine);
     }
   }
 
@@ -450,7 +468,9 @@
         if (trx.isCostCalculated() && !trx.isCostPermanent()) {
           BigDecimal adjAmt = costAdjAmt.multiply(trx.getMovementQuantity().abs()).divide(
               inoutline.getMovementQuantity().abs(), precission, RoundingMode.HALF_UP);
-          insertCostAdjustmentLine(trx, adjAmt, _costAdjLine);
+          final CostAdjustmentLineParameters lineParameters = new CostAdjustmentLineParameters(trx,
+              adjAmt, getCostAdj());
+          insertCostAdjustmentLine(lineParameters, _costAdjLine);
         }
 
         if (counter % 1000 == 0) {
--- a/src/org/openbravo/costing/CostingServer.java	Mon Jan 14 16:03:40 2019 +0100
+++ b/src/org/openbravo/costing/CostingServer.java	Mon Dec 17 17:50:52 2018 +0100
@@ -47,7 +47,6 @@
 import org.openbravo.model.common.enterprise.DocumentType;
 import org.openbravo.model.common.enterprise.Organization;
 import org.openbravo.model.materialmgmt.cost.CostAdjustment;
-import org.openbravo.model.materialmgmt.cost.CostAdjustmentLine;
 import org.openbravo.model.materialmgmt.cost.CostingRule;
 import org.openbravo.model.materialmgmt.cost.LCReceipt;
 import org.openbravo.model.materialmgmt.cost.LandedCost;
@@ -369,20 +368,19 @@
   }
 
   private boolean createAdjustment(String type, BigDecimal amount) {
-
     CostAdjustment costAdjustmentHeader = CostAdjustmentUtils.insertCostAdjustmentHeader(
         transaction.getOrganization(), type);
-    CostAdjustmentLine cal = CostAdjustmentUtils.insertCostAdjustmentLine(transaction,
-        costAdjustmentHeader, amount, Boolean.TRUE, transaction.getMovementDate());
-
+    final CostAdjustmentLineParameters lineParameters = new CostAdjustmentLineParameters(
+        transaction, amount, costAdjustmentHeader);
+    lineParameters.setSource(true);
     if (StringUtils.equals(type, "BDT")) {
-      cal.setBackdatedTrx(Boolean.TRUE);
+      lineParameters.setBackdatedTransaction(true);
     } else if (StringUtils.equals(type, "NSC")) {
-      cal.setNegativeStockCorrection(Boolean.TRUE);
-      cal.setUnitCost(Boolean.FALSE);
+      lineParameters.setNegativeCorrection(true);
     }
 
-    OBDal.getInstance().save(cal);
+    CostAdjustmentUtils.insertCostAdjustmentLine(lineParameters, transaction.getMovementDate());
+
     OBDal.getInstance().flush();
     JSONObject message = CostAdjustmentProcess.doProcessCostAdjustment(costAdjustmentHeader);
 
--- a/src/org/openbravo/costing/FixBackdatedTransactionsProcess.java	Mon Jan 14 16:03:40 2019 +0100
+++ b/src/org/openbravo/costing/FixBackdatedTransactionsProcess.java	Mon Dec 17 17:50:52 2018 +0100
@@ -23,6 +23,8 @@
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 import org.codehaus.jettison.json.JSONException;
 import org.codehaus.jettison.json.JSONObject;
 import org.hibernate.ScrollMode;
@@ -37,13 +39,10 @@
 import org.openbravo.erpCommon.utility.OBMessageUtils;
 import org.openbravo.model.common.enterprise.Organization;
 import org.openbravo.model.materialmgmt.cost.CostAdjustment;
-import org.openbravo.model.materialmgmt.cost.CostAdjustmentLine;
 import org.openbravo.model.materialmgmt.cost.CostingRule;
 import org.openbravo.model.materialmgmt.transaction.MaterialTransaction;
 import org.openbravo.service.db.DbUtility;
 import org.openbravo.service.json.JsonUtils;
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.LogManager;
 
 public class FixBackdatedTransactionsProcess extends BaseProcessActionHandler {
   private static final Logger log4j = LogManager.getLogger();
@@ -101,10 +100,11 @@
             if (CostAdjustmentUtils.isNeededBackdatedCostAdjustment(trx,
                 rule.isWarehouseDimension(), CostingUtils.getCostingRuleStartingDate(rule))) {
               createCostAdjustmenHeader(rule.getOrganization());
-              CostAdjustmentLine cal = CostAdjustmentUtils.insertCostAdjustmentLine(trx,
-                  costAdjHeader, null, Boolean.TRUE, trx.getMovementDate());
-              cal.setBackdatedTrx(Boolean.TRUE);
-              OBDal.getInstance().save(cal);
+              final CostAdjustmentLineParameters lineParameters = new CostAdjustmentLineParameters(
+                  trx, null, costAdjHeader);
+              lineParameters.setSource(true);
+              lineParameters.setBackdatedTransaction(true);
+              CostAdjustmentUtils.insertCostAdjustmentLine(lineParameters, trx.getMovementDate());
               i++;
               OBDal.getInstance().flush();
               if ((i % 100) == 0) {
--- a/src/org/openbravo/costing/LCMatchingProcess.java	Mon Jan 14 16:03:40 2019 +0100
+++ b/src/org/openbravo/costing/LCMatchingProcess.java	Mon Dec 17 17:50:52 2018 +0100
@@ -44,7 +44,6 @@
 import org.openbravo.erpCommon.utility.OBMessageUtils;
 import org.openbravo.model.common.enterprise.Organization;
 import org.openbravo.model.materialmgmt.cost.CostAdjustment;
-import org.openbravo.model.materialmgmt.cost.CostAdjustmentLine;
 import org.openbravo.model.materialmgmt.cost.LCDistributionAlgorithm;
 import org.openbravo.model.materialmgmt.cost.LCMatched;
 import org.openbravo.model.materialmgmt.cost.LCReceipt;
@@ -184,12 +183,13 @@
         ShipmentInOutLine receiptLine = OBDal.getInstance().get(ShipmentInOutLine.class,
             receiptAmt[1]);
         MaterialTransaction trx = receiptLine.getMaterialMgmtMaterialTransactionList().get(0);
-        CostAdjustmentLine cal = CostAdjustmentUtils.insertCostAdjustmentLine(trx, ca, amt, true,
-            referenceDate);
-        cal.setNeedsPosting(Boolean.FALSE);
-        cal.setUnitCost(Boolean.FALSE);
-        cal.setCurrency(lcCost.getCurrency());
-        OBDal.getInstance().save(cal);
+        final CostAdjustmentLineParameters lineParameters = new CostAdjustmentLineParameters(trx,
+            amt, ca);
+        lineParameters.setSource(true);
+        lineParameters.setUnitCost(false);
+        lineParameters.setNeedPosting(false);
+        CostAdjustmentUtils.insertCostAdjustmentLine(lineParameters, referenceDate,
+            lcCost.getCurrency());
 
         if (i % 100 == 0) {
           OBDal.getInstance().flush();
--- a/src/org/openbravo/costing/LandedCostProcess.java	Mon Jan 14 16:03:40 2019 +0100
+++ b/src/org/openbravo/costing/LandedCostProcess.java	Mon Dec 17 17:50:52 2018 +0100
@@ -51,7 +51,6 @@
 import org.openbravo.model.common.currency.Currency;
 import org.openbravo.model.common.enterprise.Organization;
 import org.openbravo.model.materialmgmt.cost.CostAdjustment;
-import org.openbravo.model.materialmgmt.cost.CostAdjustmentLine;
 import org.openbravo.model.materialmgmt.cost.LCDistributionAlgorithm;
 import org.openbravo.model.materialmgmt.cost.LCMatched;
 import org.openbravo.model.materialmgmt.cost.LCReceipt;
@@ -255,13 +254,14 @@
             receiptAmt[2]);
         // MaterialTransaction receiptLine = (MaterialTransaction) record[1];
         MaterialTransaction trx = receiptLine.getMaterialMgmtMaterialTransactionList().get(0);
-        CostAdjustmentLine cal = CostAdjustmentUtils.insertCostAdjustmentLine(trx, ca, amt, true,
-            referenceDate, lcCostCurrency);
-        cal.setNeedsPosting(Boolean.FALSE);
-        cal.setUnitCost(Boolean.FALSE);
-        cal.setCurrency(lcCostCurrency);
-        cal.setLineNo((i + 1) * 10L);
-        OBDal.getInstance().save(cal);
+        final CostAdjustmentLineParameters lineParameters = new CostAdjustmentLineParameters(trx,
+            amt, ca);
+        lineParameters.setSource(true);
+        lineParameters.setUnitCost(false);
+        lineParameters.setNeedPosting(false);
+        Long lineNo = (i + 1) * 10L;
+        CostAdjustmentUtils.insertCostAdjustmentLine(lineParameters, referenceDate, lineNo,
+            lcCostCurrency);
 
         if (i % 100 == 0) {
           OBDal.getInstance().flush();
--- a/src/org/openbravo/costing/ManualCostAdjustmentProcessHandler.java	Mon Jan 14 16:03:40 2019 +0100
+++ b/src/org/openbravo/costing/ManualCostAdjustmentProcessHandler.java	Mon Dec 17 17:50:52 2018 +0100
@@ -22,6 +22,8 @@
 import java.util.Date;
 import java.util.Map;
 
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 import org.codehaus.jettison.json.JSONException;
 import org.codehaus.jettison.json.JSONObject;
 import org.openbravo.base.exception.OBException;
@@ -31,13 +33,10 @@
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.erpCommon.utility.OBMessageUtils;
 import org.openbravo.model.materialmgmt.cost.CostAdjustment;
-import org.openbravo.model.materialmgmt.cost.CostAdjustmentLine;
 import org.openbravo.model.materialmgmt.cost.TransactionCost;
 import org.openbravo.model.materialmgmt.transaction.MaterialTransaction;
 import org.openbravo.service.db.DbUtility;
 import org.openbravo.service.json.JsonUtils;
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.LogManager;
 
 public class ManualCostAdjustmentProcessHandler extends BaseActionHandler {
   private static final Logger log = LogManager.getLogger();
@@ -83,11 +82,14 @@
         }
         costAdjusted = newAmountCost.subtract(totalCost);
       }
-      CostAdjustmentLine cal = CostAdjustmentUtils.insertCostAdjustmentLine(transaction,
-          costAdjustmentHeader, costAdjusted, Boolean.TRUE, acctDate);
+
+      final CostAdjustmentLineParameters lineParameters = new CostAdjustmentLineParameters(
+          transaction, costAdjusted, costAdjustmentHeader);
+      lineParameters.setSource(true);
       if (isIncremental) {
-        cal.setUnitCost(isUnitCost);
+        lineParameters.setUnitCost(isUnitCost);
       }
+      CostAdjustmentUtils.insertCostAdjustmentLine(lineParameters, acctDate);
 
       OBDal.getInstance().flush();
       JSONObject message = CostAdjustmentProcess.doProcessCostAdjustment(costAdjustmentHeader);
--- a/src/org/openbravo/costing/PriceDifferenceProcess.java	Mon Jan 14 16:03:40 2019 +0100
+++ b/src/org/openbravo/costing/PriceDifferenceProcess.java	Mon Dec 17 17:50:52 2018 +0100
@@ -40,7 +40,6 @@
 import org.openbravo.model.common.invoice.Invoice;
 import org.openbravo.model.common.plm.Product;
 import org.openbravo.model.materialmgmt.cost.CostAdjustment;
-import org.openbravo.model.materialmgmt.cost.CostAdjustmentLine;
 import org.openbravo.model.materialmgmt.transaction.MaterialTransaction;
 import org.openbravo.model.materialmgmt.transaction.ShipmentInOutLine;
 import org.openbravo.model.procurement.ReceiptInvoiceMatch;
@@ -142,10 +141,10 @@
       BigDecimal trxCostDifference = (expectedUnitCost.multiply(receiptQty))
           .subtract(currentTrxCost);
 
-      CostAdjustmentLine costAdjLine = CostAdjustmentUtils.insertCostAdjustmentLine(
-          materialTransaction, costAdjHeader, trxCostDifference, Boolean.TRUE, costAdjDateAcct);
-      costAdjLine.setNeedsPosting(Boolean.TRUE);
-      OBDal.getInstance().save(costAdjLine);
+      final CostAdjustmentLineParameters lineParameters = new CostAdjustmentLineParameters(
+          materialTransaction, trxCostDifference, costAdjHeader);
+      lineParameters.setSource(true);
+      CostAdjustmentUtils.insertCostAdjustmentLine(lineParameters, costAdjDateAcct);
       costAdjCreated = true;
     }
 
--- a/src/org/openbravo/costing/StandardCostAdjustment.java	Mon Jan 14 16:03:40 2019 +0100
+++ b/src/org/openbravo/costing/StandardCostAdjustment.java	Mon Dec 17 17:50:52 2018 +0100
@@ -76,8 +76,10 @@
           adjAmount = transaction.getMovementQuantity();
         }
 
-        CostAdjustmentLine newCAL = insertCostAdjustmentLine(trx, adjAmount, null);
-        newCAL.setRelatedTransactionAdjusted(true);
+        final CostAdjustmentLineParameters lineParameters = new CostAdjustmentLineParameters(trx,
+            adjAmount, getCostAdj());
+        lineParameters.setRelatedTransactionAdjusted(true);
+        insertCostAdjustmentLine(lineParameters);
 
         i++;
         if (i % 100 == 0) {