public ActionResult GetAllDataForAllBranch()
        {
            //RBACUser rUser = new RBACUser(Session["UserName"].ToString());
            //if (!rUser.HasPermission("Receive_Insert"))
            //{
            //    return Json("X", JsonRequestBehavior.AllowGet);
            //}
            var    Bills = default(dynamic);
            var    gset  = _GsetService.All().FirstOrDefault();
            string sql   = string.Format("select  Accode,AcName,BranchCode,'Cash' as AccType from NewChart where Accode LIKE ('" + gset.GCa + "%')  Union select  Accode ,AcName,BranchCode,'Bank' as AccType from NewChart where( Accode LIKE ('" + gset.GBa + "%'))");
            //  string sql = string.Format("select *from NewChart");
            IEnumerable <branchWiseShow> VchrLst;

            using (AcclineERPContext dbContext = new AcclineERPContext())
            {
                VchrLst = dbContext.Database.SqlQuery <branchWiseShow>(sql).ToList();
                Bills   = VchrLst;
            }
            return(Json(new { data = Bills }, JsonRequestBehavior.AllowGet));
        }
        public ActionResult SaveDepositToBank(Deposit deposit)
        {
            RBACUser rUser = new RBACUser(Session["UserName"].ToString());

            if (!rUser.HasPermission("BankDeposit_Insert"))
            {
                return(Json("X", JsonRequestBehavior.AllowGet));
            }
            List <CashOperationVModel> CashOPVM = new List <CashOperationVModel>();

            try
            {
                var IfExit = _DepositToBankService.All().Where(x => x.DepositNo == deposit.DepositNo).FirstOrDefault();
                if (IfExit == null)
                {
                    if (!string.IsNullOrEmpty(deposit.bankAccode) && deposit.bankAccode.Trim() != "Select Bank A/C")
                    {
                        deposit.BranchCode = Session["BranchCode"].ToString();
                        var GCa = _AcBrService.All().Where(s => s.BranchCode == deposit.BranchCode && s.Ca_Ba == "Ca").Select(x => x.Accode).FirstOrDefault();
                        if (GCa == null)
                        {
                            var gset = _gsetService.All().LastOrDefault();
                            GCa = gset.GCa;
                        }

                        deposit.FinYear  = Session["FinYear"].ToString();
                        deposit.NewChart = _NewChartService.All().ToList().FirstOrDefault(x => x.Accode == deposit.bankAccode.Trim());
                        //cashReceipt.Customer = _CustomerService.All().ToList().FirstOrDefault(x => x.CustCode == cashReceipt.CustCode);
                        CashOperation cashOperation   = new CashOperation(deposit.DepositDate, 0.0, 0.0, 0.0, 0.0, "", DateTime.Now, false, deposit.FinYear, false, deposit.BranchCode);
                        var           isAlreadyClosed = _CashOperationService.All().ToList().FirstOrDefault(x => x.BranchCode == deposit.BranchCode && x.TrDate.ToString("MM-dd-yyyy") == deposit.DepositDate.ToString("MM-dd-yyyy"));


                        var           openBal       = GetOpenBal(deposit.DepositDate, deposit.bankAccode);
                        var           rcecBal       = GetAllRemittances(deposit.DepositDate, deposit.BranchCode, deposit.bankAccode).Sum(x => x.Amount);
                        var           payTotal      = GetAllPayment(deposit.DepositDate, deposit.BranchCode, deposit.bankAccode).Sum(x => x.Amount);
                        var           closBal       = openBal + rcecBal - payTotal;
                        BankOperation bankOperation = new BankOperation(deposit.DepositDate, openBal, deposit.Amount, 0.0, closBal, deposit.BranchCode, deposit.FinYear, false, deposit.bankAccode);

                        var sysSet = _sysSetService.All().FirstOrDefault();


                        var isAlreadySaved = _BankOperationService.All().ToList().FirstOrDefault(x => x.TrDate.ToString("MM-dd-yyyy") == deposit.DepositDate.ToString("MM-dd-yyyy") && x.BranchCode == deposit.BranchCode && x.bankAccode == deposit.bankAccode);

                        if (isAlreadyClosed == null || isAlreadySaved == null)
                        {
                            using (var transaction = new TransactionScope())
                            {
                                try
                                {
                                    _CashOperationService.Add(cashOperation);
                                    _CashOperationService.Save();
                                    _BankOperationService.Add(bankOperation);
                                    _BankOperationService.Save();
                                    _DepositToBankService.Add(deposit);
                                    _DepositToBankService.Save();
                                    TransactionLogService.SaveTransactionLog(_transactionLogService, "Deposit To Bank", "Save", deposit.DepositNo, Session["UserName"].ToString());
                                    LoadDropDown.journalVoucherSave("BD", "I", deposit.DepositNo, deposit.VoucherNo, deposit.FinYear, "01", deposit.BranchCode, deposit.DepositDate, GCa, Session["UserName"].ToString());
                                    CashOPVM = GetAllExpenseAndDeposit(deposit.DepositDate, deposit.BranchCode);
                                    transaction.Complete();
                                }
                                catch (Exception)
                                {
                                    transaction.Dispose();
                                    return(Json("0", JsonRequestBehavior.AllowGet));
                                }
                                return(Json(CashOPVM, JsonRequestBehavior.AllowGet));
                            }
                        }
                        else if (isAlreadyClosed != null && sysSet.CashRule == "O" ? true : isAlreadyClosed.IsClosed == false)
                        {
                            var rcecBals = GetAllRemittances(deposit.DepositDate, deposit.BranchCode, deposit.bankAccode).Sum(x => x.Amount);
                            isAlreadySaved.RecTotal = rcecBals + deposit.Amount;
                            isAlreadySaved.CloseBal = isAlreadySaved.OpenBal + isAlreadySaved.RecTotal - isAlreadySaved.PayTotal;

                            using (var transaction = new TransactionScope())
                            {
                                try
                                {
                                    _BankOperationService.Update(isAlreadySaved);
                                    _BankReceiptAppService.Save();
                                    _DepositToBankService.Add(deposit);
                                    _DepositToBankService.Save();
                                    TransactionLogService.SaveTransactionLog(_transactionLogService, "Deposit To Bank", "Save", deposit.DepositNo, Session["UserName"].ToString());
                                    LoadDropDown.journalVoucherSave("BD", "I", deposit.DepositNo, deposit.VoucherNo, deposit.FinYear, "01", deposit.BranchCode, deposit.DepositDate, GCa, Session["UserName"].ToString());
                                    if (sysSet.CashRule == "O")
                                    {
                                        LoadDropDown.CashRecalculate(Session["ProjCode"].ToString(), deposit.BranchCode, deposit.FinYear, Convert.ToDecimal(deposit.Amount), deposit.DepositDate);
                                    }
                                    CashOPVM = GetAllExpenseAndDeposit(deposit.DepositDate, deposit.BranchCode);
                                    transaction.Complete();
                                }
                                catch (Exception)
                                {
                                    transaction.Dispose();
                                    return(Json("0", JsonRequestBehavior.AllowGet));
                                }
                                return(Json(CashOPVM, JsonRequestBehavior.AllowGet));
                            }
                        }
                        else
                        {
                            return(Json("2", JsonRequestBehavior.AllowGet));
                        }
                    }
                    else
                    {
                        return(Json("1", JsonRequestBehavior.AllowGet));
                    }
                }
                else
                {
                    return(Json("3", JsonRequestBehavior.AllowGet));
                }
            }
            catch (Exception ex)
            {
                return(Json("0", JsonRequestBehavior.AllowGet));
            }
        }
        public ActionResult SavePayment(Payment payment)
        {
            RBACUser rUser = new RBACUser(Session["UserName"].ToString());

            if (!rUser.HasPermission("BankPayment_Insert"))
            {
                return(Json("X", JsonRequestBehavior.AllowGet));
            }
            List <BankOperationVModel> CashOPVM = new List <BankOperationVModel>();
            var gset = _gsetService.All().LastOrDefault();

            try
            {
                var IfExit = _paymentService.All().Where(x => x.PayCode == payment.PayCode).FirstOrDefault();
                if (IfExit == null)
                {
                    payment.BranchCode = Session["BranchCode"].ToString();
                    payment.FinYear    = Session["FinYear"].ToString();
                    payment.bankAccode = Session["BankAccode"].ToString();


                    var openBal  = GetOpenBal(payment.PayDate);
                    var rcecBal  = GetAllRemittances(payment.PayDate, payment.BranchCode, payment.bankAccode).Sum(x => x.Amount);
                    var payTotal = GetAllPayment(payment.PayDate, payment.BranchCode, payment.bankAccode).Sum(x => x.Amount);
                    var closBal  = openBal + rcecBal - payTotal;

                    BankOperation bankOperation = new BankOperation(payment.PayDate, openBal, 0.0, payment.Amount, closBal, payment.BranchCode, payment.FinYear, false, payment.bankAccode);
                    // BankOperation bankOperation = new BankOperation(payment.PayDate, 0.0, 0.0, 0.0, 0.0, payment.BranchCode, payment.FinYear, false, payment.bankAccode);
                    var isAlreadySaved = _bankOperationService.All().ToList().FirstOrDefault(x => x.TrDate.ToString("MM-dd-yyyy") == payment.PayDate.ToString("MM-dd-yyyy") && x.BranchCode == payment.BranchCode && x.bankAccode == payment.bankAccode);

                    if (isAlreadySaved == null)
                    {
                        using (var transaction = new TransactionScope())
                        {
                            try
                            {
                                _bankOperationService.Add(bankOperation);
                                _bankOperationService.Save();
                                _paymentService.Add(payment);
                                _paymentService.Save();
                                //Insert to provision
                                LoadDropDown.journalVoucherSave("BP", "I", payment.PayCode, payment.VoucherNo, payment.FinYear, "01", payment.BranchCode, payment.PayDate, payment.bankAccode, Session["UserName"].ToString());
                                TransactionLogService.SaveTransactionLog(_transactionLogService, "Payment", "Save", payment.PayCode, User.Identity.Name);
                                CashOPVM = GetAllPayment(payment.PayDate, payment.BranchCode, payment.bankAccode);
                                transaction.Complete();
                            }
                            catch (Exception)
                            {
                                transaction.Dispose();
                                return(Json("0", JsonRequestBehavior.AllowGet));
                            }
                        }
                        return(Json(CashOPVM, JsonRequestBehavior.AllowGet));
                    }
                    else
                    {
                        var payTotals = GetAllPayment(payment.PayDate, payment.BranchCode, payment.bankAccode).Sum(x => x.Amount);
                        isAlreadySaved.PayTotal = payTotals + payment.Amount;
                        isAlreadySaved.CloseBal = isAlreadySaved.OpenBal + isAlreadySaved.RecTotal - isAlreadySaved.PayTotal;
                        using (var transaction = new TransactionScope())
                        {
                            try
                            {
                                _bankOperationService.Update(isAlreadySaved);
                                _bankOperationService.Save();
                                _paymentService.Add(payment);
                                _paymentService.Save();
                                //Insert to provision
                                LoadDropDown.journalVoucherSave("BP", "I", payment.PayCode, payment.VoucherNo, payment.FinYear, "01", payment.BranchCode, payment.PayDate, payment.bankAccode, Session["UserName"].ToString());
                                var sysSet = _sysSetService.All().FirstOrDefault();
                                if (sysSet.CashRule == "O")
                                {
                                    LoadDropDown.BankRecalculate(payment.PayDate, Convert.ToDecimal(payment.Amount), Session["ProjCode"].ToString(), payment.BranchCode, payment.FinYear, payment.bankAccode);
                                }
                                TransactionLogService.SaveTransactionLog(_transactionLogService, "Payment", "Save", payment.PayCode, User.Identity.Name);
                                CashOPVM = GetAllPayment(payment.PayDate, payment.BranchCode, payment.bankAccode);
                                transaction.Complete();
                            }
                            catch (Exception)
                            {
                                transaction.Dispose();
                                return(Json("0", JsonRequestBehavior.AllowGet));
                            }
                        }

                        return(Json(CashOPVM, JsonRequestBehavior.AllowGet));
                    }
                }
                else
                {
                    return(Json("1", JsonRequestBehavior.AllowGet));
                }
            }
            catch (Exception)
            {
                return(Json("0", JsonRequestBehavior.AllowGet));
            }
        }
        public ActionResult SaveCashMR(MoneyReceiptVM MrVM, List <MoneyReceiptExt> AdjBills)
        {
            using (var transaction = new TransactionScope())
            {
                try
                {
                    RBACUser rUser = new RBACUser(Session["UserName"].ToString());
                    if (!rUser.HasPermission("MoneyReceiptCash_Insert"))
                    {
                        return(Json("X", JsonRequestBehavior.AllowGet));
                    }
                    //----Edit Nasir---//
                    //var cabkop = "";
                    //var sysSet = _sysSetService.All().FirstOrDefault();
                    //if (sysSet.ActualBasis == true)
                    //{
                    //var cabkop = _defACService.All().ToList().FirstOrDefault().CashAc;
                    //}
                    //else
                    //{
                    //    cabkop = _defACService.All().ToList().FirstOrDefault().CashAc;
                    //}
                    //---  ---//
                    string CRNo   = "";
                    var    IfExit = _moneyReceiptService.All().Where(x => x.MRNo == MrVM.MRNo).FirstOrDefault();
                    if (IfExit == null)
                    {
                        //var todayDate = DateTime.Now;
                        CashReceipt           CR   = new CashReceipt();
                        CashReceiptSubDetails CRSD = new CashReceiptSubDetails();
                        MoneyReceipt          MR   = new MoneyReceipt();
                        MR.MRSL        = MrVM.MRSL;
                        MR.BranchCode  = (MrVM.BranchCode == null) ? Session["BranchCode"].ToString() : MrVM.BranchCode;
                        MR.ProjCode    = (MrVM.ProjCode == null) ? Session["ProjCode"].ToString() : MrVM.ProjCode;
                        MR.MRNo        = MrVM.MRNo;
                        MR.MRDate      = MrVM.MRDate; //.AddHours(todayDate.Hour).AddMinutes(todayDate.Minute).AddSeconds(todayDate.Second).AddMilliseconds(todayDate.Millisecond);
                        MR.MRAgainst   = MrVM.MRAgainst;
                        MR.VchrNo      = MrVM.VchrNo;
                        MR.PayMode     = "Ca";
                        MR.Posted      = MrVM.Posted;
                        MR.AdjWithBill = MrVM.AdjWithBill;
                        MR.MRAmount    = MrVM.Amount;

                        // Accode ????
                        MR.Accode = MrVM.MRAgainst; // "1.2.001";

                        MR.CustCode = MrVM.CustCode;
                        MR.FinYear  = Session["FinYear"].ToString();
                        MR.Remarks  = MrVM.Remarks;
                        MR.JobNo    = MrVM.JobNo;
                        //MR.Ca_Bk = cabkop;

                        // CR.ReceiptNo = _CashReceiptController.GenerateReceiptNo(MR.BranchCode).Substring(6, 0);//MrVM.MRNo;
                        CR.ReceiptNo = _CashReceiptController.GenerateReceiptNo(MR.BranchCode);
                        // CR.ReceiptNo = MrVM.MRNo;
                        CRNo           = CR.ReceiptNo;
                        CR.ReceiptDate = (DateTime)MrVM.MRDate;
                        CR.purAccode   = MrVM.MRAgainst;// "1.2.004";
                        CR.RefNo       = MrVM.MRSL;
                        CR.Amount      = (Double)MrVM.Amount;
                        CR.Advance     = false;
                        CR.Remarks     = MrVM.Remarks;
                        CR.GLPost      = false;
                        CR.BranchCode  = MrVM.BranchCode;
                        CR.VoucherNo   = MrVM.VchrNo;
                        CR.BranchCode  = MR.BranchCode;
                        CR.FinYear     = Session["FinYear"].ToString();

                        CRSD.SubCode       = MrVM.CustCode;
                        CRSD.Accode        = MrVM.MRAgainst;
                        CRSD.Amount        = (Double)MrVM.Amount;
                        CRSD.CashReceiptNo = CRNo;

                        //CR.purAccode = cabkop; // "1.2.004";
                        _moneyReceiptService.Add(MR);
                        _CashReceiptService.Add(CR);
                        _CashReceiptSubDetailsAppService.Add(CRSD);
                        _CashReceiptService.Save();
                        _moneyReceiptService.Save();
                        _CashReceiptSubDetailsAppService.Save();

                        //List<MoneyReceiptExt> MRExtList = new List<MoneyReceiptExt>();

                        if (AdjBills != null)
                        {
                            decimal TotAmt = MrVM.Amount;
                            foreach (var bill in AdjBills)
                            {
                                MoneyReceiptExt MRExt    = new MoneyReceiptExt();
                                var             saleMain = _salesMainService.All().Where(s => s.SaleNo == bill.SaleNo).FirstOrDefault();
                                MRExt.MRId        = _moneyReceiptService.All().OrderBy(s => s.MRId).LastOrDefault().MRId;
                                MRExt.SaleNo      = bill.SaleNo;
                                MRExt.InstallNo   = _moneyReceiptExtService.All().OrderBy(s => s.InstallNo).Where(x => x.SaleNo == bill.SaleNo).Select(s => s.InstallNo).LastOrDefault();
                                MRExt.InstallNo   = MRExt.InstallNo + 1;
                                MRExt.InstallDate = DateTime.Now;
                                if (TotAmt >= bill.Amount)
                                {
                                    MRExt.Amount = bill.Amount;
                                    TotAmt       = TotAmt - bill.Amount;

                                    saleMain.ReceiptNo  = MrVM.MRNo;
                                    saleMain.ReceiptAmt = bill.Amount;
                                    saleMain.IsPaid     = true;
                                    _salesMainService.Update(saleMain);
                                    _salesMainService.Save();

                                    _moneyReceiptExtService.Add(MRExt);
                                    _moneyReceiptExtService.Save();
                                }
                                else
                                {
                                    if (bill.IsPaid_mre)
                                    {
                                        saleMain.IsPaid = true;
                                        MRExt.Amount    = TotAmt;

                                        //TotAmt = TotAmt - bill.Amount;
                                    }
                                    else
                                    {
                                        saleMain.IsPaid = false;
                                        MRExt.Amount    = TotAmt;
                                        // MRExt.Amount = bill.Amount;
                                        //TotAmt = TotAmt - bill.Amount;
                                    }


                                    saleMain.ReceiptNo  = MrVM.MRNo;
                                    saleMain.ReceiptAmt = TotAmt;

                                    _moneyReceiptExtService.Add(MRExt);
                                    _moneyReceiptExtService.Save();

                                    string sql = string.Format("select sm.ReceiptAmt,sm.SaleNo, sm.SaleDate, (sm.NetAmount - isnull(sum(mre.Amount),0)) as Billamount from SalesMain sm left join MoneyReceiptExt mre on mre.SaleNo = sm.SaleNo where sm.CustCode = " + MrVM.CustCode + " and sm.IsPaid = 'false'  group by sm.ReceiptAmt, mre.SaleNo, sm.NetAmount, sm.SaleNo, sm.SaleDate, mre.Amount order by sm.SaleNo");
                                    IEnumerable <billShow> BillLst;
                                    using (AcclineERPContext dbContext = new AcclineERPContext())
                                    {
                                        BillLst = dbContext.Database.SqlQuery <billShow>(sql).ToList();
                                    }
                                    foreach (var Salebill in BillLst)
                                    {
                                        if (Salebill.BillAmount == 0)
                                        {
                                            saleMain.ReceiptNo  = MrVM.MRNo;
                                            saleMain.ReceiptAmt = ((Salebill.ReceiptAmt == null) ? 0 : Salebill.ReceiptAmt) + TotAmt;
                                            saleMain.IsPaid     = true;
                                            _salesMainService.Update(saleMain);
                                            _salesMainService.Save();
                                        }
                                    }
                                }
                            } //end of  foreach
                        }
                        // New Add By Nasir
                        var GCa = _AcBrService.All().Where(s => s.BranchCode == MR.BranchCode && s.Ca_Ba == "Ca").Select(x => x.Accode).FirstOrDefault();
                        if (GCa == null)
                        {
                            var gset = _gsetService.All().LastOrDefault();
                            GCa = gset.GCa;
                        }

                        //------------------
                        TransactionLogService.SaveTransactionLog(_transactionLogService, "Cash MR", "Save", MR.MRSL, Session["UserName"].ToString());
                        LoadDropDown.journalVoucherSave("CR", "I", CRNo, MrVM.VchrNo, Session["FinYear"].ToString(), Session["ProjCode"].ToString(), Session["BranchCode"].ToString(), Convert.ToDateTime(MrVM.MRDate), GCa, Session["UserName"].ToString());

                        transaction.Complete();
                        return(Json("1", JsonRequestBehavior.AllowGet));
                    }
                    else
                    {
                        transaction.Dispose();
                        return(Json("3", JsonRequestBehavior.AllowGet));
                    }
                }

                catch (Exception)
                {
                    transaction.Dispose();
                    return(Json("0", JsonRequestBehavior.AllowGet));
                }
            }
        }
        public ActionResult SaveBankReceipt(BankReceipt bankReceipt)
        {
            RBACUser rUser = new RBACUser(Session["UserName"].ToString());

            if (!rUser.HasPermission("BankReceipt_Insert"))
            {
                return(Json("X", JsonRequestBehavior.AllowGet));
            }
            List <BankOperationVModel> CashOPVM = new List <BankOperationVModel>();
            var gset = _gsetService.All().LastOrDefault();

            try
            {
                var IfExit = _BankReceiptAppService.All().Where(x => x.BReceiptNo == bankReceipt.BReceiptNo).FirstOrDefault();
                if (IfExit == null)
                {
                    if (!string.IsNullOrEmpty(bankReceipt.purAccode) && bankReceipt.purAccode.Trim() != "Select Purpose")
                    {
                        bankReceipt.BranchCode   = Session["BranchCode"].ToString();
                        bankReceipt.bankAccode   = Session["BankAccode"].ToString();
                        bankReceipt.FinYear      = Session["FinYear"].ToString();
                        bankReceipt.NewChart     = _NewChartService.All().ToList().FirstOrDefault(x => x.Accode == bankReceipt.purAccode.Trim());
                        bankReceipt.bankNewChart = _NewChartService.All().ToList().FirstOrDefault(x => x.Accode == bankReceipt.bankAccode.Trim());


                        var           openBal       = GetOpenBal(bankReceipt.BReceiptDate);
                        var           rcecBal       = GetAllRemittances(bankReceipt.BReceiptDate, bankReceipt.BranchCode, bankReceipt.bankAccode).Sum(x => x.Amount);
                        var           payTotal      = GetAllPayment(bankReceipt.BReceiptDate, bankReceipt.BranchCode, bankReceipt.bankAccode).Sum(x => x.Amount);
                        var           closBal       = openBal + rcecBal - payTotal;
                        BankOperation bankOperation = new BankOperation(bankReceipt.BReceiptDate, openBal, bankReceipt.Amount, 0.0, closBal, bankReceipt.BranchCode, bankReceipt.FinYear, false, bankReceipt.bankAccode);


                        //cashReceipt.Customer = _CustomerService.All().ToList().FirstOrDefault(x => x.CustCode == cashReceipt.CustCode);
                        //BankOperation bankOperation = new BankOperation(bankReceipt.BReceiptDate, 0.0, 0.0, 0.0, 0.0, bankReceipt.BranchCode, bankReceipt.FinYear, false, bankReceipt.bankAccode);
                        var isAlreadySaved = _BankOperationService.All().ToList().FirstOrDefault(x => x.TrDate.ToString("MM-dd-yyyy") == bankReceipt.BReceiptDate.ToString("MM-dd-yyyy") && x.BranchCode == bankReceipt.BranchCode && x.bankAccode == bankReceipt.bankAccode);

                        if (isAlreadySaved == null)
                        {
                            using (var transaction = new TransactionScope())
                            {
                                try
                                {
                                    _BankOperationService.Add(bankOperation);
                                    _BankOperationService.Save();

                                    _BankReceiptAppService.Add(bankReceipt);
                                    _BankReceiptAppService.Save();

                                    //insert to provision
                                    LoadDropDown.journalVoucherSave("BR", "I", bankReceipt.BReceiptNo, bankReceipt.VoucherNo, bankReceipt.FinYear, "01", bankReceipt.BranchCode, bankReceipt.BReceiptDate, bankReceipt.bankAccode, Session["UserName"].ToString());

                                    TransactionLogService.SaveTransactionLog(_transactionLogService, "BankReceipt", "Save", bankReceipt.BReceiptNo, User.Identity.Name);
                                    CashOPVM = GetAllRemittances(bankReceipt.BReceiptDate, bankReceipt.BranchCode, bankReceipt.bankAccode);
                                    transaction.Complete();
                                }
                                catch (Exception)
                                {
                                    transaction.Dispose();
                                    return(Json("0", JsonRequestBehavior.AllowGet));
                                }
                            }
                            return(Json(CashOPVM, JsonRequestBehavior.AllowGet));
                        }
                        else
                        {
                            var rcecBals = GetAllRemittances(bankReceipt.BReceiptDate, bankReceipt.BranchCode, bankReceipt.bankAccode).Sum(x => x.Amount);
                            isAlreadySaved.RecTotal = rcecBals + bankReceipt.Amount;
                            isAlreadySaved.CloseBal = isAlreadySaved.OpenBal + isAlreadySaved.RecTotal - isAlreadySaved.PayTotal;

                            using (var transaction = new TransactionScope())
                            {
                                try
                                {
                                    _BankOperationService.Update(isAlreadySaved);
                                    _BankReceiptAppService.Save();

                                    _BankReceiptAppService.Add(bankReceipt);
                                    _BankReceiptAppService.Save();
                                    var sysSet = _sysSetService.All().FirstOrDefault();
                                    if (sysSet.CashRule == "O")
                                    {
                                        LoadDropDown.journalVoucherSave("BR", "I", bankReceipt.BReceiptNo, bankReceipt.VoucherNo, bankReceipt.FinYear, "01", bankReceipt.BranchCode, bankReceipt.BReceiptDate, bankReceipt.bankAccode, Session["UserName"].ToString());
                                    }
                                    //insert to provision
                                    LoadDropDown.BankRecalculate(bankReceipt.BReceiptDate, Convert.ToDecimal(bankReceipt.Amount), Session["ProjCode"].ToString(), bankReceipt.BranchCode, bankReceipt.FinYear, bankReceipt.bankAccode);
                                    TransactionLogService.SaveTransactionLog(_transactionLogService, "BankReceipt", "Save", bankReceipt.BReceiptNo, User.Identity.Name);
                                    CashOPVM = GetAllRemittances(bankReceipt.BReceiptDate, bankReceipt.BranchCode, bankReceipt.bankAccode);
                                    transaction.Complete();
                                }
                                catch (Exception)
                                {
                                    transaction.Dispose();
                                    return(Json("0", JsonRequestBehavior.AllowGet));
                                }
                            }

                            return(Json(CashOPVM, JsonRequestBehavior.AllowGet));
                        }
                    }
                    else
                    {
                        return(Json("0", JsonRequestBehavior.AllowGet));
                    }
                }
                else
                {
                    return(Json("1", JsonRequestBehavior.AllowGet));
                }
            }

            catch (Exception)
            {
                return(Json("0", JsonRequestBehavior.AllowGet));
            }
        }
        public ActionResult UpdateOnlineMR(MROnlineVM MrVM)
        {
            using (var transaction = new TransactionScope())
            {
                try
                {
                    RBACUser rUser = new RBACUser(Session["UserName"].ToString());
                    if (!rUser.HasPermission("MoneyReceiptOnline_Update"))
                    {
                        return(Json("U", JsonRequestBehavior.AllowGet));
                    }
                    var MR = _moneyReceiptService.All().Where(x => x.MRSL == MrVM.MRSL && x.MRNo == MrVM.MRNo && MrVM.Posted == false).FirstOrDefault();
                    if (MR != null)
                    {
                        //For MR
                        MR.MRSL       = MrVM.MRSL;
                        MR.BranchCode = (MrVM.BranchCode == null) ? Session["BranchCode"].ToString() : MrVM.BranchCode;
                        MR.ProjCode   = (MrVM.ProjCode == null) ? Session["ProjCode"].ToString() : MrVM.ProjCode;
                        MR.MRNo       = MrVM.MRNo;
                        MR.MRDate     = MrVM.MRDate;
                        MR.MRAgainst  = MrVM.MRAgainst;
                        MR.VchrNo     = MrVM.VchrNo;
                        //MR.Posted = MrVM.Posted;
                        //MR.AdjWithBill = MrVM.AdjWithBill;
                        MR.MRAmount = MrVM.MRAmount;
                        MR.Accode   = MrVM.Accode;
                        if (MrVM.Accode == "0" || MrVM.Accode == null)
                        {
                            MR.Accode = _defACService.All().ToList().FirstOrDefault().CashAc;
                        }
                        MR.CustCode    = MrVM.CustCode;
                        MR.FinYear     = Session["FinYear"].ToString();
                        MR.JobNo       = MrVM.JobNo;
                        MR.Remarks     = MrVM.Remarks;
                        MR.DepositBank = MrVM.DepositBank;
                        MR.EncashDate  = MrVM.DepositDate;

                        _moneyReceiptService.Update(MR);
                        _moneyReceiptService.Save();

                        var GCa = _AcBrService.All().Where(s => s.BranchCode == MR.BranchCode && s.Ca_Ba == "Ca").Select(x => x.Accode).FirstOrDefault();
                        if (GCa == null)
                        {
                            var gset = _gsetService.All().LastOrDefault();
                            GCa = gset.GCa;
                        }
                        TransactionLogService.SaveTransactionLog(_transactionLogService, "Online MR", "Update", MR.MRSL, Session["UserName"].ToString());
                        LoadDropDown.journalVoucherRemoval("CR", MrVM.MRNo, MrVM.VchrNo, Session["FinYear"].ToString());
                        LoadDropDown.journalVoucherSave("CR", "I", MrVM.MRNo, MrVM.VchrNo, Session["FinYear"].ToString(), Session["ProjCode"].ToString(), Session["BranchCode"].ToString(), Convert.ToDateTime(MrVM.MRDate), GCa, Session["UserName"].ToString());
                        transaction.Complete();
                        return(Json("1", JsonRequestBehavior.AllowGet));
                    }
                    else
                    {
                        transaction.Dispose();
                        return(Json("3", JsonRequestBehavior.AllowGet));
                    }
                }

                catch (Exception)
                {
                    transaction.Dispose();
                    return(Json("0", JsonRequestBehavior.AllowGet));
                }
            }
        }