Related to Issue 39822. Code Review Changes:
authorDavid Miguelez <david.miguelez@openbravo.com>
Wed, 26 Dec 2018 17:28:40 +0100
changeset 35315 4b812cac3744
parent 35314 11dbadbe4ba4
child 35316 a913d96060c7
Related to Issue 39822. Code Review Changes:

* Fixed typo in Message value
* Changed name of class variable from costAdjusted to adjustmentAmt (changed getter too)
* Added currency variable for parameters class
* If not specified, it uses the currency of the transaction
* If specified, it is used to create the line and to search
for existing adjustment lines
src-db/database/sourcedata/AD_MESSAGE.xml
src/org/openbravo/costing/CostAdjustmentLineParameters.java
src/org/openbravo/costing/CostAdjustmentUtils.java
src/org/openbravo/costing/CostingAlgorithmAdjustmentImp.java
src/org/openbravo/costing/LCMatchingProcess.java
src/org/openbravo/costing/LandedCostProcess.java
--- a/src-db/database/sourcedata/AD_MESSAGE.xml	Mon Dec 17 17:50:52 2018 +0100
+++ b/src-db/database/sourcedata/AD_MESSAGE.xml	Wed Dec 26 17:28:40 2018 +0100
@@ -26878,7 +26878,7 @@
 <!--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-->  <VALUE><![CDATA[CostAdjustmentCalculationError]]></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>
--- a/src/org/openbravo/costing/CostAdjustmentLineParameters.java	Mon Dec 17 17:50:52 2018 +0100
+++ b/src/org/openbravo/costing/CostAdjustmentLineParameters.java	Wed Dec 26 17:28:40 2018 +0100
@@ -23,6 +23,7 @@
 
 import org.openbravo.base.exception.OBException;
 import org.openbravo.erpCommon.utility.OBMessageUtils;
+import org.openbravo.model.common.currency.Currency;
 import org.openbravo.model.materialmgmt.cost.CostAdjustment;
 import org.openbravo.model.materialmgmt.transaction.MaterialTransaction;
 
@@ -41,7 +42,8 @@
 public class CostAdjustmentLineParameters {
   private MaterialTransaction transaction;
   private CostAdjustment costAdjustmentHeader;
-  private BigDecimal costadjusted;
+  private BigDecimal adjustmentAmt;
+  private Currency currency;
   private boolean isSource;
   private boolean isUnitCost;
   private boolean isBackdatedTransaction;
@@ -63,20 +65,48 @@
    * 
    * @param transaction
    *          The Transaction for which the adjustment is going to be made
-   * @param costAdjusted
+   * @param adjustmentAmt
    *          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) {
+      final BigDecimal adjustmentAmt, final CostAdjustment costAdjustmentHeader) {
+    this(transaction, adjustmentAmt, costAdjustmentHeader, transaction.getCurrency());
+  }
+
+  /**
+   * 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 adjustmentAmt
+   *          The amount that will be adjusted against the Transaction
+   * @param costAdjustmentHeader
+   *          The Cost Adjustment Document that will contain the adjustment line
+   * @param currency
+   *          The Currency for which the adjustment amount is done
+   */
+  public CostAdjustmentLineParameters(final MaterialTransaction transaction,
+      final BigDecimal adjustmentAmt, final CostAdjustment costAdjustmentHeader,
+      final Currency currency) {
     if (transaction == null || costAdjustmentHeader == null) {
-      throw new OBException(OBMessageUtils.messageBD("CostAdjsutmentCalculationError"));
+      throw new OBException(OBMessageUtils.messageBD("CostAdjustmentCalculationError"));
     }
 
     this.transaction = transaction;
     this.costAdjustmentHeader = costAdjustmentHeader;
-    this.costadjusted = costAdjusted;
+    this.adjustmentAmt = adjustmentAmt;
+    this.currency = currency;
     this.isSource = false;
     this.isUnitCost = true;
     this.isBackdatedTransaction = false;
@@ -226,8 +256,15 @@
    * 
    * @return The amount that is going to be adjusted to the transaction
    */
-  public BigDecimal getCostadjusted() {
-    return costadjusted;
+  public BigDecimal getAdjustmentAmount() {
+    return adjustmentAmt;
   }
 
+  /**
+   * 
+   * @return The currency for which the adjustment is made
+   */
+  public Currency getCurrency() {
+    return currency;
+  }
 }
--- a/src/org/openbravo/costing/CostAdjustmentUtils.java	Mon Dec 17 17:50:52 2018 +0100
+++ b/src/org/openbravo/costing/CostAdjustmentUtils.java	Wed Dec 26 17:28:40 2018 +0100
@@ -148,9 +148,9 @@
       CostAdjustment costAdjustmentHeader, BigDecimal costAdjusted, boolean isSource,
       Date accountingDate, Long lineNo, Currency currency) {
     final CostAdjustmentLineParameters lineParameters = new CostAdjustmentLineParameters(
-        transaction, costAdjusted, costAdjustmentHeader);
+        transaction, costAdjusted, costAdjustmentHeader, currency);
     lineParameters.setSource(isSource);
