From ec7ee69efbc9824cc5e034329c739b15ba651180 Mon Sep 17 00:00:00 2001 From: Heng Sin Low Date: Thu, 27 Sep 2018 16:40:52 +0800 Subject: [PATCH] IDEMPIERE-3697 Wrong organziation (*) in matched PO document when reversing MR with invoice. --- .../src/org/compiere/model/MInOut.java | 31 +++++++++ .../src/org/compiere/model/MInvoice.java | 15 +++-- .../src/org/compiere/model/MMatchPO.java | 66 +++++++++++++------ 3 files changed, 88 insertions(+), 24 deletions(-) diff --git a/org.adempiere.base/src/org/compiere/model/MInOut.java b/org.adempiere.base/src/org/compiere/model/MInOut.java index d02e7ea254..3b6d4460f6 100644 --- a/org.adempiere.base/src/org/compiere/model/MInOut.java +++ b/org.adempiere.base/src/org/compiere/model/MInOut.java @@ -18,7 +18,9 @@ package org.compiere.model; import java.io.File; import java.math.BigDecimal; +import java.sql.PreparedStatement; import java.sql.ResultSet; +import java.sql.SQLException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; @@ -26,6 +28,7 @@ import java.util.Properties; import java.util.logging.Level; import org.adempiere.exceptions.AdempiereException; +import org.adempiere.exceptions.DBException; import org.adempiere.exceptions.NegativeInventoryDisallowedException; import org.adempiere.exceptions.PeriodClosedException; import org.compiere.print.MPrintFormat; @@ -2240,6 +2243,34 @@ public class MInOut extends X_M_InOut implements DocAction asset.setDescription(asset.getDescription() + " (" + reversal.getDocumentNo() + " #" + rLine.getLine() + "<-)"); asset.saveEx(); } + // Un-Link inoutline to Invoiceline + String sql = "SELECT C_InvoiceLine_ID FROM C_InvoiceLine WHERE M_InOutLine_ID=?"; + PreparedStatement pstmt = null; + ResultSet rs = null; + try + { + pstmt = DB.prepareStatement(sql, get_TrxName()); + pstmt.setInt(1, sLines[i].getM_InOutLine_ID()); + rs = pstmt.executeQuery(); + while (rs.next()) + { + int invoiceLineId = rs.getInt(1); + if (invoiceLineId > 0 ){ + MInvoiceLine iLine = new MInvoiceLine(getCtx(),invoiceLineId , get_TrxName()); + iLine.setM_InOutLine_ID(0); + iLine.saveEx(); + } + } + } + catch (SQLException e) + { + throw new DBException(e, sql); + } + finally + { + DB.close(rs, pstmt); + rs = null; pstmt = null; + } } reversal.setC_Order_ID(getC_Order_ID()); // Set M_RMA_ID diff --git a/org.adempiere.base/src/org/compiere/model/MInvoice.java b/org.adempiere.base/src/org/compiere/model/MInvoice.java index aa53bdc5a5..65aab2a5cb 100644 --- a/org.adempiere.base/src/org/compiere/model/MInvoice.java +++ b/org.adempiere.base/src/org/compiere/model/MInvoice.java @@ -2420,12 +2420,19 @@ public class MInvoice extends X_C_Invoice implements DocAction if (mPO[i].getM_InOutLine_ID() == 0) { - if (!mPO[i].reverse(reversalDate)) + if(mPO[i].isPosted()) { - m_processMsg = "Could not Reverse MatchPO"; - return null; + if (!mPO[i].reverse(reversalDate)) + { + m_processMsg = "Could not Reverse MatchPO"; + return null; + } + addDocsPostProcess(new MMatchPO(Env.getCtx(), mPO[i].getReversal_ID(), get_TrxName())); + } + else + { + mPO[i].deleteEx(true); } - addDocsPostProcess(new MMatchPO(Env.getCtx(), mPO[i].getReversal_ID(), get_TrxName())); } else { diff --git a/org.adempiere.base/src/org/compiere/model/MMatchPO.java b/org.adempiere.base/src/org/compiere/model/MMatchPO.java index 167c843f76..44308e982d 100644 --- a/org.adempiere.base/src/org/compiere/model/MMatchPO.java +++ b/org.adempiere.base/src/org/compiere/model/MMatchPO.java @@ -531,10 +531,7 @@ public class MMatchPO extends X_M_MatchPO otherMatchPO = matchPO; iLine = new MInvoiceLine(retValue.getCtx(), matchPO.getC_InvoiceLine_ID(), retValue.get_TrxName()); matchPO.setQty(matchPO.getQty().subtract(retValue.getQty())); - if (matchPO.getQty().signum() == 0 ) - matchPO.deleteEx(true); - else - matchPO.saveEx(); + matchPO.saveEx(); break; } @@ -594,6 +591,8 @@ public class MMatchPO extends X_M_MatchPO } catch (Exception e) {} } } + if (otherMatchPO.getQty().signum() == 0 ) + otherMatchPO.deleteEx(true); } } if (!retValue.save()) @@ -865,7 +864,7 @@ public class MMatchPO extends X_M_MatchPO // Bayu, Sistematika // BF [ 2240484 ] Re MatchingPO, MMatchPO doesn't contains Invoice info // If newRecord, set c_invoiceline_id while null - if (newRecord && getC_InvoiceLine_ID() == 0) + if (newRecord && getC_InvoiceLine_ID() == 0 && getReversal_ID()==0) { MMatchInv[] mpi = MMatchInv.getInOutLine(getCtx(), getM_InOutLine_ID(), get_TrxName()); for (int i = 0; i < mpi.length; i++) @@ -873,6 +872,12 @@ public class MMatchPO extends X_M_MatchPO if (mpi[i].getC_InvoiceLine_ID() != 0 && mpi[i].getM_AttributeSetInstance_ID() == getM_AttributeSetInstance_ID()) { + //verify m_matchpo not created yet + int cnt = DB.getSQLValue(get_TrxName(), "SELECT Count(*) FROM M_MatchPO WHERE M_InOutLine_ID="+getM_InOutLine_ID() + +" AND C_InvoiceLine_ID="+mpi[i].getC_InvoiceLine_ID()); + if (cnt > 0) + continue; + if (mpi[i].getQty().compareTo(getQty()) == 0) // same quantity { setC_InvoiceLine_ID(mpi[i].getC_InvoiceLine_ID()); @@ -992,7 +997,7 @@ public class MMatchPO extends X_M_MatchPO { MInOutLine line = new MInOutLine(getCtx(), getM_InOutLine_ID(), get_TrxName()); BigDecimal matchedQty = DB.getSQLValueBD(get_TrxName(), "SELECT Coalesce(SUM(Qty),0) FROM M_MatchPO WHERE M_InOutLine_ID=?" , getM_InOutLine_ID()); - if (matchedQty != null && matchedQty.compareTo(line.getMovementQty()) > 0) + if (line.getMovementQty().signum() > 0 && matchedQty != null && matchedQty.compareTo(line.getMovementQty()) > 0) { throw new IllegalStateException("Total matched qty > movement qty. MatchedQty="+matchedQty+", MovementQty="+line.getMovementQty()+", Line="+line); } @@ -1000,8 +1005,8 @@ public class MMatchPO extends X_M_MatchPO if (getC_InvoiceLine_ID() > 0) { - MInvoiceLine line = new MInvoiceLine(getCtx(), getC_InvoiceLine_ID(), get_TrxName()); - BigDecimal matchedQty = DB.getSQLValueBD(get_TrxName(), "SELECT Coalesce(SUM(Qty),0) FROM M_MatchPO WHERE C_InvoiceLine_ID=?" , getC_InvoiceLine_ID()); + MInvoiceLine line = new MInvoiceLine(getCtx(), getC_InvoiceLine_ID(), get_TrxName()); + BigDecimal matchedQty = DB.getSQLValueBD(get_TrxName(), "SELECT Coalesce(SUM(Qty),0) FROM M_MatchPO WHERE C_InvoiceLine_ID=? AND Reversal_ID IS NULL " , getC_InvoiceLine_ID() ); if (matchedQty != null && matchedQty.compareTo(line.getQtyInvoiced()) > 0) { throw new IllegalStateException("Total matched qty > invoiced qty. MatchedQty="+matchedQty+", InvoicedQty="+line.getQtyInvoiced()+", Line="+line); @@ -1039,15 +1044,21 @@ public class MMatchPO extends X_M_MatchPO { if (getM_InOutLine_ID() != 0) // new delivery orderLine.setQtyDelivered(orderLine.getQtyDelivered().add(getQty())); - else // if (getM_InOutLine_ID() == 0) // reset to 0 + else if (!newRecord) // if (getM_InOutLine_ID() == 0) // reset to 0 orderLine.setQtyDelivered(orderLine.getQtyDelivered().subtract(getQty())); orderLine.setDateDelivered(getDateTrx()); // overwrite=last } + else if (!newRecord && getM_InOutLine_ID() > 0 && is_ValueChanged(COLUMNNAME_Qty)) + { + BigDecimal oldQty = (BigDecimal)(get_ValueOld(COLUMNNAME_Qty)); + orderLine.setQtyDelivered(orderLine.getQtyDelivered().subtract(oldQty.subtract(getQty()))); + } + if (m_isInvoiceLineChange && (newRecord || getC_InvoiceLine_ID() != get_ValueOldAsInt("C_InvoiceLine_ID"))) { if (getC_InvoiceLine_ID() != 0) // first time orderLine.setQtyInvoiced(orderLine.getQtyInvoiced().add(getQty())); - else // if (getC_InvoiceLine_ID() == 0) // set to 0 + else if (!newRecord) // if (getC_InvoiceLine_ID() == 0) // set to 0 orderLine.setQtyInvoiced(orderLine.getQtyInvoiced().subtract(getQty())); orderLine.setDateInvoiced(getDateTrx()); // overwrite=last } @@ -1253,8 +1264,14 @@ public class MMatchPO extends X_M_MatchPO { MMatchPO reversal = new MMatchPO (getCtx(), 0, get_TrxName()); reversal.setC_InvoiceLine_ID(getC_InvoiceLine_ID()); - reversal.setM_InOutLine_ID(getM_InOutLine_ID()); - PO.copyValues(this, reversal); + reversal.setM_InOutLine_ID(getM_InOutLine_ID()); + if (getC_OrderLine_ID() != 0) + reversal.setC_OrderLine_ID(getC_OrderLine_ID()); + else{ + reversal.setC_OrderLine_ID(getM_InOutLine().getC_OrderLine_ID()); + } + reversal.setM_Product_ID(getM_Product_ID()); + reversal.setM_AttributeSetInstance_ID(getM_AttributeSetInstance_ID()); reversal.setAD_Org_ID(this.getAD_Org_ID()); reversal.setDescription("(->" + this.getDocumentNo() + ")"); reversal.setQty(this.getQty().negate()); @@ -1262,14 +1279,16 @@ public class MMatchPO extends X_M_MatchPO reversal.setDateTrx(reversalDate); reversal.set_ValueNoCheck ("DocumentNo", null); reversal.setPosted (false); - reversal.setReversal_ID(getM_MatchPO_ID()); + reversal.setProcessed(true); + reversal.setReversal_ID(getM_MatchPO_ID()); reversal.saveEx(); + this.setDescription("(" + reversal.getDocumentNo() + "<-)"); this.setReversal_ID(reversal.getM_MatchPO_ID()); this.saveEx(); - - // reversal of mr if have both shipment and invoice line - if ( reversal.getM_InOutLine_ID() > 0 && reversal.getC_InvoiceLine_ID() > 0) + + // auto create new matchpo if have invoice line + if ( reversal.getC_InvoiceLine_ID() > 0) { MMatchPO[] matchPOs = MMatchPO.getOrderLine(reversal.getCtx(), reversal.getC_OrderLine_ID(), reversal.get_TrxName()); BigDecimal matchQty = getQty(); @@ -1288,12 +1307,19 @@ public class MMatchPO extends X_M_MatchPO if (matchQty.signum() != 0) { - MMatchPO matchPO = new MMatchPO (getCtx(), 0, get_TrxName()); - PO.copyValues(this, matchPO); + MMatchPO matchPO = new MMatchPO (getCtx(), 0, get_TrxName()); + matchPO.setC_OrderLine_ID(getC_OrderLine_ID()); matchPO.setC_InvoiceLine_ID(getC_InvoiceLine_ID()); - matchPO.setM_InOutLine_ID(0); + matchPO.setM_InOutLine_ID(0); + matchPO.setAD_Org_ID(getAD_Org_ID()); + matchPO.setQty(getQty()); + matchPO.setDateAcct(getDateAcct()); + matchPO.setDateTrx(getDateTrx()); + matchPO.setM_AttributeSetInstance_ID(getM_AttributeSetInstance_ID()); + matchPO.setM_Product_ID(getM_Product_ID()); matchPO.setDescription(null); - matchPO.setPosted (false); + matchPO.setProcessed(true); + matchPO.setPosted (false); matchPO.saveEx(); } }