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));
            }
        }
Пример #2
0
        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 SaveCheRec(ChequeReceipt CheRec, List <ChequeReceiptExt> AdjBills)
        {
            using (var transaction = new TransactionScope())
            {
                try
                {
                    RBACUser rUser = new RBACUser(Session["UserName"].ToString());
                    if (!rUser.HasPermission("ChequeReceipt_Insert"))
                    {
                        return(Json("X", JsonRequestBehavior.AllowGet));
                    }

                    string BrNo   = "";
                    var    items  = _chequeReceiptService.All().ToList().FirstOrDefault(x => x.ChqReceiptNo == CheRec.ChqReceiptNo);
                    var    chqArc = _chequeArchiveService.All().ToList().Where(x => x.ChqNo == CheRec.ChqNo).Select(s => s.ChqStatus).ToList();
                    if (chqArc.Count == 0 || chqArc.Contains("Deposit") && CheRec.ChqStatus != "Receive" || chqArc.Contains("Encash") && CheRec.ChqStatus != "Deposit" ||
                        chqArc.Contains("Dishonour") && CheRec.ChqStatus != "Encash" || chqArc.Contains("Return") && CheRec.ChqStatus != "Dishonour")
                    {
                        if (items == null)
                        {
                            ChequeReceipt cr = new ChequeReceipt();
                            cr            = CheRec;
                            cr.MRTing     = 0;
                            cr.MRTingTime = null;
                            cr.ProjCode   = Session["ProjCode"].ToString();
                            cr.FinYear    = Session["FinYear"].ToString();
                            _chequeReceiptService.Add(cr);
                            _chequeReceiptService.Save();

                            ChequeArchive CA = new ChequeArchive();
                            CA.ChqNo      = CheRec.ChqNo;
                            CA.ChqStatus  = CheRec.ChqStatus;
                            CA.UpdateBy   = CheRec.UpdateBy;
                            CA.UpdateDate = CheRec.UpdateDate;
                            CA.Reason     = CheRec.Reason;
                            CA.Remarks    = CheRec.Remarks;
                            _chequeArchiveService.Add(CA);
                            _chequeArchiveService.Save();

                            BankReceipt br = new BankReceipt();
                            br.BReceiptNo   = GenerateBankReceiptNo(CheRec.BranchCode);
                            BrNo            = br.BReceiptNo;
                            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.Add(br);
                            _bankReceiptService.Save();

                            if (AdjBills != null)
                            {
                                decimal TotAmt = CheRec.Amount;
                                foreach (var bill in AdjBills)
                                {
                                    ChequeReceiptExt chqExt = new ChequeReceiptExt();
                                    var saleMain            = _salesMainService.All().Where(s => s.SaleNo == bill.BillNo).FirstOrDefault();
                                    chqExt.ChqReceiptID = _chequeReceiptService.All().LastOrDefault().ChqReceiptId;
                                    chqExt.BillNo       = bill.BillNo;
                                    chqExt.BillDate     = saleMain.SaleDate;
                                    if (TotAmt >= bill.BillAmount)
                                    {
                                        chqExt.BillAmount = bill.BillAmount;
                                        TotAmt            = TotAmt - bill.BillAmount;

                                        saleMain.ReceiptNo  = CheRec.ChqReceiptNo;
                                        saleMain.ReceiptAmt = bill.BillAmount;
                                        saleMain.IsPaid     = true;
                                        _salesMainService.Update(saleMain);
                                        _salesMainService.Save();

                                        _chqRecExtService.Add(chqExt);
                                        _chqRecExtService.Save();
                                    }
                                    else
                                    {
                                        if (bill.IsPaid_mre)
                                        {
                                            saleMain.IsPaid  = true;
                                            chqExt.AdjAmount = TotAmt;
                                        }
                                        else
                                        {
                                            saleMain.IsPaid  = false;
                                            chqExt.AdjAmount = TotAmt;
                                        }

                                        chqExt.BillAmount = TotAmt;
                                        _chqRecExtService.Add(chqExt);
                                        _chqRecExtService.Save();

                                        string sql = string.Format("select sm.ReceiptAmt, sm.SaleNo as BillNo, sm.SaleDate as BillDate, (sm.NetAmount - isnull(sum(cre.BillAmount),0)) as Billamount from SalesMain sm left join ChequeReceiptExt cre on cre.BillNo = sm.SaleNo where sm.CustCode = " + CheRec.SubCode + " and sm.IsPaid = 'false'  group by sm.ReceiptAmt, cre.BillNo, sm.NetAmount, sm.SaleNo, sm.SaleDate, cre.BillAmount 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  = CheRec.ChqReceiptNo;
                                                saleMain.ReceiptAmt = ((Salebill.ReceiptAmt == null) ? 0 : Salebill.ReceiptAmt) + TotAmt;
                                                saleMain.IsPaid     = true;
                                                _salesMainService.Update(saleMain);
                                                _salesMainService.Save();
                                            }
                                        }
                                    }
                                }
                            }

                            TransactionLogService.SaveTransactionLog(_transactionLogService, "ChequeReceipt", "Save", 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));
                }
            }
        }
        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 SaveMROnline(MROnlineVM MrVM, List <MoneyReceiptExt> AdjBills)
        {
            using (var transaction = new TransactionScope())
            {
                try
                {
                    RBACUser rUser = new RBACUser(Session["UserName"].ToString());
                    if (!rUser.HasPermission("MoneyReceiptOnline_Insert"))
                    {
                        return(Json("X", JsonRequestBehavior.AllowGet));
                    }
                    var IfExit = _moneyReceiptService.All().Where(x => x.MRSL == MrVM.MRSL && x.MRNo == MrVM.MRNo).FirstOrDefault();
                    if (IfExit == null)
                    {
                        //var todayDate = DateTime.Now;
                        //CashReceipt CR = new CashReceipt();
                        BankReceipt  br = new BankReceipt();
                        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.MRAmount;
                        MR.Accode   = MrVM.MRAgainst;
                        //if (MrVM.Accode == "0")
                        //{
                        //    MR.Accode = _defACService.All().ToList().FirstOrDefault().CashAc;
                        //}
                        MR.CustCode    = MrVM.CustCode;
                        MR.FinYear     = Session["FinYear"].ToString();
                        MR.Remarks     = MrVM.Remarks;
                        MR.JobNo       = MrVM.JobNo;
                        MR.CollectedBy = MrVM.CollectedBy;
                        MR.GetwayId    = MrVM.GetwayId;
                        MR.DepositBank = MrVM.DepositBank;
                        MR.EncashDate  = MrVM.DepositDate;

                        br.BReceiptNo   = MrVM.MRSL;
                        br.BReceiptDate = (DateTime)MrVM.MRDate;
                        br.purAccode    = MrVM.MRAgainst;
                        br.RefNo        = MrVM.MRNo;
                        br.bankAccode   = MrVM.DepositBank;
                        br.ChequeNo     = "Online";
                        br.ChequeDate   = DateTime.Now;
                        br.Amount       = (double)MrVM.MRAmount;
                        br.Advance      = false;
                        br.Remarks      = MrVM.Remarks;
                        br.FinYear      = Session["FinYear"].ToString();
                        br.GLPost       = false;
                        br.BranchCode   = (MrVM.BranchCode == null) ? Session["BranchCode"].ToString() : MrVM.BranchCode;
                        br.VoucherNo    = MrVM.VchrNo;
                        br.SubCode      = MrVM.CustCode;
                        br.bankCode     = MrVM.DepositBank;


                        //CR.ReceiptNo = MrVM.MRNo;
                        //CR.ReceiptDate = (DateTime)MrVM.MRDate;
                        //CR.purAccode = MrVM.MRAgainst;
                        ////if (MrVM.Accode == "0")
                        ////{
                        ////    CR.purAccode = _defACService.All().ToList().FirstOrDefault().CashAc;
                        ////}
                        //CR.RefNo = MrVM.MRSL;
                        //CR.Amount = (Double)MrVM.MRAmount;
                        //CR.Advance = false;
                        //CR.Remarks = MrVM.Remarks;
                        //CR.GLPost = false;
                        //CR.BranchCode = MrVM.BranchCode;
                        //CR.VoucherNo = MrVM.VchrNo;
                        //CR.JobNo = MrVM.JobNo;
                        //CR.FinYear = Session["FinYear"].ToString();
                        _moneyReceiptService.Add(MR);
                        //_CashReceiptService.Add(CR);
                        //_CashReceiptService.Save();
                        _bankReceiptService.Add(br);

                        _moneyReceiptService.Save();

                        _bankReceiptService.Save();


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

                        if (AdjBills != null)
                        {
                            decimal TotAmt = MrVM.MRAmount;
                            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(x => x.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;
                                    }
                                    else
                                    {
                                        saleMain.IsPaid = false;
                                        MRExt.Amount    = TotAmt;
                                    }

                                    saleMain.ReceiptNo  = MrVM.MRNo;
                                    saleMain.ReceiptAmt = bill.Amount;
                                    //MRExt.Amount = 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();
                                        }
                                    }
                                }
                            }
                        }

                        //var GCa = _AcBrService.All().Where(s => s.BranchCode == MR.BranchCode && s.Ca_Ba == "Ba").Select(x => x.Accode).FirstOrDefault();
                        //if (GCa == null)
                        //{
                        //    var gset = _gsetService.All().LastOrDefault();
                        //    GCa = gset.GCa;
                        //}

                        TransactionLogService.SaveTransactionLog(_transactionLogService, "Online MR", "Save", MR.MRSL, Session["UserName"].ToString());

                        LoadDropDown.journalVoucherSave("BR", "I", MrVM.MRSL, MrVM.VchrNo, Session["FinYear"].ToString(), Session["ProjCode"].ToString(), Session["BranchCode"].ToString(), Convert.ToDateTime(MrVM.MRDate), MrVM.DepositBank, 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));
                }
            }
        }