/// <summary> /// Create Facts (the accounting logic) for /// MXP. /// <pre> /// Product PPV <difference> /// PPV_Offset <difference> /// </pre> /// </summary> /// <param name="as1"></param> /// <returns></returns> public override List <Fact> CreateFacts(MAcctSchema as1) { List <Fact> facts = new List <Fact>(); // if (GetM_Product_ID() == 0 || // Nothing to do if no Product Env.Signum(Utility.Util.GetValueOfDecimal(GetQty())) == 0 || _M_InOutLine_ID == 0) // No posting if not matched to Shipment { log.Fine("No Product/Qty - M_Product_ID=" + GetM_Product_ID() + ",Qty=" + GetQty()); return(facts); } // create Fact Header Fact fact = new Fact(this, as1, Fact.POST_Actual); SetC_Currency_ID(as1.GetC_Currency_ID()); // Purchase Order Line Decimal poCost = _oLine.GetPriceCost(); if (Env.Signum(poCost) == 0) { poCost = _oLine.GetPriceActual(); } poCost = Decimal.Multiply(poCost, Utility.Util.GetValueOfDecimal(GetQty())); // Delivered so far // Different currency if (_oLine.GetC_Currency_ID() != as1.GetC_Currency_ID()) { MOrder order = _oLine.GetParent(); Decimal rate = MConversionRate.GetRate( order.GetC_Currency_ID(), as1.GetC_Currency_ID(), order.GetDateAcct(), order.GetC_ConversionType_ID(), _oLine.GetAD_Client_ID(), _oLine.GetAD_Org_ID()); if (rate.ToString() == null) { _error = "Purchase Order not convertible - " + as1.GetName(); return(null); } poCost = Decimal.Multiply(poCost, rate); if (Env.Scale(poCost) > as1.GetCostingPrecision()) { poCost = Decimal.Round(poCost, as1.GetCostingPrecision(), MidpointRounding.AwayFromZero); } } MOrder order1 = _oLine.GetParent(); bool isReturnTrx = order1.IsReturnTrx(); log.Fine("Temp"); if (!IsPosted()) { // Create PO Cost Detail Record firs MCostDetail.CreateOrder(as1, _oLine.GetAD_Org_ID(), GetM_Product_ID(), _M_AttributeSetInstance_ID, _C_OrderLine_ID, 0, // no cost element isReturnTrx ? Decimal.Negate(poCost) : poCost, isReturnTrx ? Decimal.Negate(Utility.Util.GetValueOfDecimal(GetQty())) : Utility.Util.GetValueOfDecimal(GetQty()), // Delivered _oLine.GetDescription(), GetTrx(), GetRectifyingProcess()); } // Current Costs String costingMethod = as1.GetCostingMethod(); MProduct product = MProduct.Get(GetCtx(), GetM_Product_ID()); MProductCategoryAcct pca = MProductCategoryAcct.Get(GetCtx(), product.GetM_Product_Category_ID(), as1.GetC_AcctSchema_ID(), GetTrx()); if (pca.GetCostingMethod() != null) { costingMethod = pca.GetCostingMethod(); } Decimal?costs = _pc.GetProductCosts(as1, GetAD_Org_ID(), costingMethod, _C_OrderLine_ID, false); // non-zero costs // No Costs yet - no PPV if (costs == null || Env.Signum(Utility.Util.GetValueOfDecimal(costs)) == 0) { _error = "Resubmit - No Costs for " + product.GetName(); log.Log(Level.SEVERE, _error); return(null); } // Difference Decimal difference = Decimal.Subtract(poCost, Utility.Util.GetValueOfDecimal(costs)); /***********************************************************************************/ //05,Sep,2011 //Special Check to restic Price varience posting in case of //AvarageInvoice Selected on product Category.Then Neglact the AverageInvoice Cost try { if (as1.IsNotPostPOVariance()) { difference = 0; } } catch (Exception ex) { log.SaveError("AccountSchemaColumnError", ex); } /***********************************************************************************/ // Nothing to post if (Env.Signum(difference) == 0) { log.Log(Level.FINE, "No Cost Difference for M_Product_ID=" + GetM_Product_ID()); facts.Add(fact); return(facts); } // Product PPV FactLine cr = fact.CreateLine(null, _pc.GetAccount(ProductCost.ACCTTYPE_P_PPV, as1), as1.GetC_Currency_ID(), difference); if (cr != null) { cr.SetQty(GetQty()); cr.SetC_BPartner_ID(_oLine.GetC_BPartner_ID()); cr.SetC_Activity_ID(_oLine.GetC_Activity_ID()); cr.SetC_Campaign_ID(_oLine.GetC_Campaign_ID()); cr.SetC_Project_ID(_oLine.GetC_Project_ID()); cr.SetC_UOM_ID(_oLine.GetC_UOM_ID()); cr.SetUser1_ID(_oLine.GetUser1_ID()); cr.SetUser2_ID(_oLine.GetUser2_ID()); } // PPV Offset FactLine dr = fact.CreateLine(null, GetAccount(Doc.ACCTTYPE_PPVOffset, as1), as1.GetC_Currency_ID(), Decimal.Negate(difference)); if (dr != null) { dr.SetQty((Decimal?)Decimal.Negate(Utility.Util.GetValueOfDecimal(GetQty()))); dr.SetC_BPartner_ID(_oLine.GetC_BPartner_ID()); dr.SetC_Activity_ID(_oLine.GetC_Activity_ID()); dr.SetC_Campaign_ID(_oLine.GetC_Campaign_ID()); dr.SetC_Project_ID(_oLine.GetC_Project_ID()); dr.SetC_UOM_ID(_oLine.GetC_UOM_ID()); dr.SetUser1_ID(_oLine.GetUser1_ID()); dr.SetUser2_ID(_oLine.GetUser2_ID()); } // facts.Add(fact); return(facts); }
/// <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 Landed Cost accounting & Cost lines /// </summary> /// <param name="as1">accounting schema</param> /// <param name="fact">fact</param> /// <param name="line">document line</param> /// <param name="dr">DR entry (normal api)</param> /// <returns>true if landed costs were created</returns> private bool LandedCost(MAcctSchema as1, Fact fact, DocLine line, bool dr) { int C_InvoiceLine_ID = line.Get_ID(); MLandedCostAllocation[] lcas = MLandedCostAllocation.GetOfInvoiceLine( GetCtx(), C_InvoiceLine_ID, GetTrx()); if (lcas.Length == 0) { return(false); } // Delete Old String sql = "DELETE FROM M_CostDetail WHERE C_InvoiceLine_ID=" + C_InvoiceLine_ID; int no = DataBase.DB.ExecuteQuery(sql, null, GetTrx()); if (no != 0) { log.Config("CostDetail Deleted #" + no); } // Calculate Total Base double totalBase = 0; for (int i = 0; i < lcas.Length; i++) { totalBase += Convert.ToDouble(lcas[i].GetBase());//.doubleValue(); } // Create New MInvoiceLine il = new MInvoiceLine(GetCtx(), C_InvoiceLine_ID, GetTrx()); for (int i = 0; i < lcas.Length; i++) { MLandedCostAllocation lca = lcas[i]; if (Env.Signum(lca.GetBase()) == 0) { continue; } double percent = totalBase / Convert.ToDouble(lca.GetBase()); String desc = il.GetDescription(); if (desc == null) { desc = percent + "%"; } else { desc += " - " + percent + "%"; } if (line.GetDescription() != null) { desc += " - " + line.GetDescription(); } // Accounting ProductCost pc = new ProductCost(GetCtx(), lca.GetM_Product_ID(), lca.GetM_AttributeSetInstance_ID(), GetTrx()); Decimal?drAmt = null; Decimal?crAmt = null; if (dr) { drAmt = lca.GetAmt(); } else { crAmt = lca.GetAmt(); } FactLine fl = fact.CreateLine(line, pc.GetAccount(ProductCost.ACCTTYPE_P_CostAdjustment, as1), GetC_Currency_ID(), drAmt, crAmt); fl.SetDescription(desc); // Cost Detail - Convert to AcctCurrency Decimal allocationAmt = lca.GetAmt(); if (GetC_Currency_ID() != as1.GetC_Currency_ID()) { allocationAmt = MConversionRate.Convert(GetCtx(), allocationAmt, GetC_Currency_ID(), as1.GetC_Currency_ID(), GetDateAcct(), GetC_ConversionType_ID(), GetAD_Client_ID(), GetAD_Org_ID()); } if (Env.Scale(allocationAmt) > as1.GetCostingPrecision()) { allocationAmt = Decimal.Round(allocationAmt, as1.GetCostingPrecision(), MidpointRounding.AwayFromZero); } if (!dr) { allocationAmt = Decimal.Negate(allocationAmt); } if (!IsPosted()) { MCostDetail cd = new MCostDetail(as1, lca.GetAD_Org_ID(), lca.GetM_Product_ID(), lca.GetM_AttributeSetInstance_ID(), lca.GetM_CostElement_ID(), allocationAmt, lca.GetQty(), // Qty desc, GetTrx()); cd.SetC_InvoiceLine_ID(C_InvoiceLine_ID); bool ok = cd.Save(); if (ok && !cd.IsProcessed()) { MClient client = MClient.Get(as1.GetCtx(), as1.GetAD_Client_ID()); if (client.IsCostImmediate()) { cd.Process(); } } } } log.Config("Created #" + lcas.Length); return(true); }