public List <BankOperationVModel> GetAllRemittances(DateTime dateTime, string branchCode, string bankAccode)
        {
            branchCode = Session["BranchCode"].ToString();
            // bankAccode = Session["BankAccode"].ToString();
            var bank = new List <BankOperationVModel>();
            int i    = 0;

            _HORemitService.All().ToList().Where(x => x.BranchCode == branchCode && x.bankAccode == bankAccode &&
                                                 x.RemitDate.ToString("MM-dd-yyyy") == dateTime.ToString("MM-dd-yyyy")).ToList().ForEach(x => bank.Add(
                                                                                                                                             new BankOperationVModel(++i, "H/O Remittance", x.Amount, x.RemitNo, "RT", x.VoucherNo)));

            _BankReceiptAppService.All().ToList().Where(x => x.BranchCode == branchCode && x.bankAccode == bankAccode &&
                                                        x.BReceiptDate.ToString("MM-dd-yyyy") == dateTime.ToString("MM-dd-yyyy")).ToList().ForEach(x => bank.Add(
                                                                                                                                                       new BankOperationVModel(++i, x.NewChart.AcName, x.Amount, x.BReceiptNo, "BR", x.VoucherNo)));

            _DepositToBankService.All().ToList().Where(x => x.BranchCode == branchCode && x.bankAccode == bankAccode &&
                                                       x.DepositDate.ToString("MM-dd-yyyy") == dateTime.ToString("MM-dd-yyyy")).ToList().ForEach(x => bank.Add(
                                                                                                                                                     new BankOperationVModel(++i, "Deposit To Bank", x.Amount, x.DepositNo, "BD", x.VoucherNo)));
            return(bank);
        }
        public ActionResult DelBankOperation(string tag, string transactionNo, string dateTime)
        {
            if (tag == "RT")
            {
                RBACUser rUser = new RBACUser(Session["UserName"].ToString());
                if (!rUser.HasPermission("Remittance_Delete"))
                {
                    return(Json("D", JsonRequestBehavior.AllowGet));
                }
            }
            if (tag == "BR")
            {
                RBACUser rUser = new RBACUser(Session["UserName"].ToString());
                if (!rUser.HasPermission("BankReceipt_Delete"))
                {
                    return(Json("D", JsonRequestBehavior.AllowGet));
                }
            }
            if (tag == "BP")
            {
                RBACUser rUser = new RBACUser(Session["UserName"].ToString());
                if (!rUser.HasPermission("BankPayment_Delete"))
                {
                    return(Json("D", JsonRequestBehavior.AllowGet));
                }
            }
            List <BankOperationVModel> CashOPVM = new List <BankOperationVModel>();
            string branchCode = Session["BranchCode"].ToString();
            string bankAccode = Session["BankAccode"].ToString();

            switch (tag)
            {
            case "RT":
            {
                var hoRemit = _hoRemitService.All().ToList().FirstOrDefault(x => x.RemitNo == transactionNo.Trim());
                if (hoRemit != null)
                {
                    using (var transaction = new TransactionScope())
                    {
                        try
                        {
                            _hoRemitService.Delete(hoRemit);
                            _hoRemitService.Save();
                            TransactionLogService.SaveTransactionLog(_transactionLogService, "H/O Remittance", "Delete", hoRemit.RemitNo, User.Identity.Name);
                            var entryNo = hoRemit.RemitNo;
                            // var entryNo = Convert.ToInt64(hoRemit.VoucherNo.Substring(2, 8)).ToString().PadLeft(8, '0');
                            LoadDropDown.journalVoucherRemoval("RT", entryNo, hoRemit.VoucherNo, hoRemit.FinYear);
                            CashOPVM = GetAllRemittances(Convert.ToDateTime(dateTime), branchCode, bankAccode);
                            transaction.Complete();
                        }
                        catch (Exception)
                        {
                            transaction.Dispose();
                            return(Json("0", JsonRequestBehavior.AllowGet));
                        }
                    }

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

            case "BR":
            {
                var bankReceipt = _BankReceiptAppService.All().ToList().FirstOrDefault(x => x.BReceiptNo == transactionNo.Trim());
                if (bankReceipt != null)
                {
                    //bankReceipt.BranchCode = Session["BranchCode"].ToString();
                    //bankReceipt.FinYear = Session["FinYear"].ToString();
                    //bankReceipt.bankAccode = Session["BankAccode"].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());
                    using (var transaction = new TransactionScope())
                    {
                        try
                        {
                            _BankReceiptAppService.Delete(bankReceipt);
                            _BankReceiptAppService.Save();
                            TransactionLogService.SaveTransactionLog(_transactionLogService, "Bank Receipt", "Delete", bankReceipt.BReceiptNo, User.Identity.Name);
                            var entryNo = bankReceipt.BReceiptNo;
                            // var entryNo = Convert.ToInt64(bankReceipt.VoucherNo.Substring(2, 8)).ToString().PadLeft(8, '0');
                            LoadDropDown.journalVoucherRemoval("BR", entryNo, bankReceipt.VoucherNo, bankReceipt.FinYear);
                            CashOPVM = GetAllRemittances(bankReceipt.BReceiptDate, bankReceipt.BranchCode, bankAccode);
                            transaction.Complete();
                        }
                        catch (Exception)
                        {
                            transaction.Dispose();
                            return(Json("0", JsonRequestBehavior.AllowGet));
                        }
                    }

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

            case "BW":
            {
                return(Json("2", JsonRequestBehavior.AllowGet));
            }

            case "BP":
            {
                var payment = _paymentService.All().ToList().FirstOrDefault(x => x.PayCode == transactionNo.Trim());
                if (payment != null)
                {
                    using (var transaction = new TransactionScope())
                    {
                        try
                        {
                            _paymentService.Delete(payment);
                            _paymentService.Save();
                            TransactionLogService.SaveTransactionLog(_transactionLogService, "Expene", "Delete", payment.PayCode, User.Identity.Name);
                            var entryNo = payment.PayCode;
                            // var entryNo = Convert.ToInt64(payment.VoucherNo.Substring(2, 8)).ToString().PadLeft(8, '0');
                            LoadDropDown.journalVoucherRemoval("BP", entryNo, payment.VoucherNo, payment.FinYear);
                            CashOPVM = GetAllPayment(payment.PayDate, payment.BranchCode, bankAccode);
                            transaction.Complete();
                        }
                        catch (Exception)
                        {
                            transaction.Dispose();
                            return(Json("0", JsonRequestBehavior.AllowGet));
                        }
                    }

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

            case "BD":
            {
                return(Json("2", JsonRequestBehavior.AllowGet));
            }

            default:
            {
                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 UpdateCheRec(ChequeReceipt CheRec)
        {
            RBACUser rUser = new RBACUser(Session["UserName"].ToString());

            if (!rUser.HasPermission("ChequeReceipt_Update"))
            {
                return(Json("U", JsonRequestBehavior.AllowGet));
            }
            using (var transaction = new TransactionScope())
            {
                try
                {
                    var VchrExist   = _vchrMainService.All().FirstOrDefault(s => s.VchrNo == CheRec.VchrNo);
                    var chqArcExist = _chequeArchiveService.All().ToList().Where(x => x.ChqNo == CheRec.oldChqNo).Select(s => s.ChqStatus).ToList();
                    if (chqArcExist.Contains("Receive") && CheRec.ChqStatus == "Receive" || chqArcExist.Contains("Receive") && CheRec.ChqStatus == "Deposit" || chqArcExist.Contains("Deposit") && CheRec.ChqStatus == "Encash" ||
                        chqArcExist.Contains("Encash") && CheRec.ChqStatus != "Dishonour" && CheRec.ChqStatus != "Return")
                    {
                        if (VchrExist == null)
                        {
                            ChequeReceipt cr = new ChequeReceipt();
                            cr            = CheRec;
                            cr.MRTing     = 0;
                            cr.MRTingTime = null;
                            cr.ProjCode   = Session["ProjCode"].ToString();
                            cr.FinYear    = Session["FinYear"].ToString();

                            _chequeReceiptService.Update(cr);
                            _chequeReceiptService.Save();

                            var chqArc = _chequeArchiveService.All().FirstOrDefault(s => s.ChqNo == CheRec.oldChqNo && s.ChqStatus == CheRec.ChqStatus);
                            if (chqArc != null && chqArc.ChqStatus == CheRec.ChqStatus)
                            {
                                chqArc.ChqNo      = CheRec.ChqNo;
                                chqArc.UpdateBy   = CheRec.UpdateBy;
                                chqArc.UpdateDate = CheRec.UpdateDate;
                                chqArc.Reason     = CheRec.Reason;
                                chqArc.Remarks    = CheRec.Remarks;
                                _chequeArchiveService.Update(chqArc);
                                _chequeArchiveService.Save();
                            }
                            else
                            {
                                ChequeArchive ca = new ChequeArchive();
                                ca.ChqStatus  = CheRec.ChqStatus;
                                ca.ChqNo      = CheRec.ChqNo;
                                ca.UpdateBy   = CheRec.UpdateBy;
                                ca.UpdateDate = CheRec.UpdateDate;
                                ca.Reason     = CheRec.Reason;
                                ca.Remarks    = CheRec.Remarks;
                                _chequeArchiveService.Add(ca);
                                _chequeArchiveService.Save();
                            }

                            BankReceipt br = _bankReceiptService.All().Where(s => s.RefNo == CheRec.ChqReceiptNo && s.ChequeNo == CheRec.ChqNo).FirstOrDefault();
                            if (CheRec.ChqStatus == "Deposit")
                            {
                                br.BReceiptDate = CheRec.ChqReceiptDate;
                                br.purAccode    = CheRec.MRAgainst.ToString();
                                br.RefNo        = CheRec.ChqReceiptNo;
                                br.bankAccode   = CheRec.DepositBank;
                                br.ChequeNo     = CheRec.ChqNo;
                                br.ChequeDate   = CheRec.ChqDate;
                                br.Amount       = (double)CheRec.Amount;
                                br.Advance      = false;
                                br.Remarks      = CheRec.Remarks;
                                br.FinYear      = Session["FinYear"].ToString();
                                br.GLPost       = CheRec.GLPost;
                                br.BranchCode   = CheRec.BranchCode;
                                br.VoucherNo    = CheRec.VchrNo;
                                br.SubCode      = CheRec.SubCode;
                                br.bankCode     = CheRec.BankCode;
                                _bankReceiptService.Update(br);
                                _bankReceiptService.Save();
                            }

                            var MoneyR = _moneyReceiptService.All().Where(s => s.MRNo == CheRec.ChqReceiptNo).FirstOrDefault();
                            //insert to provision and money receipt table
                            if (CheRec.ChqStatus == "Encash" && (MoneyR == null ? "000000" : MoneyR.MRNo) != CheRec.ChqReceiptNo)
                            {
                                MoneyReceipt MR = new MoneyReceipt();
                                MR.MRSL        = LoadDropDown.GenerateRecvSlNo(_moneyReceiptService, CheRec.BranchCode, Session["BranchCode"].ToString(), CheRec.ProjCode, Session["VchrConv"].ToString());
                                MR.BranchCode  = (CheRec.BranchCode == null) ? Session["BranchCode"].ToString() : CheRec.BranchCode;
                                MR.ProjCode    = (CheRec.ProjCode == null) ? Session["ProjCode"].ToString() : CheRec.ProjCode;
                                MR.MRNo        = CheRec.ChqReceiptNo;
                                MR.MRDate      = CheRec.ChqReceiptDate;
                                MR.MRAgainst   = CheRec.MRAgainst;
                                MR.VchrNo      = CheRec.VchrNo;
                                MR.PayMode     = "Cq";
                                MR.Posted      = CheRec.PostDated;
                                MR.AdjWithBill = CheRec.AdjWithBill;
                                MR.MRAmount    = CheRec.Amount;
                                MR.Accode      = CheRec.MRAgainst;
                                MR.CustCode    = CheRec.SubCode;
                                MR.FinYear     = Session["FinYear"].ToString();
                                MR.Remarks     = CheRec.Remarks;
                                MR.JobNo       = CheRec.JobNo;
                                MR.Ca_Bk       = CheRec.DepositBank;
                                MR.ChqNo       = CheRec.ChqNo;
                                MR.ChqDate     = CheRec.ChqDate;
                                MR.EncashDate  = CheRec.EncashDate;
                                MR.DepositBank = CheRec.DepositBank;
                                _moneyReceiptService.Add(MR);
                                _moneyReceiptService.Save();

                                var chqExt = _chqRecExtService.All().ToList().Where(s => s.ChqReceiptID == CheRec.ChqReceiptId);
                                foreach (var chqExtitem in chqExt)
                                {
                                    MoneyReceiptExt MRExt = new MoneyReceiptExt();
                                    MRExt.MRId   = _moneyReceiptService.All().OrderBy(s => s.MRId).LastOrDefault().MRId;
                                    MRExt.SaleNo = chqExtitem.BillNo;
                                    MRExt.Amount = chqExtitem.BillAmount;
                                    _moneyReceiptExtService.Add(MRExt);
                                    _moneyReceiptExtService.Save();
                                }
                                LoadDropDown.journalVoucherSave("BR", "I", br.BReceiptNo, CheRec.VchrNo, Session["FinYear"].ToString(), Session["ProjCode"].ToString(), CheRec.BranchCode, CheRec.ChqReceiptDate, CheRec.DepositBank, Session["UserName"].ToString());
                            }
                            ////update to provision
                            //LoadDropDown.journalVoucherRemoval("BR", CheRec.ChqReceiptNo, CheRec.VchrNo, Session["FinYear"].ToString());
                            //LoadDropDown.journalVoucherSave("BR", "I", CheRec.ChqReceiptNo, CheRec.VchrNo, Session["FinYear"].ToString(), Session["ProjCode"].ToString(), CheRec.BranchCode, CheRec.ChqReceiptDate, CheRec.DepositBank, Session["UserName"].ToString());
                            TransactionLogService.SaveTransactionLog(_transactionLogService, "ChequeReceipt", "Update", CheRec.ChqReceiptNo, Session["UserName"].ToString());

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