details:   https://code.openbravo.com/erp/devel/pi/rev/a5064f422fe0
changeset: 32876:a5064f422fe0
user:      Nono Carballo <nonofce <at> gmail.com>
date:      Mon Oct 23 17:40:09 2017 +0200
summary:   Fixes issue 37033: "Price Difference Correction" adjustment 
calculated when it
should not

Cost adjustment is created just if expected unitary cost excedes transaction
unitary cost.

details:   https://code.openbravo.com/erp/devel/pi/rev/68b79a76a32d
changeset: 32877:68b79a76a32d
user:      David Miguelez <david.miguelez <at> openbravo.com>
date:      Tue Oct 24 09:12:56 2017 +0200
summary:   Related to Issue 37033. Code Review changes

* Take into account correct quantity when calculating the expected unit cost
* Use correct quantity to calculate Transaction Cost Difference

diffstat:

 src/org/openbravo/costing/PriceDifferenceProcess.java |  97 +++++++++++++-----
 1 files changed, 68 insertions(+), 29 deletions(-)

diffs (135 lines):

diff -r 75838b0fd787 -r 68b79a76a32d 
src/org/openbravo/costing/PriceDifferenceProcess.java
--- a/src/org/openbravo/costing/PriceDifferenceProcess.java     Fri Oct 13 
09:34:21 2017 +0200
+++ b/src/org/openbravo/costing/PriceDifferenceProcess.java     Tue Oct 24 
09:12:56 2017 +0200
@@ -83,10 +83,9 @@
     }
 
     Date costAdjDateAcct = null;
-    BigDecimal invoiceAmt = BigDecimal.ZERO;
 
     // Calculate current transaction unit cost including existing adjustments.
-    BigDecimal currentTrxUnitCost = 
CostAdjustmentUtils.getTrxCost(materialTransaction, true,
+    BigDecimal currentTrxCost = 
CostAdjustmentUtils.getTrxCost(materialTransaction, true,
         trxCurrency);
 
     // Calculate expected transaction unit cost based on current invoice 
amounts and purchase price.
@@ -94,30 +93,11 @@
     BigDecimal invoiceQty = BigDecimal.ZERO;
     for (ReceiptInvoiceMatch matchInv : 
receiptLine.getProcurementReceiptInvoiceMatchList()) {
       Invoice invoice = matchInv.getInvoiceLine().getInvoice();
-      if (invoice.getDocumentStatus().equals("VO")) {
-        // Skip voided invoices.
-        continue;
-      }
-      if (!invoice.isProcessed()) {
-        // Skip not processed invoices.
-        continue;
-      }
-      if (isNegativeReceipt) {
-        // If the receipt is negative negate the invoiced quantities.
-        invoiceQty = invoiceQty.add(matchInv.getQuantity().negate());
-      } else {
-        invoiceQty = invoiceQty.add(matchInv.getQuantity());
-      }
-      invoiceAmt = 
matchInv.getQuantity().multiply(matchInv.getInvoiceLine().getUnitPrice());
-
-      invoiceAmt = FinancialUtils.getConvertedAmount(invoiceAmt, 
invoice.getCurrency(),
-          trxCurrency, trxDate, trxOrg, FinancialUtils.PRECISION_STANDARD,
-          invoice.getCurrencyConversionRateDocList());
-      expectedCost = expectedCost.add(invoiceAmt);
-
-      Date invoiceDate = invoice.getInvoiceDate();
-      if (costAdjDateAcct == null || costAdjDateAcct.before(invoiceDate)) {
-        costAdjDateAcct = invoiceDate;
+      if (invoiceIsNotVoidedAndIsProcessed(invoice)) {
+        invoiceQty = calculateInvoiceQuantity(isNegativeReceipt, invoiceQty, 
matchInv);
+        expectedCost = calculateExpectedCost(trxCurrency, trxOrg, trxDate, 
expectedCost, matchInv,
+            invoice);
+        costAdjDateAcct = getCostAdjustmentDate(costAdjDateAcct, invoice);
       }
     }
 
@@ -142,16 +122,29 @@
       }
       expectedCost = expectedCost.add(baseAmt);
     }