-    return insertCostAdjustmentLine(lineParameters, accountingDate, lineNo, currency);
+    return insertCostAdjustmentLine(lineParameters, accountingDate, lineNo);
   }
 
   /**
@@ -165,25 +165,7 @@
   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);
+    return insertCostAdjustmentLine(lineParameters, accountingDate, lineNo);
   }
 
   /**
@@ -196,27 +178,21 @@
    *          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 lineNo) {
     final Long stdPrecission = lineParameters.getTransaction().getCurrency().getStandardPrecision();
-    Currency adjustmentCurrency = currency;
-    if (adjustmentCurrency == null) {
-      adjustmentCurrency = lineParameters.getTransaction().getCurrency();
-    }
 
     CostAdjustmentLine costAdjustmentLine = getExistingCostAdjustmentLine(lineParameters,
-        accountingDate, adjustmentCurrency);
+        accountingDate);
     if (costAdjustmentLine == null) {
       costAdjustmentLine = OBProvider.getInstance().get(CostAdjustmentLine.class);
       costAdjustmentLine
           .setOrganization(lineParameters.getCostAdjustmentHeader().getOrganization());
       costAdjustmentLine.setCostAdjustment(lineParameters.getCostAdjustmentHeader());
-      costAdjustmentLine.setCurrency(adjustmentCurrency);
+      costAdjustmentLine.setCurrency(lineParameters.getCurrency());
       costAdjustmentLine.setInventoryTransaction(lineParameters.getTransaction());
       costAdjustmentLine.setSource(lineParameters.isSource());
       costAdjustmentLine.setAccountingDate(accountingDate);
@@ -228,12 +204,12 @@
       costAdjustmentLine.setRelatedTransactionAdjusted(lineParameters
           .isRelatedTransactionAdjusted());
     }
-    if (lineParameters.getCostadjusted() == null) {
+    if (lineParameters.getAdjustmentAmount() == null) {
       costAdjustmentLine.setAdjustmentAmount(null);
     } else {
       BigDecimal previouslyAdjustedAmount = costAdjustmentLine.getAdjustmentAmount() == null ? BigDecimal.ZERO
           : costAdjustmentLine.getAdjustmentAmount();
-      costAdjustmentLine.setAdjustmentAmount(lineParameters.getCostadjusted()
+      costAdjustmentLine.setAdjustmentAmount(lineParameters.getAdjustmentAmount()
           .add(previouslyAdjustedAmount).setScale(stdPrecission.intValue(), RoundingMode.HALF_UP));
     }
 
@@ -243,7 +219,7 @@
   }
 
   private static CostAdjustmentLine getExistingCostAdjustmentLine(
-      CostAdjustmentLineParameters lineParameters, Date accountingDate, Currency currency) {
+      CostAdjustmentLineParameters lineParameters, Date accountingDate) {
     StringBuilder hql = new StringBuilder("");
     hql.append(" costAdjustment.id = :costAdjustmentId ");
     hql.append(" and inventoryTransaction.id = :transactionId ");
@@ -259,7 +235,7 @@
         hql.toString());
     obc.setNamedParameter("costAdjustmentId", lineParameters.getCostAdjustmentHeader().getId());
     obc.setNamedParameter("transactionId", lineParameters.getTransaction().getId());
-    obc.setNamedParameter("currencyId", currency.getId());
+    obc.setNamedParameter("currencyId", lineParameters.getCurrency().getId());
     obc.setNamedParameter("isSource", lineParameters.isSource());
     obc.setNamedParameter("accountingDate", accountingDate);
     obc.setNamedParameter("isUnitCost", lineParameters.isUnitCost());
--- a/src/org/openbravo/costing/CostingAlgorithmAdjustmentImp.java	Mon Dec 17 17:50:52 2018 +0100
+++ b/src/org/openbravo/costing/CostingAlgorithmAdjustmentImp.java	Wed Dec 26 17:28:40 2018 +0100
@@ -234,7 +234,7 @@
     }
 
     CostAdjustmentLine newCAL = CostAdjustmentUtils.insertCostAdjustmentLine(lineParameters,
-        dateAcct, getNextLineNo(), null);
+        dateAcct, getNextLineNo());
     if (!newCAL.getId().equals(parentLine.getId())) {
       newCAL.setParentCostAdjustmentLine(parentLine);
       OBDal.getInstance().save(newCAL);
--- a/src/org/openbravo/costing/LCMatchingProcess.java	Mon Dec 17 17:50:52 2018 +0100
+++ b/src/org/openbravo/costing/LCMatchingProcess.java	Wed Dec 26 17:28:40 2018 +0100
@@ -28,6 +28,8 @@
 import javax.inject.Inject;
 
 import org.apache.commons.lang.StringUtils;
+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;
@@ -51,8 +53,6 @@
 import org.openbravo.model.materialmgmt.cost.LandedCostCost;
 import org.openbravo.model.materialmgmt.transaction.MaterialTransaction;
 import org.openbravo.model.materialmgmt.transaction.ShipmentInOutLine;
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.LogManager;
 
 public class LCMatchingProcess {
   private static final Logger log = LogManager.getLogger();
@@ -184,12 +184,11 @@
             receiptAmt[1]);
         MaterialTransaction trx = receiptLine.getMaterialMgmtMaterialTransactionList().get(0);
         final CostAdjustmentLineParameters lineParameters = new CostAdjustmentLineParameters(trx,
-            amt, ca);
+            amt, ca, lcCost.getCurrency());
         lineParameters.setSource(true);
         lineParameters.setUnitCost(false);
         lineParameters.setNeedPosting(false);
-        CostAdjustmentUtils.insertCostAdjustmentLine(lineParameters, referenceDate,
-            lcCost.getCurrency());
+        CostAdjustmentUtils.insertCostAdjustmentLine(lineParameters, referenceDate);
 
         if (i % 100 == 0) {
           OBDal.getInstance().flush();
--- a/src/org/openbravo/costing/LandedCostProcess.java	Mon Dec 17 17:50:52 2018 +0100
+++ b/src/org/openbravo/costing/LandedCostProcess.java	Wed Dec 26 17:28:40 2018 +0100
@@ -29,6 +29,8 @@
 import javax.inject.Inject;
 
 import org.apache.commons.lang.StringUtils;
+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;
@@ -60,8 +62,6 @@
 import org.openbravo.model.materialmgmt.transaction.MaterialTransaction;
 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;
 
 public class LandedCostProcess {
   private static final Logger log = LogManager.getLogger();
@@ -255,13 +255,12 @@
         // MaterialTransaction receiptLine = (MaterialTransaction) record[1];
         MaterialTransaction trx = receiptLine.getMaterialMgmtMaterialTransactionList().get(0);
         final CostAdjustmentLineParameters lineParameters = new CostAdjustmentLineParameters(trx,
-            amt, ca);
+            amt, ca, lcCostCurrency);
         lineParameters.setSource(true);
         lineParameters.setUnitCost(false);
         lineParameters.setNeedPosting(false);
         Long lineNo = (i + 1) * 10L;
-        CostAdjustmentUtils.insertCostAdjustmentLine(lineParameters, referenceDate, lineNo,
-            lcCostCurrency);
+        CostAdjustmentUtils.insertCostAdjustmentLine(lineParameters, referenceDate, lineNo);
 
         if (i % 100 == 0) {
           OBDal.getInstance().flush();