[costadj]Preferences are needed to enable automatic adjustments.
authorGorka Ion Damián <gorkaion.damian@openbravo.com>
Tue, 30 Sep 2014 12:44:08 +0200
changeset 24967 b9c16113f824
parent 24966 f5d224afc5d9
child 24968 89e11de0431c
[costadj]Preferences are needed to enable automatic adjustments.
src-db/database/sourcedata/AD_REF_LIST.xml
src/org/openbravo/costing/CostAdjustmentUtils.java
src/org/openbravo/costing/CostingAlgorithmAdjustmentImp.java
--- a/src-db/database/sourcedata/AD_REF_LIST.xml	Tue Sep 30 12:35:22 2014 +0200
+++ b/src-db/database/sourcedata/AD_REF_LIST.xml	Tue Sep 30 12:44:08 2014 +0200
@@ -9615,7 +9615,7 @@
 <!--6336EEE0A73E4C199F2222180BAF266C-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
 <!--6336EEE0A73E4C199F2222180BAF266C-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
 <!--6336EEE0A73E4C199F2222180BAF266C-->  <VALUE><![CDATA[enableAutomaticPriceCorrectionTrxs]]></VALUE>
-<!--6336EEE0A73E4C199F2222180BAF266C-->  <NAME><![CDATA[Enable automatic Price Difference Corrections when Receipt cost is calculated]]></NAME>
+<!--6336EEE0A73E4C199F2222180BAF266C-->  <NAME><![CDATA[Enable automatic Price Difference Corrections]]></NAME>
 <!--6336EEE0A73E4C199F2222180BAF266C-->  <AD_REFERENCE_ID><![CDATA[A26BA480E2014707B47257024C3CBFF7]]></AD_REFERENCE_ID>
 <!--6336EEE0A73E4C199F2222180BAF266C-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
 <!--6336EEE0A73E4C199F2222180BAF266C--></AD_REF_LIST>
--- a/src/org/openbravo/costing/CostAdjustmentUtils.java	Tue Sep 30 12:35:22 2014 +0200
+++ b/src/org/openbravo/costing/CostAdjustmentUtils.java	Tue Sep 30 12:44:08 2014 +0200
@@ -42,6 +42,7 @@
 import org.openbravo.model.common.enterprise.Locator;
 import org.openbravo.model.common.enterprise.Organization;
 import org.openbravo.model.common.enterprise.Warehouse;
+import org.openbravo.model.common.plm.AttributeSetInstance;
 import org.openbravo.model.common.plm.Product;
 import org.openbravo.model.materialmgmt.cost.CostAdjustment;
 import org.openbravo.model.materialmgmt.cost.CostAdjustmentLine;
