[costadj]Automatic adjustments are disabled by default.
authorGorka Ion Damián <gorkaion.damian@openbravo.com>
Tue, 30 Sep 2014 12:35:22 +0200
changeset 24966 f5d224afc5d9
parent 24965 ab4e66e15458
child 24967 b9c16113f824
[costadj]Automatic adjustments are disabled by default.

The meaning of the preferences has been changed and now you have to create
them in order to have automated adjustments.
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
--- a/src-db/database/sourcedata/AD_REF_LIST.xml	Tue Sep 30 12:27:54 2014 +0200
+++ b/src-db/database/sourcedata/AD_REF_LIST.xml	Tue Sep 30 12:35:22 2014 +0200
@@ -9283,8 +9283,8 @@
 <!--4E0A4D67A83E4A1BBF194BF0006A089C-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
 <!--4E0A4D67A83E4A1BBF194BF0006A089C-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
 <!--4E0A4D67A83E4A1BBF194BF0006A089C-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
-<!--4E0A4D67A83E4A1BBF194BF0006A089C-->  <VALUE><![CDATA[doNotCheckNegativeStockCorrecctionTrxs]]></VALUE>
-<!--4E0A4D67A83E4A1BBF194BF0006A089C-->  <NAME><![CDATA[Do Not Check Negative Stock Correction Transactions]]></NAME>
+<!--4E0A4D67A83E4A1BBF194BF0006A089C-->  <VALUE><![CDATA[enableNegativeStockCorrections]]></VALUE>
+<!--4E0A4D67A83E4A1BBF194BF0006A089C-->  <NAME><![CDATA[Enable Negative Stock Corrections]]></NAME>
 <!--4E0A4D67A83E4A1BBF194BF0006A089C-->  <AD_REFERENCE_ID><![CDATA[A26BA480E2014707B47257024C3CBFF7]]></AD_REFERENCE_ID>
 <!--4E0A4D67A83E4A1BBF194BF0006A089C-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
 <!--4E0A4D67A83E4A1BBF194BF0006A089C--></AD_REF_LIST>
@@ -9614,8 +9614,8 @@
 <!--6336EEE0A73E4C199F2222180BAF266C-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
 <!--6336EEE0A73E4C199F2222180BAF266C-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
 <!--6336EEE0A73E4C199F2222180BAF266C-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
-<!--6336EEE0A73E4C199F2222180BAF266C-->  <VALUE><![CDATA[doNotCheckPriceCorrectionTrxs]]></VALUE>
-<!--6336EEE0A73E4C199F2222180BAF266C-->  <NAME><![CDATA[Do not Check Price Correction Transactions]]></NAME>
+<!--6336EEE0A73E4C199F2222180BAF266C-->  <VALUE><![CDATA[enableAutomaticPriceCorrectionTrxs]]></VALUE>
+<!--6336EEE0A73E4C199F2222180BAF266C-->  <NAME><![CDATA[Enable automatic Price Difference Corrections when Receipt cost is calculated]]></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/AverageCostAdjustment.java	Tue Sep 30 12:27:54 2014 +0200
+++ b/src/org/openbravo/costing/AverageCostAdjustment.java	Tue Sep 30 12:35:22 2014 +0200
@@ -227,7 +227,7 @@
           BigDecimal trxPrice = trxCost.add(trxAdjAmt).divide(trx.getMovementQuantity().abs(),
               costCurPrecission, RoundingMode.HALF_UP);
 
