/// <summary>
        /// Create Facts (the accounting logic) for
        ///  ARP, APP.
        ///<pre>
        ///  ARP
        ///      BankInTransit   DR
        ///      UnallocatedCash         CR
        ///      or Charge/C_Prepayment
        ///  APP
        ///      PaymentSelect   DR
        ///      or Charge/V_Prepayment
        ///      BankInTransit           CR
        ///  CashBankTransfer
        ///      -
        ///  </pre>
        /// </summary>
        /// <param name="as1"></param>
        /// <returns>fact</returns>
        public override List <Fact> CreateFacts(MAcctSchema as1)
        {
            //  create Fact Header
            Fact fact = new Fact(this, as1, Fact.POST_Actual);

            //	Cash Transfer
            if ("X".Equals(_TenderType))
            {
                List <Fact> facts = new List <Fact>();
                facts.Add(fact);
                return(facts);
            }

            int AD_Org_ID = GetBank_Org_ID();           //	Bank Account Org

            if (GetDocumentType().Equals(MDocBaseType.DOCBASETYPE_ARRECEIPT))
            {
                // Work done by Bharat for the posting of ED008 Module

                bool addPost = false;
                //Check For Module
                Tuple <String, String, String> aInfo = null;
                if (Env.HasModulePrefix("ED008_", out aInfo))
                {
                    addPost = true;
                }
                else
                {
                    addPost = false;
                }

                if (addPost == true)
                {
                    // Tender Type RIBA
                    if ("R".Equals(_TenderType))
                    {
                        MAccount acct       = null;
                        MAccount portAcct   = null;
                        int      validComID = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED000_RIBA_Acct FROM C_BankAccount_Acct WHERE C_BankAccount_ID=" + GetC_BankAccount_ID() + " AND AD_Client_ID = " + GetAD_Client_ID()));
                        if (validComID > 0)
                        {
                            acct = MAccount.Get(Env.GetCtx(), validComID);
                        }

                        if (acct == null)
                        {
                            validComID = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED000_RIBA_Acct FROM C_AcctSchema_Default WHERE C_AcctSchema_ID=" + as1.GetC_AcctSchema_ID() + " AND AD_Client_ID = " + GetAD_Client_ID()));
                            acct       = MAccount.Get(Env.GetCtx(), validComID);
                        }
                        FactLine fl = fact.CreateLine(null, acct, GetC_Currency_ID(), GetAmount(), null);

                        int ComID = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED000_Portfolio_Acct FROM C_BP_Customer_Acct WHERE C_BPartner_ID=" + GetC_BPartner_ID() + " AND AD_Client_ID = " + GetAD_Client_ID()));
                        if (ComID > 0)
                        {
                            portAcct = MAccount.Get(Env.GetCtx(), ComID);
                        }

                        if (portAcct == null)
                        {
                            ComID    = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED000_Portfolio_Acct FROM C_AcctSchema_Default WHERE C_AcctSchema_ID=" + as1.GetC_AcctSchema_ID() + " AND AD_Client_ID = " + GetAD_Client_ID()));
                            portAcct = MAccount.Get(Env.GetCtx(), ComID);
                        }
                        fl = fact.CreateLine(null, portAcct, GetC_Currency_ID(), null, GetAmount());
                    }
                    // Tender Type MAV
                    else if ("M".Equals(_TenderType))
                    {
                        MAccount acct       = null;
                        MAccount portAcct   = null;
                        int      validComID = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED000_MAV_Acct FROM C_BankAccount_Acct WHERE C_BankAccount_ID=" + GetC_BankAccount_ID() + " AND AD_Client_ID = " + GetAD_Client_ID()));
                        if (validComID > 0)
                        {
                            acct = MAccount.Get(Env.GetCtx(), validComID);
                        }

                        if (acct == null)
                        {
                            validComID = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED000_MAV_Acct FROM C_AcctSchema_Default WHERE C_AcctSchema_ID=" + as1.GetC_AcctSchema_ID() + " AND AD_Client_ID = " + GetAD_Client_ID()));
                            acct       = MAccount.Get(Env.GetCtx(), validComID);
                        }
                        FactLine fl = fact.CreateLine(null, acct,
                                                      GetC_Currency_ID(), GetAmount(), null);

                        int ComID = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED000_Portfolio_Acct FROM C_BP_Customer_Acct WHERE C_BPartner_ID=" + GetC_BPartner_ID() + " AND AD_Client_ID = " + GetAD_Client_ID()));
                        if (ComID > 0)
                        {
                            portAcct = MAccount.Get(Env.GetCtx(), ComID);
                        }

                        if (portAcct == null)
                        {
                            ComID    = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED000_Portfolio_Acct FROM C_AcctSchema_Default WHERE C_AcctSchema_ID=" + as1.GetC_AcctSchema_ID() + " AND AD_Client_ID = " + GetAD_Client_ID()));
                            portAcct = MAccount.Get(Env.GetCtx(), ComID);
                        }
                        fl = fact.CreateLine(null, portAcct,
                                             GetC_Currency_ID(), null, GetAmount());
                    }
                    // Tender Type BOE
                    else if ("E".Equals(_TenderType))
                    {
                        MAccount acct     = null;
                        MAccount portAcct = null;
                        string   boeType  = Util.GetValueOfString(DB.ExecuteScalar(@"SELECT ED008_BOEType FROM C_Payment WHERE C_Payment_ID = " + Get_ID() + " AND AD_Client_ID = " + GetAD_Client_ID()));
                        int      boeID    = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED008_BOE_ID FROM C_Payment WHERE C_Payment_ID = " + Get_ID() + " AND AD_Client_ID = " + GetAD_Client_ID()));
                        if ("T".Equals(boeType))
                        {
                            int validComID = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED000_BOE_Acct FROM ED008_BOEAccounting WHERE ED008_BOE_ID=" + boeID + " AND AD_Client_ID = " + GetAD_Client_ID()));
                            if (validComID > 0)
                            {
                                acct = MAccount.Get(Env.GetCtx(), validComID);
                            }

                            if (acct == null)
                            {
                                validComID = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED000_BOE_Acct FROM C_AcctSchema_Default WHERE C_AcctSchema_ID=" + as1.GetC_AcctSchema_ID() + " AND AD_Client_ID = " + GetAD_Client_ID()));
                                acct       = MAccount.Get(Env.GetCtx(), validComID);
                            }
                            FactLine fl = fact.CreateLine(null, acct, GetC_Currency_ID(), GetAmount(), null);

                            int ComID = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED000_BOETransit_Acct FROM ED008_BOEAccounting WHERE ED008_BOE_ID=" + boeID + " AND AD_Client_ID = " + GetAD_Client_ID()));
                            if (ComID > 0)
                            {
                                portAcct = MAccount.Get(Env.GetCtx(), ComID);
                            }

                            if (portAcct == null)
                            {
                                ComID    = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED000_BOETransit_Acct FROM C_AcctSchema_Default WHERE C_AcctSchema_ID=" + as1.GetC_AcctSchema_ID() + " AND AD_Client_ID = " + GetAD_Client_ID()));
                                portAcct = MAccount.Get(Env.GetCtx(), ComID);
                            }
                            fl = fact.CreateLine(null, portAcct, GetC_Currency_ID(), null, GetAmount());
                        }

                        else if ("R".Equals(boeType))
                        {
                            FactLine fl = fact.CreateLine(null, GetAccount(Doc.ACCTTYPE_BankInTransit, as1), GetC_Currency_ID(), GetAmount(), null);

                            int ComID = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED000_BOE_Acct FROM ED008_BOEAccounting WHERE ED008_BOE_ID=" + boeID + " AND AD_Client_ID = " + GetAD_Client_ID()));
                            if (ComID > 0)
                            {
                                portAcct = MAccount.Get(Env.GetCtx(), ComID);
                            }

                            if (portAcct == null)
                            {
                                ComID    = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED000_BOE_Acct FROM C_AcctSchema_Default WHERE C_AcctSchema_ID=" + as1.GetC_AcctSchema_ID() + " AND AD_Client_ID = " + GetAD_Client_ID()));
                                portAcct = MAccount.Get(Env.GetCtx(), ComID);
                            }
                            fl = fact.CreateLine(null, portAcct, GetC_Currency_ID(), null, GetAmount());
                        }
                    }
                    // Tender Type RID
                    else if ("I".Equals(_TenderType))
                    {
                        MAccount acct       = null;
                        MAccount portAcct   = null;
                        int      validComID = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED000_RID_Acct FROM C_BankAccount_Acct WHERE C_BankAccount_ID=" + GetC_BankAccount_ID() + " AND AD_Client_ID = " + GetAD_Client_ID()));
                        if (validComID > 0)
                        {
                            acct = MAccount.Get(Env.GetCtx(), validComID);
                        }

                        if (acct == null)
                        {
                            validComID = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED000_RID_Acct FROM C_AcctSchema_Default WHERE C_AcctSchema_ID=" + as1.GetC_AcctSchema_ID() + " AND AD_Client_ID = " + GetAD_Client_ID()));
                            acct       = MAccount.Get(Env.GetCtx(), validComID);
                        }
                        FactLine fl = fact.CreateLine(null, acct,
                                                      GetC_Currency_ID(), GetAmount(), null);

                        int ComID = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED000_Portfolio_Acct FROM C_BP_Customer_Acct WHERE C_BPartner_ID=" + GetC_BPartner_ID() + " AND AD_Client_ID = " + GetAD_Client_ID()));
                        if (ComID > 0)
                        {
                            portAcct = MAccount.Get(Env.GetCtx(), ComID);
                        }

                        if (portAcct == null)
                        {
                            ComID    = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED000_Portfolio_Acct FROM C_AcctSchema_Default WHERE C_AcctSchema_ID=" + as1.GetC_AcctSchema_ID() + " AND AD_Client_ID = " + GetAD_Client_ID()));
                            portAcct = MAccount.Get(Env.GetCtx(), ComID);
                        }
                        fl = fact.CreateLine(null, portAcct,
                                             GetC_Currency_ID(), null, GetAmount());
                    }

                    else
                    {
                        //	Asset
                        FactLine fl = fact.CreateLine(null, GetAccount(Doc.ACCTTYPE_BankInTransit, as1),
                                                      GetC_Currency_ID(), GetAmount(), null);
                        if (fl != null && AD_Org_ID != 0)
                        {
                            fl.SetAD_Org_ID(AD_Org_ID);
                        }
                        //
                        MAccount acct = null;
                        if (GetC_Charge_ID() != 0)
                        {
                            acct = MCharge.GetAccount(GetC_Charge_ID(), as1, GetAmount());
                        }
                        else if (_Prepayment)
                        {
                            acct = GetAccount(Doc.ACCTTYPE_C_Prepayment, as1);
                        }
                        else
                        {
                            acct = GetAccount(Doc.ACCTTYPE_UnallocatedCash, as1);
                        }
                        fl = fact.CreateLine(null, acct, GetC_Currency_ID(), null, GetAmount());
                        if (fl != null && AD_Org_ID != 0 &&
                            GetC_Charge_ID() == 0)              //	don't overwrite charge
                        {
                            fl.SetAD_Org_ID(AD_Org_ID);
                        }
                    }
                }
                // Default Posting Logic
                else
                {
                    //	Asset
                    FactLine fl = fact.CreateLine(null, GetAccount(Doc.ACCTTYPE_BankInTransit, as1),
                                                  GetC_Currency_ID(), GetAmount(), null);
                    if (fl != null && AD_Org_ID != 0)
                    {
                        fl.SetAD_Org_ID(AD_Org_ID);
                    }
                    //
                    MAccount acct = null;
                    if (GetC_Charge_ID() != 0)
                    {
                        acct = MCharge.GetAccount(GetC_Charge_ID(), as1, GetAmount());
                    }
                    else if (_Prepayment)
                    {
                        acct = GetAccount(Doc.ACCTTYPE_C_Prepayment, as1);
                    }
                    else
                    {
                        acct = GetAccount(Doc.ACCTTYPE_UnallocatedCash, as1);
                    }
                    fl = fact.CreateLine(null, acct,
                                         GetC_Currency_ID(), null, GetAmount());
                    if (fl != null && AD_Org_ID != 0 &&
                        GetC_Charge_ID() == 0)                  //	don't overwrite charge
                    {
                        fl.SetAD_Org_ID(AD_Org_ID);
                    }
                }
            }
            //  APP
            else if (GetDocumentType().Equals(MDocBaseType.DOCBASETYPE_APPAYMENT))
            {
                // Work Done For Ed008 Module
                bool addPost = false;
                //Check For Module
                Tuple <String, String, String> aInfo = null;
                if (Env.HasModulePrefix("ED008_", out aInfo) || Env.HasModulePrefix("ED010_", out aInfo))
                {
                    addPost = true;
                }

                if (addPost == true)
                {
                    MAccount acct     = null;
                    MAccount portAcct = null;
                    string   boeType  = Util.GetValueOfString(DB.ExecuteScalar(@"SELECT ED008_BOEType FROM C_Payment WHERE C_Payment_ID = " + Get_ID() + " AND AD_Client_ID = " + GetAD_Client_ID()));
                    int      boeID    = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED008_BOE_ID FROM C_Payment WHERE C_Payment_ID = " + Get_ID() + " AND AD_Client_ID = " + GetAD_Client_ID()));
                    if ("E".Equals(_TenderType))
                    {
                        if ("T".Equals(boeType))
                        {
                            int validComID = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED000_BOETransit_Acct FROM ED008_BOEAccounting WHERE ED008_BOE_ID=" + boeID + " AND AD_Client_ID = " + GetAD_Client_ID()));
                            if (validComID > 0)
                            {
                                acct = MAccount.Get(Env.GetCtx(), validComID);
                            }

                            if (acct == null)
                            {
                                validComID = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED000_BOETransit_Acct FROM C_AcctSchema_Default WHERE C_AcctSchema_ID=" + as1.GetC_AcctSchema_ID() + " AND AD_Client_ID = " + GetAD_Client_ID()));
                                acct       = MAccount.Get(Env.GetCtx(), validComID);
                            }
                            FactLine f2 = fact.CreateLine(null, acct, GetC_Currency_ID(), GetAmount(), null);

                            int ComID = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED000_BOE_Acct FROM ED008_BOEAccounting WHERE ED008_BOE_ID=" + boeID + " AND AD_Client_ID = " + GetAD_Client_ID()));
                            if (ComID > 0)
                            {
                                portAcct = MAccount.Get(Env.GetCtx(), ComID);
                            }

                            if (portAcct == null)
                            {
                                ComID    = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED000_BOE_Acct FROM C_AcctSchema_Default WHERE C_AcctSchema_ID=" + as1.GetC_AcctSchema_ID() + " AND AD_Client_ID = " + GetAD_Client_ID()));
                                portAcct = MAccount.Get(Env.GetCtx(), ComID);
                            }
                            f2 = fact.CreateLine(null, portAcct, GetC_Currency_ID(), null, GetAmount());
                        }
                    }
                    else
                    {
                        FactLine fl = fact.CreateLine(null, GetAccount(Doc.ACCTTYPE_BankInTransit, as1), GetC_Currency_ID(), null, GetAmount());
                        if (fl != null && AD_Org_ID != 0 &&
                            GetC_Charge_ID() == 0)              //	don't overwrite charge
                        {
                            fl.SetAD_Org_ID(AD_Org_ID);
                        }

                        DataSet ds = null;
                        DataSet dsSSCode = null;
                        decimal amount = 0, WithholdingAmt = 0, sscAmt = 0, payAmt = 0;
                        payAmt = GetAmount();

                        string sql = @"SELECT cl.m_product_id, cl.linenetamt,prd.ed010_sscode_id,holddet.ed010_appliedpercentage,holddet.ed010_actualpercentage,withAcct.Withholding_Acct
                        FROM C_Payment pay INNER JOIN c_invoice inv ON pay.c_invoice_id = inv.c_invoice_id inner join c_invoiceline cl  on pay.c_invoice_id=cl.c_invoice_id inner join C_BPartner cb 
                        on pay.c_bpartner_id=cb.c_bpartner_id INNER JOIN m_product prd ON prd.m_product_ID = cl.m_product_ID LEFT JOIN c_withholding hold ON hold.C_WithHolding_Id = prd.c_withholding_id
                        LEFT JOIN ed010_withholdingdetails holddet ON holddet.C_WithHolding_Id = hold.C_WithHolding_Id left join C_Withholding_Acct withAcct on hold.C_WithHolding_Id=withAcct.C_WithHolding_Id
                        where cb.ED010_IsWithholding='Y' and cb.ED010_IsSSCode='Y' and pay.ED010_WithholdingAmt > 0 AND " + GlobalVariable.TO_DATE(DateTime.Now.ToLocalTime(), true) +
                                     @"BETWEEN holddet.ED010_FromDate AND holddet.ED010_ToDate and pay.AD_Org_ID = " + GetCtx().GetAD_Org_ID() + " and pay.c_payment_id = " + Get_ID();

                        ds = new DataSet();
                        ds = DB.ExecuteDataset(sql, null, null);
                        if (ds.Tables.Count > 0)
                        {
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                                {
                                    MAccount withAcct = null;
                                    int      validID  = 0;
                                    amount         = (Decimal.Divide(Decimal.Multiply(Util.GetValueOfDecimal(ds.Tables[0].Rows[i]["linenetamt"]), Util.GetValueOfDecimal(ds.Tables[0].Rows[i]["ed010_appliedpercentage"])), 100));
                                    WithholdingAmt = (Decimal.Divide(Decimal.Multiply(amount, Util.GetValueOfDecimal(ds.Tables[0].Rows[i]["ed010_actualpercentage"])), 100));

                                    validID = Util.GetValueOfInt(ds.Tables[0].Rows[i]["Withholding_Acct"]);
                                    if (validID > 0)
                                    {
                                        withAcct = MAccount.Get(Env.GetCtx(), validID);
                                    }
                                    if (withAcct == null)
                                    {
                                        validID  = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT Withholding_Acct FROM C_AcctSchema_Default WHERE C_AcctSchema_ID=" + as1.GetC_AcctSchema_ID() + " AND AD_Client_ID = " + GetAD_Client_ID()));
                                        withAcct = MAccount.Get(Env.GetCtx(), validID);
                                    }
                                    fl = fact.CreateLine(null, withAcct, GetC_Currency_ID(), null, Decimal.Round(WithholdingAmt, 2));
                                    if (fl != null)
                                    {
                                        payAmt += Decimal.Round(WithholdingAmt, 2);
                                    }
                                    if (fl != null && AD_Org_ID != 0)
                                    {
                                        fl.SetAD_Org_ID(AD_Org_ID);
                                    }
                                }
                            }
                        }
                        ds.Dispose();

                        sql = @"SELECT cl.m_product_id,cb.c_bpartner_id,cl.linenetamt,prd.ed010_sscode_id,sdet.ed010_percentagetype,sdet.ed010_maxamt,sdet.ed010_minamt,sdet.ed010_socialsecurityprcnt,sdet.ed010_orgpercentage,
                        sdet.ed010_vendorpercentage,sscAcct.ED000_SecCodeAcct FROM C_Payment pay INNER JOIN c_invoice inv ON pay.c_invoice_id = inv.c_invoice_id inner join c_invoiceline cl  on pay.c_invoice_id=cl.c_invoice_id inner join C_BPartner cb 
                        on pay.c_bpartner_id=cb.c_bpartner_id INNER JOIN m_product prd ON prd.m_product_ID = cl.m_product_ID LEFT JOIN ed010_sscode scode ON scode.ed010_sscode_ID = prd.ed010_sscode_id
                        LEFT JOIN ed010_sscodedetails sdet ON sdet.ed010_sscode_ID = scode.ed010_sscode_ID left join ED010_SSCode_Acct sscAcct on scode.ED010_SSCode_ID=sscAcct.ED010_SSCode_ID
                        where cb.ED010_IsWithholding='Y' and cb.ED010_IsSSCode='Y' and pay.ED010_WithholdingAmt > 0 AND " + GlobalVariable.TO_DATE(DateTime.Now.ToLocalTime(), true) +
                              @"BETWEEN sdet.ED010_FromDate AND sdet.ED010_ToDate AND pay.AD_Org_ID = " + GetCtx().GetAD_Org_ID() + " and pay.c_payment_id = " + Get_ID();

                        dsSSCode = new DataSet();
                        dsSSCode = DB.ExecuteDataset(sql, null, null);
                        if (dsSSCode.Tables.Count > 0)
                        {
                            if (dsSSCode.Tables[0].Rows.Count > 0)
                            {
                                for (int i = 0; i < dsSSCode.Tables[0].Rows.Count; i++)
                                {
                                    MAccount sscAcct = null;
                                    int      vlID    = 0;
                                    sql = @"SELECT SUM(grandtotal) FROM c_invoice WHERE IsActive = 'Y' AND  docstatus = 'CO' AND c_bpartner_Id = " + Util.GetValueOfInt(dsSSCode.Tables[0].Rows[i]["c_bpartner_id"]);
                                    decimal result = Util.GetValueOfDecimal(DB.ExecuteScalar(sql, null, null));
                                    if (result <= Util.GetValueOfDecimal(dsSSCode.Tables[0].Rows[i]["ed010_maxamt"]) && result >= Util.GetValueOfDecimal(dsSSCode.Tables[0].Rows[i]["ed010_minamt"]))
                                    {
                                        if (Util.GetValueOfString(dsSSCode.Tables[0].Rows[i]["ed010_percentagetype"]) == "S")
                                        {
                                            sscAmt = (Decimal.Divide(Decimal.Multiply(Util.GetValueOfDecimal(dsSSCode.Tables[0].Rows[i]["linenetamt"]), Util.GetValueOfDecimal(dsSSCode.Tables[0].Rows[i]["ed010_socialsecurityprcnt"])), 100));
                                        }
                                        else if (Util.GetValueOfString(dsSSCode.Tables[0].Rows[i]["ed010_percentagetype"]) == "M")
                                        {
                                            //  amount = (Decimal.Divide(Decimal.Multiply(Util.GetValueOfDecimal(dsSSCode.Tables[0].Rows[i]["linenetamt"]), Util.GetValueOfDecimal(dsSSCode.Tables[0].Rows[i]["ed010_vendorpercentage"])), 100));
                                            sscAmt = (Decimal.Divide(Decimal.Multiply(Util.GetValueOfDecimal(dsSSCode.Tables[0].Rows[i]["linenetamt"]), Util.GetValueOfDecimal(dsSSCode.Tables[0].Rows[i]["ed010_orgpercentage"])), 100));
                                        }
                                    }

                                    vlID = Util.GetValueOfInt(dsSSCode.Tables[0].Rows[i]["ED000_SecCodeAcct"]);
                                    if (vlID > 0)
                                    {
                                        sscAcct = MAccount.Get(Env.GetCtx(), vlID);
                                    }
                                    if (sscAcct == null)
                                    {
                                        vlID    = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT ED000_SecCodeAcct FROM C_AcctSchema_Default WHERE C_AcctSchema_ID=" + as1.GetC_AcctSchema_ID() + " AND AD_Client_ID = " + GetAD_Client_ID()));
                                        sscAcct = MAccount.Get(Env.GetCtx(), vlID);
                                    }
                                    fl = fact.CreateLine(null, sscAcct, GetC_Currency_ID(), null, Decimal.Round(sscAmt, 2));
                                    if (fl != null)
                                    {
                                        payAmt += Decimal.Round(sscAmt, 2);
                                    }
                                    if (fl != null && AD_Org_ID != 0)
                                    {
                                        fl.SetAD_Org_ID(AD_Org_ID);
                                    }
                                }
                            }
                        }
                        dsSSCode.Dispose();

                        fl = fact.CreateLine(null, GetAccount(Doc.ACCTTYPE_PaymentSelect, as1), GetC_Currency_ID(), payAmt, null);
                        if (fl != null && AD_Org_ID != 0 && GetC_Charge_ID() == 0)              //	don't overwrite charge
                        {
                            fl.SetAD_Org_ID(AD_Org_ID);
                        }
                    }
                }

                // Default Posting Logic
                else
                {
                    MAccount acct = null;
                    if (GetC_Charge_ID() != 0)
                    {
                        acct = MCharge.GetAccount(GetC_Charge_ID(), as1, GetAmount());
                    }
                    else if (_Prepayment)
                    {
                        acct = GetAccount(Doc.ACCTTYPE_V_Prepayment, as1);
                    }
                    else
                    {
                        acct = GetAccount(Doc.ACCTTYPE_PaymentSelect, as1);
                    }
                    FactLine fl = fact.CreateLine(null, acct,
                                                  GetC_Currency_ID(), GetAmount(), null);
                    if (fl != null && AD_Org_ID != 0 &&
                        GetC_Charge_ID() == 0)                  //	don't overwrite charge
                    {
                        fl.SetAD_Org_ID(AD_Org_ID);
                    }

                    //	Asset
                    fl = fact.CreateLine(null, GetAccount(Doc.ACCTTYPE_BankInTransit, as1),
                                         GetC_Currency_ID(), null, GetAmount());
                    if (fl != null && AD_Org_ID != 0)
                    {
                        fl.SetAD_Org_ID(AD_Org_ID);
                    }
                }
            }
            else
            {
                _error = "DocumentType unknown: " + GetDocumentType();
                log.Log(Level.SEVERE, _error);
                fact = null;
            }
            //
            List <Fact> facts1 = new List <Fact>();

            facts1.Add(fact);
            return(facts1);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Create Facts (the accounting logic) for
        /// CMC.
        /// <pre>
        /// Expense
        /// CashExpense     DR
        ///        CashAsset               CR
        ///Receipt
        ///        CashAsset       DR
        ///        CashReceipt             CR
        ///  Charge
        ///        Charge          DR
        ///          CashAsset               CR
        ///  Difference
        ///          CashDifference  DR
        ///          CashAsset               CR
        ///  Invoice
        ///          CashAsset       DR
        ///          CashTransfer            CR
        ///  Transfer
        ///          BankInTransit   DR
        ///          CashAsset               CR
        ///  </pre>
        /// </summary>
        /// <param name="?"></param>
        /// <returns>Fact</returns>
        public override List <Fact> CreateFacts(MAcctSchema as1)
        {
            //  Need to have CashBook
            if (GetC_CashBook_ID() == 0)
            {
                _error = "C_CashBook_ID not set";
                log.Log(Level.SEVERE, _error);
                return(null);
            }

            //  create Fact Header
            Fact fact = new Fact(this, as1, Fact.POST_Actual);

            //  Header posting amt as1 Invoices and Transfer could be differenet currency
            //  CashAsset Total
            Decimal assetAmt           = Env.ZERO;
            Decimal TotalCurrLoss      = Env.ZERO;
            Decimal TotalCurrGain      = Env.ZERO;
            int     headerCashCurrency = 0;
            int     childCashCurrency  = 0;
            int     headerCashOrg      = 0;
            int     childCashOrg       = 0;



            //  Lines
            for (int i = 0; i < _lines.Length; i++)
            {
                DocLine_Cash line     = (DocLine_Cash)_lines[i];
                String       CashType = line.GetCashType();

                if (CashType.Equals(DocLine_Cash.CASHTYPE_EXPENSE))
                {   //  amount is negative
                    //  CashExpense     DR
                    //  CashAsset               CR
                    fact.CreateLine(line, GetAccount(Doc.ACCTTYPE_CashExpense, as1),
                                    GetC_Currency_ID(), Decimal.Negate(line.GetAmount()), null);
                    //	fact.CreateLine(line, GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                    //		p_vo.C_Currency_ID, null, line.GetAmount().negate());
                    assetAmt = Decimal.Subtract(assetAmt, Decimal.Negate(line.GetAmount()));
                }
                else if (CashType.Equals(DocLine_Cash.CASHTYPE_RECEIPT))
                {   //  amount is positive
                    //  CashAsset       DR
                    //  CashReceipt             CR
                    //	fact.CreateLine(line, GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                    //		p_vo.C_Currency_ID, line.GetAmount(), null);
                    assetAmt = Decimal.Add(assetAmt, line.GetAmount());
                    fact.CreateLine(line, GetAccount(Doc.ACCTTYPE_CashReceipt, as1),
                                    GetC_Currency_ID(), null, line.GetAmount());
                }
                else if (CashType.Equals(DocLine_Cash.CASHTYPE_CHARGE))
                {   //  amount is negative
                    //  Charge          DR
                    //  CashAsset               CR
                    fact.CreateLine(line, line.GetChargeAccount(as1, GetAmount()),
                                    GetC_Currency_ID(), Decimal.Negate(line.GetAmount()), null);
                    //	fact.CreateLine(line, GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                    //		p_vo.C_Currency_ID, null, line.GetAmount().negate());
                    assetAmt = Decimal.Subtract(assetAmt, Decimal.Negate(line.GetAmount()));
                }
                else if (CashType.Equals(DocLine_Cash.CASHTYPE_DIFFERENCE))
                {   //  amount is pos/neg
                    //  CashDifference  DR
                    //  CashAsset               CR
                    fact.CreateLine(line, GetAccount(Doc.ACCTTYPE_CashDifference, as1),
                                    GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                    //	fact.CreateLine(line, GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                    //		p_vo.C_Currency_ID, line.GetAmount());
                    assetAmt = Decimal.Add(assetAmt, line.GetAmount());
                }
                else if (CashType.Equals(DocLine_Cash.CASHTYPE_INVOICE))
                {   //  amount is pos/neg
                    //  CashAsset       DR      dr      --   Invoice is in Invoice Currency !
                    //  CashTransfer    cr      CR
                    if (line.GetC_Currency_ID() == GetC_Currency_ID())
                    {
                        assetAmt = Decimal.Add(assetAmt, line.GetAmount());
                    }
                    else
                    {
                        fact.CreateLine(line,
                                        GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                                        line.GetC_Currency_ID(), line.GetAmount());
                    }
                    fact.CreateLine(line,
                                    GetAccount(Doc.ACCTTYPE_CashTransfer, as1),
                                    line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                }
                else if (CashType.Equals(DocLine_Cash.CASHTYPE_TRANSFER))
                {   //  amount is pos/neg
                    //  BankInTransit   DR      dr      --  Transfer is in Bank Account Currency
                    //  CashAsset       dr      CR
                    int temp = GetC_BankAccount_ID();
                    SetC_BankAccount_ID(line.GetC_BankAccount_ID());
                    fact.CreateLine(line,
                                    GetAccount(Doc.ACCTTYPE_BankInTransit, as1),
                                    line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                    SetC_BankAccount_ID(temp);
                    if (line.GetC_Currency_ID() == GetC_Currency_ID())
                    {
                        assetAmt = Decimal.Add(assetAmt, line.GetAmount());
                    }
                    else
                    {
                        fact.CreateLine(line,
                                        GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                                        line.GetC_Currency_ID(), line.GetAmount());
                    }
                }
                // Change to Apply Posting Logic against BusinessPartner
                else if (CashType.Equals(DocLine_Cash.CASHTYPE_BUSINESSPARTNER))
                {   //  amount is pos/neg
                    //  CashAsset       DR      dr      --   Invoice is in Invoice Currency !
                    //  CashTransfer    cr      CR

                    MBPartner bPartner = new MBPartner(GetCtx(), line.GetC_BPartner_ID(), null);
                    if (bPartner != null)
                    {
                        if (bPartner.IsEmployee())
                        {
                            if (line.GetC_Currency_ID() == GetC_Currency_ID())
                            {
                                assetAmt = Decimal.Add(assetAmt, line.GetAmount());
                            }
                            else
                            {
                                fact.CreateLine(line,
                                                GetAccount(Doc.ACCTTYPE_E_Prepayment, as1, line.GetC_BPartner_ID()),
                                                line.GetC_Currency_ID(), line.GetAmount());
                            }
                            fact.CreateLine(line,
                                            GetAccount(Doc.ACCTTYPE_E_Prepayment, as1, line.GetC_BPartner_ID()),
                                            line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                        }
                        else if (bPartner.IsVendor())
                        {
                            if (line.GetC_Currency_ID() == GetC_Currency_ID())
                            {
                                assetAmt = Decimal.Add(assetAmt, line.GetAmount());
                            }
                            else
                            {
                                fact.CreateLine(line,
                                                GetAccount(Doc.ACCTTYPE_V_Prepayment, as1, line.GetC_BPartner_ID()),
                                                line.GetC_Currency_ID(), line.GetAmount());
                            }
                            fact.CreateLine(line,
                                            GetAccount(Doc.ACCTTYPE_V_Prepayment, as1, line.GetC_BPartner_ID()),
                                            line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                        }
                        else if (bPartner.IsCustomer())
                        {
                            if (line.GetC_Currency_ID() == GetC_Currency_ID())
                            {
                                assetAmt = Decimal.Add(assetAmt, line.GetAmount());
                            }
                            else
                            {
                                fact.CreateLine(line,
                                                GetAccount(Doc.ACCTTYPE_C_Prepayment, as1, line.GetC_BPartner_ID()),
                                                line.GetC_Currency_ID(), line.GetAmount());
                            }
                            fact.CreateLine(line,
                                            GetAccount(Doc.ACCTTYPE_C_Prepayment, as1, line.GetC_BPartner_ID()),
                                            line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                        }
                    }
                    else
                    {
                        if (line.GetC_Currency_ID() == GetC_Currency_ID())
                        {
                            assetAmt = Decimal.Add(assetAmt, line.GetAmount());
                        }
                        else
                        {
                            fact.CreateLine(line,
                                            GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                                            line.GetC_Currency_ID(), line.GetAmount());
                        }
                        fact.CreateLine(line,
                                        GetAccount(Doc.ACCTTYPE_CashTransfer, as1),
                                        line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                    }
                }


                //Code Added to CashBookTransfer

                else if (CashType.Equals(DocLine_Cash.CASHTYPE_CASHBOOKTRANSFER))
                {
                    ////  amount is negative
                    ////  Charge          DR
                    ////  CashAsset               CR
                    //fact.CreateLine(line, line.GetChargeAccount(as1, GetAmount()),
                    //    GetC_Currency_ID(), Decimal.Negate(line.GetAmount()), null);
                    ////	fact.CreateLine(line, GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                    ////		p_vo.C_Currency_ID, null, line.GetAmount().negate());
                    //assetAmt = Decimal.Subtract(assetAmt, Decimal.Negate(line.GetAmount()));
                    int temp = GetC_CashBook_ID();
                    SetC_CashBook_ID(line.Get_C_CashBook_ID());

                    if (BeginningBalance > 0)
                    {
                        fact.CreateLine(line,
                                        GetAccount(Doc.ACCTTYPE_CashTransfer, as1),
                                        line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                        SetC_CashBook_ID(temp);
                        //if (line.GetC_Currency_ID() == (new MCashBook(GetCtx(), GetC_CashBook_ID(), null)).GetC_Currency_ID())// GetC_Currency_ID())
                        //{
                        //if (BeginningBalance > assetAmt)
                        //if (BeginningBalance >= Math.Abs(line.GetAmount()))
                        //if (assetAmt <= Math.Abs(line.GetAmount()))
                        //{
                        assetAmt = (Decimal.Subtract(assetAmt, Decimal.Negate(line.GetAmount())));
                        //}
                        //else
                        //{
                        //    assetAmt = (Decimal.Subtract(assetAmt, Decimal.Negate(line.GetAmount())));
                        //}
                        //}
                        //else
                        //{
                        //    fact.CreateLine(line,
                        //        GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                        //        line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                        //}
                    }
                    else
                    {
                        //fact.CreateLine(line,
                        //    GetAccount(Doc.ACCTTYPE_CashTransfer, as1),
                        //    line.GetC_Currency_ID(), decimal.Negate(line.GetAmount()));
                        fact.CreateLine(line,
                                        GetAccount(Doc.ACCTTYPE_CashTransfer, as1),
                                        (new MCashBook(GetCtx(), HeaderCasbookID, null)).GetC_Currency_ID(), decimal.Negate(line.GetAmount()));
                        SetC_CashBook_ID(temp);
                        //if (line.GetC_Currency_ID() == (new MCashBook(GetCtx(), GetC_CashBook_ID(), null)).GetC_Currency_ID())// GetC_Currency_ID())
                        //{
                        assetAmt = (Decimal.Add(assetAmt, line.GetAmount()));
                        //}
                        //else
                        //{
                        //    fact.CreateLine(line,
                        //        GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                        //        line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                        //}
                    }
                }

                else if (CashType.Equals(DocLine_Cash.CASHTYPE_CASHRECIEVEDFROM))
                {
                    int temp = GetC_CashBook_ID();
                    //SetC_CashBook_ID(line.Get_C_CashBook_ID());
                    //fact.CreateLine(line,
                    //    GetAccount(Doc.ACCTTYPE_CashTransfer, as1),
                    //    line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                    //SetC_CashBook_ID(temp);
                    //if (line.GetC_Currency_ID() == GetC_Currency_ID())
                    //{
                    //assetAmt = (Decimal.Add(assetAmt, line.GetAmount()));
                    //}
                    //else
                    //{
                    //    fact.CreateLine(line,
                    //        GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                    //        line.GetC_Currency_ID(), line.GetAmount());
                    //}
                    headerCashCurrency = (new MCashBook(GetCtx(), HeaderCasbookID, null).GetC_Currency_ID());
                    childCashCurrency  = (new MCashBook(GetCtx(), line.Get_C_CashBook_ID(), null).GetC_Currency_ID());
                    headerCashOrg      = (new MCashBook(GetCtx(), HeaderCasbookID, null).GetAD_Org_ID());
                    childCashOrg       = (new MCashBook(GetCtx(), line.Get_C_CashBook_ID(), null).GetAD_Org_ID());

                    //else
                    //{
                    if (headerCashCurrency != childCashCurrency)
                    {
                        Decimal transferdAmt = Util.GetValueOfDecimal(DB.ExecuteScalar("SELECT Amount FROM C_CashLine WHERE C_CashLine_ID =" + line.Get_C_CashLine_Ref_ID()));
                        Decimal recievedAmt  = MConversionRate.Convert(GetCtx(), line.GetAmount(), headerCashCurrency, childCashCurrency, GetAD_Client_ID(), GetAD_Org_ID());
                        Decimal res          = Decimal.Subtract(recievedAmt, Math.Abs(transferdAmt));
                        if (res > 0)
                        {
                            TotalCurrGain = Decimal.Add(TotalCurrGain, res);
                        }
                        else if (res < 0)
                        {
                            TotalCurrLoss = Decimal.Add(TotalCurrLoss, Decimal.Negate(res));
                        }

                        SetC_CashBook_ID(line.Get_C_CashBook_ID());

                        int OrgID = line.GetAD_Org_ID();
                        if (headerCashOrg != childCashOrg)
                        {
                            OrgID = childCashOrg;
                        }
                        //else
                        //{
                        transferdAmt = MConversionRate.Convert(GetCtx(), transferdAmt, childCashCurrency, headerCashCurrency, GetAD_Client_ID(), GetAD_Org_ID());
                        //}
                        fact.CreateLine(line,
                                        GetAccount(Doc.ACCTTYPE_CashTransfer, as1),
                                        headerCashCurrency, transferdAmt, OrgID);
                        SetC_CashBook_ID(temp);
                    }
                    else
                    {
                        SetC_CashBook_ID(line.Get_C_CashBook_ID());
                        if (headerCashOrg != childCashOrg)
                        {
                            fact.CreateLine(line,
                                            GetAccount(Doc.ACCTTYPE_CashTransfer, as1),
                                            line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()), childCashOrg);
                        }
                        else
                        {
                            fact.CreateLine(line,
                                            GetAccount(Doc.ACCTTYPE_CashTransfer, as1),
                                            line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                        }
                        SetC_CashBook_ID(temp);
                    }


                    //if (headerCashOrg != childCashOrg)
                    //{
                    //    DataSet ds = DB.ExecuteDataset("SELECT INTERCOMPANYDUETO_ACCT,INTERCOMPANYDUEFROM_ACCT FROM C_AcctSchema_GL WHERE AD_Client_ID=" + GetAD_Client_ID());
                    //    int dueFrom = 0;
                    //    int dueTo = 0;
                    //    if (ds != null && ds.Tables[0].Rows.Count > 0)
                    //    {
                    //        dueTo = Util.GetValueOfInt(ds.Tables[0].Rows[0]["INTERCOMPANYDUETO_ACCT"]);
                    //        dueFrom = Util.GetValueOfInt(ds.Tables[0].Rows[0]["INTERCOMPANYDUEFROM_ACCT"]);
                    //        if (dueFrom > 0 && dueTo > 0)
                    //        {
                    //            fact.CreateLine(line, MAccount.Get(GetCtx(), dueFrom),line.GetC_Currency_ID(), line.GetAmount());
                    //            fact.CreateLine(line, MAccount.Get(GetCtx(), dueTo), line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                    //        }
                    //    }

                    //}
                    //}
                    assetAmt = (Decimal.Add(assetAmt, line.GetAmount()));
                }
            }   //  lines

            if (TotalCurrGain != Env.ZERO)
            {
                int      validComID = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT C_ValidCombination_ID FROM C_ValidCombination WHERE Account_ID= ( SELECT C_ElementValue_ID FROM C_ElementValue WHERE Value='80540' AND AD_Client_ID = " + GetAD_Client_ID() + " )"));
                MAccount acct       = MAccount.Get(GetCtx(), validComID);
                TotalCurrGain = MConversionRate.Convert(GetCtx(), TotalCurrGain, childCashCurrency, headerCashCurrency, GetAD_Client_ID(), GetAD_Org_ID());


                fact.CreateLine(null, acct,
                                GetC_Currency_ID(), Decimal.Negate(TotalCurrGain));
            }
            if (TotalCurrLoss != Env.ZERO)
            {
                int      validComID = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT C_ValidCombination_ID FROM C_ValidCombination WHERE Account_ID= ( SELECT C_ElementValue_ID FROM C_ElementValue WHERE Value='82540' AND AD_Client_ID = " + GetAD_Client_ID() + " )"));
                MAccount acct       = MAccount.Get(GetCtx(), validComID);
                TotalCurrLoss = MConversionRate.Convert(GetCtx(), TotalCurrLoss, childCashCurrency, headerCashCurrency, GetAD_Client_ID(), GetAD_Org_ID());


                fact.CreateLine(null, acct,
                                GetC_Currency_ID(), (TotalCurrLoss));
            }
            //

            //  Cash Asset
            fact.CreateLine(null, GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                            GetC_Currency_ID(), assetAmt);
            List <Fact> facts = new List <Fact>();

            facts.Add(fact);
            return(facts);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Create Facts (the accounting logic) for
        ///  ARP, APP.
        ///<pre>
        ///  ARP
        ///      BankInTransit   DR
        ///      UnallocatedCash         CR
        ///      or Charge/C_Prepayment
        ///  APP
        ///      PaymentSelect   DR
        ///      or Charge/V_Prepayment
        ///      BankInTransit           CR
        ///  CashBankTransfer
        ///      -
        ///  </pre>
        /// </summary>
        /// <param name="as1"></param>
        /// <returns>fact</returns>
        public override List <Fact> CreateFacts(MAcctSchema as1)
        {
            //  create Fact Header
            Fact fact = new Fact(this, as1, Fact.POST_Actual);

            //	Cash Transfer
            if ("X".Equals(_TenderType))
            {
                List <Fact> facts = new List <Fact>();
                facts.Add(fact);
                return(facts);
            }

            int AD_Org_ID = GetBank_Org_ID();           //	Bank Account Org

            if (GetDocumentType().Equals(MDocBaseType.DOCBASETYPE_ARRECEIPT))
            {
                //	Asset
                FactLine fl = fact.CreateLine(null, GetAccount(Doc.ACCTTYPE_BankInTransit, as1),
                                              GetC_Currency_ID(), GetAmount(), null);
                if (fl != null && AD_Org_ID != 0)
                {
                    fl.SetAD_Org_ID(AD_Org_ID);
                }
                //
                MAccount acct = null;
                if (GetC_Charge_ID() != 0)
                {
                    acct = MCharge.GetAccount(GetC_Charge_ID(), as1, GetAmount());
                }
                else if (_Prepayment)
                {
                    acct = GetAccount(Doc.ACCTTYPE_C_Prepayment, as1);
                }
                else
                {
                    acct = GetAccount(Doc.ACCTTYPE_UnallocatedCash, as1);
                }
                fl = fact.CreateLine(null, acct,
                                     GetC_Currency_ID(), null, GetAmount());
                if (fl != null && AD_Org_ID != 0 &&
                    GetC_Charge_ID() == 0)              //	don't overwrite charge
                {
                    fl.SetAD_Org_ID(AD_Org_ID);
                }
            }
            //  APP
            else if (GetDocumentType().Equals(MDocBaseType.DOCBASETYPE_APPAYMENT))
            {
                MAccount acct = null;
                if (GetC_Charge_ID() != 0)
                {
                    acct = MCharge.GetAccount(GetC_Charge_ID(), as1, GetAmount());
                }
                else if (_Prepayment)
                {
                    acct = GetAccount(Doc.ACCTTYPE_V_Prepayment, as1);
                }
                else
                {
                    acct = GetAccount(Doc.ACCTTYPE_PaymentSelect, as1);
                }
                FactLine fl = fact.CreateLine(null, acct,
                                              GetC_Currency_ID(), GetAmount(), null);
                if (fl != null && AD_Org_ID != 0 &&
                    GetC_Charge_ID() == 0)              //	don't overwrite charge
                {
                    fl.SetAD_Org_ID(AD_Org_ID);
                }

                //	Asset
                fl = fact.CreateLine(null, GetAccount(Doc.ACCTTYPE_BankInTransit, as1),
                                     GetC_Currency_ID(), null, GetAmount());
                if (fl != null && AD_Org_ID != 0)
                {
                    fl.SetAD_Org_ID(AD_Org_ID);
                }
            }
            else
            {
                _error = "DocumentType unknown: " + GetDocumentType();
                log.Log(Level.SEVERE, _error);
                fact = null;
            }
            //
            List <Fact> facts1 = new List <Fact>();

            facts1.Add(fact);
            return(facts1);
        }
Ejemplo n.º 4
0
        public override List <Fact> CreateFacts(MAcctSchema as1)
        {
            List <Fact>          facts   = new List <Fact>();
            MProductCategoryAcct pca     = null;
            String costingMethodOfSchema = as1.GetCostingMethod();
            String costingLevelOfSchema  = as1.GetCostingLevel();

            // Get the costing method and the costing level of the product Category for the current accounting schema.
            if (mpc != null)
            {
                pca = MProductCategoryAcct.Get(GetCtx(), mpc.GetM_Product_Category_ID(), as1.GetC_AcctSchema_ID(), null);
                if (pca.GetCostingMethod() != null)
                {
                    costingMethod = pca.GetCostingMethod();
                }
                else
                {
                    costingMethod = costingMethodOfSchema;
                }

                if (pca.GetCostingLevel() != null)
                {
                    costingLevel = pca.GetCostingLevel();
                }
                else
                {
                    costingLevel = costingLevelOfSchema;
                }

                // proceed only if the costing method is standard
                if (!costingMethod.Equals(X_C_AcctSchema.COSTINGMETHOD_StandardCosting))
                {
                    return(facts);
                }
            }

            Fact fact = new Fact(this, as1, Fact.POST_Actual);

            FactLine dr = null;
            FactLine cr = null;

            for (int i = 0; i < _lines.Length; i++)
            {
                DocLine line = _lines[i];

                if (mpc == null)
                {
                    pca = MProductCategoryAcct.Get(GetCtx(), MProduct.Get(GetCtx(), line.GetM_Product_ID()).
                                                   GetM_Product_Category_ID(),
                                                   as1.GetC_AcctSchema_ID(), null);
                    if (pca.GetCostingMethod() != null)
                    {
                        costingMethod = pca.GetCostingMethod();
                    }
                    else
                    {
                        costingMethod = costingMethodOfSchema;
                    }

                    if (pca.GetCostingLevel() != null)
                    {
                        costingLevel = pca.GetCostingLevel();
                    }
                    else
                    {
                        costingLevel = costingLevelOfSchema;
                    }

                    // proceed only if the costing method is standard
                    if (!costingMethod.Equals(X_C_AcctSchema.COSTINGMETHOD_StandardCosting))
                    {
                        return(facts);
                    }
                }
                Decimal currentCost = Env.ZERO;
                Decimal oldCost     = Env.ZERO;
                Decimal Qty         = Env.ZERO;
                String  sql         = " SELECT Sum(CurrentCostPrice), Sum(LastCostPrice), Sum(CurrentQty)"
                                      + " FROM M_Cost WHERE M_Product_ID = " + line.GetM_Product_ID()
                                      + " AND C_AcctSchema_ID = " + as1.GetC_AcctSchema_ID()
                                      + " AND M_CostElement_ID = " + m_ce.GetM_CostElement_ID()
                                      + " AND M_CostType_ID = " + as1.GetM_CostType_ID()
                                      + " AND AD_Client_ID = " + GetAD_Client_ID();
                if (costingLevel.Equals(X_C_AcctSchema.COSTINGLEVEL_Client))
                {
                    sql += " AND AD_Org_ID = 0"
                           + " AND M_AttributeSetInstance_ID  = 0";
                }
                else if (costingLevel.Equals(X_C_AcctSchema.COSTINGLEVEL_Organization))
                {
                    sql += " AND M_AttributeSetInstance_ID  = 0";
                }
                else if (costingLevel.Equals(X_C_AcctSchema.COSTINGLEVEL_BatchLot))
                {
                    sql += " AND AD_Org_ID = 0";
                }

                IDataReader idr = null;
                try
                {
                    idr = DB.ExecuteReader(sql, null, GetTrx());
                    while (idr.Read())
                    {
                        currentCost = Util.GetValueOfDecimal(idr[0]);
                        oldCost     = Util.GetValueOfDecimal(idr[1]);
                        Qty         = Util.GetValueOfDecimal(idr[2]);
                        line.SetQty(Qty, costupdate.IsSOTrx());

                        Decimal PriceCost = Decimal.Subtract(currentCost, oldCost);
                        Decimal amt       = Env.ZERO;
                        if (Env.Signum(PriceCost) != 0)
                        {
                            amt = Decimal.Multiply(Qty, PriceCost);
                        }

                        line.SetAmount(amt);

                        if (Env.Signum(amt) == 0)
                        {
                            continue;
                        }
                        MAccount db_acct, cr_acct;

                        /* Decide the Credit and Debit Accounts */
                        if (Env.Signum(amt) == 1)
                        {
                            db_acct = line.GetAccount(ProductCost.ACCTTYPE_P_Asset, as1);
                            cr_acct = line.GetAccount(ProductCost.ACCTTYPE_P_CostAdjustment, as1);
                        }
                        else
                        {
                            cr_acct = line.GetAccount(ProductCost.ACCTTYPE_P_Asset, as1);
                            db_acct = line.GetAccount(ProductCost.ACCTTYPE_P_CostAdjustment, as1);
                        }

                        /* Create Credit and Debit lines*/
                        dr = fact.CreateLine(line, db_acct, as1.GetC_Currency_ID(), Math.Abs(amt), null);
                        if (dr == null)
                        {
                            log.SaveError("NoProductCosts", "No Product Costs");
                            return(null);
                        }

                        cr = fact.CreateLine(line, cr_acct, as1.GetC_Currency_ID(), null, Math.Abs(amt));
                        if (cr == null)
                        {
                            log.SaveError("NoProductCosts", "No Product Costs");
                            return(null);
                        }
                    }
                    idr.Close();
                }
                catch (Exception e)
                {
                    log.Log(Level.SEVERE, sql, e);
                }
                finally
                {
                    if (idr != null)
                    {
                        idr.Close();
                        idr = null;
                    }
                }
            }
            facts.Add(fact);
            return(facts);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Create Facts (the accounting logic) for
        /// CMB.
        /// <pre>
        /// BankAsset       DR      CR  (Statement)
        /// BankInTransit   DR      CR              (Payment)
        /// Charge          DR          (Charge)
        /// Interest        DR      CR  (Interest)
        /// </pre>
        /// </summary>
        /// <param name="as1"></param>
        /// <returns></returns>
        public override List <Fact> CreateFacts(MAcctSchema as1)
        {
            //  create Fact Header
            Fact fact = new Fact(this, as1, Fact.POST_Actual);

            //  Header -- there may be different currency amounts

            FactLine fl        = null;
            int      AD_Org_ID = GetBank_Org_ID(); //	Bank Account Org

            //  Lines
            for (int i = 0; i < _lines.Length; i++)
            {
                DocLine_Bank line          = (DocLine_Bank)_lines[i];
                int          C_BPartner_ID = line.GetC_BPartner_ID();

                //  BankAsset       DR      CR  (Statement)
                fl = fact.CreateLine(line,
                                     GetAccount(Doc.ACCTTYPE_BankAsset, as1),
                                     line.GetC_Currency_ID(), line.GetStmtAmt());

                if (fl != null && AD_Org_ID != 0)
                {
                    fl.SetAD_Org_ID(AD_Org_ID);
                }
                if (fl != null && C_BPartner_ID != 0)
                {
                    fl.SetC_BPartner_ID(C_BPartner_ID);
                }

                //  BankInTransit   DR      CR              (Payment)
                fl = fact.CreateLine(line,
                                     GetAccount(Doc.ACCTTYPE_BankInTransit, as1),
                                     line.GetC_Currency_ID(), Decimal.Negate(line.GetTrxAmt()));
                if (fl != null)
                {
                    if (C_BPartner_ID != 0)
                    {
                        fl.SetC_BPartner_ID(C_BPartner_ID);
                    }
                    if (AD_Org_ID != 0)
                    {
                        fl.SetAD_Org_ID(AD_Org_ID);
                    }
                    else
                    {
                        fl.SetAD_Org_ID(line.GetAD_Org_ID(true)); // from payment
                    }
                }
                //  Charge          DR          (Charge)
                fl = fact.CreateLine(line, line.GetChargeAccount(as1, Decimal.Negate(line.GetChargeAmt())), line.GetC_Currency_ID(), Decimal.Negate(line.GetChargeAmt()), null);
                if (fl != null && C_BPartner_ID != 0)
                {
                    fl.SetC_BPartner_ID(C_BPartner_ID);
                }

                //  Interest        DR      CR  (Interest)
                if (Env.Signum(line.GetInterestAmt()) < 0)
                {
                    fl = fact.CreateLine(line,
                                         GetAccount(Doc.ACCTTYPE_InterestExp, as1), GetAccount(Doc.ACCTTYPE_InterestExp, as1),
                                         line.GetC_Currency_ID(), Decimal.Negate(line.GetInterestAmt()));
                }
                else
                {
                    fl = fact.CreateLine(line,
                                         GetAccount(Doc.ACCTTYPE_InterestRev, as1), GetAccount(Doc.ACCTTYPE_InterestRev, as1),
                                         line.GetC_Currency_ID(), Decimal.Negate(line.GetInterestAmt()));
                }
                if (fl != null && C_BPartner_ID != 0)
                {
                    fl.SetC_BPartner_ID(C_BPartner_ID);
                }
                //
                //	fact.createTaxCorrection();
            }
            //
            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);
        }
Ejemplo n.º 7
0
 private void CreateFactLine(Fact fact, DocLine_Cash line, MAcctSchema as1, decimal assetAmt)
 {
     fact.CreateLine(line, GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                     GetC_Currency_ID(), assetAmt);
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Create Facts (the accounting logic) for
        ///  MMP.
        ///  <pre>
        ///  Production
        ///      Inventory       DR      CR
        ///  </pre>
        /// </summary>
        /// <param name="as1"></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 pointer
            FactLine fl = null;

            for (int i = 0; i < _lines.Length; i++)
            {
                DocLine line = _lines[i];
                //	Calculate Costs
                Decimal?costs = null;
                if (line.IsProductionBOM())
                {
                    //	Get BOM Cost - Sum of individual lines
                    Decimal bomCost = Env.ZERO;
                    for (int ii = 0; ii < _lines.Length; ii++)
                    {
                        DocLine line0 = _lines[ii];
                        if (line0.GetM_ProductionPlan_ID() != line.GetM_ProductionPlan_ID())
                        {
                            continue;
                        }
                        if (!line0.IsProductionBOM())
                        {
                            bomCost = Decimal.Add(bomCost, line0.GetProductCosts(as1, line.GetAD_Org_ID(), false));
                        }
                    }
                    costs = Decimal.Negate(bomCost);
                }
                else
                {
                    costs = line.GetProductCosts(as1, line.GetAD_Org_ID(), false);
                }

                //  Inventory       DR      CR
                fl = fact.CreateLine(line,
                                     line.GetAccount(ProductCost.ACCTTYPE_P_Asset, as1),
                                     as1.GetC_Currency_ID(), costs);
                if (fl == null)
                {
                    _error = "No Costs for Line " + line.GetLine() + " - " + line;
                    return(null);
                }
                fl.SetM_Locator_ID(line.GetM_Locator_ID());
                fl.SetQty(line.GetQty());

                //	Cost Detail
                String description = line.GetDescription();
                if (description == null)
                {
                    description = "";
                }
                if (line.IsProductionBOM())
                {
                    description += "(*)";
                }

                if (!IsPosted())
                {
                    MCostDetail.CreateProduction(as1, line.GetAD_Org_ID(),
                                                 line.GetM_Product_ID(), line.GetM_AttributeSetInstance_ID(),
                                                 line.Get_ID(), 0,
                                                 Utility.Util.GetValueOfInt(costs), line.GetQty().Value,
                                                 description, GetTrx(), GetRectifyingProcess());
                }
            }
            //
            List <Fact> facts = new List <Fact>();

            facts.Add(fact);
            return(facts);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Create Facts (the accounting logic) for
        /// SOO, POO.
        /// <pre>
        /// Reservation (release)
        /// Expense			DR
        /// Offset					CR
        /// Commitment
        /// (to be released by Invoice Matching)
        /// Expense					CR
        /// Offset			DR
        /// </pre>
        /// </summary>
        /// <param name="as1">accounting schema</param>
        /// <returns>Fact</returns>
        public override List <Fact> CreateFacts(MAcctSchema as1)
        {
            List <Fact> facts = new List <Fact>();

            //  Purchase Order
            if (GetDocumentType().Equals(MDocBaseType.DOCBASETYPE_PURCHASEORDER))
            {
                UpdateProductPO(as1);

                //	Decimal grossAmt = getAmount(Doc.AMTTYPE_Gross);

                //  Commitment
                if (as1.IsCreateCommitment())
                {
                    Fact    fact  = new Fact(this, as1, Fact.POST_Commitment);
                    Decimal total = Env.ZERO;
                    for (int i = 0; i < _lines.Length; i++)
                    {
                        DocLine line = _lines[i];
                        Decimal cost = line.GetAmtSource();
                        total = Decimal.Add(total, cost);

                        //	Account
                        MAccount expense = line.GetAccount(ProductCost.ACCTTYPE_P_Expense, as1);
                        fact.CreateLine(line, expense, GetC_Currency_ID(), cost, null);
                    }
                    //	Offset
                    MAccount offset = GetAccount(ACCTTYPE_CommitmentOffset, as1);
                    if (offset == null)
                    {
                        _error = "@NotFound@ @CommitmentOffset_Acct@";
                        log.Log(Level.SEVERE, _error);
                        return(null);
                    }
                    fact.CreateLine(null, offset, GetC_Currency_ID(), null, total);
                    //
                    facts.Add(fact);
                }

                //  Reverse Reservation
                if (as1.IsCreateReservation())
                {
                    Fact    fact  = new Fact(this, as1, Fact.POST_Reservation);
                    Decimal total = Env.ZERO;
                    if (_requisitions == null)
                    {
                        _requisitions = LoadRequisitions();
                    }
                    for (int i = 0; i < _requisitions.Length; i++)
                    {
                        DocLine line = _requisitions[i];
                        Decimal cost = line.GetAmtSource();
                        total = Decimal.Add(total, cost);

                        //	Account
                        MAccount expense = line.GetAccount(ProductCost.ACCTTYPE_P_Expense, as1);
                        fact.CreateLine(line, expense, GetC_Currency_ID(), null, cost);
                    }
                    //	Offset
                    MAccount offset = GetAccount(ACCTTYPE_CommitmentOffset, as1);
                    if (offset == null)
                    {
                        _error = "@NotFound@ @CommitmentOffset_Acct@";
                        log.Log(Level.SEVERE, _error);
                        return(null);
                    }
                    fact.CreateLine(null, offset, GetC_Currency_ID(), total, null);
                    //
                    facts.Add(fact);
                }       //	reservations
            }
            //	SO
            return(facts);
        }
Ejemplo n.º 10
0
        /// <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 ((pca.GetCostingMethod() == "I" || as1.GetCostingMethod() == "I") && 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);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Create Facts (the accounting logic) for
        ///  MMI.
        ///  <pre>
        ///  Inventory
        ///      Inventory       DR      CR
        ///      InventoryDiff   DR      CR   (or Charge)
        ///  </pre>
        /// </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());

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

            for (int i = 0; i < _lines.Length; i++)
            {
                DocLine line  = _lines[i];
                Decimal costs = line.GetProductCosts(as1, line.GetAD_Org_ID(), false);
                if (Env.Signum(costs) == 0)
                {
                    _error = "No Costs for " + line.GetProduct().GetName();
                    return(null);
                }
                //  Inventory       DR      CR
                dr = fact.CreateLine(line,
                                     line.GetAccount(ProductCost.ACCTTYPE_P_Asset, as1),
                                     as1.GetC_Currency_ID(), costs);
                //  may be zero difference - no line created.
                if (dr == null)
                {
                    continue;
                }
                dr.SetM_Locator_ID(line.GetM_Locator_ID());

                //  InventoryDiff   DR      CR
                //	or Charge
                MAccount invDiff = line.GetChargeAccount(as1, Decimal.Negate(costs));
                if (invDiff == null)
                {
                    invDiff = GetAccount(Doc.ACCTTYPE_InvDifferences, as1);
                }
                cr = fact.CreateLine(line, invDiff,
                                     as1.GetC_Currency_ID(), Decimal.Negate(costs));
                if (cr == null)
                {
                    continue;
                }
                cr.SetM_Locator_ID(line.GetM_Locator_ID());
                cr.SetQty(Decimal.Negate(line.GetQty().Value));
                if (line.GetC_Charge_ID() != 0) //	explicit overwrite for charge
                {
                    cr.SetAD_Org_ID(line.GetAD_Org_ID());
                }
                if (!IsPosted())
                {
                    //	Cost Detail
                    MCostDetail.CreateInventory(as1, line.GetAD_Org_ID(),
                                                line.GetM_Product_ID(), line.GetM_AttributeSetInstance_ID(),
                                                line.Get_ID(), 0,
                                                costs, line.GetQty().Value,
                                                line.GetDescription(), GetTrx(), GetRectifyingProcess());
                }
            }
            //
            List <Fact> facts = new List <Fact>();

            facts.Add(fact);
            return(facts);
        }