/// <summary>
        /// Create Facts (the accounting logic) for
        ///  PJI
        ///  <pre>
        ///  Issue
        ///      ProjectWIP      DR
        ///      Inventory               CR
        ///  </pre>
        ///  Project Account is either Asset or WIP depending on Project Type
        /// </summary>
        /// <param name="?"></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());

            MProject project         = new MProject(GetCtx(), _issue.GetC_Project_ID(), null);
            String   ProjectCategory = project.GetProjectCategory();
            MProduct product         = MProduct.Get(GetCtx(), _issue.GetM_Product_ID());

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

            //  Issue Cost
            Decimal?cost = null;

            if (_issue.GetM_InOutLine_ID() != 0)
            {
                cost = GetPOCost(as1);
            }
            else if (_issue.GetS_TimeExpenseLine_ID() != 0)
            {
                cost = GetLaborCost(as1);
            }
            if (cost == null)   //	standard Product Costs
            {
                cost = _line.GetProductCosts(as1, GetAD_Org_ID(), false);
            }

            //  Project         DR
            int acctType = ACCTTYPE_ProjectWIP;

            if (MProject.PROJECTCATEGORY_AssetProject.Equals(ProjectCategory))
            {
                acctType = ACCTTYPE_ProjectAsset;
            }
            dr = fact.CreateLine(_line,
                                 GetAccount(acctType, as1), as1.GetC_Currency_ID(), cost, null);
            dr.SetQty((Decimal?)Decimal.Negate(Utility.Util.GetValueOfDecimal(_line.GetQty())));

            //  Inventory               CR
            acctType = ProductCost.ACCTTYPE_P_Asset;
            if (product.IsService())
            {
                acctType = ProductCost.ACCTTYPE_P_Expense;
            }
            cr = fact.CreateLine(_line,
                                 _line.GetAccount(acctType, as1),
                                 as1.GetC_Currency_ID(), null, cost);
            cr.SetM_Locator_ID(_line.GetM_Locator_ID());
            cr.SetLocationFromLocator(_line.GetM_Locator_ID(), true);   // from Loc
            //
            List <Fact> facts = new List <Fact>();

            facts.Add(fact);
            return(facts);
        }
        /// <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);
        }