details:   https://code.openbravo.com/erp/devel/pi/rev/11dbadbe4ba4
changeset: 35314:11dbadbe4ba4
user:      David Miguelez <david.miguelez <at> openbravo.com>
date:      Mon Dec 17 17:50:52 2018 +0100
summary:   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

details:   https://code.openbravo.com/erp/devel/pi/rev/4b812cac3744
changeset: 35315:4b812cac3744
user:      David Miguelez <david.miguelez <at> openbravo.com>
date:      Wed Dec 26 17:28:40 2018 +0100
summary:   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

details:   https://code.openbravo.com/erp/devel/pi/rev/a913d96060c7
changeset: 35316:a913d96060c7
user:      David Miguelez <david.miguelez <at> openbravo.com>
date:      Mon Jan 14 09:40:46 2019 +0100
summary:   Fixes Issue 39888. When canceling a cost adjustment:

* Keep the Negative Stock Correction and Backdated Transactions
  flags with the same values as the original record
* When retrieving the lines of the Adjustment, retrieve also the lines
  from Adjusments in Voided Status

details:   https://code.openbravo.com/erp/devel/pi/rev/00f3ad11d13d
changeset: 35317:00f3ad11d13d
user:      David Miguelez <david.miguelez <at> openbravo.com>
date:      Mon Jan 14 12:10:40 2019 +0100
summary:   Related to Issue 39822. Added previously deleted method and set it 
as deprecated
to avoid API Change.

This method however should be avoided as it should be removed in the future,
as the other deprecated methods in this issue.

diffstat:

 src-db/database/model/tables/M_COSTADJUSTMENTLINE.xml             |    2 +-
 src-db/database/sourcedata/AD_MESSAGE.xml                         |   12 +
 src/org/openbravo/costing/AverageCostAdjustment.java              |   51 +-
 src/org/openbravo/costing/CancelCostAdjustment.java               |    7 +-
 src/org/openbravo/costing/CostAdjustmentLineParameters.java       |  270 
++++++++++
 src/org/openbravo/costing/CostAdjustmentUtils.java                |   95 ++-
 src/org/openbravo/costing/CostingAlgorithmAdjustmentImp.java      |   63 +-
 src/org/openbravo/costing/CostingServer.java                      |   16 +-
 src/org/openbravo/costing/FixBackdatedTransactionsProcess.java    |   14 +-
 src/org/openbravo/costing/LCMatchingProcess.java                  |   17 +-
 src/org/openbravo/costing/LandedCostProcess.java                  |   19 +-
 src/org/openbravo/costing/ManualCostAdjustmentProcessHandler.java |   14 +-
 src/org/openbravo/costing/PriceDifferenceProcess.java             |    9 +-
 src/org/openbravo/costing/StandardCostAdjustment.java             |    6 +-
 14 files changed, 477 insertions(+), 118 deletions(-)

diffs (truncated from 1039 to 300 lines):

diff -r b4c3e3431595 -r 00f3ad11d13d 
src-db/database/model/tables/M_COSTADJUSTMENTLINE.xml
--- 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 Jan 14 
12:10:40 2019 +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">
diff -r b4c3e3431595 -r 00f3ad11d13d src-db/database/sourcedata/AD_MESSAGE.xml
--- 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 Jan 14 12:10:40 2019 +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[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>
+<!--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>
diff -r b4c3e3431595 -r 00f3ad11d13d 
src/org/openbravo/costing/AverageCostAdjustment.java
--- a/src/org/openbravo/costing/AverageCostAdjustment.java      Mon Jan 14 
16:03:40 2019 +0100
+++ b/src/org/openbravo/costing/AverageCostAdjustment.java      Mon Jan 14 
12:10:40 2019 +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());
             }
           }
@@ -767,7 +768,7 @@
     
critLines.add(Restrictions.eq(CostAdjustmentLine.PROPERTY_INVENTORYTRANSACTION, 
trx));
     critLines.add(Restrictions.or(//
         Restrictions.eq("ca.id", getCostAdj().getId()),//
-        Restrictions.eq("ca." + CostAdjustment.PROPERTY_DOCUMENTSTATUS, 
"CO")));
+        Restrictions.not(Restrictions.eq("ca." + 
CostAdjustment.PROPERTY_DOCUMENTSTATUS, "DR"))));
 
     return critLines.list();
   }
diff -r b4c3e3431595 -r 00f3ad11d13d 
src/org/openbravo/costing/CancelCostAdjustment.java
--- a/src/org/openbravo/costing/CancelCostAdjustment.java       Mon Jan 14 
16:03:40 2019 +0100
+++ b/src/org/openbravo/costing/CancelCostAdjustment.java       Mon Jan 14 
12:10:40 2019 +0100
@@ -23,6 +23,8 @@
 import java.util.HashMap;
 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.hibernate.ScrollMode;
@@ -40,8 +42,6 @@
 import org.openbravo.model.materialmgmt.cost.CostAdjustment;
 import org.openbravo.model.materialmgmt.cost.CostAdjustmentLine;
 import org.openbravo.service.db.DbUtility;
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.LogManager;
 
 public class CancelCostAdjustment extends BaseActionHandler {
   private static final Logger log = LogManager.getLogger();
@@ -118,8 +118,7 @@
         lineCancel.setCreatedBy(OBContext.getOBContext().getUser());
         lineCancel.setCostAdjustment(cacProxy);
         
lineCancel.setAdjustmentAmount(lineOrig.getAdjustmentAmount().negate());
-        lineCancel.setBackdatedTrx(Boolean.FALSE);
-        lineCancel.setNegativeStockCorrection(Boolean.FALSE);
+
         if (lineOrig.getInventoryTransaction().isCostPermanent()) {
           lineOrig.getInventoryTransaction().setCostPermanent(Boolean.FALSE);
           OBDal.getInstance().save(lineOrig.getInventoryTransaction());
diff -r b4c3e3431595 -r 00f3ad11d13d 
src/org/openbravo/costing/CostAdjustmentLineParameters.java
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/openbravo/costing/CostAdjustmentLineParameters.java       Mon Jan 
14 12:10:40 2019 +0100
@@ -0,0 +1,270 @@
+/*
+ *************************************************************************
+ * 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.common.currency.Currency;
+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 adjustmentAmt;
+  private Currency currency;
+  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 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 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("CostAdjustmentCalculationError"));
+    }
+
+    this.transaction = transaction;
+    this.costAdjustmentHeader = costAdjustmentHeader;
+    this.adjustmentAmt = adjustmentAmt;
+    this.currency = currency;
+    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.


_______________________________________________
Openbravo-commits mailing list
Openbravo-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to