From d52ef4b32e7a99a45f9ee4349d9a367636bf7f60 Mon Sep 17 00:00:00 2001 From: Carlos Ruiz Date: Thu, 10 Apr 2008 01:08:02 +0000 Subject: [PATCH] Fix [ 1938912 ] Problem detected with clearing account and interorg posting https://sourceforge.net/tracker/?func=detail&atid=879332&aid=1938912&group_id=176962 --- .../src/org/compiere/acct/Doc_Allocation.java | 64 ++++++++++++++++++- base/src/org/compiere/acct/Doc_Bank.java | 30 +++++++++ base/src/org/compiere/acct/Doc_MatchInv.java | 20 +++++- 3 files changed, 110 insertions(+), 4 deletions(-) diff --git a/base/src/org/compiere/acct/Doc_Allocation.java b/base/src/org/compiere/acct/Doc_Allocation.java index 6bbabb48f3..311bba9d2a 100644 --- a/base/src/org/compiere/acct/Doc_Allocation.java +++ b/base/src/org/compiere/acct/Doc_Allocation.java @@ -24,6 +24,7 @@ import java.util.logging.Level; import org.compiere.model.MAccount; import org.compiere.model.MAcctSchema; +import org.compiere.model.MAcctSchemaElement; import org.compiere.model.MAllocationHdr; import org.compiere.model.MAllocationLine; import org.compiere.model.MCashLine; @@ -31,6 +32,7 @@ import org.compiere.model.MConversionRate; import org.compiere.model.MFactAcct; import org.compiere.model.MInvoice; import org.compiere.model.MInvoiceLine; +import org.compiere.model.MOrder; import org.compiere.model.MPayment; import org.compiere.util.CLogger; import org.compiere.util.DB; @@ -173,6 +175,7 @@ public class Doc_Allocation extends Doc // create Fact Header Fact fact = new Fact(this, as, Fact.POST_Actual); Fact factForRGL = new Fact(this, as, Fact.POST_Actual); // dummy fact (not posted) to calculate Realized Gain & Loss + boolean isInterOrg = isInterOrg(as); for (int i = 0; i < p_lines.length; i++) { @@ -239,7 +242,7 @@ public class Doc_Allocation extends Doc acct_unallocated_cash = getCashAcct(as, line.getC_CashLine_ID()); MAccount acct_receivable = getAccount(Doc.ACCTTYPE_C_Receivable, as); - if ((!as.isPostIfClearingEqual()) && acct_unallocated_cash != null && acct_unallocated_cash.equals(acct_receivable)) { + if ((!as.isPostIfClearingEqual()) && acct_unallocated_cash != null && acct_unallocated_cash.equals(acct_receivable) && (!isInterOrg)) { // if not using clearing accounts, then don't post amtsource // change the allocationsource to be writeoff + discount @@ -329,7 +332,7 @@ public class Doc_Allocation extends Doc // Save original allocation source for realized gain & loss purposes allocationSourceForRGL = allocationSourceForRGL.negate(); - if ((!as.isPostIfClearingEqual()) && acct_payment_select != null && acct_payment_select.equals(acct_liability)) { + if ((!as.isPostIfClearingEqual()) && acct_payment_select != null && acct_payment_select.equals(acct_liability) && (!isInterOrg)) { // if not using clearing accounts, then don't post amtsource // change the allocationsource to be writeoff + discount @@ -433,7 +436,7 @@ public class Doc_Allocation extends Doc } // for all lines // FR [ 1840016 ] Avoid usage of clearing accounts - subject to C_AcctSchema.IsPostIfClearingEqual - if ( ( ! as.isPostIfClearingEqual() ) && p_lines.length > 0) { + if ( (!as.isPostIfClearingEqual()) && p_lines.length > 0 && (!isInterOrg)) { boolean allEquals = true; // more than one line (i.e. crossing one payment+ with a payment-, or an invoice against a credit memo) // verify if the sum of all facts is zero net @@ -464,6 +467,61 @@ public class Doc_Allocation extends Doc return m_facts; } // createFact + /** Verify if the posting involves two or more organizations + @return true if there are more than one org involved on the posting + */ + private boolean isInterOrg(MAcctSchema as) { + MAcctSchemaElement elementorg = as.getAcctSchemaElement(MAcctSchemaElement.ELEMENTTYPE_Organization); + if (elementorg == null || !elementorg.isBalanced()) { + // no org element or not need to be balanced + return false; + } + + if (p_lines.length <= 0) { + // no lines + return false; + } + + int startorg = p_lines[0].getAD_Org_ID(); + // validate if the allocation involves more than one org + for (int i = 0; i < p_lines.length; i++) { + DocLine_Allocation line = (DocLine_Allocation)p_lines[i]; + int orgpayment = startorg; + MPayment payment = null; + if (line.getC_Payment_ID() != 0) { + payment = new MPayment (getCtx(), line.getC_Payment_ID(), getTrxName()); + orgpayment = payment.getAD_Org_ID(); + } + int orginvoice = startorg; + MInvoice invoice = null; + if (line.getC_Invoice_ID() != 0) { + invoice = new MInvoice (getCtx(), line.getC_Invoice_ID(), getTrxName()); + orginvoice = invoice.getAD_Org_ID(); + } + int orgcashline = startorg; + MCashLine cashline = null; + if (line.getC_CashLine_ID() != 0) { + cashline = new MCashLine (getCtx(), line.getC_CashLine_ID(), getTrxName()); + orgcashline = cashline.getAD_Org_ID(); + } + int orgorder = startorg; + MOrder order = null; + if (line.getC_Order_ID() != 0) { + order = new MOrder (getCtx(), line.getC_Order_ID(), getTrxName()); + orgorder = order.getAD_Org_ID(); + } + + if ( line.getAD_Org_ID() != startorg + || orgpayment != startorg + || orginvoice != startorg + || orgcashline != startorg + || orgorder != startorg) + return true; + } + + return false; + } + /** * Compare the dimension ID's from two factlines * @param allEquals diff --git a/base/src/org/compiere/acct/Doc_Bank.java b/base/src/org/compiere/acct/Doc_Bank.java index 337818423a..31e2e8c97f 100644 --- a/base/src/org/compiere/acct/Doc_Bank.java +++ b/base/src/org/compiere/acct/Doc_Bank.java @@ -151,6 +151,7 @@ public class Doc_Bank extends Doc { // create Fact Header Fact fact = new Fact(this, as, Fact.POST_Actual); + // boolean isInterOrg = isInterOrg(as); // Header -- there may be different currency amounts @@ -169,6 +170,8 @@ public class Doc_Bank extends Doc MAccount acct_bank_asset = getAccount(Doc.ACCTTYPE_BankAsset, as); MAccount acct_bank_in_transit = getAccount(Doc.ACCTTYPE_BankInTransit, as); + // if ((!as.isPostIfClearingEqual()) && acct_bank_asset.equals(acct_bank_in_transit) && (!isInterOrg)) { + // don't validate interorg on banks for this - normally banks are balanced by orgs if ((!as.isPostIfClearingEqual()) && acct_bank_asset.equals(acct_bank_in_transit)) { // Not using clearing accounts // just post the difference (if any) @@ -250,6 +253,33 @@ public class Doc_Bank extends Doc return facts; } // createFact + /** Verify if the posting involves two or more organizations + @return true if there are more than one org involved on the posting + private boolean isInterOrg(MAcctSchema as) { + MAcctSchemaElement elementorg = as.getAcctSchemaElement(MAcctSchemaElement.ELEMENTTYPE_Organization); + if (elementorg == null || !elementorg.isBalanced()) { + // no org element or not need to be balanced + return false; + } + + if (p_lines.length <= 0) { + // no lines + return false; + } + + int startorg = getBank_Org_ID(); + if (startorg == 0) + startorg = p_lines[0].getAD_Org_ID(); + // validate if the allocation involves more than one org + for (int i = 0; i < p_lines.length; i++) { + if (p_lines[i].getAD_Org_ID() != startorg) + return true; + } + + return false; + } + */ + /** * Get AD_Org_ID from Bank Account * @return AD_Org_ID or 0 diff --git a/base/src/org/compiere/acct/Doc_MatchInv.java b/base/src/org/compiere/acct/Doc_MatchInv.java index ede4e12569..aadb96dab8 100644 --- a/base/src/org/compiere/acct/Doc_MatchInv.java +++ b/base/src/org/compiere/acct/Doc_MatchInv.java @@ -129,6 +129,7 @@ public class Doc_MatchInv extends Doc // create Fact Header Fact fact = new Fact(this, as, Fact.POST_Actual); setC_Currency_ID (as.getC_Currency_ID()); + boolean isInterOrg = isInterOrg(as); /** Needs to be handeled in PO Matching as no Receipt info if (m_pc.isService()) @@ -232,7 +233,7 @@ public class Doc_MatchInv extends Doc MAccount acct_db = dr.getAccount(); // not_invoiced_receipts MAccount acct_cr = cr.getAccount(); // inventory_clearing - if ((!as.isPostIfClearingEqual()) && acct_db.equals(acct_cr)) { + if ((!as.isPostIfClearingEqual()) && acct_db.equals(acct_cr) && (!isInterOrg)) { BigDecimal debit = dr.getAmtSourceDr(); BigDecimal credit = cr.getAmtSourceCr(); @@ -328,6 +329,23 @@ public class Doc_MatchInv extends Doc return facts; } // createFact + /** Verify if the posting involves two or more organizations + @return true if there are more than one org involved on the posting + */ + private boolean isInterOrg(MAcctSchema as) { + MAcctSchemaElement elementorg = as.getAcctSchemaElement(MAcctSchemaElement.ELEMENTTYPE_Organization); + if (elementorg == null || !elementorg.isBalanced()) { + // no org element or not need to be balanced + return false; + } + + // verify if org of receipt line is different from org of invoice line + if (m_receiptLine != null && m_invoiceLine != null && m_receiptLine.getAD_Org_ID() != m_invoiceLine.getAD_Org_ID()) + return true; + + return false; + } + /** * Update Product Info (old). * - Costing (CostStandardCumQty, CostStandardCumAmt, CostAverageCumQty, CostAverageCumAmt)