Beispiel #1
0
        public async Task <IActionResult> Payment(int id, [Bind("PaymentFromAccount, AccountID, PaidOff, Amount, LoanBalance")] LoanPaymentVM loanPaymentVM)
        {
            Loan loan = await _loanRepo.Get(id);

            loanPaymentVM.AccountBalance = (await _AcctRepo.Get(loanPaymentVM.AccountID)).Balance;
            loanPaymentVM.Accounts       = await _AcctRepo.Get(UserManager.GetUserId(User));

            loanPaymentVM.LoanBalance = loan.Balance;
            if (ModelState.IsValid)
            {
                if (loanPaymentVM.AccountBalance < loanPaymentVM.Amount)
                {
                    ModelState.AddModelError("Amount", "An account can't go negative in paying off a loan.");
                    return(View(loanPaymentVM));
                }
                try
                {
                    if (loanPaymentVM.PaymentFromAccount)
                    {
                        var account = await _AcctRepo.Get(loanPaymentVM.AccountID);

                        await _loanRepo.Pay(loan, loanPaymentVM.Amount);

                        await _AcctRepo.LoanWithdrawl(account, loanPaymentVM.Amount, loan.Id);
                    }
                    else
                    {
                        await _loanRepo.Pay(loan, loanPaymentVM.Amount);
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!LoanExists(id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(loanPaymentVM));
        }
Beispiel #2
0
        public async Task <IActionResult> Payment(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            var loan = await _loanRepo.Get(id);

            if (loan == null)
            {
                return(NotFound());
            }
            LoanPaymentVM loanPaymentVM = new LoanPaymentVM
            {
                Accounts    = await _AcctRepo.Get(UserManager.GetUserId(User)),
                PaidOff     = loan.PaidOff,
                LoanBalance = loan.Balance,
            };

            return(View(loanPaymentVM));
        }