private int ProcessPayments(string PaymentId, string OrderId, string totalCost, string CurrentAmountPaid,
                                    string AccountId, float AmountRemaining, string RemainingAmount, string DatePaid, string Type)
        {
            if (Page.IsValid)
            {
                Classes.CBankOfAccount      cba = new Classes.CBankOfAccount();
                Classes.CAccountTransaction cat = new Classes.CAccountTransaction();
                Classes.CPayment            cp  = new Classes.CPayment();
                Classes.CPaymentLine        cpl = new Classes.CPaymentLine();
                Classes.CSaleTransations    cs  = new Classes.CSaleTransations();
                Models.MAccountTransaction  mat = new Models.MAccountTransaction();
                Models.MPayments            mp  = new Models.MPayments();
                Models.PaymentLine          mpl = new Models.PaymentLine();

                #region Payments
                mp.Paid = (Convert.ToSingle(totalCost) - AmountRemaining).ToString();
                if (cp.UpdateAmountPaid(Convert.ToInt32(PaymentId), mp.Paid) < 0)
                {
                    return(-1);
                }
                #endregion

                #region PaymentLine
                mpl.PaymentId        = Convert.ToInt32(PaymentId);
                mpl.BankId           = Convert.ToInt32(AccountId);
                mpl.Date             = DatePaid;
                mpl.PaidAmount       = CurrentAmountPaid;
                mpl.RemainingAmount  = RemainingAmount;
                mpl.CumulativeAmount = (Convert.ToSingle(cpl.LastPaidAmount(mpl.PaymentId))
                                        + Convert.ToSingle(CurrentAmountPaid)).ToString();
                if (cpl.Save(mpl) < 0)
                {
                    return(-2);
                }
                #endregion

                #region Account Transactions
                float AccountTotal = cba.ReturnTotalOfAccountById(Convert.ToInt32(AccountId));
                mat.AccountId = AccountId;
                if (Type.Contains("Vendor"))
                {
                    mat.Description = "Payment Of Order Id[" + OrderId + "] Paid, Amount ["
                                      + CurrentAmountPaid + "]";
                    mat.Debit              = CurrentAmountPaid;
                    mat.Credit             = "0";
                    mat.Total              = (AccountTotal - Convert.ToSingle(CurrentAmountPaid)).ToString();
                    mat.CurrentTransaction = cs.GetIdByOrderId(Convert.ToInt32(OrderId)).ToString();
                    mat.Transactiontype    = "Debit";

                    mat.FiscalYearId = Session["FiscalYear"].ToString();
                    mat.eDate        = Convert.ToDateTime(DatePaid);
                }
                else if (Type.Contains("Client"))
                {
                    mat.Description = "Payment Of Order Id[" + OrderId + "] Recieved, Amount ["
                                      + CurrentAmountPaid + "]";
                    mat.Debit              = "0";
                    mat.Credit             = CurrentAmountPaid;
                    mat.CurrentTransaction = cs.GetIdByOrderId(Convert.ToInt32(OrderId)).ToString();
                    mat.Total              = (AccountTotal + Convert.ToSingle(CurrentAmountPaid)).ToString();
                    mat.Transactiontype    = "Credit";
                    mat.FiscalYearId       = Session["FiscalYear"].ToString();
                    mat.eDate              = Convert.ToDateTime(DatePaid);
                }
                else
                {
                    return(-3);
                }

                if (cat.Save(mat) < 0)
                {
                    return(-4);
                }
                if (cba.SetNewAccountTotal(Convert.ToInt32(AccountId), Convert.ToSingle(mat.Total)) < 0)
                {
                    return(-5);
                }
                #endregion

                #region Accounts
                if (Convert.ToSingle(mp.Paid) > 0)
                {
                    if (Type == "Vendor")
                    {
                        Classes.CJournal cj = new Classes.CJournal();
                        Models.MJournal  mj = new Models.MJournal();
                        mj.acc_id = Convert.ToInt32(Common.Constants.Accounts.ChartOfAccounts.MerchandiseInventory).ToString();
                        mj.amount = mp.Paid;
                        mj.des    = "Payment Recieved of Order id [" + OrderId + "]";
                        mj.e_date = (DatePaid);
                        mj.type   = Common.Constants.Accounts.Type.Debit.ToString();
                        cj.Save(mj);

                        mj        = new Models.MJournal();
                        mj.acc_id = Convert.ToInt32(Common.Constants.Accounts.ChartOfAccounts.AccountsPayable).ToString();
                        mj.amount = mp.Paid;
                        mj.des    = "Payment Recieved of Order id [" + OrderId + "]";
                        mj.e_date = (DatePaid);
                        mj.type   = Common.Constants.Accounts.Type.Credit.ToString();
                        cj.Save(mj);
                    }
                    else if (Type == "Client")
                    {
                        Classes.CJournal cj = new Classes.CJournal();
                        Models.MJournal  mj = new Models.MJournal();
                        mj.acc_id = Convert.ToInt32(Common.Constants.Accounts.ChartOfAccounts.CostOfGoodsSold).ToString();
                        mj.amount = mp.Paid;
                        mj.des    = "Payment Recieved of Order id [" + OrderId + "]";
                        mj.e_date = (DatePaid);
                        mj.type   = Common.Constants.Accounts.Type.Debit.ToString();
                        cj.Save(mj);

                        mj        = new Models.MJournal();
                        mj.acc_id = Convert.ToInt32(Common.Constants.Accounts.ChartOfAccounts.MerchandiseInventory).ToString();
                        mj.amount = mp.Paid;
                        mj.des    = "Payment Recieved of Order id [" + OrderId + "]";
                        mj.e_date = (DatePaid);
                        mj.type   = Common.Constants.Accounts.Type.Credit.ToString();
                        cj.Save(mj);

                        cj        = new Classes.CJournal();
                        mj        = new Models.MJournal();
                        mj.acc_id = Convert.ToInt32(Common.Constants.Accounts.ChartOfAccounts.AccountsRecievalbes).ToString();
                        mj.amount = mp.Paid;
                        mj.des    = "Payment Recieved of Order id [" + OrderId + "]";
                        mj.e_date = (DatePaid);
                        mj.type   = Common.Constants.Accounts.Type.Debit.ToString();
                        cj.Save(mj);

                        mj        = new Models.MJournal();
                        mj.acc_id = Convert.ToInt32(Common.Constants.Accounts.ChartOfAccounts.Sales).ToString();
                        mj.amount = mp.Paid;
                        mj.des    = "Payment Recieved of Order id [" + OrderId + "]";
                        mj.e_date = (DatePaid);
                        mj.type   = Common.Constants.Accounts.Type.Credit.ToString();
                        cj.Save(mj);
                    }
                }
                #endregion
            }

            return(1);
        }
        private int ProcessPayments(string PaymentId, string OrderId, string totalCost, string CurrentAmountPaid,
                                    string AccountId, float AmountRemaining, string RemainingAmount, string DatePaid, string Type, string Cheque)
        {
            if (Page.IsValid)
            {
                Classes.CBankOfAccount      cba = new Classes.CBankOfAccount();
                Classes.CAccountTransaction cat = new Classes.CAccountTransaction();
                Classes.CCashAccount        cha = new Classes.CCashAccount();
                Classes.CCashTransaction    cht = new Classes.CCashTransaction();
                Classes.CPayment            cp  = new Classes.CPayment();
                Classes.CPaymentLine        cpl = new Classes.CPaymentLine();
                Classes.CSaleTransations    cs  = new Classes.CSaleTransations();
                Models.MAccountTransaction  mat = new Models.MAccountTransaction();
                Models.MPayments            mp  = new Models.MPayments();
                Models.PaymentLine          mpl = new Models.PaymentLine();
                Models.MCashTransactions    mht = new Models.MCashTransactions();
                Models.MCashAccount         mha = new Models.MCashAccount();

                #region Payments
                mp.Paid = (Convert.ToSingle(totalCost) - AmountRemaining).ToString();
                if (cp.UpdateAmountPaid(Convert.ToInt32(PaymentId), mp.Paid) < 0)
                {
                    return(-1);
                }
                #endregion

                #region PaymentLine
                mpl.PaymentId        = Convert.ToInt32(PaymentId);
                mpl.BankId           = Convert.ToInt32(AccountId);
                mpl.Date             = DatePaid;
                mpl.PaidAmount       = CurrentAmountPaid;
                mpl.RemainingAmount  = RemainingAmount;
                mpl.CumulativeAmount = (Convert.ToSingle(cpl.LastPaidAmount(mpl.PaymentId))
                                        + Convert.ToSingle(CurrentAmountPaid)).ToString();
                int OptionValue = Convert.ToInt32(ddlOption.SelectedValue);
                mpl.Cheque = Cheque;
                switch (OptionValue)
                {
                case -1:
                {
                    return(-1);
                }

                case 1:
                {
                    #region Account Transactions
                    mpl.ModeOfPayment = Common.Constants.ModeOfPayment.Cheque.ToString();
                    float AccountTotal = cba.ReturnTotalOfAccountById(Convert.ToInt32(AccountId));
                    mat.AccountId = AccountId;
                    if (Type.Contains("Vendor"))
                    {
                        mat.Description = "Payment Of Order Id[" + OrderId + "] Paid, Amount ["
                                          + CurrentAmountPaid + "]";
                        mat.Debit              = CurrentAmountPaid;
                        mat.Credit             = "0";
                        mat.Total              = (AccountTotal - Convert.ToSingle(CurrentAmountPaid)).ToString();
                        mat.CurrentTransaction = cs.GetIdByOrderId(Convert.ToInt32(OrderId)).ToString();
                        mat.Transactiontype    = "Debit";

                        mat.FiscalYearId = Session["FiscalYear"].ToString();
                        mat.eDate        = Convert.ToDateTime(DatePaid);
                    }
                    else if (Type.Contains("Client"))
                    {
                        mat.Description = "Payment Of Order Id[" + OrderId + "] Recieved, Amount ["
                                          + CurrentAmountPaid + "]";
                        mat.Debit              = "0";
                        mat.Credit             = CurrentAmountPaid;
                        mat.CurrentTransaction = cs.GetIdByOrderId(Convert.ToInt32(OrderId)).ToString();
                        mat.Total              = (AccountTotal + Convert.ToSingle(CurrentAmountPaid)).ToString();
                        mat.Transactiontype    = "Credit";
                        mat.FiscalYearId       = Session["FiscalYear"].ToString();
                        mat.eDate              = Convert.ToDateTime(DatePaid);
                    }
                    else
                    {
                        return(-3);
                    }

                    if (cat.Save(mat) < 0)
                    {
                        return(-4);
                    }
                    if (cba.SetNewAccountTotal(Convert.ToInt32(AccountId), Convert.ToSingle(mat.Total)) < 0)
                    {
                        return(-5);
                    }
                    #endregion
                    break;
                }

                case 2:
                {
                    #region Cash Transaction
                    mpl.ModeOfPayment = Common.Constants.ModeOfPayment.Cash.ToString();
                    mpl.Cheque        = "-";
                    float AccountTotal = cha.ReturnTotalOfCashAccount(Convert.ToInt32(AccountId));
                    mht.CashAccountId = Convert.ToInt32(AccountId);
                    if (Type.Contains("Vendor"))
                    {
                        mht.Credit      = "0";
                        mht.Debit       = CurrentAmountPaid;
                        mht.Description = "Payment Of Order Id[" + OrderId + "] Paid, Amount ["
                                          + CurrentAmountPaid + "]";
                        mht.eDate           = (DatePaid);
                        mht.FiscalYearId    = Convert.ToInt32(Session["FiscalYear"].ToString());
                        mht.OrderId         = Convert.ToInt32(OrderId);
                        mht.Total           = totalCost;
                        mht.TransactionId   = -1;
                        mht.TransactionType = "Debit";
                        mht.UserId          = Session["User"].ToString();
                        mht.WareHouseId     = Convert.ToInt32(Session["WareHouse"].ToString());
                    }
                    else if (Type.Contains("Client"))
                    {
                        mht.Credit      = CurrentAmountPaid;
                        mht.Debit       = "0";
                        mht.Description = "Payment Of Order Id[" + OrderId + "] Recieved, Amount ["
                                          + CurrentAmountPaid + "]";
                        mht.eDate           = (DatePaid);
                        mht.FiscalYearId    = Convert.ToInt32(Session["FiscalYear"].ToString());
                        mht.OrderId         = Convert.ToInt32(OrderId);
                        mht.Total           = totalCost;
                        mht.TransactionId   = -1;
                        mht.TransactionType = "Debit";
                        mht.UserId          = Session["User"].ToString();
                        mht.WareHouseId     = Convert.ToInt32(Session["WareHouse"].ToString());
                    }
                    else
                    {
                        return(-3);
                    }

                    if (cht.Save(mht) < 0)
                    {
                        return(-4);
                    }
                    if (cha.SetNewAccountTotal(Convert.ToInt32(AccountId), Convert.ToSingle(mat.Total)) < 0)
                    {
                        return(-5);
                    }
                    #endregion
                    break;
                }

                default:
                    return(-1);
                }
                if (cpl.Save(mpl) < 0)
                {
                    return(-2);
                }
                #endregion



                #region Accounts
                if (Convert.ToSingle(mp.Paid) > 0)
                {
                    if (Type == "Vendor")
                    {
                        Classes.CJournal cj = new Classes.CJournal();
                        Models.MJournal  mj = new Models.MJournal();
                        mj.acc_id = Convert.ToInt32(Common.Constants.Accounts.ChartOfAccounts.MerchandiseInventory).ToString();
                        mj.amount = mp.Paid;
                        mj.des    = "Payment Recieved of Order id [" + OrderId + "]";
                        mj.e_date = (DatePaid);
                        mj.type   = Common.Constants.Accounts.Type.Debit.ToString();
                        cj.Save(mj);

                        mj        = new Models.MJournal();
                        mj.acc_id = Convert.ToInt32(Common.Constants.Accounts.ChartOfAccounts.AccountsPayable).ToString();
                        mj.amount = mp.Paid;
                        mj.des    = "Payment Recieved of Order id [" + OrderId + "]";
                        mj.e_date = (DatePaid);
                        mj.type   = Common.Constants.Accounts.Type.Credit.ToString();
                        cj.Save(mj);
                    }
                    else if (Type == "Client")
                    {
                        Classes.CJournal cj = new Classes.CJournal();
                        Models.MJournal  mj = new Models.MJournal();
                        mj.acc_id = Convert.ToInt32(Common.Constants.Accounts.ChartOfAccounts.CostOfGoodsSold).ToString();
                        mj.amount = mp.Paid;
                        mj.des    = "Payment Recieved of Order id [" + OrderId + "]";
                        mj.e_date = (DatePaid);
                        mj.type   = Common.Constants.Accounts.Type.Debit.ToString();
                        cj.Save(mj);

                        mj        = new Models.MJournal();
                        mj.acc_id = Convert.ToInt32(Common.Constants.Accounts.ChartOfAccounts.MerchandiseInventory).ToString();
                        mj.amount = mp.Paid;
                        mj.des    = "Payment Recieved of Order id [" + OrderId + "]";
                        mj.e_date = (DatePaid);
                        mj.type   = Common.Constants.Accounts.Type.Credit.ToString();
                        cj.Save(mj);

                        cj        = new Classes.CJournal();
                        mj        = new Models.MJournal();
                        mj.acc_id = Convert.ToInt32(Common.Constants.Accounts.ChartOfAccounts.AccountsRecievalbes).ToString();
                        mj.amount = mp.Paid;
                        mj.des    = "Payment Recieved of Order id [" + OrderId + "]";
                        mj.e_date = (DatePaid);
                        mj.type   = Common.Constants.Accounts.Type.Debit.ToString();
                        cj.Save(mj);

                        mj        = new Models.MJournal();
                        mj.acc_id = Convert.ToInt32(Common.Constants.Accounts.ChartOfAccounts.Sales).ToString();
                        mj.amount = mp.Paid;
                        mj.des    = "Payment Recieved of Order id [" + OrderId + "]";
                        mj.e_date = (DatePaid);
                        mj.type   = Common.Constants.Accounts.Type.Credit.ToString();
                        cj.Save(mj);
                    }
                }
                #endregion
            }
            lblSalesAmountRemaining.Text = AmountRemaining.ToString();
            lblSalesAmountPaid.Text      = (Convert.ToSingle(totalCost) - AmountRemaining).ToString();
            return(1);
        }