From a8c9df56e4ab65fa3432ecc6ba4e9ad439f2f18d Mon Sep 17 00:00:00 2001 From: Carlos Ruiz Date: Wed, 23 Jan 2019 17:55:22 +0100 Subject: [PATCH] IDEMPIERE-493 Add validation for matching / fix the validation for negative order/receipt/invoice - validation was written just for positive orders and is failing with negative orders (the usual way to process a return when there is no previous receipt) --- .../src/org/compiere/model/MMatchInv.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/org.adempiere.base/src/org/compiere/model/MMatchInv.java b/org.adempiere.base/src/org/compiere/model/MMatchInv.java index 9bfb064ae4..d1ee2ed0c6 100644 --- a/org.adempiere.base/src/org/compiere/model/MMatchInv.java +++ b/org.adempiere.base/src/org/compiere/model/MMatchInv.java @@ -230,9 +230,15 @@ public class MMatchInv extends X_M_MatchInv { MInOutLine line = new MInOutLine(getCtx(), getM_InOutLine_ID(), get_TrxName()); BigDecimal matchedQty = DB.getSQLValueBD(get_TrxName(), "SELECT Coalesce(SUM(Qty),0) FROM M_MatchInv WHERE M_InOutLine_ID=?" , getM_InOutLine_ID()); - if (matchedQty != null && matchedQty.compareTo(line.getMovementQty()) > 0) + BigDecimal matchedQtyDB = matchedQty; + BigDecimal movementQty = line.getMovementQty(); + if (movementQty.signum() < 0) { + movementQty = movementQty.negate(); + matchedQty = matchedQty.negate(); + } + if (matchedQty != null && matchedQty.compareTo(movementQty) > 0) { - throw new IllegalStateException("Total matched qty > movement qty. MatchedQty="+matchedQty+", MovementQty="+line.getMovementQty()+", Line="+line); + throw new IllegalStateException("Total matched qty > movement qty. MatchedQty="+matchedQtyDB+", MovementQty="+line.getMovementQty()+", Line="+line); } } @@ -240,9 +246,15 @@ public class MMatchInv extends X_M_MatchInv { MInvoiceLine line = new MInvoiceLine(getCtx(), getC_InvoiceLine_ID(), get_TrxName()); BigDecimal matchedQty = DB.getSQLValueBD(get_TrxName(), "SELECT Coalesce(SUM(Qty),0) FROM M_MatchInv WHERE C_InvoiceLine_ID=?" , getC_InvoiceLine_ID()); - if (matchedQty != null && matchedQty.compareTo(line.getQtyInvoiced()) > 0) + BigDecimal matchedQtyDB = matchedQty; + BigDecimal qtyInvoiced = line.getQtyInvoiced(); + if (qtyInvoiced.signum() < 0) { + qtyInvoiced = qtyInvoiced.negate(); + matchedQty = matchedQty.negate(); + } + if (matchedQty != null && matchedQty.compareTo(qtyInvoiced) > 0) { - throw new IllegalStateException("Total matched qty > invoiced qty. MatchedQty="+matchedQty+", InvoicedQty="+line.getQtyInvoiced()+", Line="+line); + throw new IllegalStateException("Total matched qty > invoiced qty. MatchedQty="+matchedQtyDB+", InvoicedQty="+line.getQtyInvoiced()+", Line="+line); } } return true;