public ActionResult BankSelection()
        {
            string          branchCode = Session["BranchCode"].ToString();
            var             banks      = _AcBRService.All().Where(x => x.BranchCode == branchCode && x.Ca_Ba == "Ba").ToList();
            List <NewChart> bankList   = new List <NewChart>();

            foreach (var item in banks)
            {
                var bank = _newChartService.All().ToList().FirstOrDefault(x => x.Accode == item.Accode);
                bankList.Add(bank);
            }
            ViewBag.bankAccode = new SelectList(bankList, "Accode", "AcName");
            return(View());
        }
        public ActionResult SaveCashBank(string BranchCode, List <NewChart> Check)
        {
            using (var transaction = new TransactionScope())
            {
                try
                {
                    RBACUser rUser = new RBACUser(Session["UserName"].ToString());
                    if (!rUser.HasPermission("BranchWiseCashBank_Insert"))
                    {
                        return(Json("X", JsonRequestBehavior.AllowGet));
                    }

                    var IsExistBr = _acbrServic.All().Where(x => x.BranchCode == BranchCode).ToList();
                    foreach (var data in IsExistBr)
                    {
                        _acbrServic.Delete(data);
                        _acbrServic.Save();
                    }

                    if (Check != null)
                    {
                        foreach (var data in Check)
                        {
                            if (Check.Count != 0)
                            {
                                List <AcBR> AcBrList = new List <AcBR>();

                                AcBR acb = new AcBR();
                                acb.Accode = data.Accode;
                                //acb.BranchCode = data.BranchCode;
                                acb.BranchCode = BranchCode;
                                acb.Ca_Ba      = data.AccType;

                                AcBrList.Add(acb);
                                _acbrServic.Add(acb);
                                _acbrServic.Save();
                            }
                        }
                    }
                    transaction.Complete();
                    return(Json("1", JsonRequestBehavior.AllowGet));
                }
                catch (Exception)
                {
                    transaction.Dispose();
                    return(Json("0", 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 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 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));
                }
            }
        }