details: https://code.openbravo.com/erp/devel/pi/rev/05d253f229e4 changeset: 35928:05d253f229e4 user: Nono Carballo <nonofce <at> gmail.com> date: Thu May 23 14:30:35 2019 -0400 summary: Fixes issue 40863: Fixes cost calculation for voided receipt transaction
When registering a transaction for a voided Goods Receipt or Shipment, the transaction cost is set to the original transaction cost. Also costs adjustments are created based on cost adjustment lines from original transaction. diffstat: src-test/src/org/openbravo/test/costing/TestCostingNoSourceAdjustments.java | 31 +++++-- src/org/openbravo/costing/CostingAlgorithm.java | 5 +- src/org/openbravo/costing/CostingServer.java | 41 +++++++++- 3 files changed, 64 insertions(+), 13 deletions(-) diffs (209 lines): diff -r aad48da6ff1e -r 05d253f229e4 src-test/src/org/openbravo/test/costing/TestCostingNoSourceAdjustments.java --- a/src-test/src/org/openbravo/test/costing/TestCostingNoSourceAdjustments.java Thu May 30 02:01:07 2019 +0000 +++ b/src-test/src/org/openbravo/test/costing/TestCostingNoSourceAdjustments.java Thu May 23 14:30:35 2019 -0400 @@ -11,7 +11,7 @@ * under the License. * The Original Code is Openbravo ERP. * The Initial Developer of the Original Code is Openbravo SLU - * All portions are Copyright (C) 2018 Openbravo SLU + * All portions are Copyright (C) 2018-2019 Openbravo SLU * All Rights Reserved. * Contributor(s): ______________________________________. ************************************************************************ @@ -1099,6 +1099,7 @@ final BigDecimal price2 = new BigDecimal("30.00"); final BigDecimal quantity1 = new BigDecimal("180"); final BigDecimal quantity2 = new BigDecimal("80"); + final BigDecimal half = new BigDecimal("0.5"); try { @@ -1128,7 +1129,8 @@ TestCostingUtils.runPriceBackground(); // Cancel goods shipment - ShipmentInOut goodsShipment2 = TestCostingUtils.cancelGoodsShipment(goodsShipment1, price2); + ShipmentInOut goodsShipment2 = TestCostingUtils.cancelGoodsShipment(goodsShipment1, + price2.multiply(half)); // Assert product transactions List<ProductTransactionAssert> productTransactionAssertList = new ArrayList<ProductTransactionAssert>(); @@ -1143,7 +1145,7 @@ productTransactionAssertList.add(new ProductTransactionAssert(OBDal.getInstance() .get(ShipmentInOut.class, goodsShipment2.getId()) .getMaterialMgmtShipmentInOutLineList() - .get(0), price2, price2, true)); + .get(0), price2.multiply(half), price2, true)); TestCostingUtils.assertProductTransaction(product.getId(), productTransactionAssertList); // Assert product costing @@ -1152,8 +1154,9 @@ List<ProductCostingAssert> productCostingAssertList = new ArrayList<ProductCostingAssert>(); productCostingAssertList .add(new ProductCostingAssert(transactionList.get(0), price2, price1, price2, quantity1)); - productCostingAssertList - .add(new ProductCostingAssert(transactionList.get(2), price2, null, price2, quantity1)); + productCostingAssertList.add(new ProductCostingAssert(transactionList.get(2), price2, + TestCostingUtils.getProductCostings(product.getId()).get(1).getOriginalCost(), price2, + quantity1)); TestCostingUtils.assertProductCosting(product.getId(), productCostingAssertList); // Assert cost adjustment @@ -1165,6 +1168,10 @@ costAdjustmentAssertLineList.add(new CostAdjustmentAssert(transactionList.get(1), "PDC", quantity2.multiply(price2).add(quantity2.multiply(price1).negate()), day2, false)); costAdjustmentAssertList.add(costAdjustmentAssertLineList); + List<CostAdjustmentAssert> costAdjustmentAssertLineList1 = new ArrayList<CostAdjustmentAssert>(); + costAdjustmentAssertLineList1.add(new CostAdjustmentAssert(transactionList.get(2), "PDC", + quantity2.multiply(price2).add(quantity2.multiply(price1).negate()), day2, true)); + costAdjustmentAssertList.add(costAdjustmentAssertLineList1); TestCostingUtils.assertCostAdjustment(costAdjustmentList, costAdjustmentAssertList); // Post cost adjustment 1 and assert it @@ -1308,6 +1315,7 @@ final BigDecimal price1 = new BigDecimal("15.00"); final BigDecimal price2 = new BigDecimal("7.50"); final BigDecimal quantity1 = new BigDecimal("180"); + final BigDecimal two = new BigDecimal("2"); try { @@ -1333,14 +1341,15 @@ TestCostingUtils.runPriceBackground(); // Cancel goods receipt - ShipmentInOut goodsReceipt2 = TestCostingUtils.cancelGoodsReceipt(goodsReceipt1, price2); + ShipmentInOut goodsReceipt2 = TestCostingUtils.cancelGoodsReceipt(goodsReceipt1, + price2.multiply(two)); // Assert product transactions List<ProductTransactionAssert> productTransactionAssertList = new ArrayList<ProductTransactionAssert>(); productTransactionAssertList.add(new ProductTransactionAssert(OBDal.getInstance() .get(ShipmentInOut.class, goodsReceipt2.getId()) .getMaterialMgmtShipmentInOutLineList() - .get(0), price2, price2, true)); + .get(0), price2.multiply(two), price2, true)); productTransactionAssertList.add(new ProductTransactionAssert(OBDal.getInstance() .get(ShipmentInOut.class, goodsReceipt1.getId()) .getMaterialMgmtShipmentInOutLineList() @@ -1353,8 +1362,8 @@ List<ProductCostingAssert> productCostingAssertList = new ArrayList<ProductCostingAssert>(); productCostingAssertList .add(new ProductCostingAssert(transactionList.get(1), price2, price1, price2, quantity1)); - productCostingAssertList.add( - new ProductCostingAssert(transactionList.get(0), price2, null, price2, BigDecimal.ZERO)); + productCostingAssertList.add(new ProductCostingAssert(transactionList.get(0), + price2.multiply(two), null, price2, BigDecimal.ZERO)); TestCostingUtils.assertProductCosting(product.getId(), productCostingAssertList); // Assert cost adjustment @@ -1364,6 +1373,10 @@ costAdjustmentAssertLineList.add(new CostAdjustmentAssert(transactionList.get(1), "PDC", quantity1.multiply(price2).add(quantity1.multiply(price1).negate()), day1, true)); costAdjustmentAssertList.add(costAdjustmentAssertLineList); + List<CostAdjustmentAssert> costAdjustmentAssertLineList1 = new ArrayList<CostAdjustmentAssert>(); + costAdjustmentAssertLineList1.add(new CostAdjustmentAssert(transactionList.get(0), "PDC", + quantity1.multiply(price2).add(quantity1.multiply(price1).negate()), day1, true)); + costAdjustmentAssertList.add(costAdjustmentAssertLineList1); TestCostingUtils.assertCostAdjustment(costAdjustmentList, costAdjustmentAssertList); // Post cost adjustment 1 and assert it diff -r aad48da6ff1e -r 05d253f229e4 src/org/openbravo/costing/CostingAlgorithm.java --- a/src/org/openbravo/costing/CostingAlgorithm.java Thu May 30 02:01:07 2019 +0000 +++ b/src/org/openbravo/costing/CostingAlgorithm.java Thu May 23 14:30:35 2019 -0400 @@ -11,7 +11,7 @@ * under the License. * The Original Code is Openbravo ERP. * The Initial Developer of the Original Code is Openbravo SLU - * All portions are Copyright (C) 2012-2017 Openbravo SLU + * All portions are Copyright (C) 2012-2019 Openbravo SLU * All Rights Reserved. * Contributor(s): ______________________________________. ************************************************************************* @@ -340,8 +340,7 @@ .getMaterialMgmtMaterialTransactionList() .get(0); - return CostingUtils.getTransactionCost(origInOutLineTrx, - transaction.getTransactionProcessDate(), costCurrency); + return origInOutLineTrx.getTransactionCost(); } /** diff -r aad48da6ff1e -r 05d253f229e4 src/org/openbravo/costing/CostingServer.java --- a/src/org/openbravo/costing/CostingServer.java Thu May 30 02:01:07 2019 +0000 +++ b/src/org/openbravo/costing/CostingServer.java Thu May 23 14:30:35 2019 -0400 @@ -11,7 +11,7 @@ * under the License. * The Original Code is Openbravo ERP. * The Initial Developer of the Original Code is Openbravo SLU - * All portions are Copyright (C) 2012-2018 Openbravo SLU + * All portions are Copyright (C) 2012-2019 Openbravo SLU * All Rights Reserved. * Contributor(s): ______________________________________. ************************************************************************* @@ -24,6 +24,8 @@ import java.util.Date; import java.util.List; +import javax.persistence.Tuple; + import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.time.DateUtils; import org.apache.logging.log4j.LogManager; @@ -32,6 +34,7 @@ import org.codehaus.jettison.json.JSONObject; import org.hibernate.ScrollMode; import org.hibernate.ScrollableResults; +import org.hibernate.query.Query; import org.openbravo.advpaymentmngt.utility.FIN_Utility; import org.openbravo.base.exception.OBException; import org.openbravo.base.provider.OBProvider; @@ -367,11 +370,47 @@ } } + if (trxType == TrxType.ReceiptVoid || trxType == TrxType.ShipmentVoid) { + createCostAdjustmentForVoidedReceiptOrShipment(); + } // update trxCost after cost adjustments transaction = OBDal.getInstance().get(MaterialTransaction.class, transaction.getId()); trxCost = CostAdjustmentUtils.getTrxCost(transaction, false, getCostCurrency()); } + private void createCostAdjustmentForVoidedReceiptOrShipment() { + MaterialTransaction origInOutLineTrx = transaction.getGoodsShipmentLine() + .getCanceledInoutLine() + .getMaterialMgmtMaterialTransactionList() + .get(0); + boolean isCostPermanent = transaction.isCostPermanent(); + transaction.setCostPermanent(false); + try (ScrollableResults scroll = getCostAdjustmentLines(origInOutLineTrx)) { + while (scroll.next()) { + Tuple result = (Tuple) scroll.get()[0]; + BigDecimal cost = (BigDecimal) result.get("cost"); + String sourceProcess = (String) result.get("sourceProcess"); + createAdjustment(sourceProcess, cost); + } + } + OBDal.getInstance().refresh(transaction); + transaction.setCostPermanent(isCostPermanent); + OBDal.getInstance().flush(); + } + + private ScrollableResults getCostAdjustmentLines(final MaterialTransaction origInOutLineTrx) { + + final String hqlQuery = "select tc.cost as cost, ca.sourceProcess as sourceProcess " + + "from TransactionCost tc join tc.costAdjustmentLine tal " + "join tal.costAdjustment ca " + + "where tc.inventoryTransaction = :transactionId"; + + final Query<Tuple> query = OBDal.getInstance().getSession().createQuery(hqlQuery, Tuple.class); + query.setParameter("transactionId", origInOutLineTrx); + query.setFetchSize(1000); + + return query.scroll(ScrollMode.FORWARD_ONLY); + } + private boolean createAdjustment(String type, BigDecimal amount) { CostAdjustment costAdjustmentHeader = CostAdjustmentUtils .insertCostAdjustmentHeader(transaction.getOrganization(), type); _______________________________________________ Openbravo-commits mailing list Openbravo-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/openbravo-commits