@@ -64,6 +65,8 @@
   public static final String ENABLE_NEGATIVE_STOCK_CORRECTION_PREF = "enableNegativeStockCorrections";
 
   /**
+   * Returns a new header for a Cost Adjustment
+   * 
    * @param organization
    *          organization set in record
    * 
@@ -92,6 +95,8 @@
   }
 
   /**
+   * Creates a new Cost Adjustment Line and returns it.
+   * 
    * @param transaction
    *          transaction to apply the cost adjustment
    * 
@@ -127,11 +132,7 @@
   }
 
   /**
-   * @param transaction
-   *          transaction to check if cost adjustment should be applied
-   * @param startingDate
-   *          initial date of the current Costing Rule. Only transactions calculated by current rule
-   *          needs to be considered.
+   * Calculates if the given Material Transaction is a beckdated transaction or not.
    */
   public static boolean isNeededBackdatedCostAdjustment(MaterialTransaction transaction,
       boolean includeWarehouseDimension, Date startingDate) {
@@ -312,8 +313,6 @@
   /**
    * Calculates the stock of the product on the given date and for the given cost dimensions. It
    * only takes transactions that have its cost calculated.
-   * 
-   * @param areBackdatedTrxFixed
    */
   public static BigDecimal getStockOnTransactionDate(Organization costorg, MaterialTransaction trx,
       HashMap<CostDimension, BaseOBObject> _costDimensions, boolean isManufacturingProduct,
@@ -395,6 +394,19 @@
   public static BigDecimal getValuedStockOnMovementDate(Product product, Organization org,
       Date _date, HashMap<CostDimension, BaseOBObject> _costDimensions, Currency currency,
       boolean backdatedTransactionsFixed) {
+    return getValuedStockOnMovementDateByAttrAndLocator(product, org, _date, _costDimensions, null,
+        null, currency, backdatedTransactionsFixed);
+  }
+
+  /**
+   * Calculates the value of the stock of the product on the given date, for the given cost
+   * dimensions and for the given currency. It only takes transactions that have its cost
+   * calculated.
+   */
+  public static BigDecimal getValuedStockOnMovementDateByAttrAndLocator(Product product,
+      Organization org, Date _date, HashMap<CostDimension, BaseOBObject> _costDimensions,
+      Locator locator, AttributeSetInstance asi, Currency currency,
+      boolean backdatedTransactionsFixed) {
     Date date = _date;
     HashMap<CostDimension, BaseOBObject> costDimensions = _costDimensions;
 
@@ -463,6 +475,12 @@
     if (costDimensions.get(CostDimension.Warehouse) != null) {
       select.append("  and locator." + Locator.PROPERTY_WAREHOUSE + ".id = :warehouse");
     }
+    if (locator != null) {
+      select.append("   and locator = :locator");
+    }
+    if (asi != null) {
+      select.append("   and trx." + MaterialTransaction.PROPERTY_ATTRIBUTESETVALUE + " = :asi");
+    }
     select.append("   and trx." + MaterialTransaction.PROPERTY_ORGANIZATION + ".id in (:orgs)");
 
     select.append(" group by tc." + TransactionCost.PROPERTY_CURRENCY);
@@ -475,6 +493,12 @@
     if (costDimensions.get(CostDimension.Warehouse) != null) {
       trxQry.setParameter("warehouse", costDimensions.get(CostDimension.Warehouse).getId());
     }
+    if (locator != null) {
+      trxQry.setParameter("locator", locator);
+    }
+    if (asi != null) {
+      trxQry.setParameter("asi", asi);
+    }
     trxQry.setParameterList("orgs", orgs);
     @SuppressWarnings("unchecked")
     List<Object[]> o = trxQry.list();
@@ -594,7 +618,7 @@
     return costsum;
   }
 
-  /*
+  /**
    * Returns the last transaction process date of a non backdated transactions for the given
    * movement date or previous date.
    */
--- a/src/org/openbravo/costing/CostingAlgorithmAdjustmentImp.java	Tue Sep 30 12:35:22 2014 +0200
+++ b/src/org/openbravo/costing/CostingAlgorithmAdjustmentImp.java	Tue Sep 30 12:44:08 2014 +0200
@@ -224,7 +224,7 @@
     return newCAL;
   }
 
-  /*
+  /**
    * When the cost of a Closing Inventory is adjusted it is needed to adjust with the same amount
    * the related Opening Inventory.
    */
@@ -466,12 +466,14 @@
       // difference.
       adjAmt = getDefaultCostDifference(calTrxType, costAdjLine);
       break;
+    case InventoryClosing:
+      adjAmt = getInventoryClosingAmt(costAdjLine);
+      break;
 
     case Shipment:
     case ReceiptReturn:
     case ReceiptNegative:
     case InventoryDecrease:
-    case InventoryClosing:
     case IntMovementFrom:
     case InternalCons:
     case BOMPart:
@@ -504,9 +506,18 @@
     BigDecimal defaultCost = CostingUtils.getDefaultCost(trx.getProduct(),
         trx.getMovementQuantity(), costOrg, trxDate, trx.getMovementDate(), bp, getCostCurrency(),
         getCostDimensions());
-    // FIXME: Review if previous adjustment cost need to be considered.
     BigDecimal trxCalculatedCost = CostAdjustmentUtils.getTrxCost(trx, true, getCostCurrency());
-    return trxCalculatedCost.subtract(defaultCost);
+    return defaultCost.subtract(trxCalculatedCost);
+  }
+
+  private BigDecimal getInventoryClosingAmt(CostAdjustmentLine costAdjLine) {
+    MaterialTransaction trx = costAdjLine.getInventoryTransaction();
+    BigDecimal trxCalculatedCost = CostAdjustmentUtils.getTrxCost(trx, true, getCostCurrency());
+    BigDecimal trxExpectedCost = CostAdjustmentUtils.getValuedStockOnMovementDateByAttrAndLocator(
+        trx.getProduct(), getCostOrg(), trx.getMovementDate(), getCostDimensions(),
+        trx.getStorageBin(), trx.getAttributeSetValue(), getCostCurrency(), areBackdatedTrxFixed);
+
+    return trxExpectedCost.subtract(trxCalculatedCost);
   }
 
   /**