[costAdj] Fixes issue managing trx with same trxprocessdate
authorGorka Ion Damián <gorkaion.damian@openbravo.com>
Tue, 30 Sep 2014 02:00:49 +0200
changeset 24962 2fea2e1d33d3
parent 24961 e8af86988cfa
child 24963 657f50cf0340
[costAdj] Fixes issue managing trx with same trxprocessdate

When there are more than one trx with the same trxprocessdate it is needed to
add more ordering criterias. The sequence number of the movementtype ref list
is used.
src-db/database/model/tables/M_TRANSACTION_COST.xml
src-db/database/sourcedata/AD_COLUMN.xml
src-db/database/sourcedata/AD_REF_LIST.xml
src/org/openbravo/costing/AverageCostAdjustment.java
src/org/openbravo/costing/CostAdjustmentUtils.java
src/org/openbravo/costing/CostingAlgorithmAdjustmentImp.java
src/org/openbravo/costing/CostingServer.java
src/org/openbravo/costing/CostingUtils.java
src/org/openbravo/costing/FixBackdatedTransactionsProcess.java
src/org/openbravo/costing/StandardAlgorithm.java
src/org/openbravo/erpCommon/ad_callouts/SL_InvAmtUpd_ProductRefDate.java
--- a/src-db/database/model/tables/M_TRANSACTION_COST.xml	Mon Sep 29 22:38:44 2014 +0200
+++ b/src-db/database/model/tables/M_TRANSACTION_COST.xml	Tue Sep 30 02:00:49 2014 +0200
@@ -57,7 +57,7 @@
         <default><![CDATA[Y]]></default>
         <onCreateDefault><![CDATA['Y']]></onCreateDefault>
       </column>
-      <column name="DATEACCT" primaryKey="false" required="false" type="TIMESTAMP" size="7" autoIncrement="false">
+      <column name="DATEACCT" primaryKey="false" required="true" type="TIMESTAMP" size="7" autoIncrement="false">
         <default/>
         <onCreateDefault/>
       </column>
--- a/src-db/database/sourcedata/AD_COLUMN.xml	Mon Sep 29 22:38:44 2014 +0200
+++ b/src-db/database/sourcedata/AD_COLUMN.xml	Tue Sep 30 02:00:49 2014 +0200
@@ -220002,7 +220002,7 @@
 <!--001B37992B26DD8BE050007F010070C7-->  <FIELDLENGTH><![CDATA[19]]></FIELDLENGTH>
 <!--001B37992B26DD8BE050007F010070C7-->  <ISKEY><![CDATA[N]]></ISKEY>
 <!--001B37992B26DD8BE050007F010070C7-->  <ISPARENT><![CDATA[N]]></ISPARENT>
-<!--001B37992B26DD8BE050007F010070C7-->  <ISMANDATORY><![CDATA[N]]></ISMANDATORY>
+<!--001B37992B26DD8BE050007F010070C7-->  <ISMANDATORY><![CDATA[Y]]></ISMANDATORY>
 <!--001B37992B26DD8BE050007F010070C7-->  <ISUPDATEABLE><![CDATA[Y]]></ISUPDATEABLE>
 <!--001B37992B26DD8BE050007F010070C7-->  <ISIDENTIFIER><![CDATA[N]]></ISIDENTIFIER>
 <!--001B37992B26DD8BE050007F010070C7-->  <SEQNO><![CDATA[150]]></SEQNO>
--- a/src-db/database/sourcedata/AD_REF_LIST.xml	Mon Sep 29 22:38:44 2014 +0200
+++ b/src-db/database/sourcedata/AD_REF_LIST.xml	Tue Sep 30 02:00:49 2014 +0200
@@ -2135,6 +2135,7 @@
 <!--312-->  <NAME><![CDATA[Customer Shipment]]></NAME>
 <!--312-->  <AD_REFERENCE_ID><![CDATA[189]]></AD_REFERENCE_ID>
 <!--312-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--312-->  <SEQNO><![CDATA[80]]></SEQNO>
 <!--312--></AD_REF_LIST>
 
 <!--313--><AD_REF_LIST>
@@ -2157,6 +2158,7 @@
 <!--314-->  <NAME><![CDATA[Vendor Receipts]]></NAME>
 <!--314-->  <AD_REFERENCE_ID><![CDATA[189]]></AD_REFERENCE_ID>
 <!--314-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--314-->  <SEQNO><![CDATA[10]]></SEQNO>
 <!--314--></AD_REF_LIST>
 
 <!--315--><AD_REF_LIST>
@@ -2179,6 +2181,7 @@
 <!--316-->  <NAME><![CDATA[Inventory Out]]></NAME>
 <!--316-->  <AD_REFERENCE_ID><![CDATA[189]]></AD_REFERENCE_ID>
 <!--316-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--316-->  <SEQNO><![CDATA[50]]></SEQNO>
 <!--316--></AD_REF_LIST>
 
 <!--317--><AD_REF_LIST>
@@ -2190,6 +2193,7 @@
 <!--317-->  <NAME><![CDATA[Inventory In]]></NAME>
 <!--317-->  <AD_REFERENCE_ID><![CDATA[189]]></AD_REFERENCE_ID>
 <!--317-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--317-->  <SEQNO><![CDATA[20]]></SEQNO>
 <!--317--></AD_REF_LIST>
 
 <!--318--><AD_REF_LIST>
@@ -2514,6 +2518,7 @@
 <!--349-->  <NAME><![CDATA[Movement From]]></NAME>
 <!--349-->  <AD_REFERENCE_ID><![CDATA[189]]></AD_REFERENCE_ID>
 <!--349-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--349-->  <SEQNO><![CDATA[30]]></SEQNO>
 <!--349--></AD_REF_LIST>
 
 <!--350--><AD_REF_LIST>
@@ -2525,6 +2530,7 @@
 <!--350-->  <NAME><![CDATA[Movement To]]></NAME>
 <!--350-->  <AD_REFERENCE_ID><![CDATA[189]]></AD_REFERENCE_ID>
 <!--350-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--350-->  <SEQNO><![CDATA[40]]></SEQNO>
 <!--350--></AD_REF_LIST>
 
 <!--351--><AD_REF_LIST>
@@ -2933,6 +2939,7 @@
 <!--391-->  <NAME><![CDATA[Production +]]></NAME>
 <!--391-->  <AD_REFERENCE_ID><![CDATA[189]]></AD_REFERENCE_ID>
 <!--391-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--391-->  <SEQNO><![CDATA[70]]></SEQNO>
 <!--391--></AD_REF_LIST>
 
 <!--392--><AD_REF_LIST>
@@ -2944,6 +2951,7 @@
 <!--392-->  <NAME><![CDATA[Production -]]></NAME>
 <!--392-->  <AD_REFERENCE_ID><![CDATA[189]]></AD_REFERENCE_ID>
 <!--392-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--392-->  <SEQNO><![CDATA[60]]></SEQNO>
 <!--392--></AD_REF_LIST>
 
 <!--393--><AD_REF_LIST>
