/// <summary> /// Create Facts (the accounting logic) for /// MXI. /// (single line) /// <pre> /// NotInvoicedReceipts DR (Receipt Org) /// InventoryClearing CR /// InvoicePV DR CR (difference) /// Commitment /// Expense CR /// Offset DR /// </pre> /// </summary> /// <param name="as1"></param> /// <returns></returns> public override List <Fact> CreateFacts(MAcctSchema as1) { List <Fact> facts = new List <Fact>(); // Nothing to do if (GetM_Product_ID() == 0 || // no Product Env.Signum(GetQty().Value) == 0 || Env.Signum(_receiptLine.GetMovementQty()) == 0) // Qty = 0 { log.Fine("No Product/Qty - M_Product_ID=" + GetM_Product_ID() + ",Qty=" + GetQty() + ",InOutQty=" + _receiptLine.GetMovementQty()); return(facts); } MMatchInv matchInv = (MMatchInv)GetPO(); // create Fact Header Fact fact = new Fact(this, as1, Fact.POST_Actual); SetC_Currency_ID(as1.GetC_Currency_ID()); /** Needs to be handeled in PO Matching as1 no Receipt info * if (_pc.isService()) * { * log.Fine("Service - skipped"); * return fact; * } **/ // NotInvoicedReceipt DR // From Receipt Decimal multiplier = Math.Abs(Decimal.Round(Decimal.Divide(GetQty().Value, _receiptLine.GetMovementQty()), 12, MidpointRounding.AwayFromZero)); FactLine dr = fact.CreateLine(null, GetAccount(Doc.ACCTTYPE_NotInvoicedReceipts, as1), as1.GetC_Currency_ID(), Env.ONE, null); // updated below if (dr == null) { _error = "No Product Costs"; return(null); } dr.SetQty(GetQty()); // dr.setM_Locator_ID(_receiptLine.getM_Locator_ID()); // MInOut receipt = _receiptLine.getParent(); // dr.setLocationFromBPartner(receipt.getC_BPartner_Location_ID(), true); // from Loc // dr.setLocationFromLocator(_receiptLine.getM_Locator_ID(), false); // to Loc Decimal temp = dr.GetAcctBalance(); // Set AmtAcctCr/Dr from Receipt (sets also Project) if (!dr.UpdateReverseLine(MInOut.Table_ID, // Amt updated _receiptLine.GetM_InOut_ID(), _receiptLine.GetM_InOutLine_ID(), multiplier)) { _error = "Mat.Receipt not posted yet"; return(null); } log.Fine("CR - Amt(" + temp + "->" + dr.GetAcctBalance() + ") - " + dr.ToString()); // InventoryClearing CR // From Invoice MAccount expense = _pc.GetAccount(ProductCost.ACCTTYPE_P_InventoryClearing, as1); if (_pc.IsService()) { expense = _pc.GetAccount(ProductCost.ACCTTYPE_P_Expense, as1); } Decimal LineNetAmt = _invoiceLine.GetLineNetAmt(); multiplier = Math.Abs(Decimal.Round(Decimal.Divide(GetQty().Value, _invoiceLine.GetQtyInvoiced()), 12, MidpointRounding.AwayFromZero)); if (multiplier.CompareTo(Env.ONE) != 0) { LineNetAmt = Decimal.Multiply(LineNetAmt, multiplier); } if (_pc.IsService()) { LineNetAmt = dr.GetAcctBalance(); // book out exact receipt amt } FactLine cr = null; if (as1.IsAccrual()) { cr = fact.CreateLine(null, expense, as1.GetC_Currency_ID(), null, LineNetAmt); // updated below if (cr == null) { log.Fine("Line Net Amt=0 - M_Product_ID=" + GetM_Product_ID() + ",Qty=" + GetQty() + ",InOutQty=" + _receiptLine.GetMovementQty()); facts.Add(fact); return(facts); } cr.SetQty(Decimal.Negate(GetQty().Value)); temp = cr.GetAcctBalance(); // Set AmtAcctCr/Dr from Invoice (sets also Project) if (as1.IsAccrual() && !cr.UpdateReverseLine(MInvoice.Table_ID, // Amt updated _invoiceLine.GetC_Invoice_ID(), _invoiceLine.GetC_InvoiceLine_ID(), multiplier)) { _error = "Invoice not posted yet"; return(null); } log.Fine("DR - Amt(" + temp + "->" + cr.GetAcctBalance() + ") - " + cr.ToString()); } else // Cash Acct { MInvoice invoice = _invoiceLine.GetParent(); if (as1.GetC_Currency_ID() == invoice.GetC_Currency_ID()) { LineNetAmt = MConversionRate.Convert(GetCtx(), LineNetAmt, invoice.GetC_Currency_ID(), as1.GetC_Currency_ID(), invoice.GetDateAcct(), invoice.GetC_ConversionType_ID(), invoice.GetAD_Client_ID(), invoice.GetAD_Org_ID()); } cr = fact.CreateLine(null, expense, as1.GetC_Currency_ID(), null, LineNetAmt); cr.SetQty(Decimal.Negate(Decimal.Multiply(GetQty().Value, multiplier))); } cr.SetC_Activity_ID(_invoiceLine.GetC_Activity_ID()); cr.SetC_Campaign_ID(_invoiceLine.GetC_Campaign_ID()); cr.SetC_Project_ID(_invoiceLine.GetC_Project_ID()); cr.SetC_UOM_ID(_invoiceLine.GetC_UOM_ID()); cr.SetUser1_ID(_invoiceLine.GetUser1_ID()); cr.SetUser2_ID(_invoiceLine.GetUser2_ID()); // Invoice Price Variance difference Decimal ipv = Decimal.Negate(Decimal.Add(cr.GetAcctBalance(), dr.GetAcctBalance())); if (Env.Signum(ipv) != 0) { FactLine pv = fact.CreateLine(null, _pc.GetAccount(ProductCost.ACCTTYPE_P_IPV, as1), as1.GetC_Currency_ID(), ipv); pv.SetC_Activity_ID(_invoiceLine.GetC_Activity_ID()); pv.SetC_Campaign_ID(_invoiceLine.GetC_Campaign_ID()); pv.SetC_Project_ID(_invoiceLine.GetC_Project_ID()); pv.SetC_UOM_ID(_invoiceLine.GetC_UOM_ID()); pv.SetUser1_ID(_invoiceLine.GetUser1_ID()); pv.SetUser2_ID(_invoiceLine.GetUser2_ID()); } log.Fine("IPV=" + ipv + "; Balance=" + fact.GetSourceBalance()); MInOut inOut = _receiptLine.GetParent(); bool isReturnTrx = inOut.IsReturnTrx(); if (!IsPosted()) { // Cost Detail Record - data from Expense/IncClearing (CR) record MCostDetail.CreateInvoice(as1, GetAD_Org_ID(), GetM_Product_ID(), matchInv.GetM_AttributeSetInstance_ID(), _invoiceLine.GetC_InvoiceLine_ID(), 0, // No cost element Decimal.Negate(cr.GetAcctBalance()), isReturnTrx ? Decimal.Negate(Utility.Util.GetValueOfDecimal(GetQty())) : Utility.Util.GetValueOfDecimal(GetQty()), // correcting GetDescription(), GetTrx(), GetRectifyingProcess()); // Update Costing UpdateProductInfo(as1.GetC_AcctSchema_ID(), MAcctSchema.COSTINGMETHOD_StandardCosting.Equals(as1.GetCostingMethod())); } // facts.Add(fact); /** Commitment release ****/ if (as1.IsAccrual() && as1.IsCreateCommitment()) { fact = Doc_Order.GetCommitmentRelease(as1, this, Utility.Util.GetValueOfDecimal(GetQty()), _invoiceLine.GetC_InvoiceLine_ID(), Env.ONE); if (fact == null) { return(null); } facts.Add(fact); } // Commitment return(facts); }
/// <summary> /// Create Facts (the accounting logic) for /// ARI, ARC, ARF, API, APC. /// <pre> /// ARI, ARF /// Receivables DR /// Charge CR /// TaxDue CR ///Revenue CR /// ARC /// Receivables CR /// Charge DR /// TaxDue DR /// Revenue RR /// /// API /// Payables CR /// Charge DR /// TaxCredit DR /// Expense DR /// /// APC /// Payables DR /// Charge CR /// TaxCredit CR /// Expense CR /// </pre> /// </summary> /// <param name="as1">accounting schema</param> /// <returns> Fact</returns> public override List <Fact> CreateFacts(MAcctSchema as1) { // List <Fact> facts = new List <Fact>(); // create Fact Header Fact fact = new Fact(this, as1, Fact.POST_Actual); // Cash based accounting if (!as1.IsAccrual()) { return(facts); } // ** ARI, ARF if (GetDocumentType().Equals(MDocBaseType.DOCBASETYPE_ARINVOICE) || GetDocumentType().Equals(MDocBaseType.DOCBASETYPE_ARPROFORMAINVOICE)) { Decimal grossAmt = GetAmount(Doc.AMTTYPE_Gross).Value; Decimal serviceAmt = Env.ZERO; // Header Charge CR Decimal amt = GetAmount(Doc.AMTTYPE_Charge).Value; if (Env.Signum(amt) != 0) { fact.CreateLine(null, GetAccount(Doc.ACCTTYPE_Charge, as1), GetC_Currency_ID(), null, amt); } // TaxDue CR for (int i = 0; i < _taxes.Length; i++) { amt = _taxes[i].GetAmount(); if (Env.Signum(amt) != 0) { FactLine tl = fact.CreateLine(null, _taxes[i].GetAccount(DocTax.ACCTTYPE_TaxDue, as1), GetC_Currency_ID(), null, amt); if (tl != null) { tl.SetC_Tax_ID(_taxes[i].GetC_Tax_ID()); } } } // Revenue CR for (int i = 0; i < _lines.Length; i++) { amt = _lines[i].GetAmtSource(); Decimal?dAmt = null; if (as1.IsTradeDiscountPosted()) { Decimal discount = _lines[i].GetDiscount(); if (Env.Signum(discount) != 0) { amt = Decimal.Add(amt, discount); dAmt = discount; } } fact.CreateLine(_lines[i], _lines[i].GetAccount(ProductCost.ACCTTYPE_P_Revenue, as1), GetC_Currency_ID(), dAmt, amt); if (!_lines[i].IsItem()) { grossAmt = Decimal.Subtract(grossAmt, amt); serviceAmt = Decimal.Add(serviceAmt, amt); } } // Set Locations FactLine[] fLines = fact.GetLines(); for (int i = 0; i < fLines.Length; i++) { if (fLines[i] != null) { fLines[i].SetLocationFromOrg(fLines[i].GetAD_Org_ID(), true); // from Loc fLines[i].SetLocationFromBPartner(GetC_BPartner_Location_ID(), false); // to Loc } } // Receivables DR int receivables_ID = GetValidCombination_ID(Doc.ACCTTYPE_C_Receivable, as1); int receivablesServices_ID = GetValidCombination_ID(Doc.ACCTTYPE_C_Receivable_Services, as1); if (_allLinesItem || !as1.IsPostServices() || receivables_ID == receivablesServices_ID) { grossAmt = GetAmount(Doc.AMTTYPE_Gross).Value; serviceAmt = Env.ZERO; } else if (_allLinesService) { serviceAmt = GetAmount(Doc.AMTTYPE_Gross).Value; grossAmt = Env.ZERO; } if (Env.Signum(grossAmt) != 0) { fact.CreateLine(null, MAccount.Get(GetCtx(), receivables_ID), GetC_Currency_ID(), grossAmt, null); } if (Env.Signum(serviceAmt) != 0) { fact.CreateLine(null, MAccount.Get(GetCtx(), receivablesServices_ID), GetC_Currency_ID(), serviceAmt, null); } } // ARC else if (GetDocumentType().Equals(MDocBaseType.DOCBASETYPE_ARCREDITMEMO)) { Decimal grossAmt = GetAmount(Doc.AMTTYPE_Gross).Value; Decimal serviceAmt = Env.ZERO; // Header Charge DR Decimal amt = GetAmount(Doc.AMTTYPE_Charge).Value; if (Env.Signum(amt) != 0) { fact.CreateLine(null, GetAccount(Doc.ACCTTYPE_Charge, as1), GetC_Currency_ID(), amt, null); } // TaxDue DR for (int i = 0; i < _taxes.Length; i++) { amt = _taxes[i].GetAmount(); if (Env.Signum(amt) != 0) { FactLine tl = fact.CreateLine(null, _taxes[i].GetAccount(DocTax.ACCTTYPE_TaxDue, as1), GetC_Currency_ID(), amt, null); if (tl != null) { tl.SetC_Tax_ID(_taxes[i].GetC_Tax_ID()); } } } // Revenue CR for (int i = 0; i < _lines.Length; i++) { amt = _lines[i].GetAmtSource(); Decimal?dAmt = null; if (as1.IsTradeDiscountPosted()) { Decimal discount = _lines[i].GetDiscount(); if (Env.Signum(discount) != 0) { amt = Decimal.Add(amt, discount); dAmt = discount; } } fact.CreateLine(_lines[i], _lines[i].GetAccount(ProductCost.ACCTTYPE_P_Revenue, as1), GetC_Currency_ID(), amt, dAmt); if (!_lines[i].IsItem()) { grossAmt = Decimal.Subtract(grossAmt, amt); serviceAmt = Decimal.Add(serviceAmt, amt); } } // Set Locations FactLine[] fLines = fact.GetLines(); for (int i = 0; i < fLines.Length; i++) { if (fLines[i] != null) { fLines[i].SetLocationFromOrg(fLines[i].GetAD_Org_ID(), true); // from Loc fLines[i].SetLocationFromBPartner(GetC_BPartner_Location_ID(), false); // to Loc } } // Receivables CR int receivables_ID = GetValidCombination_ID(Doc.ACCTTYPE_C_Receivable, as1); int receivablesServices_ID = GetValidCombination_ID(Doc.ACCTTYPE_C_Receivable_Services, as1); if (_allLinesItem || !as1.IsPostServices() || receivables_ID == receivablesServices_ID) { grossAmt = GetAmount(Doc.AMTTYPE_Gross).Value; serviceAmt = Env.ZERO; } else if (_allLinesService) { serviceAmt = GetAmount(Doc.AMTTYPE_Gross).Value; grossAmt = Env.ZERO; } if (Env.Signum(grossAmt) != 0) { fact.CreateLine(null, MAccount.Get(GetCtx(), receivables_ID), GetC_Currency_ID(), null, grossAmt); } if (Env.Signum(serviceAmt) != 0) { fact.CreateLine(null, MAccount.Get(GetCtx(), receivablesServices_ID), GetC_Currency_ID(), null, serviceAmt); } } // ** API else if (GetDocumentType().Equals(MDocBaseType.DOCBASETYPE_APINVOICE)) { Decimal grossAmt = GetAmount(Doc.AMTTYPE_Gross).Value; Decimal serviceAmt = Env.ZERO; // Charge DR fact.CreateLine(null, GetAccount(Doc.ACCTTYPE_Charge, as1), GetC_Currency_ID(), GetAmount(Doc.AMTTYPE_Charge), null); // TaxCredit DR for (int i = 0; i < _taxes.Length; i++) { FactLine tl = fact.CreateLine(null, _taxes[i].GetAccount(_taxes[i].GetAPTaxType(), as1), GetC_Currency_ID(), _taxes[i].GetAmount(), null); if (tl != null) { tl.SetC_Tax_ID(_taxes[i].GetC_Tax_ID()); } } // Expense DR for (int i = 0; i < _lines.Length; i++) { DocLine line = _lines[i]; bool landedCost = LandedCost(as1, fact, line, true); if (landedCost && as1.IsExplicitCostAdjustment()) { fact.CreateLine(line, line.GetAccount(ProductCost.ACCTTYPE_P_Expense, as1), GetC_Currency_ID(), line.GetAmtSource(), null); // FactLine fl = fact.CreateLine(line, line.GetAccount(ProductCost.ACCTTYPE_P_Expense, as1), GetC_Currency_ID(), null, line.GetAmtSource()); String desc = line.GetDescription(); if (desc == null) { desc = "100%"; } else { desc += " 100%"; } fl.SetDescription(desc); } if (!landedCost) { MAccount expense = line.GetAccount(ProductCost.ACCTTYPE_P_Expense, as1); if (line.IsItem()) { expense = line.GetAccount(ProductCost.ACCTTYPE_P_InventoryClearing, as1); } Decimal amt = line.GetAmtSource(); Decimal?dAmt = null; if (as1.IsTradeDiscountPosted() && !line.IsItem()) { Decimal discount = line.GetDiscount(); if (Env.Signum(discount) != 0) { amt = Decimal.Add(amt, discount); dAmt = discount; } } fact.CreateLine(line, expense, GetC_Currency_ID(), amt, dAmt); if (!line.IsItem()) { grossAmt = Decimal.Subtract(grossAmt, amt); serviceAmt = Decimal.Add(serviceAmt, amt); } // if (line.GetM_Product_ID() != 0 && line.GetProduct().IsService()) // otherwise Inv Matching { if (!IsPosted()) { MCostDetail.CreateInvoice(as1, line.GetAD_Org_ID(), line.GetM_Product_ID(), line.GetM_AttributeSetInstance_ID(), line.Get_ID(), 0, // No Cost Element line.GetAmtSource(), line.GetQty().Value, line.GetDescription(), GetTrx(), GetRectifyingProcess()); } } } } // Set Locations FactLine[] fLines = fact.GetLines(); for (int i = 0; i < fLines.Length; i++) { if (fLines[i] != null) { fLines[i].SetLocationFromBPartner(GetC_BPartner_Location_ID(), true); // from Loc fLines[i].SetLocationFromOrg(fLines[i].GetAD_Org_ID(), false); // to Loc } } // Liability CR int payables_ID = GetValidCombination_ID(Doc.ACCTTYPE_V_Liability, as1); int payablesServices_ID = GetValidCombination_ID(Doc.ACCTTYPE_V_Liability_Services, as1); if (_allLinesItem || !as1.IsPostServices() || payables_ID == payablesServices_ID) { grossAmt = GetAmount(Doc.AMTTYPE_Gross).Value; serviceAmt = Env.ZERO; } else if (_allLinesService) { serviceAmt = GetAmount(Doc.AMTTYPE_Gross).Value; grossAmt = Env.ZERO; } if (Env.Signum(grossAmt) != 0) { fact.CreateLine(null, MAccount.Get(GetCtx(), payables_ID), GetC_Currency_ID(), null, grossAmt); } if (Env.Signum(serviceAmt) != 0) { fact.CreateLine(null, MAccount.Get(GetCtx(), payablesServices_ID), GetC_Currency_ID(), null, serviceAmt); } // UpdateProductPO(as1); // Only API } // APC else if (GetDocumentType().Equals(MDocBaseType.DOCBASETYPE_APCREDITMEMO)) { Decimal grossAmt = GetAmount(Doc.AMTTYPE_Gross).Value; Decimal serviceAmt = Env.ZERO; // Charge CR fact.CreateLine(null, GetAccount(Doc.ACCTTYPE_Charge, as1), GetC_Currency_ID(), null, GetAmount(Doc.AMTTYPE_Charge)); // TaxCredit CR for (int i = 0; i < _taxes.Length; i++) { FactLine tl = fact.CreateLine(null, _taxes[i].GetAccount(_taxes[i].GetAPTaxType(), as1), GetC_Currency_ID(), null, _taxes[i].GetAmount()); if (tl != null) { tl.SetC_Tax_ID(_taxes[i].GetC_Tax_ID()); } } // Expense CR for (int i = 0; i < _lines.Length; i++) { DocLine line = _lines[i]; bool landedCost = LandedCost(as1, fact, line, false); if (landedCost && as1.IsExplicitCostAdjustment()) { fact.CreateLine(line, line.GetAccount(ProductCost.ACCTTYPE_P_Expense, as1), GetC_Currency_ID(), null, line.GetAmtSource()); // FactLine fl = fact.CreateLine(line, line.GetAccount(ProductCost.ACCTTYPE_P_Expense, as1), GetC_Currency_ID(), line.GetAmtSource(), null); String desc = line.GetDescription(); if (desc == null) { desc = "100%"; } else { desc += " 100%"; } fl.SetDescription(desc); } if (!landedCost) { MAccount expense = line.GetAccount(ProductCost.ACCTTYPE_P_Expense, as1); if (line.IsItem()) { expense = line.GetAccount(ProductCost.ACCTTYPE_P_InventoryClearing, as1); } Decimal amt = line.GetAmtSource(); Decimal?dAmt = null; if (as1.IsTradeDiscountPosted() && !line.IsItem()) { Decimal discount = line.GetDiscount(); if (Env.Signum(discount) != 0) { amt = Decimal.Add(amt, discount); dAmt = discount; } } fact.CreateLine(line, expense, GetC_Currency_ID(), dAmt, amt); if (!line.IsItem()) { grossAmt = Decimal.Subtract(grossAmt, amt); serviceAmt = Decimal.Add(serviceAmt, amt); } // if (line.GetM_Product_ID() != 0 && line.GetProduct().IsService()) // otherwise Inv Matching { if (!IsPosted()) { MCostDetail.CreateInvoice(as1, line.GetAD_Org_ID(), line.GetM_Product_ID(), line.GetM_AttributeSetInstance_ID(), line.Get_ID(), 0, // No Cost Element Decimal.Negate(line.GetAmtSource()), line.GetQty().Value, line.GetDescription(), GetTrx(), GetRectifyingProcess()); } } } } // Set Locations FactLine[] fLines = fact.GetLines(); for (int i = 0; i < fLines.Length; i++) { if (fLines[i] != null) { fLines[i].SetLocationFromBPartner(GetC_BPartner_Location_ID(), true); // from Loc fLines[i].SetLocationFromOrg(fLines[i].GetAD_Org_ID(), false); // to Loc } } // Liability DR int payables_ID = GetValidCombination_ID(Doc.ACCTTYPE_V_Liability, as1); int payablesServices_ID = GetValidCombination_ID(Doc.ACCTTYPE_V_Liability_Services, as1); if (_allLinesItem || !as1.IsPostServices() || payables_ID == payablesServices_ID) { grossAmt = GetAmount(Doc.AMTTYPE_Gross).Value; serviceAmt = Env.ZERO; } else if (_allLinesService) { serviceAmt = GetAmount(Doc.AMTTYPE_Gross).Value; grossAmt = Env.ZERO; } if (Env.Signum(grossAmt) != 0) { fact.CreateLine(null, MAccount.Get(GetCtx(), payables_ID), GetC_Currency_ID(), grossAmt, null); } if (Env.Signum(serviceAmt) != 0) { fact.CreateLine(null, MAccount.Get(GetCtx(), payablesServices_ID), GetC_Currency_ID(), serviceAmt, null); } } else { _error = "DocumentType unknown: " + GetDocumentType(); log.Log(Level.SEVERE, _error); fact = null; } // facts.Add(fact); return(facts); }