-          if (currentStock.compareTo(trx.getMovementQuantity()) < 0
+          if (checkNegativeStockCorrection && currentStock.compareTo(trx.getMovementQuantity()) < 0
               && cost.compareTo(trxPrice) != 0) {
             // stock was negative and cost different than trx price then Negative Stock Correction
             // is added
--- a/src/org/openbravo/costing/CostAdjustmentUtils.java	Tue Sep 30 12:27:54 2014 +0200
+++ b/src/org/openbravo/costing/CostAdjustmentUtils.java	Tue Sep 30 12:35:22 2014 +0200
@@ -24,7 +24,6 @@
 import java.util.List;
 import java.util.Set;
 
-import org.apache.log4j.Logger;
 import org.hibernate.Query;
 import org.hibernate.criterion.Restrictions;
 import org.openbravo.advpaymentmngt.utility.FIN_Utility;
@@ -50,15 +49,19 @@
 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;
 
 public class CostAdjustmentUtils {
-  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";
+  private static final Logger log4j = LoggerFactory.getLogger(CostAdjustmentUtils.class);
+  public static final String strCategoryCostAdj = "CAD";
+  public static final String strTableCostAdj = "M_CostAdjustment";
+  public static final String propADListPriority = org.openbravo.model.ad.domain.List.PROPERTY_SEQUENCENUMBER;
+  public static final String propADListReference = org.openbravo.model.ad.domain.List.PROPERTY_REFERENCE;
+  public static final String propADListValue = org.openbravo.model.ad.domain.List.PROPERTY_SEARCHKEY;
+  public static final String MovementTypeRefID = "189";
+  public static final String ENABLE_AUTO_PRICE_CORRECTION_PREF = "enableAutomaticPriceCorrectionTrxs";
+  public static final String ENABLE_NEGATIVE_STOCK_CORRECTION_PREF = "enableNegativeStockCorrections";
 
   /**
    * @param organization
@@ -214,8 +217,8 @@
     // log4j.debug("Get Transaction Cost");
     if (!trx.isCostCalculated()) {
       // Transaction hasn't been calculated yet.
-      log4j.error("  *** No cost found for transaction " + trx.getIdentifier() + " with id "
-          + trx.getId());
+      log4j.error("  *** No cost found for transaction {} with id {}", trx.getIdentifier(),
+          trx.getId());
       throw new OBException("@NoCostFoundForTrxOnDate@ @Transaction@: " + trx.getIdentifier());
     }
     BigDecimal cost = BigDecimal.ZERO;
--- a/src/org/openbravo/costing/CostingAlgorithmAdjustmentImp.java	Tue Sep 30 12:27:54 2014 +0200
+++ b/src/org/openbravo/costing/CostingAlgorithmAdjustmentImp.java	Tue Sep 30 12:35:22 2014 +0200
@@ -34,8 +34,11 @@
 import org.openbravo.costing.CostingAlgorithm.CostDimension;
 import org.openbravo.costing.CostingServer.TrxType;
 import org.openbravo.dal.core.DalUtil;
+import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.dal.service.OBQuery;
+import org.openbravo.erpCommon.businessUtility.Preferences;
+import org.openbravo.erpCommon.utility.PropertyException;
 import org.openbravo.model.common.businesspartner.BusinessPartner;
 import org.openbravo.model.common.currency.Currency;
 import org.openbravo.model.common.enterprise.Organization;
@@ -68,6 +71,7 @@
   protected String strClientId;
   protected boolean isManufacturingProduct;
   protected boolean areBackdatedTrxFixed;
+  protected boolean checkNegativeStockCorrection;
   protected HashMap<CostDimension, String> costDimensionIds = new HashMap<CostDimension, String>();
 
   /**
@@ -107,6 +111,15 @@
       }
       costDimensionIds.put(costDimension, value);
     }
+    try {
+      checkNegativeStockCorrection = Preferences.getPreferenceValue(
+          CostAdjustmentUtils.ENABLE_NEGATIVE_STOCK_CORRECTION_PREF, true,
+          OBContext.getOBContext().getCurrentClient(),
+          OBContext.getOBContext().getCurrentOrganization(), OBContext.getOBContext().getUser(),
+          OBContext.getOBContext().getRole(), null).equals("Y");
+    } catch (PropertyException e1) {
+      checkNegativeStockCorrection = false;
+    }
   }
 
   /**
--- a/src/org/openbravo/costing/CostingServer.java	Tue Sep 30 12:27:54 2014 +0200
+++ b/src/org/openbravo/costing/CostingServer.java	Tue Sep 30 12:35:22 2014 +0200
@@ -140,18 +140,19 @@
         return;
       }
     }
-    boolean doNotCheckPriceCorrectionTrxs = false;
-    boolean doNotCheckNegativeStockCorrectionTrxs = false;
+    boolean checkPriceCorrectionTrxs = false;
+    boolean checkNegativeStockCorrectionTrxs = false;
     // check if price correction is needed
     try {
-      doNotCheckPriceCorrectionTrxs = Preferences.getPreferenceValue(
-          "doNotCheckPriceCorrectionTrxs", true, OBContext.getOBContext().getCurrentClient(),
+      checkPriceCorrectionTrxs = Preferences.getPreferenceValue(
+          CostAdjustmentUtils.ENABLE_AUTO_PRICE_CORRECTION_PREF, true,
+          OBContext.getOBContext().getCurrentClient(),
           OBContext.getOBContext().getCurrentOrganization(), OBContext.getOBContext().getUser(),
           OBContext.getOBContext().getRole(), null).equals("Y");
     } catch (PropertyException e1) {
-      doNotCheckPriceCorrectionTrxs = false;
+      checkPriceCorrectionTrxs = false;
     }
-    if (!doNotCheckPriceCorrectionTrxs && transaction.isCheckpricedifference()) {
+    if (checkPriceCorrectionTrxs && transaction.isCheckpricedifference()) {
       PriceDifferenceProcess.processPriceDifferenceTransaction(transaction);
     }
 
@@ -247,21 +248,21 @@
 
     // check if negative stock correction should be done
     try {
-      doNotCheckNegativeStockCorrectionTrxs = Preferences.getPreferenceValue(
-          "doNotCheckNegativeStockCorrecctionTrxs", true,
+      checkNegativeStockCorrectionTrxs = Preferences.getPreferenceValue(
+          CostAdjustmentUtils.ENABLE_NEGATIVE_STOCK_CORRECTION_PREF, true,
           OBContext.getOBContext().getCurrentClient(),
           OBContext.getOBContext().getCurrentOrganization(), OBContext.getOBContext().getUser(),
           OBContext.getOBContext().getRole(), null).equals("Y");
     } catch (PropertyException e1) {
-      doNotCheckNegativeStockCorrectionTrxs = false;
+      checkNegativeStockCorrectionTrxs = false;
     }
 
     boolean modifiesAvg = AverageAlgorithm.modifiesAverage(TrxType.getTrxType(transaction));
-    BigDecimal currentStock = CostAdjustmentUtils.getStockOnTransactionDate(getOrganization(), transaction,
-        getCostingAlgorithm().costDimensions, transaction.getProduct().isProduction(),
+    BigDecimal currentStock = CostAdjustmentUtils.getStockOnTransactionDate(getOrganization(),
+        transaction, getCostingAlgorithm().costDimensions, transaction.getProduct().isProduction(),
         costingRule.isBackdatedTransactionsFixed());
     // the stock previous to transaction was negative
-    if (!doNotCheckNegativeStockCorrectionTrxs
+    if (checkNegativeStockCorrectionTrxs
         && currentStock.compareTo(transaction.getMovementQuantity()) < 0 && modifiesAvg) {
 
       CostAdjustment costAdjustmentHeader = CostAdjustmentUtils.insertCostAdjustmentHeader(