@@ -6008,6 +6016,7 @@
 <!--800141-->  <NAME><![CDATA[Internal Consumption +]]></NAME>
 <!--800141-->  <AD_REFERENCE_ID><![CDATA[189]]></AD_REFERENCE_ID>
 <!--800141-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--800141-->  <SEQNO><![CDATA[100]]></SEQNO>
 <!--800141--></AD_REF_LIST>
 
 <!--800142--><AD_REF_LIST>
@@ -6019,6 +6028,7 @@
 <!--800142-->  <NAME><![CDATA[Internal Consumption -]]></NAME>
 <!--800142-->  <AD_REFERENCE_ID><![CDATA[189]]></AD_REFERENCE_ID>
 <!--800142-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--800142-->  <SEQNO><![CDATA[90]]></SEQNO>
 <!--800142--></AD_REF_LIST>
 
 <!--800143--><AD_REF_LIST>
--- a/src/org/openbravo/costing/AverageCostAdjustment.java	Mon Sep 29 22:38:44 2014 +0200
+++ b/src/org/openbravo/costing/AverageCostAdjustment.java	Tue Sep 30 02:00:49 2014 +0200
@@ -26,13 +26,11 @@
 import java.util.Set;
 
 import org.apache.commons.lang.StringUtils;
-import org.hibernate.Query;
 import org.hibernate.ScrollMode;
 import org.hibernate.ScrollableResults;
 import org.hibernate.criterion.Restrictions;
 import org.openbravo.base.exception.OBException;
 import org.openbravo.base.provider.OBProvider;
-import org.openbravo.base.session.OBPropertiesProvider;
 import org.openbravo.base.structure.BaseOBObject;
 import org.openbravo.client.kernel.ComponentProvider;
 import org.openbravo.costing.CostingAlgorithm.CostDimension;
@@ -56,8 +54,6 @@
 import org.openbravo.model.materialmgmt.cost.CostingRule;
 import org.openbravo.model.materialmgmt.cost.TransactionCost;
 import org.openbravo.model.materialmgmt.transaction.MaterialTransaction;
-import org.openbravo.model.materialmgmt.transaction.ShipmentInOut;
-import org.openbravo.model.materialmgmt.transaction.ShipmentInOutLine;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -112,9 +108,11 @@
     }
 
     // Initialize current stock qty and value amt.
-    BigDecimal currentStock = CostingUtils.getCurrentStock(getCostOrg(), basetrx,
+    BigDecimal currentStock = CostAdjustmentUtils.getStockOnTransactionDate(getCostOrg(), basetrx,
         getCostDimensions(), isManufacturingProduct, areBackdatedTrxFixed);
-    BigDecimal currentValueAmt = getCurrentValuedStock(basetrx);
+    BigDecimal currentValueAmt = CostAdjustmentUtils.getValuedStockOnTransactionDate(getCostOrg(),
+        basetrx, getCostDimensions(), isManufacturingProduct, areBackdatedTrxFixed,
+        getCostCurrency());
     log.debug("Adjustment balance: " + adjustmentBalance.toPlainString()
         + ", current stock {}, current value {}", currentStock.toPlainString(),
         currentValueAmt.toPlainString());
@@ -195,7 +193,7 @@
                   getCostOrg(), FinancialUtils.PRECISION_STANDARD);
             }
             trxAdjAmt = trxAdjAmt.add(adjustmentAmt);
-            adjustmentBalance = adjustmentBalance.add(adjustmentAmt.multiply(signMultiplier));
+            adjustmentBalance = adjustmentBalance.add(adjustmentAmt.multiply(trxSignMultiplier));
           }
 
           existingCAL.setRelatedTransactionAdjusted(Boolean.TRUE);
