From e8b984a1e8d81f7c747c9438cea111d3206c49c4 Mon Sep 17 00:00:00 2001 From: Heng Sin Low Date: Wed, 25 Sep 2013 12:25:33 +0800 Subject: [PATCH] #1002861 Product Costing in Bizidum. Fixed reversal for matchpo, matchinv and cost adjustment. IDEMPIERE-1180 IDEMPIERE-1188 --- .../src/org/compiere/acct/Doc_MatchInv.java | 96 +++++++--- .../src/org/compiere/acct/Doc_MatchPO.java | 179 ++++++++++-------- .../src/org/compiere/acct/FactLine.java | 26 ++- .../src/org/compiere/model/MCostDetail.java | 58 +----- .../src/org/compiere/model/MInOut.java | 33 +--- .../src/org/compiere/model/MInventory.java | 2 + .../src/org/compiere/model/MInvoice.java | 67 +++---- .../src/org/compiere/model/MMatchInv.java | 69 +------ .../src/org/compiere/model/MMatchPO.java | 56 ------ 9 files changed, 238 insertions(+), 348 deletions(-) diff --git a/org.adempiere.base/src/org/compiere/acct/Doc_MatchInv.java b/org.adempiere.base/src/org/compiere/acct/Doc_MatchInv.java index f1bd5733ef..01ec920734 100644 --- a/org.adempiere.base/src/org/compiere/acct/Doc_MatchInv.java +++ b/org.adempiere.base/src/org/compiere/acct/Doc_MatchInv.java @@ -72,6 +72,7 @@ public class Doc_MatchInv extends Doc private MInOutLine m_receiptLine = null; private ProductCost m_pc = null; + private MMatchInv m_matchInv; /** Commitments */ // private DocLine[] m_commitments = null; @@ -83,21 +84,21 @@ public class Doc_MatchInv extends Doc protected String loadDocumentDetails () { setC_Currency_ID (Doc.NO_CURRENCY); - MMatchInv matchInv = (MMatchInv)getPO(); - setDateDoc(matchInv.getDateTrx()); - setQty (matchInv.getQty()); + m_matchInv = (MMatchInv)getPO(); + setDateDoc(m_matchInv.getDateTrx()); + setQty (m_matchInv.getQty()); // Invoice Info - int C_InvoiceLine_ID = matchInv.getC_InvoiceLine_ID(); + int C_InvoiceLine_ID = m_matchInv.getC_InvoiceLine_ID(); m_invoiceLine = new MInvoiceLine (getCtx(), C_InvoiceLine_ID, getTrxName()); // BP for NotInvoicedReceipts int C_BPartner_ID = m_invoiceLine.getParent().getC_BPartner_ID(); setC_BPartner_ID(C_BPartner_ID); // - int M_InOutLine_ID = matchInv.getM_InOutLine_ID(); + int M_InOutLine_ID = m_matchInv.getM_InOutLine_ID(); m_receiptLine = new MInOutLine (getCtx(), M_InOutLine_ID, getTrxName()); // m_pc = new ProductCost (Env.getCtx(), - getM_Product_ID(), matchInv.getM_AttributeSetInstance_ID(), getTrxName()); + getM_Product_ID(), m_matchInv.getM_AttributeSetInstance_ID(), getTrxName()); m_pc.setQty(getQty()); return null; @@ -171,18 +172,26 @@ public class Doc_MatchInv extends Doc return null; } dr.setQty(getQty()); - // dr.setM_Locator_ID(m_receiptLine.getM_Locator_ID()); - // MInOut receipt = m_receiptLine.getParent(); - // dr.setLocationFromBPartner(receipt.getC_BPartner_Location_ID(), true); // from Loc - // dr.setLocationFromLocator(m_receiptLine.getM_Locator_ID(), false); // to Loc BigDecimal temp = dr.getAcctBalance(); // Set AmtAcctCr/Dr from Receipt (sets also Project) - if (!dr.updateReverseLine (MInOut.Table_ID, // Amt updated - m_receiptLine.getM_InOut_ID(), m_receiptLine.getM_InOutLine_ID(), - multiplier)) + if (m_matchInv.getReversal_ID() > 0) { - p_Error = "Mat.Receipt not posted yet"; - return null; + if (!dr.updateReverseLine (MMatchInv.Table_ID, // Amt updated + m_matchInv.getReversal_ID(), 0, BigDecimal.ONE)) + { + p_Error = "Failed to create reversal entry"; + return null; + } + } + else + { + if (!dr.updateReverseLine (MInOut.Table_ID, // Amt updated + m_receiptLine.getM_InOut_ID(), m_receiptLine.getM_InOutLine_ID(), + multiplier)) + { + p_Error = "Mat.Receipt not posted yet"; + return null; + } } if (log.isLoggable(Level.FINE)) log.fine("CR - Amt(" + temp + "->" + dr.getAcctBalance() + ") - " + dr.toString()); @@ -214,14 +223,26 @@ public class Doc_MatchInv extends Doc cr.setAmtAcctCr(BigDecimal.ZERO); cr.setAmtSourceCr(BigDecimal.ZERO); } - cr.setQty(getQty().negate()); temp = cr.getAcctBalance(); - // Set AmtAcctCr/Dr from Invoice (sets also Project) - if (as.isAccrual() && !cr.updateReverseLine (MInvoice.Table_ID, // Amt updated - m_invoiceLine.getC_Invoice_ID(), m_invoiceLine.getC_InvoiceLine_ID(), multiplier)) + if (m_matchInv.getReversal_ID() > 0) { - p_Error = "Invoice not posted yet"; - return null; + if (!cr.updateReverseLine (MMatchInv.Table_ID, // Amt updated + m_matchInv.getReversal_ID(), 0, BigDecimal.ONE)) + { + p_Error = "Failed to create reversal entry"; + return null; + } + } + else + { + cr.setQty(getQty().negate()); + // Set AmtAcctCr/Dr from Invoice (sets also Project) + if (!cr.updateReverseLine (MInvoice.Table_ID, // Amt updated + m_invoiceLine.getC_Invoice_ID(), m_invoiceLine.getC_InvoiceLine_ID(), multiplier)) + { + p_Error = "Invoice not posted yet"; + return null; + } } if (log.isLoggable(Level.FINE)) log.fine("DR - Amt(" + temp + "->" + cr.getAcctBalance() + ") - " + cr.toString()); @@ -236,16 +257,31 @@ public class Doc_MatchInv extends Doc invoice.getAD_Client_ID(), invoice.getAD_Org_ID()); cr = fact.createLine (null, expense, as.getC_Currency_ID(), null, LineNetAmt); - cr.setQty(getQty().multiply(multiplier).negate()); + if (m_matchInv.getReversal_ID() > 0) + { + if (!cr.updateReverseLine (MMatchInv.Table_ID, // Amt updated + m_matchInv.getReversal_ID(), 0, BigDecimal.ONE)) + { + p_Error = "Failed to create reversal entry"; + return null; + } + } + else + { + cr.setQty(getQty().multiply(multiplier).negate()); + } + } + if (m_matchInv.getReversal_ID() == 0) + { + cr.setC_Activity_ID(m_invoiceLine.getC_Activity_ID()); + cr.setC_Campaign_ID(m_invoiceLine.getC_Campaign_ID()); + cr.setC_Project_ID(m_invoiceLine.getC_Project_ID()); + cr.setC_ProjectPhase_ID(m_invoiceLine.getC_ProjectPhase_ID()); + cr.setC_ProjectTask_ID(m_invoiceLine.getC_ProjectTask_ID()); + cr.setC_UOM_ID(m_invoiceLine.getC_UOM_ID()); + cr.setUser1_ID(m_invoiceLine.getUser1_ID()); + cr.setUser2_ID(m_invoiceLine.getUser2_ID()); } - cr.setC_Activity_ID(m_invoiceLine.getC_Activity_ID()); - cr.setC_Campaign_ID(m_invoiceLine.getC_Campaign_ID()); - cr.setC_Project_ID(m_invoiceLine.getC_Project_ID()); - cr.setC_ProjectPhase_ID(m_invoiceLine.getC_ProjectPhase_ID()); - cr.setC_ProjectTask_ID(m_invoiceLine.getC_ProjectTask_ID()); - cr.setC_UOM_ID(m_invoiceLine.getC_UOM_ID()); - cr.setUser1_ID(m_invoiceLine.getUser1_ID()); - cr.setUser2_ID(m_invoiceLine.getUser2_ID()); //AZ Goodwill //Desc: Source Not Balanced problem because Currency is Difference - PO=CNY but AP=USD diff --git a/org.adempiere.base/src/org/compiere/acct/Doc_MatchPO.java b/org.adempiere.base/src/org/compiere/acct/Doc_MatchPO.java index f62e68820d..536124971a 100644 --- a/org.adempiere.base/src/org/compiere/acct/Doc_MatchPO.java +++ b/org.adempiere.base/src/org/compiere/acct/Doc_MatchPO.java @@ -71,6 +71,7 @@ public class Doc_MatchPO extends Doc private ProductCost m_pc; private int m_M_AttributeSetInstance_ID = 0; + private MMatchPO m_matchPO; /** * Load Specific Document Details @@ -79,19 +80,19 @@ public class Doc_MatchPO extends Doc protected String loadDocumentDetails () { setC_Currency_ID (Doc.NO_CURRENCY); - MMatchPO matchPO = (MMatchPO)getPO(); - setDateDoc(matchPO.getDateTrx()); + m_matchPO = (MMatchPO)getPO(); + setDateDoc(m_matchPO.getDateTrx()); // - m_M_AttributeSetInstance_ID = matchPO.getM_AttributeSetInstance_ID(); - setQty (matchPO.getQty()); + m_M_AttributeSetInstance_ID = m_matchPO.getM_AttributeSetInstance_ID(); + setQty (m_matchPO.getQty()); // - m_C_OrderLine_ID = matchPO.getC_OrderLine_ID(); + m_C_OrderLine_ID = m_matchPO.getC_OrderLine_ID(); m_oLine = new MOrderLine (getCtx(), m_C_OrderLine_ID, getTrxName()); // - m_M_InOutLine_ID = matchPO.getM_InOutLine_ID(); + m_M_InOutLine_ID = m_matchPO.getM_InOutLine_ID(); m_ioLine = new MInOutLine (getCtx(), m_M_InOutLine_ID, getTrxName()); - m_C_InvoiceLine_ID = matchPO.getC_InvoiceLine_ID(); + m_C_InvoiceLine_ID = m_matchPO.getC_InvoiceLine_ID(); // m_pc = new ProductCost (Env.getCtx(), @@ -191,79 +192,105 @@ public class Doc_MatchPO extends Doc if (MAcctSchema.COSTINGMETHOD_StandardCosting.equals(costingMethod)) { - // No Costs yet - no PPV - if (costs == null || costs.signum() == 0) + if (m_matchPO.getReversal_ID() > 0) { - p_Error = "Resubmit - No Costs for " + product.getName(); - log.log(Level.SEVERE, p_Error); - return null; - } - - // Difference - BigDecimal difference = poCost.subtract(costs); - // Nothing to post - if (difference.signum() == 0) - { - if (log.isLoggable(Level.FINE))log.log(Level.FINE, "No Cost Difference for M_Product_ID=" + getM_Product_ID()); - return facts; - } - - // Product PPV - FactLine cr = fact.createLine(null, - m_pc.getAccount(ProductCost.ACCTTYPE_P_PPV, as), - as.getC_Currency_ID(), isReturnTrx ? difference.negate() : difference); - MAccount acct_cr = null; - if (cr != null) - { - cr.setQty(isReturnTrx ? getQty().negate() : getQty()); - cr.setC_BPartner_ID(m_oLine.getC_BPartner_ID()); - cr.setC_Activity_ID(m_oLine.getC_Activity_ID()); - cr.setC_Campaign_ID(m_oLine.getC_Campaign_ID()); - cr.setC_Project_ID(m_oLine.getC_Project_ID()); - cr.setC_ProjectPhase_ID(m_oLine.getC_ProjectPhase_ID()); - cr.setC_ProjectTask_ID(m_oLine.getC_ProjectTask_ID()); - cr.setC_UOM_ID(m_oLine.getC_UOM_ID()); - cr.setUser1_ID(m_oLine.getUser1_ID()); - cr.setUser2_ID(m_oLine.getUser2_ID()); - acct_cr = cr.getAccount(); // PPV Offset - } - - // PPV Offset - FactLine dr = fact.createLine(null, - getAccount(Doc.ACCTTYPE_PPVOffset, as), - as.getC_Currency_ID(), isReturnTrx ? difference : difference.negate()); - MAccount acct_db = null; - if (dr != null) - { - dr.setQty(isReturnTrx ? getQty() : getQty().negate()); - dr.setC_BPartner_ID(m_oLine.getC_BPartner_ID()); - dr.setC_Activity_ID(m_oLine.getC_Activity_ID()); - dr.setC_Campaign_ID(m_oLine.getC_Campaign_ID()); - dr.setC_Project_ID(m_oLine.getC_Project_ID()); - dr.setC_ProjectPhase_ID(m_oLine.getC_ProjectPhase_ID()); - dr.setC_ProjectTask_ID(m_oLine.getC_ProjectTask_ID()); - dr.setC_UOM_ID(m_oLine.getC_UOM_ID()); - dr.setUser1_ID(m_oLine.getUser1_ID()); - dr.setUser2_ID(m_oLine.getUser2_ID()); - acct_db = dr.getAccount(); // PPV - } - - // Avoid usage of clearing accounts - // If both accounts Purchase Price Variance and Purchase Price Variance Offset are equal - // then remove the posting - - if ((!as.isPostIfClearingEqual()) && acct_db!=null && acct_db.equals(acct_cr) && (!isInterOrg)) { - - BigDecimal debit = dr.getAmtSourceDr(); - BigDecimal credit = cr.getAmtSourceCr(); - - if (debit.compareTo(credit) == 0) { - fact.remove(dr); + // Product PPV + FactLine cr = fact.createLine(null, + m_pc.getAccount(ProductCost.ACCTTYPE_P_PPV, as), + as.getC_Currency_ID(), Env.ONE); + if (!cr.updateReverseLine(MMatchPO.Table_ID, m_matchPO.getM_MatchPO_ID(), 0, Env.ONE)) + { fact.remove(cr); + cr = null; + } + if (cr != null) + { + // PPV Offset + FactLine dr = fact.createLine(null, + getAccount(Doc.ACCTTYPE_PPVOffset, as), as.getC_Currency_ID(), Env.ONE); + if (!dr.updateReverseLine(MMatchPO.Table_ID, m_matchPO.getM_MatchPO_ID(), 0, Env.ONE)) + { + p_Error = "Failed to create reversal entry for ACCTTYPE_PPVOffset"; + return null; + } } - } - // End Avoid usage of clearing accounts + else + { + // No Costs yet - no PPV + if (costs == null || costs.signum() == 0) + { + p_Error = "Resubmit - No Costs for " + product.getName(); + log.log(Level.SEVERE, p_Error); + return null; + } + + // Difference + BigDecimal difference = poCost.subtract(costs); + // Nothing to post + if (difference.signum() == 0) + { + if (log.isLoggable(Level.FINE))log.log(Level.FINE, "No Cost Difference for M_Product_ID=" + getM_Product_ID()); + return facts; + } + + // Product PPV + FactLine cr = fact.createLine(null, + m_pc.getAccount(ProductCost.ACCTTYPE_P_PPV, as), + as.getC_Currency_ID(), isReturnTrx ? difference.negate() : difference); + MAccount acct_cr = null; + if (cr != null) + { + cr.setQty(isReturnTrx ? getQty().negate() : getQty()); + cr.setC_BPartner_ID(m_oLine.getC_BPartner_ID()); + cr.setC_Activity_ID(m_oLine.getC_Activity_ID()); + cr.setC_Campaign_ID(m_oLine.getC_Campaign_ID()); + cr.setC_Project_ID(m_oLine.getC_Project_ID()); + cr.setC_ProjectPhase_ID(m_oLine.getC_ProjectPhase_ID()); + cr.setC_ProjectTask_ID(m_oLine.getC_ProjectTask_ID()); + cr.setC_UOM_ID(m_oLine.getC_UOM_ID()); + cr.setUser1_ID(m_oLine.getUser1_ID()); + cr.setUser2_ID(m_oLine.getUser2_ID()); + acct_cr = cr.getAccount(); // PPV Offset + } + + // PPV Offset + FactLine dr = fact.createLine(null, + getAccount(Doc.ACCTTYPE_PPVOffset, as), + as.getC_Currency_ID(), isReturnTrx ? difference : difference.negate()); + MAccount acct_db = null; + if (dr != null) + { + dr.setQty(isReturnTrx ? getQty() : getQty().negate()); + dr.setC_BPartner_ID(m_oLine.getC_BPartner_ID()); + dr.setC_Activity_ID(m_oLine.getC_Activity_ID()); + dr.setC_Campaign_ID(m_oLine.getC_Campaign_ID()); + dr.setC_Project_ID(m_oLine.getC_Project_ID()); + dr.setC_ProjectPhase_ID(m_oLine.getC_ProjectPhase_ID()); + dr.setC_ProjectTask_ID(m_oLine.getC_ProjectTask_ID()); + dr.setC_UOM_ID(m_oLine.getC_UOM_ID()); + dr.setUser1_ID(m_oLine.getUser1_ID()); + dr.setUser2_ID(m_oLine.getUser2_ID()); + acct_db = dr.getAccount(); // PPV + } + + // Avoid usage of clearing accounts + // If both accounts Purchase Price Variance and Purchase Price Variance Offset are equal + // then remove the posting + + if ((!as.isPostIfClearingEqual()) && acct_db!=null && acct_db.equals(acct_cr) && (!isInterOrg)) { + + BigDecimal debit = dr.getAmtSourceDr(); + BigDecimal credit = cr.getAmtSourceCr(); + + if (debit.compareTo(credit) == 0) { + fact.remove(dr); + fact.remove(cr); + } + + } + // End Avoid usage of clearing accounts + } // facts.add(fact); diff --git a/org.adempiere.base/src/org/compiere/acct/FactLine.java b/org.adempiere.base/src/org/compiere/acct/FactLine.java index f7a50dcba8..5163dc93a7 100644 --- a/org.adempiere.base/src/org/compiere/acct/FactLine.java +++ b/org.adempiere.base/src/org/compiere/acct/FactLine.java @@ -1074,7 +1074,15 @@ public final class FactLine extends X_Fact_Acct StringBuilder sql = new StringBuilder("SELECT * ") .append("FROM Fact_Acct ") .append("WHERE C_AcctSchema_ID=? AND AD_Table_ID=? AND Record_ID=?") - .append(" AND Line_ID=? AND Account_ID=?"); + .append(" AND Account_ID=?"); + if (Line_ID > 0) + { + sql.append(" AND Line_ID=? "); + } + else + { + sql.append(" AND Line_ID IS NULL "); + } // MZ Goodwill // for Inventory Move if (MMovement.Table_ID == AD_Table_ID) @@ -1084,16 +1092,20 @@ public final class FactLine extends X_Fact_Acct ResultSet rs = null; try { + int pindex=1; pstmt = DB.prepareStatement(sql.toString(), get_TrxName()); - pstmt.setInt(1, getC_AcctSchema_ID()); - pstmt.setInt(2, AD_Table_ID); - pstmt.setInt(3, Record_ID); - pstmt.setInt(4, Line_ID); - pstmt.setInt(5, m_acct.getAccount_ID()); + pstmt.setInt(pindex++, getC_AcctSchema_ID()); + pstmt.setInt(pindex++, AD_Table_ID); + pstmt.setInt(pindex++, Record_ID); + pstmt.setInt(pindex++, m_acct.getAccount_ID()); + if (Line_ID > 0) + { + pstmt.setInt(pindex++, Line_ID); + } // MZ Goodwill // for Inventory Move if (MMovement.Table_ID == AD_Table_ID) - pstmt.setInt(6, getM_Locator_ID()); + pstmt.setInt(pindex++, getM_Locator_ID()); // end MZ rs = pstmt.executeQuery(); if (rs.next()) diff --git a/org.adempiere.base/src/org/compiere/model/MCostDetail.java b/org.adempiere.base/src/org/compiere/model/MCostDetail.java index 1d80ca77a7..811368d03f 100644 --- a/org.adempiere.base/src/org/compiere/model/MCostDetail.java +++ b/org.adempiere.base/src/org/compiere/model/MCostDetail.java @@ -1287,59 +1287,15 @@ public class MCostDetail extends X_M_CostDetail history.setNewCostPrice(cost.getCurrentCostPrice()); history.setNewCAmt(cost.getCumulatedAmt()); history.setNewCQty(cost.getCumulatedQty()); - if (!history.save()) - return false; + //save history if there are movement of qty or costprice + if (history.getNewQty().compareTo(history.getOldQty()) != 0 + || history.getNewCostPrice().compareTo(history.getOldCostPrice()) != 0) + { + if (!history.save()) + return false; + } return cost.save(); } // process - // Elaine 2008/6/20 - protected boolean afterDelete (boolean success) - { - if(success) - { - // recalculate MCost - boolean ok = false; - // get costing level for product - MAcctSchema as = new MAcctSchema (getCtx(), getC_AcctSchema_ID(), null); - MProduct product = MProduct.get(getCtx(), getM_Product_ID()); - String CostingLevel = product.getCostingLevel(as); - // Org Element - int Org_ID = getAD_Org_ID(); - int M_ASI_ID = getM_AttributeSetInstance_ID(); - if (MAcctSchema.COSTINGLEVEL_Client.equals(CostingLevel)) - { - Org_ID = 0; - M_ASI_ID = 0; - } - else if (MAcctSchema.COSTINGLEVEL_Organization.equals(CostingLevel)) - M_ASI_ID = 0; - else if (MAcctSchema.COSTINGLEVEL_BatchLot.equals(CostingLevel)) - Org_ID = 0; - - // Create Material Cost elements - if (getM_CostElement_ID() == 0) - { - MCostElement[] ces = MCostElement.getCostingMethods(this); - for (int i = 0; i < ces.length; i++) - { - MCostElement ce = ces[i]; - ok = process (as, product, ce, Org_ID, M_ASI_ID); - if (!ok) - break; - } - } // Material Cost elements - else - { - MCostElement ce = MCostElement.get(getCtx(), getM_CostElement_ID()); - ok = process (as, product, ce, Org_ID, M_ASI_ID); - } - - return ok; - } - - return super.afterDelete(success); - } - // - } // MCostDetail diff --git a/org.adempiere.base/src/org/compiere/model/MInOut.java b/org.adempiere.base/src/org/compiere/model/MInOut.java index 6414e9215b..7f154cb003 100644 --- a/org.adempiere.base/src/org/compiere/model/MInOut.java +++ b/org.adempiere.base/src/org/compiere/model/MInOut.java @@ -2152,29 +2152,8 @@ public class MInOut extends X_M_InOut implements DocAction // Reverse/Delete Matching if (!isSOTrx()) { - if (accrual) - { - if (!reverseMatching(reversalDate)) - return null; - } - else - { - MMatchInv[] mInv = MMatchInv.getInOut(getCtx(), getM_InOut_ID(), get_TrxName()); - for (int i = 0; i < mInv.length; i++) - mInv[i].deleteEx(true); - MMatchPO[] mPO = MMatchPO.getInOut(getCtx(), getM_InOut_ID(), get_TrxName()); - for (int i = 0; i < mPO.length; i++) - { - if (mPO[i].getC_InvoiceLine_ID() == 0) - mPO[i].deleteEx(true); - else - { - mPO[i].setM_InOutLine_ID(0); - mPO[i].saveEx(); - - } - } - } + if (!reverseMatching(reversalDate)) + return null; } // Deep Copy @@ -2262,7 +2241,10 @@ public class MInOut extends X_M_InOut implements DocAction private boolean reverseMatching(Timestamp reversalDate) { MMatchInv[] mInv = MMatchInv.getInOut(getCtx(), getM_InOut_ID(), get_TrxName()); for (MMatchInv mMatchInv : mInv) - { + { + if (mMatchInv.getReversal_ID() > 0) + continue; + String description = mMatchInv.getDescription(); if (description == null || !description.endsWith("<-)")) { @@ -2276,6 +2258,9 @@ public class MInOut extends X_M_InOut implements DocAction MMatchPO[] mMatchPOList = MMatchPO.getInOut(getCtx(), getM_InOut_ID(), get_TrxName()); for (MMatchPO mMatchPO : mMatchPOList) { + if (mMatchPO.getReversal_ID() > 0) + continue; + String description = mMatchPO.getDescription(); if (description == null || !description.endsWith("<-)")) { diff --git a/org.adempiere.base/src/org/compiere/model/MInventory.java b/org.adempiere.base/src/org/compiere/model/MInventory.java index 275fae9c4d..b7bde95275 100644 --- a/org.adempiere.base/src/org/compiere/model/MInventory.java +++ b/org.adempiere.base/src/org/compiere/model/MInventory.java @@ -835,6 +835,8 @@ public class MInventory extends X_M_Inventory implements DocAction rLine.setQtyBook (oLine.getQtyCount()); // switch rLine.setQtyCount (oLine.getQtyBook()); rLine.setQtyInternalUse (oLine.getQtyInternalUse().negate()); + rLine.setNewCostPrice(oLine.getCurrentCostPrice()); + rLine.setCurrentCostPrice(oLine.getNewCostPrice()); rLine.saveEx(); diff --git a/org.adempiere.base/src/org/compiere/model/MInvoice.java b/org.adempiere.base/src/org/compiere/model/MInvoice.java index b410b221ac..6066970004 100644 --- a/org.adempiere.base/src/org/compiere/model/MInvoice.java +++ b/org.adempiere.base/src/org/compiere/model/MInvoice.java @@ -2286,6 +2286,11 @@ public class MInvoice extends X_C_Invoice implements DocAction accrual = true; } + if (!accrual) + { + + } + if (accrual) return reverseAccrualIt(); else @@ -2379,52 +2384,36 @@ public class MInvoice extends X_C_Invoice implements DocAction // Reverse/Delete Matching if (!isSOTrx()) { - if (accrual) + MMatchInv[] mInv = MMatchInv.getInvoice(getCtx(), getC_Invoice_ID(), get_TrxName()); + for (int i = 0; i < mInv.length; i++) { - MMatchInv[] mInv = MMatchInv.getInvoice(getCtx(), getC_Invoice_ID(), get_TrxName()); - for (int i = 0; i < mInv.length; i++) + if (mInv[i].getReversal_ID() > 0) + continue; + + if (!mInv[i].reverse(reversalDate)) { - if (!mInv[i].reverse(reversalDate)) + m_processMsg = "Could not Reverse MatchInv"; + return null; + } + } + MMatchPO[] mPO = MMatchPO.getInvoice(getCtx(), getC_Invoice_ID(), get_TrxName()); + for (int i = 0; i < mPO.length; i++) + { + if (mPO[i].getReversal_ID() > 0) + continue; + + if (mPO[i].getM_InOutLine_ID() == 0) + { + if (!mPO[i].reverse(reversalDate)) { - m_processMsg = "Could not Reverse MatchInv"; + m_processMsg = "Could not Reverse MatchPO"; return null; } } - MMatchPO[] mPO = MMatchPO.getInvoice(getCtx(), getC_Invoice_ID(), get_TrxName()); - for (int i = 0; i < mPO.length; i++) + else { - if (mPO[i].getM_InOutLine_ID() == 0) - { - if (!mPO[i].reverse(reversalDate)) - { - m_processMsg = "Could not Reverse MatchPO"; - return null; - } - } - else - { - mPO[i].setC_InvoiceLine_ID(null); - mPO[i].saveEx(get_TrxName()); - } - } - } - else - { - MMatchInv[] mInv = MMatchInv.getInvoice(getCtx(), getC_Invoice_ID(), get_TrxName()); - for (int i = 0; i < mInv.length; i++) - { - mInv[i].deleteEx(true); - } - MMatchPO[] mPO = MMatchPO.getInvoice(getCtx(), getC_Invoice_ID(), get_TrxName()); - for (int i = 0; i < mPO.length; i++) - { - if (mPO[i].getM_InOutLine_ID() == 0) - mPO[i].deleteEx(true); - else - { - mPO[i].setC_InvoiceLine_ID(null); - mPO[i].saveEx(get_TrxName()); - } + mPO[i].setC_InvoiceLine_ID(null); + mPO[i].saveEx(get_TrxName()); } } } diff --git a/org.adempiere.base/src/org/compiere/model/MMatchInv.java b/org.adempiere.base/src/org/compiere/model/MMatchInv.java index dc1b9319f3..476b53a84d 100644 --- a/org.adempiere.base/src/org/compiere/model/MMatchInv.java +++ b/org.adempiere.base/src/org/compiere/model/MMatchInv.java @@ -24,7 +24,6 @@ import java.util.Properties; import org.compiere.util.CLogger; import org.compiere.util.DB; -import org.compiere.util.Env; /** * Match Invoice (Receipt<>Invoice) Model. @@ -304,35 +303,7 @@ public class MMatchInv extends X_M_MatchInv { // AZ Goodwill deleteMatchInvCostDetail(); - // end AZ - - // delete m_matchinv doesn't auto make m_matchpo invalid - // Get Order and decrease invoices - /* - MInvoiceLine iLine = new MInvoiceLine (getCtx(), getC_InvoiceLine_ID(), get_TrxName()); - int C_OrderLine_ID = iLine.getC_OrderLine_ID(); - if (C_OrderLine_ID == 0) - { - MInOutLine ioLine = new MInOutLine (getCtx(), getM_InOutLine_ID(), get_TrxName()); - C_OrderLine_ID = ioLine.getC_OrderLine_ID(); - } - // No Order Found - if (C_OrderLine_ID == 0) - return success; - // Find MatchPO - MMatchPO[] mPO = MMatchPO.get(getCtx(), C_OrderLine_ID, - getC_InvoiceLine_ID(), get_TrxName()); - for (int i = 0; i < mPO.length; i++) - { - if (mPO[i].getM_InOutLine_ID() == 0) - mPO[i].delete(true); - else - { - mPO[i].setC_InvoiceLine_ID(null); - mPO[i].saveEx(); - } - } - */ + // end AZ } return success; } // afterDelete @@ -352,43 +323,11 @@ public class MMatchInv extends X_M_MatchInv continue; } - // update/delete Cost Detail and recalculate Current Cost - MCostDetail cd = MCostDetail.get (getCtx(), "C_InvoiceLine_ID=?", - getC_InvoiceLine_ID(), getM_AttributeSetInstance_ID(), as.getC_AcctSchema_ID(), get_TrxName()); + MCostDetail cd = MCostDetail.get (getCtx(), "M_MatchInv_ID=?", + getM_MatchInv_ID(), getM_AttributeSetInstance_ID(), as.getC_AcctSchema_ID(), get_TrxName()); if (cd != null) { - if (cd.isProcessed()) - { - MInOut receipt = (new MInOutLine(getCtx(),getM_InOutLine_ID(),get_TrxName())).getParent(); - BigDecimal qty = getQty(); - if (receipt.getMovementType().equals(MInOut.MOVEMENTTYPE_VendorReturns)) - qty = getQty().negate(); - // - BigDecimal price = null; - if (cd.getQty().compareTo(Env.ZERO) == 0) // avoid division by zero - price = Env.ZERO; - else - price = cd.getAmt().divide(cd.getQty(),12,BigDecimal.ROUND_HALF_UP); - cd.setDeltaAmt(price.multiply(qty.negate())); - cd.setDeltaQty(qty.negate()); - cd.setProcessed(false); - // - cd.setAmt(price.multiply(cd.getQty().subtract(qty))); - cd.setQty(cd.getQty().subtract(qty)); - if (!cd.isProcessed()) - { - cd.process(); - } - if (cd.getQty().compareTo(Env.ZERO) == 0) - { - cd.setProcessed(false); - cd.delete(true); - } - } - else - { - cd.delete(true); - } + cd.deleteEx(true); } } diff --git a/org.adempiere.base/src/org/compiere/model/MMatchPO.java b/org.adempiere.base/src/org/compiere/model/MMatchPO.java index 8bc2e6e388..7c25af7b5d 100644 --- a/org.adempiere.base/src/org/compiere/model/MMatchPO.java +++ b/org.adempiere.base/src/org/compiere/model/MMatchPO.java @@ -1032,10 +1032,6 @@ public class MMatchPO extends X_M_MatchPO // (Reserved in VMatch and MInOut.completeIt) if (success && getC_OrderLine_ID() != 0) { - // AZ Goodwill - deleteMatchPOCostDetail(); - // end AZ - MOrderLine orderLine = new MOrderLine (getCtx(), getC_OrderLine_ID(), get_TrxName()); if (getM_InOutLine_ID() != 0) orderLine.setQtyDelivered(orderLine.getQtyDelivered().subtract(getQty())); @@ -1136,58 +1132,6 @@ public class MMatchPO extends X_M_MatchPO if (s_log.isLoggable(Level.INFO)) s_log.info("Success #" + success + " - Error #" + errors); } // consolidate - //AZ Goodwill - private String deleteMatchPOCostDetail() - { - // Get Account Schemas to delete MCostDetail - MAcctSchema[] acctschemas = MAcctSchema.getClientAcctSchema(getCtx(), getAD_Client_ID()); - for(int asn = 0; asn < acctschemas.length; asn++) - { - MAcctSchema as = acctschemas[asn]; - - if (as.isSkipOrg(getAD_Org_ID())) - { - continue; - } - - // update/delete Cost Detail and recalculate Current Cost - MCostDetail cd = MCostDetail.get (getCtx(), "C_OrderLine_ID=?", - getC_OrderLine_ID(), getM_AttributeSetInstance_ID(), as.getC_AcctSchema_ID(), get_TrxName()); - if (cd != null) - { - if (cd.isProcessed()) - { - if (cd.getQty().compareTo(Env.ZERO) > 0) - { - BigDecimal price = cd.getAmt().divide(cd.getQty(),12,BigDecimal.ROUND_HALF_UP); - cd.setDeltaAmt(price.multiply(getQty().negate())); - cd.setDeltaQty(getQty().negate()); - cd.setProcessed(false); - // - cd.setAmt(price.multiply(cd.getQty().subtract(getQty()))); - cd.setQty(cd.getQty().subtract(getQty())); - if (!cd.isProcessed()) - { - cd.process(); - } - } - //after process clean-up - if (cd.getQty().compareTo(Env.ZERO) == 0) - { - cd.setProcessed(false); - cd.delete(true); - } - } - else - { - cd.delete(true); - } - } - } - - return ""; - } - /** * Reverse MatchPO. * @param reversalDate