+
+    // Since expected Cost already takes into account invoiced and not 
invoiced amount, expected
+    // Quantity must take into account the same. If there is more invoiced 
quantity than received
+    // quantity, then the invoiced quantity is used, else the recived quantity
+    BigDecimal expectedQty = invoiceQty.compareTo(receiptQty) >= 0 ? 
invoiceQty : receiptQty;
+    BigDecimal expectedUnitCost = expectedQty.compareTo(BigDecimal.ZERO) == 0 
? BigDecimal.ZERO
+        : expectedCost.divide(expectedQty, costCurPrecission, 
RoundingMode.HALF_UP);
+    BigDecimal currentUnitCost = receiptQty.compareTo(BigDecimal.ZERO) == 0 ? 
BigDecimal.ZERO
+        : currentTrxCost.divide(receiptQty, costCurPrecission, 
RoundingMode.HALF_UP);
+
     // if the sum of trx costs with flag "isInvoiceCorrection" is distinct 
that the amount cost
     // generated by Match Invoice then New Cost Adjustment line is created by 
the difference
-    if (expectedCost.compareTo(currentTrxUnitCost) != 0) {
+    if (expectedUnitCost.compareTo(currentUnitCost) != 0) {
       if (costAdjDateAcct == null) {
         costAdjDateAcct = trxDate;
       }
       createCostAdjustmenHeader(trxOrg);
+
+      BigDecimal trxCostDifference = (expectedUnitCost.multiply(receiptQty))
+          .subtract(currentTrxCost);
+
       CostAdjustmentLine costAdjLine = 
CostAdjustmentUtils.insertCostAdjustmentLine(
-          materialTransaction, costAdjHeader, 
expectedCost.subtract(currentTrxUnitCost),
-          Boolean.TRUE, costAdjDateAcct);
+          materialTransaction, costAdjHeader, trxCostDifference, Boolean.TRUE, 
costAdjDateAcct);
       costAdjLine.setNeedsPosting(Boolean.TRUE);
       OBDal.getInstance().save(costAdjLine);
       costAdjCreated = true;
@@ -302,4 +295,50 @@
       return false;
     }
   }
+
+  private static boolean invoiceIsNotVoidedAndIsProcessed(Invoice invoice) {
+    return !invoice.getDocumentStatus().equals("VO") && invoice.isProcessed();
+  }
+
+  private static BigDecimal calculateInvoiceQuantity(boolean isNegativeReceipt,
+      BigDecimal invoiceQty, ReceiptInvoiceMatch matchInv) {
+    BigDecimal invoiceQuantity;
+    if (isNegativeReceipt) {
+      // If the receipt is negative negate the invoiced quantities.
+      invoiceQuantity = invoiceQty.add(matchInv.getQuantity().negate());
+    } else {
+      invoiceQuantity = invoiceQty.add(matchInv.getQuantity());
+    }
+    return invoiceQuantity;
+  }
+
+  private static BigDecimal calculateExpectedCost(Currency trxCurrency, 
Organization trxOrg,
+      Date trxDate, BigDecimal expectedCost, ReceiptInvoiceMatch matchInv, 
Invoice invoice) {
+    BigDecimal invoiceAmt;
+    BigDecimal cost;
+    invoiceAmt = calculateInvoiceAmount(matchInv, invoice, trxCurrency, 
trxOrg, trxDate);
+    cost = expectedCost.add(invoiceAmt);
+    return cost;
+  }
+
+  private static BigDecimal calculateInvoiceAmount(ReceiptInvoiceMatch 
matchInv, Invoice invoice,
+      Currency trxCurrency, Organization trxOrg, Date trxDate) {
+    BigDecimal invoiceAmt;
+    invoiceAmt = 
matchInv.getQuantity().multiply(matchInv.getInvoiceLine().getUnitPrice());
+
+    invoiceAmt = FinancialUtils.getConvertedAmount(invoiceAmt, 
invoice.getCurrency(), trxCurrency,
+        trxDate, trxOrg, FinancialUtils.PRECISION_STANDARD,
+        invoice.getCurrencyConversionRateDocList());
+    return invoiceAmt;
+  }
+
+  private static Date getCostAdjustmentDate(Date costAdjDateAcct, Invoice 
invoice) {
+    Date invoiceDate = invoice.getInvoiceDate();
+    Date costAdjustmentDate = costAdjDateAcct;
+    if (costAdjDateAcct == null || costAdjDateAcct.before(invoiceDate)) {
+      costAdjustmentDate = invoiceDate;
+    }
+    return costAdjustmentDate;
+  }
+
 }

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Openbravo-commits mailing list
Openbravo-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to