/// <summary>
        /// Create Facts (the accounting logic) for
        /// MMS, MMR.
        /// <pre>
        /// Shipment
        /// CoGS (RevOrg)   DR
        /// Inventory               CR
        /// Shipment of Project Issue
        /// CoGS            DR
        /// Project                 CR
        /// Receipt
        /// Inventory       DR
        /// NotInvoicedReceipt      CR
        /// </pre>
        /// </summary>
        /// <param name="as1">accounting schema</param>
        /// <returns>Fact</returns>
        public override List <Fact> CreateFacts(MAcctSchema as1)
        {
            //  create Fact Header
            Fact fact = new Fact(this, as1, Fact.POST_Actual);

            SetC_Currency_ID(as1.GetC_Currency_ID());

            //  Line pointers
            FactLine dr = null;
            FactLine cr = null;

            //  *** Sales - Shipment
            if (GetDocumentType().Equals(MDocBaseType.DOCBASETYPE_MATERIALDELIVERY))
            {
                for (int i = 0; i < _lines.Length; i++)
                {
                    DocLine    line  = _lines[i];
                    MInOutLine sLine = new MInOutLine(GetCtx(), line.Get_ID(), null);
                    Decimal    costs = 0;
                    if (sLine.GetA_Asset_ID() > 0)
                    {
                        costs = Util.GetValueOfDecimal(DB.ExecuteScalar(@"SELECT cost.CUrrentcostPrice
                                                                            FROM m_cost cost
                                                                            INNER JOIN A_Asset ass
                                                                            ON(ass.a_asset_ID=cost.a_asset_ID)
                                                                            INNER JOIN M_InOutLine IOL
                                                                            ON(IOL.A_Asset_ID       =ass.A_Asset_ID)
                                                                            WHERE IOL.M_InOutLine_ID=" + sLine.GetM_InOutLine_ID() + @"
                                                                              ORDER By cost.created desc"));
                        // Change if Cost not found against Asset then get Product Cost
                        if (Env.Signum(costs) == 0)     //	zero costs OK
                        {
                            costs = line.GetProductCosts(as1, line.GetAD_Org_ID(), true);
                        }
                    }
                    else
                    {
                        costs = line.GetProductCosts(as1, line.GetAD_Org_ID(), true);
                    }

                    if (Env.Signum(costs) == 0) //	zero costs OK
                    {
                        MProduct product = line.GetProduct();
                        if (product.IsStocked())
                        {
                            _error = "No Costs for " + line.GetProduct().GetName();
                            log.Log(Level.WARNING, _error);
                            return(null);
                        }
                        else    //	ignore service
                        {
                            continue;
                        }
                    }

                    if (!IsReturnTrx())
                    {
                        //  CoGS            DR
                        dr = fact.CreateLine(line,
                                             line.GetAccount(ProductCost.ACCTTYPE_P_Cogs, as1),
                                             as1.GetC_Currency_ID(), costs, null);
                        if (dr == null)
                        {
                            _error = "FactLine DR not created: " + line;
                            log.Log(Level.WARNING, _error);
                            return(null);
                        }
                        dr.SetM_Locator_ID(line.GetM_Locator_ID());
                        dr.SetLocationFromLocator(line.GetM_Locator_ID(), true);        //  from Loc
                        dr.SetLocationFromBPartner(GetC_BPartner_Location_ID(), false); //  to Loc
                        dr.SetAD_Org_ID(line.GetOrder_Org_ID());                        //	Revenue X-Org
                        dr.SetQty(Decimal.Negate(line.GetQty().Value));

                        //  Inventory               CR
                        cr = fact.CreateLine(line, line.GetAccount(ProductCost.ACCTTYPE_P_Asset, as1),
                                             as1.GetC_Currency_ID(), null, costs);
                        if (cr == null)
                        {
                            _error = "FactLine CR not created: " + line;
                            log.Log(Level.WARNING, _error);
                            return(null);
                        }
                        cr.SetM_Locator_ID(line.GetM_Locator_ID());
                        cr.SetLocationFromLocator(line.GetM_Locator_ID(), true);        // from Loc
                        cr.SetLocationFromBPartner(GetC_BPartner_Location_ID(), false); // to Loc
                    }
                    else // Reverse accounting entries for returns
                    {
                        //				  CoGS            CR
                        cr = fact.CreateLine(line,
                                             line.GetAccount(ProductCost.ACCTTYPE_P_Cogs, as1),
                                             as1.GetC_Currency_ID(), null, costs);
                        if (cr == null)
                        {
                            _error = "FactLine CR not created: " + line;
                            log.Log(Level.WARNING, _error);
                            return(null);
                        }
                        cr.SetM_Locator_ID(line.GetM_Locator_ID());
                        cr.SetLocationFromLocator(line.GetM_Locator_ID(), true);        //  from Loc
                        cr.SetLocationFromBPartner(GetC_BPartner_Location_ID(), false); //  to Loc
                        cr.SetAD_Org_ID(line.GetOrder_Org_ID());                        //	Revenue X-Org
                        cr.SetQty(Decimal.Negate(line.GetQty().Value));

                        //  Inventory               DR
                        dr = fact.CreateLine(line,
                                             line.GetAccount(ProductCost.ACCTTYPE_P_Asset, as1),
                                             as1.GetC_Currency_ID(), costs, null);
                        if (dr == null)
                        {
                            _error = "FactLine DR not created: " + line;
                            log.Log(Level.WARNING, _error);
                            return(null);
                        }
                        dr.SetM_Locator_ID(line.GetM_Locator_ID());
                        dr.SetLocationFromLocator(line.GetM_Locator_ID(), true);        // from Loc
                        dr.SetLocationFromBPartner(GetC_BPartner_Location_ID(), false); // to Loc
                    }
                    //
                    if (line.GetM_Product_ID() != 0)
                    {
                        if (!IsPosted())
                        {
                            MCostDetail.CreateShipment(as1, line.GetAD_Org_ID(),
                                                       line.GetM_Product_ID(), line.GetM_AttributeSetInstance_ID(),
                                                       line.Get_ID(), 0,
                                                       costs, IsReturnTrx() ? Decimal.Negate(line.GetQty().Value) : line.GetQty().Value,
                                                       line.GetDescription(), true, GetTrx(), GetRectifyingProcess());
                        }
                    }
                }       //	for all lines

                if (!IsPosted())
                {
                    UpdateProductInfo(as1.GetC_AcctSchema_ID()); //  only for SO!
                }
            }                                                    //	Shipment

            //  *** Purchasing - Receipt
            else if (GetDocumentType().Equals(MDocBaseType.DOCBASETYPE_MATERIALRECEIPT))
            {
                for (int i = 0; i < _lines.Length; i++)
                {
                    Decimal  costs   = 0;
                    DocLine  line    = _lines[i];
                    MProduct product = line.GetProduct();
                    /***********************************************************/
                    //05,Sep,2011
                    //Special Check to restic Price varience posting in case of
                    //AvarageInvoice Selected on product Category.Then Neglact the AverageInvoice Cost
                    MProductCategoryAcct pca = MProductCategoryAcct.Get(product.GetCtx(),
                                                                        product.GetM_Product_Category_ID(), as1.GetC_AcctSchema_ID(), null);
                    try
                    {
                        if (as1.IsNotPostPOVariance() && line.GetC_OrderLine_ID() > 0)
                        {
                            MOrderLine oLine         = new MOrderLine(product.GetCtx(), line.GetC_OrderLine_ID(), null);
                            MOrder     order         = new MOrder(product.GetCtx(), oLine.GetC_Order_ID(), null);
                            Decimal    convertedCost = MConversionRate.Convert(product.GetCtx(),
                                                                               oLine.GetPriceEntered(), order.GetC_Currency_ID(), as1.GetC_Currency_ID(),
                                                                               line.GetDateAcct(), order.GetC_ConversionType_ID(),
                                                                               oLine.GetAD_Client_ID(), line.GetAD_Org_ID());

                            costs = Decimal.Multiply(convertedCost, oLine.GetQtyEntered());
                        }
                        else
                        {
                            costs = line.GetProductCosts(as1, line.GetAD_Org_ID(), false);      //	non-zero costs
                        }
                    }
                    catch (Exception ex)
                    {
                        log.SaveError("AccountSchemaColumnError", ex);
                        costs = line.GetProductCosts(as1, line.GetAD_Org_ID(), false);  //	non-zero costs
                    }
                    /***********************************************************/

                    //Decimal costs = costs = line.GetProductCosts(as1, line.GetAD_Org_ID(), false);	//	non-zero costs

                    if (Env.Signum(costs) == 0)
                    {
                        _error = "Resubmit - No Costs for " + product.GetName();
                        log.Log(Level.WARNING, _error);
                        return(null);
                    }
                    //  Inventory/Asset			DR
                    MAccount assets = line.GetAccount(ProductCost.ACCTTYPE_P_Asset, as1);
                    if (product.IsService())
                    {
                        assets = line.GetAccount(ProductCost.ACCTTYPE_P_Expense, as1);
                    }

                    if (!IsReturnTrx())
                    {
                        //  Inventory/Asset			DR
                        dr = fact.CreateLine(line, assets,
                                             as1.GetC_Currency_ID(), costs, null);
                        if (dr == null)
                        {
                            _error = "DR not created: " + line;
                            log.Log(Level.WARNING, _error);
                            return(null);
                        }
                        dr.SetM_Locator_ID(line.GetM_Locator_ID());
                        dr.SetLocationFromBPartner(GetC_BPartner_Location_ID(), true); // from Loc
                        dr.SetLocationFromLocator(line.GetM_Locator_ID(), false);      // to Loc
                        //  NotInvoicedReceipt				CR
                        cr = fact.CreateLine(line,
                                             GetAccount(Doc.ACCTTYPE_NotInvoicedReceipts, as1),
                                             as1.GetC_Currency_ID(), null, costs);
                        if (cr == null)
                        {
                            _error = "CR not created: " + line;
                            log.Log(Level.WARNING, _error);
                            return(null);
                        }
                        cr.SetM_Locator_ID(line.GetM_Locator_ID());
                        cr.SetLocationFromBPartner(GetC_BPartner_Location_ID(), true); //  from Loc
                        cr.SetLocationFromLocator(line.GetM_Locator_ID(), false);      //  to Loc
                        cr.SetQty(Decimal.Negate(line.GetQty().Value));
                    }
                    else // reverse accounting entries for returns
                    {
                        //  Inventory/Asset			CR
                        cr = fact.CreateLine(line, assets,
                                             as1.GetC_Currency_ID(), null, costs);
                        if (cr == null)
                        {
                            _error = "CR not created: " + line;
                            log.Log(Level.WARNING, _error);
                            return(null);
                        }
                        cr.SetM_Locator_ID(line.GetM_Locator_ID());
                        cr.SetLocationFromBPartner(GetC_BPartner_Location_ID(), true); // from Loc
                        cr.SetLocationFromLocator(line.GetM_Locator_ID(), false);      // to Loc
                        //  NotInvoicedReceipt				DR
                        dr = fact.CreateLine(line,
                                             GetAccount(Doc.ACCTTYPE_NotInvoicedReceipts, as1),
                                             as1.GetC_Currency_ID(), costs, null);
                        if (dr == null)
                        {
                            _error = "DR not created: " + line;
                            log.Log(Level.WARNING, _error);
                            return(null);
                        }
                        dr.SetM_Locator_ID(line.GetM_Locator_ID());
                        dr.SetLocationFromBPartner(GetC_BPartner_Location_ID(), true); //  from Loc
                        dr.SetLocationFromLocator(line.GetM_Locator_ID(), false);      //  to Loc
                        dr.SetQty(Decimal.Negate(line.GetQty().Value));
                    }
                }
            }   //	Receipt
            else
            {
                _error = "DocumentType unknown: " + GetDocumentType();
                log.Log(Level.SEVERE, _error);
                return(null);
            }
            //
            List <Fact> facts = new List <Fact>();

            facts.Add(fact);
            return(facts);
        }
Beispiel #2
0
        /// <summary>
        /// Set Document Info
        /// </summary>
        /// <param name="doc">document</param>
        /// <param name="docLine">doc line</param>
        public void SetDocumentInfo(Doc doc, DocLine docLine)
        {
            _doc     = doc;
            _docLine = docLine;
            //	reset
            SetAD_Org_ID(0);
            SetC_SalesRegion_ID(0);
            //	Client
            if (GetAD_Client_ID() == 0)
            {
                SetAD_Client_ID(_doc.GetAD_Client_ID());
            }
            //	Date Trx
            SetDateTrx(_doc.GetDateDoc());
            if (_docLine != null && _docLine.GetDateDoc() != null)
            {
                SetDateTrx(_docLine.GetDateDoc());
            }
            //	Date Acct
            SetDateAcct(_doc.GetDateAcct());
            if (_docLine != null && _docLine.GetDateAcct() != null)
            {
                SetDateAcct(_docLine.GetDateAcct());
            }
            //	Period, Tax
            if (_docLine != null && _docLine.GetC_Period_ID() != 0)
            {
                SetC_Period_ID(_docLine.GetC_Period_ID());
            }
            else
            {
                SetC_Period_ID(_doc.GetC_Period_ID());
            }
            if (_docLine != null)
            {
                SetC_Tax_ID(_docLine.GetC_Tax_ID());
            }
            //	Description
            StringBuilder description = new StringBuilder(_doc.GetDocumentNo());

            if (_docLine != null)
            {
                description.Append(" #").Append(_docLine.GetLine());
                if (_docLine.GetDescription() != null)
                {
                    description.Append(" (").Append(_docLine.GetDescription()).Append(")");
                }
                else if (_doc.GetDescription() != null && _doc.GetDescription().Length > 0)
                {
                    description.Append(" (").Append(_doc.GetDescription()).Append(")");
                }
            }
            else if (_doc.GetDescription() != null && _doc.GetDescription().Length > 0)
            {
                description.Append(" (").Append(_doc.GetDescription()).Append(")");
            }
            SetDescription(description.ToString());
            //	Journal Info
            SetGL_Budget_ID(_doc.GetGL_Budget_ID());
            SetGL_Category_ID(_doc.GetGL_Category_ID());

            //	Product
            if (_docLine != null)
            {
                SetM_Product_ID(_docLine.GetM_Product_ID());
            }
            if (GetM_Product_ID() == 0)
            {
                SetM_Product_ID(_doc.GetM_Product_ID());
            }
            //	UOM
            if (_docLine != null)
            {
                SetC_UOM_ID(_docLine.GetC_UOM_ID());
            }
            //	Qty
            if (Get_Value("Qty") == null) // not previously set
            {
                SetQty(_doc.GetQty());    //	neg = outgoing
                if (_docLine != null)
                {
                    SetQty(_docLine.GetQty());
                }
            }

            //	Loc From (maybe set earlier)
            if (GetC_LocFrom_ID() == 0 && _docLine != null)
            {
                SetC_LocFrom_ID(_docLine.GetC_LocFrom_ID());
            }
            if (GetC_LocFrom_ID() == 0)
            {
                SetC_LocFrom_ID(_doc.GetC_LocFrom_ID());
            }
            //	Loc To (maybe set earlier)
            if (GetC_LocTo_ID() == 0 && _docLine != null)
            {
                SetC_LocTo_ID(_docLine.GetC_LocTo_ID());
            }
            if (GetC_LocTo_ID() == 0)
            {
                SetC_LocTo_ID(_doc.GetC_LocTo_ID());
            }
            //	BPartner
            if (_docLine != null)
            {
                SetC_BPartner_ID(_docLine.GetC_BPartner_ID());
            }
            if (GetC_BPartner_ID() == 0)
            {
                SetC_BPartner_ID(_doc.GetC_BPartner_ID());
            }
            //	Sales Region from BPLocation/Sales Rep
            //	Trx Org
            if (_docLine != null)
            {
                SetAD_OrgTrx_ID(_docLine.GetAD_OrgTrx_ID());
            }
            if (GetAD_OrgTrx_ID() == 0)
            {
                SetAD_OrgTrx_ID(_doc.GetAD_OrgTrx_ID());
            }
            //	Project
            if (_docLine != null)
            {
                SetC_Project_ID(_docLine.GetC_Project_ID());
            }
            if (GetC_Project_ID() == 0)
            {
                SetC_Project_ID(_doc.GetC_Project_ID());
            }
            //	Campaign
            if (_docLine != null)
            {
                SetC_Campaign_ID(_docLine.GetC_Campaign_ID());
            }
            if (GetC_Campaign_ID() == 0)
            {
                SetC_Campaign_ID(_doc.GetC_Campaign_ID());
            }
            //	Activity
            if (_docLine != null)
            {
                SetC_Activity_ID(_docLine.GetC_Activity_ID());
            }
            if (GetC_Activity_ID() == 0)
            {
                SetC_Activity_ID(_doc.GetC_Activity_ID());
            }
            //	User List 1
            if (_docLine != null)
            {
                SetUser1_ID(_docLine.GetUser1_ID());
            }
            if (GetUser1_ID() == 0)
            {
                SetUser1_ID(_doc.GetUser1_ID());
            }
            //	User List 2
            if (_docLine != null)
            {
                SetUser2_ID(_docLine.GetUser2_ID());
            }
            if (GetUser2_ID() == 0)
            {
                SetUser2_ID(_doc.GetUser2_ID());
            }
            //	References in setAccount
        }