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

            _paymentService.All().ToList().Where(x => x.BranchCode == branchCode && x.bankAccode == bankAccode &&
                                                 x.PayDate.ToString("MM-dd-yyyy") == dateTime.ToString("MM-dd-yyyy")).ToList().ForEach(x => bank.Add(
                                                                                                                                           new BankOperationVModel(++i, x.NewChartPur.AcName, x.Amount, x.PayCode, "BP", x.VoucherNo)));

            _withdrawService.All().ToList().Where(x => x.BranchCode == branchCode && x.bankAccode == bankAccode &&
                                                  x.WithdrawDate.ToString("MM-dd-yyyy") == dateTime.ToString("MM-dd-yyyy")).ToList().ForEach(x => bank.Add(
                                                                                                                                                 new BankOperationVModel(++i, "Withdraw From Bank", x.Amount, x.WithdrawNo, "BW", x.VoucherNo)));
            return(bank);
        }
Пример #2
0
        public ActionResult SaveWithdraw(Withdraw withdraw)
        {
            RBACUser rUser = new RBACUser(Session["UserName"].ToString());

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

            try
            {
                var IfExit = _withdrawService.All().Where(x => x.WithdrawNo == withdraw.WithdrawNo).FirstOrDefault();
                if (IfExit == null)
                {
                    if (!string.IsNullOrEmpty(withdraw.bankAccode) && withdraw.bankAccode.Trim() != "Select Bank A/C")
                    {
                        withdraw.BranchCode = Session["BranchCode"].ToString();
                        var GCa = _AcBrService.All().Where(s => s.BranchCode == withdraw.BranchCode && s.Ca_Ba == "Ca").Select(x => x.Accode).FirstOrDefault();
                        if (GCa == null)
                        {
                            var gset = _gsetService.All().LastOrDefault();
                            GCa = gset.GCa;
                        }
                        withdraw.FinYear  = Session["FinYear"].ToString();
                        withdraw.NewChart = _NewChartService.All().ToList().FirstOrDefault(x => x.Accode == withdraw.bankAccode.Trim());
                        //cashReceipt.Customer = _CustomerService.All().ToList().FirstOrDefault(x => x.CustCode == cashReceipt.CustCode);
                        CashOperation cashOperation   = new CashOperation(withdraw.WithdrawDate, 0.0, 0.0, 0.0, 0.0, "", DateTime.Now, false, withdraw.FinYear, false, withdraw.BranchCode);
                        var           isAlreadyClosed = _CashOperationService.All().ToList().FirstOrDefault(x => x.BranchCode == withdraw.BranchCode && x.TrDate.ToString("MM-dd-yyyy") == withdraw.WithdrawDate.ToString("MM-dd-yyyy"));


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

                        BankOperation bankOperation = new BankOperation(withdraw.WithdrawDate, openBal, 0.0, withdraw.Amount, closBal, withdraw.BranchCode, withdraw.FinYear, false, withdraw.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") == withdraw.WithdrawDate.ToString("MM-dd-yyyy") && x.BranchCode == withdraw.BranchCode && x.bankAccode == withdraw.bankAccode);

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

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

                                    if (isAlreadyClosed == null)
                                    {
                                        _CashOperationService.Add(cashOperation);
                                        _CashOperationService.Save();
                                    }

                                    _withdrawService.Add(withdraw);
                                    _withdrawService.Save();
                                    TransactionLogService.SaveTransactionLog(_transactionLogService, "Withdraw", "Save", withdraw.WithdrawNo, Session["UserName"].ToString());
                                    LoadDropDown.journalVoucherSave("BW", "I", withdraw.WithdrawNo, withdraw.VoucherNo, withdraw.FinYear, "01", withdraw.BranchCode, withdraw.WithdrawDate, GCa, Session["UserName"].ToString());
                                    CashOPVM = GetAllCashReceiptAndWithdraw(withdraw.WithdrawDate, withdraw.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 payTotals = GetAllPayment(withdraw.WithdrawDate, withdraw.BranchCode, withdraw.bankAccode).Sum(x => x.Amount);
                            isAlreadySaved.PayTotal = payTotals + withdraw.Amount;
                            isAlreadySaved.CloseBal = isAlreadySaved.OpenBal + isAlreadySaved.RecTotal - isAlreadySaved.PayTotal;


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


                                    _withdrawService.Add(withdraw);
                                    _withdrawService.Save();
                                    TransactionLogService.SaveTransactionLog(_transactionLogService, "Withdraw", "Save", withdraw.WithdrawNo, Session["UserName"].ToString());
                                    LoadDropDown.journalVoucherSave("BW", "I", withdraw.WithdrawNo, withdraw.VoucherNo, withdraw.FinYear, "01", withdraw.BranchCode, withdraw.WithdrawDate, GCa, Session["UserName"].ToString());
                                    if (sysSet.CashRule == "O")
                                    {
                                        LoadDropDown.CashRecalculate(Session["ProjCode"].ToString(), withdraw.BranchCode, withdraw.FinYear, Convert.ToDecimal(withdraw.Amount), withdraw.WithdrawDate);
                                    }
                                    CashOPVM = GetAllCashReceiptAndWithdraw(withdraw.WithdrawDate, withdraw.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));
            }
        }