Fixes issue 31202: NSC when you get into 0 stock
authorAlvaro Ferraz <alvaro.ferraz@openbravo.com>
Mon, 26 Oct 2015 17:00:37 +0100
changeset 27860 9c12064c0d44
parent 27859 cebfec5bc6a9
child 27861 6b0630da41f1
Fixes issue 31202: NSC when you get into 0 stock

Negative Stock Correction adjustment should be created when you get into zero stock (only in case you do not get into zero stock by a Closing Inventory), as it is created when you get into negative stock.
src/org/openbravo/costing/CostingServer.java
src/org/openbravo/costing/CostingUtils.java
--- a/src/org/openbravo/costing/CostingServer.java	Thu Nov 12 11:25:47 2015 +0100
+++ b/src/org/openbravo/costing/CostingServer.java	Mon Oct 26 17:00:37 2015 +0100
@@ -288,10 +288,13 @@
     BigDecimal currentStock = CostAdjustmentUtils.getStockOnTransactionDate(getOrganization(),
         transaction, getCostingAlgorithm().costDimensions, transaction.getProduct().isProduction(),
         costingRule.isBackdatedTransactionsFixed());
-    // the stock previous to transaction was negative
+    // the stock previous to transaction was zero or negative
     if (checkNegativeStockCorrectionTrxs
-        && currentStock.compareTo(transaction.getMovementQuantity()) < 0 && modifiesAvg
-        && !adjustmentAlreadyCreated) {
+        && modifiesAvg
+        && !adjustmentAlreadyCreated
+        && (currentStock.compareTo(transaction.getMovementQuantity()) < 0 || (trxType != TrxType.InventoryOpening
+            && currentStock.compareTo(transaction.getMovementQuantity()) == 0 && CostingUtils
+              .existsProcessedTransactions(transaction.getProduct())))) {
 
       CostAdjustment costAdjustmentHeader = CostAdjustmentUtils.insertCostAdjustmentHeader(
           transaction.getOrganization(), "NSC"); // NSC= Negative Stock Correction
--- a/src/org/openbravo/costing/CostingUtils.java	Thu Nov 12 11:25:47 2015 +0100
+++ b/src/org/openbravo/costing/CostingUtils.java	Mon Oct 26 17:00:37 2015 +0100
@@ -604,4 +604,17 @@
       }
     }
   }
+
+  /**
+   * Check if exists processed transactions for this product
+   */
+  public static boolean existsProcessedTransactions(Product product) {
+    OBCriteria<MaterialTransaction> criteria = OBDal.getInstance().createCriteria(
+        MaterialTransaction.class);
+    criteria.add(Restrictions.eq(MaterialTransaction.PROPERTY_PRODUCT, product));
+    criteria.add(Restrictions.eq(MaterialTransaction.PROPERTY_ISPROCESSED, true));
+    criteria.setFilterOnReadableOrganization(false);
+    criteria.setMaxResults(1);
+    return criteria.uniqueResult() != null;
+  }
 }