@@ -358,7 +356,7 @@
   protected BigDecimal getOutgoingBackdatedTrxAdjAmt(CostAdjustmentLine costAdjLine) {
     // Calculate the average cost on the transaction's movement date and adjust the cost if needed.
     MaterialTransaction trx = costAdjLine.getInventoryTransaction();
-    Costing costing = getTrxProductCost(trx, getCostDimensions(), getCostOrg(),
+    Costing costing = getAvgCostOnMovementDate(trx, getCostDimensions(), getCostOrg(),
         areBackdatedTrxFixed);
 
     if (costing == null) {
@@ -404,21 +402,40 @@
 
     StringBuffer where = new StringBuffer();
     where.append(" as trx");
-    where.append(" join trx." + Product.PROPERTY_ORGANIZATION + " as org");
-    where.append(" join trx." + Product.PROPERTY_STORAGEBIN + " as loc");
-    where.append(" where trx." + MaterialTransaction.PROPERTY_ISCOSTCALCULATED + " = true");
+    where.append("\n join trx." + Product.PROPERTY_ORGANIZATION + " as org");
+    where.append("\n join trx." + Product.PROPERTY_STORAGEBIN + " as loc");
+    where.append("\n , " + org.openbravo.model.ad.domain.List.ENTITY_NAME + " as trxtype");
+    where.append("\n where trxtype." + CostAdjustmentUtils.propADListReference + ".id = :refid");
+    where.append("  and trxtype." + CostAdjustmentUtils.propADListValue + " = trx."
+        + MaterialTransaction.PROPERTY_MOVEMENTTYPE);
+
+    where.append("  and trx." + MaterialTransaction.PROPERTY_ISCOSTCALCULATED + " = true");
     where.append("  and trx." + MaterialTransaction.PROPERTY_PRODUCT + " = :product");
     // Consider only transactions with movement date equal or later than the movement date of the
     // adjusted transaction. But for transactions with the same movement date only those with a
     // transaction date after the process date of the adjusted transaction.
     if (areBackdatedTrxFixed) {
-      where.append("  and (trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + " > :mvtdate");
-      where.append("    or (trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + " = :mvtdate");
-      where.append("  and trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE
-          + " > :trxdate ))");
-    } else {
-      where.append("  and trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE
-          + " > :trxdate");
+      where.append("  and (");
+      where.append("   trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + " > :mvtdate");
+      where.append("   or (");
+      where.append("    trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + " = :mvtdate");
+    }
+    // If there are more than one trx on the same trx process date filter out those types with less
+    // priority and / or higher quantity.
+    where.append(" and (");
+    where.append("  trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE + " > :trxdate");
+    where.append("  or (");
+    where.append("   trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE + " = :trxdate");
+    where.append("   and (");
+    where.append("    trxtype." + CostAdjustmentUtils.propADListPriority + " > :trxtypeprio");
+    where.append("    or (");
+    where.append("     trxtype." + CostAdjustmentUtils.propADListPriority + " = :trxtypeprio");
+    where.append("     and trx." + MaterialTransaction.PROPERTY_MOVEMENTQUANTITY + " <= :trxqty");
+    where.append(" ))))");
+    where.append(" and trx.id != :trxid");
+
+    if (areBackdatedTrxFixed) {
+      where.append("  ))");
     }
     where.append("  and org.id in (:orgs)");
     if (warehouse != null) {
@@ -430,128 +447,40 @@
     }
     where.append("  and trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE
         + " > :startdate ");
-    where.append(" order by ");
+    where.append("\n order by ");
     if (areBackdatedTrxFixed) {
       where.append(" trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + ", ");
     }
     where.append(" trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE);
-    where.append("   , trx." + MaterialTransaction.PROPERTY_MOVEMENTLINE);
-    // This makes M- to go before M+. In Oracle it must go with desc as if not, M+ would go before
-    // M-.
-    if (OBPropertiesProvider.getInstance().getOpenbravoProperties().getProperty("bbdd.rdbms")
-        .equalsIgnoreCase("oracle")) {
-      where.append("   , trx." + MaterialTransaction.PROPERTY_MOVEMENTTYPE + " desc ");
-    } else {
-      where.append("   , trx." + MaterialTransaction.PROPERTY_MOVEMENTTYPE);
-    }
+    where.append(" , trxtype." + CostAdjustmentUtils.propADListPriority);
+    where.append(" , trx." + MaterialTransaction.PROPERTY_MOVEMENTQUANTITY + " desc");
+
     OBQuery<MaterialTransaction> trxQry = OBDal.getInstance().createQuery(
         MaterialTransaction.class, where.toString());
     trxQry.setFilterOnReadableOrganization(false);
     trxQry.setFilterOnReadableClients(false);
+    trxQry.setNamedParameter("refid", CostAdjustmentUtils.MovementTypeRefID);
+    trxQry.setNamedParameter("product", trx.getProduct());
     if (areBackdatedTrxFixed) {
       trxQry.setNamedParameter("mvtdate", trx.getMovementDate());
     }
+    trxQry.setNamedParameter("trxtypeprio",
+        CostAdjustmentUtils.getTrxTypePrio(trx.getMovementType()));
     trxQry.setNamedParameter("trxdate", trx.getTransactionProcessDate());
+    trxQry.setNamedParameter("trxqty", trx.getMovementQuantity());
+    trxQry.setNamedParameter("trxid", trx.getId());
+    trxQry.setNamedParameter("orgs", orgs);
+    if (warehouse != null) {
+      trxQry.setNamedParameter("warehouse", warehouse);
+    }
     if (costingRule.getEndingDate() != null) {
       trxQry.setNamedParameter("enddate", costingRule.getEndingDate());
     }
     trxQry.setNamedParameter("startdate", costingRule.getStartingDate());
-    trxQry.setNamedParameter("orgs", orgs);
-    trxQry.setNamedParameter("product", trx.getProduct());
-    if (warehouse != null) {
-      trxQry.setNamedParameter("warehouse", warehouse);
-    }
 
     return trxQry.scroll(ScrollMode.FORWARD_ONLY);
   }
 
-  /**
-   * 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.
-   */
-  private BigDecimal getCurrentValuedStock(MaterialTransaction trx) {
-    Organization org = getCostOrg();
-    Currency currency = getCostCurrency();
-    HashMap<CostDimension, BaseOBObject> costDimensions = getCostDimensions();
-
-    // Get child tree of organizations.
-    OrganizationStructureProvider osp = OBContext.getOBContext().getOrganizationStructureProvider(
-        trx.getClient().getId());
-    Set<String> orgs = osp.getChildTree(org.getId(), true);
-    if (isManufacturingProduct) {
-      orgs = osp.getChildTree("0", false);
-      costDimensions = CostingUtils.getEmptyDimensions();
-    }
-
-    StringBuffer select = new StringBuffer();
-    select.append(" select sum(case");
-    select.append("     when trx." + MaterialTransaction.PROPERTY_MOVEMENTQUANTITY
-        + " < 0 then -tc." + TransactionCost.PROPERTY_COST);
-    select.append("     else tc." + TransactionCost.PROPERTY_COST + " end ) as cost");
-    select.append(" , tc." + TransactionCost.PROPERTY_CURRENCY + ".id as currency");
-    select.append(" , coalesce(sr." + ShipmentInOut.PROPERTY_ACCOUNTINGDATE + ", trx."
-        + MaterialTransaction.PROPERTY_MOVEMENTDATE + ") as mdate");
-    select.append(" , sum(trx." + MaterialTransaction.PROPERTY_MOVEMENTQUANTITY + ") as stock");
-
-    select.append(" from " + TransactionCost.ENTITY_NAME + " as tc");
-    select.append("  join tc." + TransactionCost.PROPERTY_INVENTORYTRANSACTION + " as trx");
-    select.append("  join trx." + MaterialTransaction.PROPERTY_STORAGEBIN + " as locator");
-    select.append("  left join trx." + MaterialTransaction.PROPERTY_GOODSSHIPMENTLINE + " as line");
-    select.append("  left join line." + ShipmentInOutLine.PROPERTY_SHIPMENTRECEIPT + " as sr");
-
-    select.append(" where trx." + MaterialTransaction.PROPERTY_PRODUCT + " = :product");
-    // Include only transactions that have its cost calculated
-    select.append("   and trx." + MaterialTransaction.PROPERTY_ISCOSTCALCULATED + " = true");
-    if (areBackdatedTrxFixed) {
-      // Consider only transactions with movement date equal or lower than the movement date of the
-      // adjusted transaction. But for transactions with the same movement date only those with a
-      // transaction date equal or before the process date of the adjusted transaction.
-      select.append("   and (trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + " < :mvtdate");
-      select.append("     or (trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + " = :mvtdate");
-      select.append("   and trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE
-          + " <= :trxdate ))");
-    } else {
-      select.append("   and trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE
-          + " <= :trxdate");
-    }
-    if (costDimensions.get(CostDimension.Warehouse) != null) {
-      select.append("  and locator." + Locator.PROPERTY_WAREHOUSE + ".id = :warehouse");
-    }
-    select.append("   and trx." + MaterialTransaction.PROPERTY_ORGANIZATION + ".id in (:orgs)");
-
-    select.append(" group by tc." + TransactionCost.PROPERTY_CURRENCY);
-    select.append("   , coalesce(sr." + ShipmentInOut.PROPERTY_ACCOUNTINGDATE + ", trx."
-        + MaterialTransaction.PROPERTY_MOVEMENTDATE + ")");
-
-    Query trxQry = OBDal.getInstance().getSession().createQuery(select.toString());
-    trxQry.setParameter("product", trx.getProduct());
-    if (areBackdatedTrxFixed) {
-      trxQry.setParameter("mvtdate", trx.getMovementDate());
-    }
-    trxQry.setParameter("trxdate", trx.getTransactionProcessDate());
-    if (costDimensions.get(CostDimension.Warehouse) != null) {
-      trxQry.setParameter("warehouse", costDimensions.get(CostDimension.Warehouse).getId());
-    }
-    trxQry.setParameterList("orgs", orgs);
-    @SuppressWarnings("unchecked")
-    List<Object[]> o = trxQry.list();
-    BigDecimal costsum = BigDecimal.ZERO;
-    for (Object[] resultSet : o) {
-      BigDecimal origAmt = (BigDecimal) resultSet[0];
-      Currency origCur = OBDal.getInstance().get(Currency.class, resultSet[1]);
-      Date convDate = (Date) resultSet[2];
-
-      if (origCur != currency) {
-        costsum = costsum.add(FinancialUtils.getConvertedAmount(origAmt, origCur, currency,
-            convDate, org, FinancialUtils.PRECISION_COSTING));
-      } else {
-        costsum = costsum.add(origAmt);
-      }
-    }
-    return costsum;
-  }
-
   private List<CostAdjustmentLine> getTrxAdjustmentLines(MaterialTransaction trx) {
     OBCriteria<CostAdjustmentLine> critLines = OBDal.getInstance().createCriteria(
         CostAdjustmentLine.class);
@@ -565,9 +494,11 @@
   @Override
   protected void calculateNegativeStockCorrectionAdjustmentAmount(CostAdjustmentLine costAdjLine) {
     MaterialTransaction basetrx = costAdjLine.getInventoryTransaction();
-    BigDecimal currentStock = CostingUtils.getCurrentStock(getCostOrg(), basetrx,
+    BigDecimal currentStock = CostAdjustmentUtils.getStockOnTransactionDate(getCostOrg(), basetrx,
         getCostDimensions(), isManufacturingProduct, areBackdatedTrxFixed);
-    BigDecimal currentValueAmt = getCurrentValuedStock(basetrx);
+    BigDecimal currentValueAmt = CostAdjustmentUtils.getValuedStockOnTransactionDate(getCostOrg(),
+        basetrx, getCostDimensions(), isManufacturingProduct, areBackdatedTrxFixed,
+        getCostCurrency());
     int precission = getCostCurrency().getCostingPrecision().intValue();
 
     BigDecimal trxCost = CostAdjustmentUtils.getTrxCost(basetrx, false, getCostCurrency());
@@ -581,13 +512,9 @@
   }
 
   /**
-   * 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.
-   * 
-   * @param areBackdatedTrxFixed
+   * Calculates the average cost value of the transaction.
    */
-  protected static Costing getTrxProductCost(MaterialTransaction trx,
+  protected static Costing getAvgCostOnMovementDate(MaterialTransaction trx,
       HashMap<CostDimension, BaseOBObject> costDimensions, Organization costOrg,
       boolean areBackdatedTrxFixed) {
 
@@ -596,53 +523,67 @@
         costOrg.getClient().getId());
     Set<String> orgs = osp.getChildTree(costOrg.getId(), true);
 
-    StringBuffer select = new StringBuffer();
-    select.append(" select c ");
+    StringBuffer where = new StringBuffer();
+    where.append(" as c");
+    where.append("\n  join c." + TransactionCost.PROPERTY_INVENTORYTRANSACTION + " as trx");
+    where.append("\n  join trx." + MaterialTransaction.PROPERTY_STORAGEBIN + " as locator");
+    where.append("\n , " + org.openbravo.model.ad.domain.List.ENTITY_NAME + " as trxtype");
+    where.append("\n where trxtype." + CostAdjustmentUtils.propADListReference + ".id = :refid");
+    where.append("  and trxtype." + CostAdjustmentUtils.propADListValue + " = trx."
+        + MaterialTransaction.PROPERTY_MOVEMENTTYPE);
 
-    select.append(" from " + Costing.ENTITY_NAME + " as c");
-    select.append("  join c." + TransactionCost.PROPERTY_INVENTORYTRANSACTION + " as trx");
-    select.append("  join trx." + MaterialTransaction.PROPERTY_STORAGEBIN + " as locator");
+    where.append("   and trx." + MaterialTransaction.PROPERTY_PRODUCT + ".id = :product");
+    if (areBackdatedTrxFixed) {
+      where.append("  and (");
+      where.append("   trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + " < :mvtdate");
+      where.append("   or (");
+      where.append("    trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + " = :mvtdate");
+    }
+    // If there are more than one trx on the same trx process date filter out those types with less
+    // priority and / or higher quantity.
+    where.append(" and (");
+    where.append("  trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE + " < :trxdate");
+    where.append("  or (");
+    where.append("   trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE + " = :trxdate");
+    where.append("   and (");
+    where.append("    trxtype." + CostAdjustmentUtils.propADListPriority + " < :trxtypeprio");
+    where.append("    or (");
+    where.append("     trxtype." + CostAdjustmentUtils.propADListPriority + " = :trxtypeprio");
+    where.append("     and trx." + MaterialTransaction.PROPERTY_MOVEMENTQUANTITY + " >= :trxqty");
+    where.append(" ))))");
 
-    select.append(" where trx." + MaterialTransaction.PROPERTY_PRODUCT + ".id = :product");
     if (areBackdatedTrxFixed) {
-      // Consider only transactions with movement date equal or lower than the movement date of the
-      // adjusted transaction. But for transactions with the same movement date only those with a
-      // transaction date equal or before the process date of the adjusted transaction.
-      select.append("   and (trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + " < :mvtdate");
-      select.append("     or (trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + " = :mvtdate");
-      select.append("   and trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE
-          + " <= :trxdate ))");
-    } else {
-      select.append("   and trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE
-          + " <= :trxdate");
+      where.append("  ))");
     }
     // Include only transactions that have its cost calculated
-    select.append("   and trx." + MaterialTransaction.PROPERTY_ISCOSTCALCULATED + " = true");
+    where.append("   and trx." + MaterialTransaction.PROPERTY_ISCOSTCALCULATED + " = true");
     if (costDimensions.get(CostDimension.Warehouse) != null) {
-      select.append("  and locator." + Locator.PROPERTY_WAREHOUSE + ".id = :warehouse");
+      where.append("  and locator." + Locator.PROPERTY_WAREHOUSE + ".id = :warehouse");
     }
-    select.append("   and trx." + MaterialTransaction.PROPERTY_ORGANIZATION + ".id in (:orgs)");
-    select.append(" order by ");
+    where.append("   and trx." + MaterialTransaction.PROPERTY_ORGANIZATION + ".id in (:orgs)");
+    where.append(" order by ");
     if (areBackdatedTrxFixed) {
-      select.append(" trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + ", ");
+      where.append(" trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + " desc, ");
     }
-    select.append(" trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE);
+    where.append(" trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE + " desc ");
+    where.append(" , trxtype." + CostAdjustmentUtils.propADListPriority + " desc ");
+    where.append(" , trx." + MaterialTransaction.PROPERTY_MOVEMENTQUANTITY);
 
-    Query trxQry = OBDal.getInstance().getSession().createQuery(select.toString());
-    trxQry.setParameter("product", trx.getProduct().getId());
-    trxQry.setParameter("mvtdate", trx.getMovementDate());
-    trxQry.setParameter("trxdate", trx.getTransactionProcessDate());
+    OBQuery<Costing> qryCost = OBDal.getInstance().createQuery(Costing.class, where.toString());
+    qryCost.setNamedParameter("refid", CostAdjustmentUtils.MovementTypeRefID);
+    qryCost.setNamedParameter("product", trx.getProduct().getId());
+    qryCost.setNamedParameter("mvtdate", trx.getMovementDate());
+    qryCost.setNamedParameter("trxdate", trx.getTransactionProcessDate());
+    qryCost.setNamedParameter("trxtypeprio",
+        CostAdjustmentUtils.getTrxTypePrio(trx.getMovementType()));
+    qryCost.setNamedParameter("trxqty", trx.getMovementQuantity());
 
     if (costDimensions.get(CostDimension.Warehouse) != null) {
-      trxQry.setParameter("warehouse", costDimensions.get(CostDimension.Warehouse).getId());
+      qryCost.setNamedParameter("warehouse", costDimensions.get(CostDimension.Warehouse).getId());
     }
-    trxQry.setParameterList("orgs", orgs);
-    @SuppressWarnings("unchecked")
-    List<Costing> o = trxQry.list();
-    if (o.size() == 0) {
-      return null;
-    }
-    return o.get(0);
+    qryCost.setNamedParameter("orgs", orgs);
+    qryCost.setMaxResult(1);
+    return qryCost.uniqueResult();
   }
 
   /**
--- a/src/org/openbravo/costing/CostAdjustmentUtils.java	Mon Sep 29 22:38:44 2014 +0200
+++ b/src/org/openbravo/costing/CostAdjustmentUtils.java	Tue Sep 30 02:00:49 2014 +0200
@@ -26,6 +26,7 @@
 
 import org.apache.log4j.Logger;
 import org.hibernate.Query;
+import org.hibernate.criterion.Restrictions;
 import org.openbravo.advpaymentmngt.utility.FIN_Utility;
 import org.openbravo.base.exception.OBException;
 import org.openbravo.base.provider.OBProvider;
@@ -33,6 +34,7 @@
 import org.openbravo.costing.CostingAlgorithm.CostDimension;
 import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.security.OrganizationStructureProvider;
+import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.dal.service.OBQuery;
 import org.openbravo.financial.FinancialUtils;
@@ -53,6 +55,10 @@
   private static final Logger log4j = Logger.getLogger(CostAdjustmentUtils.class);
   final static String strCategoryCostAdj = "CAD";
   final static String strTableCostAdj = "M_CostAdjustment";
+  final static String propADListPriority = org.openbravo.model.ad.domain.List.PROPERTY_SEQUENCENUMBER;
+  final static String propADListReference = org.openbravo.model.ad.domain.List.PROPERTY_REFERENCE;
+  final static String propADListValue = org.openbravo.model.ad.domain.List.PROPERTY_SEARCHKEY;
+  final static String MovementTypeRefID = "189";
 
   /**
    * @param organization
@@ -123,11 +129,8 @@
    * @param startingDate
    *          initial date of the current Costing Rule. Only transactions calculated by current rule
    *          needs to be considered.
-   * 
-   * @param costDimensions
-   *          dimensions used in costs
    */
-  public static boolean isNeededCostAdjustmentByBackDateTrx(MaterialTransaction transaction,
+  public static boolean isNeededBackdatedCostAdjustment(MaterialTransaction transaction,
       boolean includeWarehouseDimension, Date startingDate) {
 
     final String orgLegalId = OBContext.getOBContext()
@@ -139,11 +142,27 @@
 
     StringBuffer where = new StringBuffer();
     where.append(" as trx");
-    where.append("   join trx." + MaterialTransaction.PROPERTY_STORAGEBIN + " as locator");
-    where.append(" where trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE
+    where.append("\n join trx." + MaterialTransaction.PROPERTY_STORAGEBIN + " as locator");
+    where.append("\n , " + org.openbravo.model.ad.domain.List.ENTITY_NAME + " as trxtype");
+    where.append("\n where trxtype." + propADListReference + ".id = :refid");
+    where.append("  and trxtype." + propADListValue + " = trx."
+        + MaterialTransaction.PROPERTY_MOVEMENTTYPE);
+    where.append("   and trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE
         + " > :crStartDate");
-    where.append("   and trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE
-        + " < :processDate");
+    // If there are more than one trx on the same trx process date filter out those types with less
+    // priority and / or higher quantity.
+    where.append(" and (");
+    where.append("  trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE + " < :trxDate");
+    where.append("  or (");
+    where.append("   trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE + " = :trxDate");
+    where.append("   and trxtype." + propADListPriority + " < :trxtypeprio");
+    where.append("  )");
+    where.append("  or (");
+    where.append("   trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE + " = :trxDate");
+    where.append("   and trxtype." + propADListPriority + " = :trxtypeprio");
+    where.append("   and trx." + MaterialTransaction.PROPERTY_MOVEMENTQUANTITY + " >= :trxqty");
+    where.append(" ))");
+
     where.append("   and trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + " > :movementDate");
     where.append("   and trx." + MaterialTransaction.PROPERTY_PRODUCT + ".id = :productId");
     where.append("   and trx." + MaterialTransaction.PROPERTY_ORGANIZATION + ".id in (:orgs)");
@@ -155,10 +174,14 @@
     OBQuery<MaterialTransaction> trxQry = OBDal.getInstance().createQuery(
         MaterialTransaction.class, where.toString());
 
+    trxQry.setNamedParameter("refid", MovementTypeRefID);
     trxQry.setNamedParameter("crStartDate", startingDate);
-    trxQry.setNamedParameter("processDate", transaction.getTransactionProcessDate());
+    trxQry.setNamedParameter("trxDate", transaction.getTransactionProcessDate());
     trxQry.setNamedParameter("movementDate", transaction.getMovementDate());
     trxQry.setNamedParameter("productId", transaction.getProduct().getId());
+    trxQry.setNamedParameter("trxtypeprio",
+        CostAdjustmentUtils.getTrxTypePrio(transaction.getMovementType()));
+    trxQry.setNamedParameter("trxqty", transaction.getMovementQuantity());
     trxQry.setNamedParameter("orgs", orgs);
     if (includeWarehouseDimension) {
       trxQry.setNamedParameter("warehouse", transaction.getStorageBin().getWarehouse().getId());
@@ -214,7 +237,7 @@
    * 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.
    */
-  public static BigDecimal getCurrentStock(Product product, Organization org, Date _date,
+  public static BigDecimal getStockOnMovementDate(Product product, Organization org, Date _date,
       HashMap<CostDimension, BaseOBObject> costDimensions, boolean backdatedTransactionsFixed) {
     // Get child tree of organizations.
     Date date = _date;
@@ -284,12 +307,90 @@
   }
 
   /**
+   * 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,
+      boolean areBackdatedTrxFixed) {
+
+    // Get child tree of organizations.
+    OrganizationStructureProvider osp = OBContext.getOBContext().getOrganizationStructureProvider(
+        trx.getClient().getId());
+    Set<String> orgs = osp.getChildTree(costorg.getId(), true);
+    HashMap<CostDimension, BaseOBObject> costDimensions = _costDimensions;
+    if (isManufacturingProduct) {
+      orgs = osp.getChildTree("0", false);
+      costDimensions = CostingUtils.getEmptyDimensions();
+    }
+
+    StringBuffer select = new StringBuffer();
+    select
+        .append(" select sum(trx." + MaterialTransaction.PROPERTY_MOVEMENTQUANTITY + ") as stock");
+    select.append("\n from " + MaterialTransaction.ENTITY_NAME + " as trx");
+    select.append("\n join trx." + MaterialTransaction.PROPERTY_STORAGEBIN + " as locator");
+    select.append("\n , " + org.openbravo.model.ad.domain.List.ENTITY_NAME + " as trxtype");
+    select.append("\n where trxtype." + propADListReference + ".id = :refid");
+    select.append("  and trxtype." + propADListValue + " = trx."
+        + MaterialTransaction.PROPERTY_MOVEMENTTYPE);
+    select.append("   and trx." + MaterialTransaction.PROPERTY_PRODUCT + " = :product");
+    // Include only transactions that have its cost calculated. Should be all.
+    select.append("   and trx." + MaterialTransaction.PROPERTY_ISCOSTCALCULATED + " = true");
+    if (areBackdatedTrxFixed) {
+      select.append("  and (");
+      select.append("   trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + " < :mvtdate");
+      select.append("   or (");
+      select.append("    trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + " = :mvtdate");
+    }
+    // If there are more than one trx on the same trx process date filter out those types with less
+    // priority and / or higher quantity.
+    select.append(" and (");
+    select.append("  trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE + " < :trxdate");
+    select.append("  or (");
+    select.append("   trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE + " = :trxdate");
+    select.append("   and (");
+    select.append("    trxtype." + propADListPriority + " < :trxtypeprio");
+    select.append("    or (");
+    select.append("     trxtype." + propADListPriority + " = :trxtypeprio");
+    select.append("     and trx." + MaterialTransaction.PROPERTY_MOVEMENTQUANTITY + " >= :trxqty");
+    select.append(" ))))");
+
+    if (areBackdatedTrxFixed) {
+      select.append("  ))");
+    }
+    if (costDimensions.get(CostDimension.Warehouse) != null) {
+      select.append("  and locator." + Locator.PROPERTY_WAREHOUSE + ".id = :warehouse");
+    }
+    select.append("   and trx." + MaterialTransaction.PROPERTY_ORGANIZATION + ".id in (:orgs)");
+    Query trxQry = OBDal.getInstance().getSession().createQuery(select.toString());
+    trxQry.setParameter("refid", MovementTypeRefID);
+    trxQry.setParameter("product", trx.getProduct());
+    if (areBackdatedTrxFixed) {
+      trxQry.setParameter("mvtdate", trx.getMovementDate());
+    }
+    trxQry.setParameter("trxdate", trx.getTransactionProcessDate());
+    trxQry.setParameter("trxtypeprio", getTrxTypePrio(trx.getMovementType()));
+    trxQry.setParameter("trxqty", trx.getMovementQuantity());
+    if (costDimensions.get(CostDimension.Warehouse) != null) {
+      trxQry.setParameter("warehouse", costDimensions.get(CostDimension.Warehouse).getId());
+    }
+    trxQry.setParameterList("orgs", orgs);
+    Object stock = trxQry.uniqueResult();
+    if (stock != null) {
+      return (BigDecimal) stock;
+    }
+    return BigDecimal.ZERO;
+  }
+
+  /**
    * 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 getCurrentValuedStock(Product product, Organization org, Date _date,
-      HashMap<CostDimension, BaseOBObject> _costDimensions, Currency currency,
+  public static BigDecimal getValuedStockOnMovementDate(Product product, Organization org,
+      Date _date, HashMap<CostDimension, BaseOBObject> _costDimensions, Currency currency,
       boolean backdatedTransactionsFixed) {
     Date date = _date;
     HashMap<CostDimension, BaseOBObject> costDimensions = _costDimensions;
@@ -390,6 +491,106 @@
     return costsum;
   }
 
+  /**
+   * 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 getValuedStockOnTransactionDate(Organization costorg,
+      MaterialTransaction trx, HashMap<CostDimension, BaseOBObject> _costDimensions,
+      boolean isManufacturingProduct, boolean areBackdatedTrxFixed, Currency currency) {
+    HashMap<CostDimension, BaseOBObject> costDimensions = _costDimensions;
+
+    // Get child tree of organizations.
+    OrganizationStructureProvider osp = OBContext.getOBContext().getOrganizationStructureProvider(
+        trx.getClient().getId());
+    Set<String> orgs = osp.getChildTree(costorg.getId(), true);
+    if (isManufacturingProduct) {
+      orgs = osp.getChildTree("0", false);
+      costDimensions = CostingUtils.getEmptyDimensions();
+    }
+
+    StringBuffer select = new StringBuffer();
+    select.append(" select sum(case");
+    select.append("     when trx." + MaterialTransaction.PROPERTY_MOVEMENTQUANTITY
+        + " < 0 then -tc." + TransactionCost.PROPERTY_COST);
+    select.append("     else tc." + TransactionCost.PROPERTY_COST + " end ) as cost");
+    select.append(" , tc." + TransactionCost.PROPERTY_CURRENCY + ".id as currency");
+    select.append(" , tc." + TransactionCost.PROPERTY_ACCOUNTINGDATE + " as mdate");
+    select.append(" , sum(trx." + MaterialTransaction.PROPERTY_MOVEMENTQUANTITY + ") as stock");
+
+    select.append("\n from " + TransactionCost.ENTITY_NAME + " as tc");
+    select.append("\n  join tc." + TransactionCost.PROPERTY_INVENTORYTRANSACTION + " as trx");
+    select.append("\n  join trx." + MaterialTransaction.PROPERTY_STORAGEBIN + " as locator");
+    select.append("\n , " + org.openbravo.model.ad.domain.List.ENTITY_NAME + " as trxtype");
+    select.append("\n where trxtype." + propADListReference + ".id = :refid");
+    select.append("  and trxtype." + propADListValue + " = trx."
+        + MaterialTransaction.PROPERTY_MOVEMENTTYPE);
+
+    select.append("  and trx." + MaterialTransaction.PROPERTY_PRODUCT + " = :product");
+    // Include only transactions that have its cost calculated
+    select.append("   and trx." + MaterialTransaction.PROPERTY_ISCOSTCALCULATED + " = true");
+    if (areBackdatedTrxFixed) {
+      select.append("  and (");
+      select.append("   trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + " < :mvtdate");
+      select.append("   or (");
+      select.append("    trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + " = :mvtdate");
+    }
+    // If there are more than one trx on the same trx process date filter out those types with less
+    // priority and / or higher quantity.
+    select.append(" and (");
+    select.append("  trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE + " < :trxdate");
+    select.append("  or (");
+    select.append("   trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE + " = :trxdate");
+    select.append("   and (");
+    select.append("    trxtype." + propADListPriority + " < :trxtypeprio");
+    select.append("    or (");
+    select.append("     trxtype." + propADListPriority + " = :trxtypeprio");
+    select.append("     and trx." + MaterialTransaction.PROPERTY_MOVEMENTQUANTITY + " >= :trxqty");
+    select.append(" ))))");
+
+    if (areBackdatedTrxFixed) {
+      select.append("  ))");
+    }
+    if (costDimensions.get(CostDimension.Warehouse) != null) {
+      select.append("  and locator." + Locator.PROPERTY_WAREHOUSE + ".id = :warehouse");
+    }
+    select.append("   and trx." + MaterialTransaction.PROPERTY_ORGANIZATION + ".id in (:orgs)");
+
+    select.append(" group by tc." + TransactionCost.PROPERTY_CURRENCY);
+    select.append("   , tc." + TransactionCost.PROPERTY_ACCOUNTINGDATE);
+
+    Query trxQry = OBDal.getInstance().getSession().createQuery(select.toString());
+    trxQry.setParameter("refid", MovementTypeRefID);
+    trxQry.setParameter("product", trx.getProduct());
+    if (areBackdatedTrxFixed) {
+      trxQry.setParameter("mvtdate", trx.getMovementDate());
+    }
+    trxQry.setParameter("trxdate", trx.getTransactionProcessDate());
+    if (costDimensions.get(CostDimension.Warehouse) != null) {
+      trxQry.setParameter("warehouse", costDimensions.get(CostDimension.Warehouse).getId());
+    }
+    trxQry.setParameter("trxtypeprio", getTrxTypePrio(trx.getMovementType()));
+    trxQry.setParameter("trxqty", trx.getMovementQuantity());
+    trxQry.setParameterList("orgs", orgs);
+    @SuppressWarnings("unchecked")
+    List<Object[]> o = trxQry.list();
+    BigDecimal costsum = BigDecimal.ZERO;
+    for (Object[] resultSet : o) {
+      BigDecimal origAmt = (BigDecimal) resultSet[0];
+      Currency origCur = OBDal.getInstance().get(Currency.class, resultSet[1]);
+      Date convDate = (Date) resultSet[2];
+
+      if (origCur != currency) {
+        costsum = costsum.add(FinancialUtils.getConvertedAmount(origAmt, origCur, currency,
+            convDate, costorg, FinancialUtils.PRECISION_COSTING));
+      } else {
+        costsum = costsum.add(origAmt);
+      }
+    }
+    return costsum;
+  }
+
   /*
    * Returns the last transaction process date of a non backdated transactions for the given
    * movement date or previous date.
@@ -465,4 +666,16 @@
 
     return (Date) objMaxDate;
   }
+
+  /**
+   * Returns the priority of the given movementType.
+   */
+  public static long getTrxTypePrio(String mvmntType) {
+    OBCriteria<org.openbravo.model.ad.domain.List> crList = OBDal.getInstance().createCriteria(
+        org.openbravo.model.ad.domain.List.class);
+    crList.createAlias(propADListReference, "ref");
+    crList.add(Restrictions.eq("ref.id", MovementTypeRefID));
+    crList.add(Restrictions.eq(propADListValue, mvmntType));
+    return ((org.openbravo.model.ad.domain.List) crList.uniqueResult()).getSequenceNumber();
+  }
 }
--- a/src/org/openbravo/costing/CostingAlgorithmAdjustmentImp.java	Mon Sep 29 22:38:44 2014 +0200
+++ b/src/org/openbravo/costing/CostingAlgorithmAdjustmentImp.java	Tue Sep 30 02:00:49 2014 +0200
@@ -205,6 +205,7 @@
     newCAL.setParentCostAdjustmentLine(parentLine);
 
     OBDal.getInstance().save(newCAL);
+    OBDal.getInstance().flush();
 
     addCostDependingTrx(newCAL);
     return newCAL;
--- a/src/org/openbravo/costing/CostingServer.java	Mon Sep 29 22:38:44 2014 +0200
+++ b/src/org/openbravo/costing/CostingServer.java	Tue Sep 30 02:00:49 2014 +0200
@@ -221,7 +221,7 @@
     }
 
     if (getCostingRule().isBackdatedTransactionsFixed()
-        && CostAdjustmentUtils.isNeededCostAdjustmentByBackDateTrx(transaction, getCostingRule()
+        && CostAdjustmentUtils.isNeededBackdatedCostAdjustment(transaction, getCostingRule()
             .isWarehouseDimension(), getCostingRule().getStartingDate())) {
       // BDT= Backdated transaction
       CostAdjustment costAdjustmentHeader = CostAdjustmentUtils.insertCostAdjustmentHeader(
@@ -257,7 +257,7 @@
     }
 
     boolean modifiesAvg = AverageAlgorithm.modifiesAverage(TrxType.getTrxType(transaction));
-    BigDecimal currentStock = CostingUtils.getCurrentStock(getOrganization(), transaction,
+    BigDecimal currentStock = CostAdjustmentUtils.getStockOnTransactionDate(getOrganization(), transaction,
         getCostingAlgorithm().costDimensions, transaction.getProduct().isProduction(),
         costingRule.isBackdatedTransactionsFixed());
     // the stock previous to transaction was negative
--- a/src/org/openbravo/costing/CostingUtils.java	Mon Sep 29 22:38:44 2014 +0200
+++ b/src/org/openbravo/costing/CostingUtils.java	Tue Sep 30 02:00:49 2014 +0200
@@ -441,7 +441,7 @@
    * amount is converted to given currency. It only takes transactions that have its cost
    * calculated.
    */
-  public static BigDecimal getCurrentValuedStockByAttrAndLocator(Product product, Organization org,
+  public static BigDecimal getValuedStockByAttrAndLocatorOnMovementDate(Product product, Organization org,
       Date date, AttributeSetInstance attributesetinstance, Locator locator, Currency currency) {
     // Get child tree of organizations.
     Set<String> orgs = OBContext.getOBContext().getOrganizationStructureProvider()
@@ -463,7 +463,7 @@
     select.append("  left join line." + ShipmentInOutLine.PROPERTY_SHIPMENTRECEIPT + " as sr");
 
     select.append(" where trx." + MaterialTransaction.PROPERTY_PRODUCT + ".id = :product");
-    select.append("  and trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE + " <= :date");
+    select.append("  and trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + " <= :date");
     select.append("  and trx." + MaterialTransaction.PROPERTY_ATTRIBUTESETVALUE + " = :attrib");
     select.append("  and trx." + MaterialTransaction.PROPERTY_STORAGEBIN + " = :locator");
     // Include only transactions that have its cost calculated
@@ -569,67 +569,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 getCurrentStock(Organization org, MaterialTransaction trx,
-      HashMap<CostDimension, BaseOBObject> _costDimensions, boolean isManufacturingProduct,
-      boolean areBackdatedTrxFixed) {
-
-    // Get child tree of organizations.
-    OrganizationStructureProvider osp = OBContext.getOBContext().getOrganizationStructureProvider(
-        trx.getClient().getId());
-    Set<String> orgs = osp.getChildTree(org.getId(), true);
-    HashMap<CostDimension, BaseOBObject> costDimensions = _costDimensions;
-    if (isManufacturingProduct) {
-      orgs = osp.getChildTree("0", false);
-      costDimensions = CostingUtils.getEmptyDimensions();
-    }
-
-    StringBuffer select = new StringBuffer();
-    select
-        .append(" select sum(trx." + MaterialTransaction.PROPERTY_MOVEMENTQUANTITY + ") as stock");
-    select.append(" from " + MaterialTransaction.ENTITY_NAME + " as trx");
-    select.append("   join trx." + MaterialTransaction.PROPERTY_STORAGEBIN + " as locator");
-    select.append(" where trx." + MaterialTransaction.PROPERTY_PRODUCT + " = :product");
-    // Include only transactions that have its cost calculated. Should be all.
-    select.append("   and trx." + MaterialTransaction.PROPERTY_ISCOSTCALCULATED + " = true");
-    if (areBackdatedTrxFixed) {
-      // Consider only transactions with movement date equal or lower than the movement date of the
-      // adjusted transaction. But for transactions with the same movement date only those with a
-      // transaction date equal or before the process date of the adjusted transaction.
-      select.append("   and (trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + " < :mvtdate");
-      select.append("     or (trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + " = :mvtdate");
-      select.append("   and trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE
-          + " <= :trxdate ))");
-    } else {
-      select.append("   and trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE
-          + " <= :trxdate ");
-    }
-    if (costDimensions.get(CostDimension.Warehouse) != null) {
-      select.append("  and locator." + Locator.PROPERTY_WAREHOUSE + ".id = :warehouse");
-    }
-    select.append("   and trx." + MaterialTransaction.PROPERTY_ORGANIZATION + ".id in (:orgs)");
-    Query trxQry = OBDal.getInstance().getSession().createQuery(select.toString());
-    trxQry.setParameter("product", trx.getProduct());
-    if (areBackdatedTrxFixed) {
-      trxQry.setParameter("mvtdate", trx.getMovementDate());
-    }
-    trxQry.setParameter("trxdate", trx.getTransactionProcessDate());
-    if (costDimensions.get(CostDimension.Warehouse) != null) {
-      trxQry.setParameter("warehouse", costDimensions.get(CostDimension.Warehouse).getId());
-    }
-    trxQry.setParameterList("orgs", orgs);
-    Object stock = trxQry.uniqueResult();
-    if (stock != null) {
-      return (BigDecimal) stock;
-    }
-    return BigDecimal.ZERO;
-  }
-
-  /**
    * Returns the max transaction date with cost calculated
    */
   public static Date getMaxTransactionDate(Organization org) {
--- a/src/org/openbravo/costing/FixBackdatedTransactionsProcess.java	Mon Sep 29 22:38:44 2014 +0200
+++ b/src/org/openbravo/costing/FixBackdatedTransactionsProcess.java	Tue Sep 30 02:00:49 2014 +0200
@@ -73,7 +73,7 @@
       try {
         while (transactions.next()) {
           MaterialTransaction trx = (MaterialTransaction) transactions.get()[0];
-          if (CostAdjustmentUtils.isNeededCostAdjustmentByBackDateTrx(trx,
+          if (CostAdjustmentUtils.isNeededBackdatedCostAdjustment(trx,
               rule.isWarehouseDimension(), rule.getStartingDate())) {
             createCostAdjustmenHeader(rule.getOrganization());
             CostAdjustmentLine cal = CostAdjustmentUtils.insertCostAdjustmentLine(trx,
--- a/src/org/openbravo/costing/StandardAlgorithm.java	Mon Sep 29 22:38:44 2014 +0200
+++ b/src/org/openbravo/costing/StandardAlgorithm.java	Tue Sep 30 02:00:49 2014 +0200
@@ -83,7 +83,7 @@
     if (!costingRule.isBackdatedTransactionsFixed()) {
       return true;
     }
-    return !CostAdjustmentUtils.isNeededCostAdjustmentByBackDateTrx(transaction,
+    return !CostAdjustmentUtils.isNeededBackdatedCostAdjustment(transaction,
         costingRule.isWarehouseDimension(), costingRule.getStartingDate());
   }
 
--- a/src/org/openbravo/erpCommon/ad_callouts/SL_InvAmtUpd_ProductRefDate.java	Mon Sep 29 22:38:44 2014 +0200
+++ b/src/org/openbravo/erpCommon/ad_callouts/SL_InvAmtUpd_ProductRefDate.java	Tue Sep 30 02:00:49 2014 +0200
@@ -78,9 +78,9 @@
       } else {
         info.addResult("inpiswarehousedimension", "N");
       }
-      currentValuedStock = CostAdjustmentUtils.getCurrentValuedStock(product, organization,
+      currentValuedStock = CostAdjustmentUtils.getValuedStockOnMovementDate(product, organization,
           referenceDate, costDimensions, currency, costRule.isBackdatedTransactionsFixed());
-      currentStock = CostAdjustmentUtils.getCurrentStock(product, organization, referenceDate,
+      currentStock = CostAdjustmentUtils.getStockOnMovementDate(product, organization, referenceDate,
           costDimensions, costRule.isBackdatedTransactionsFixed());
       info.addResult("inpcurInventoryAmount", currentValuedStock);
       info.addResult("inponhandqty", currentStock);