public async Task <LoanAccount> AddLoanAccountAsync(LoanAccountViewModel accountViewModel) { var customer = await _context.Customers.Include(c => c.Accounts).FirstOrDefaultAsync(c => c.CustomerId == accountViewModel.LinkedAccount.CustomerId); var settings = _context.AccountConfigurations.First(); if (customer.Accounts.Any(a => a.AccountClass == AccountClass.Loan)) { throw new Exception($"Unable to create account. Customer already has a Loan account in the system."); } var loanAccount = new LoanAccount() { CustomerAccount = accountViewModel.LinkedAccount, IsActivated = true, AccountClass = AccountClass.Loan, AccountName = $"{customer.FirstName} {customer.LastName} Loan", DateOpened = DateTime.Now, Principal = accountViewModel.Principal, InterestRate = (double)settings.LoanInterestRate, DurationYears = accountViewModel.DurationYears, RepaymentFrequencyMonths = accountViewModel.RepaymentFrequencyMonths, StartDate = accountViewModel.StartDate, Customer = accountViewModel.LinkedAccount.Customer }; _context.Add(loanAccount); var noPaymentsPerYear = 12 / loanAccount.RepaymentFrequencyMonths; loanAccount.CompoundInterest = (loanAccount.Principal * Math.Pow((1 + (loanAccount.InterestRate / 100) / (noPaymentsPerYear)), (noPaymentsPerYear * loanAccount.DurationYears))) - loanAccount.Principal; loanAccount.AccountNumber = GenerateCustomerAccountNumber(loanAccount.AccountClass, loanAccount.CustomerId, loanAccount.GLAccountId); await _context.SaveChangesAsync(); return(loanAccount); }
public async Task <ActionResult <Account> > CreateLoan(LoanAccountViewModel account) { TryValidateModel(account); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var loanAccount = _mapper.Map(account, typeof(LoanAccountViewModel), typeof(LoanAccount)) as LoanAccount; var customer = _dbContext.Customers.SingleOrDefault(s => s.IdentityId == account.IdentityId); loanAccount.Customer = customer; loanAccount.AccountType = AccountType.Loan; await _dbContext.LoanAccounts.AddAsync(loanAccount); await _dbContext.SaveChangesAsync(); return(Created("", account)); } catch (Exception ex) { ModelState.AddModelError("creation_failed", "Failed to create the account"); return(BadRequest(ModelState)); } }
public async Task EditLoanAccountAsync(LoanAccountViewModel accountViewModel) { var loanAccount = await RetrieveLoanAccountAsync(accountViewModel.GLAccountId); loanAccount.AccountName = accountViewModel.AccountName; loanAccount.DurationYears = accountViewModel.DurationYears; loanAccount.RepaymentFrequencyMonths = accountViewModel.RepaymentFrequencyMonths; var noPaymentsPerYear = 12 / loanAccount.RepaymentFrequencyMonths; loanAccount.CompoundInterest = (loanAccount.Principal * Math.Pow((1 + (loanAccount.InterestRate / 100) / (noPaymentsPerYear)), (noPaymentsPerYear * loanAccount.DurationYears))) - loanAccount.Principal; _context.Update(loanAccount); await _context.SaveChangesAsync(); }
public async Task <LoanAccountViewModel> GetAddLoanAccount(string linkedAccountNumber) { var linkedAccount = await _context.CustomerAccounts.Include(a => a.Customer).Where(c => c.AccountNumber == linkedAccountNumber).FirstOrDefaultAsync(); var customerAccounts = await _context.CustomerAccounts.Select(c => c.AccountNumber).ToListAsync(); var interestRate = await _context.AccountConfigurations.Select(s => s.LoanInterestRate).FirstOrDefaultAsync(); var viewModel = new LoanAccountViewModel { LinkedAccount = linkedAccount, CustomerAccounts = customerAccounts, InterestRate = (double)interestRate }; return(viewModel); }
public async Task <LoanAccountViewModel> GetEditLoanAccount(int id) { var account = await RetrieveLoanAccountAsync(id); var viewModel = new LoanAccountViewModel { GLAccountId = account.GLAccountId, LinkedAccount = account.CustomerAccount, Principal = account.Principal, InterestRate = account.InterestRate, DurationYears = account.DurationYears, RepaymentFrequencyMonths = account.RepaymentFrequencyMonths, StartDate = account.StartDate, AccountNumber = account.AccountNumber }; return(viewModel); }
public async Task <IActionResult> Create([Bind("GLAccountId,CustomerAccounts,InterestRate,AccountName,LinkedAccount,Principal,DurationYears,RepaymentFrequencyMonths,StartDate")] LoanAccountViewModel accountViewModel) { if (ModelState.IsValid) { try { var account = await _gLAccountService.AddLoanAccountAsync(accountViewModel); } catch (Exception ex) { ViewBag.Message = new StatusMessage { Message = ex.Message, Type = StatusType.Error }; return(View()); } return(RedirectToAction(nameof(Index))); } return(View(accountViewModel)); }
public async Task <IActionResult> Edit(int id, [Bind("GLAccountId,CustomerAccounts,InterestRate,AccountName,LinkedAccount,Principal,DurationYears,RepaymentFrequencyMonths,StartDate")] LoanAccountViewModel accountViewModel) { if (id != accountViewModel.GLAccountId) { return(NotFound()); } if (ModelState.IsValid) { try { await _gLAccountService.EditLoanAccountAsync(accountViewModel); } catch (DbUpdateConcurrencyException) { if (!await _gLAccountService.GLAccountExists(accountViewModel.GLAccountId)) { return(NotFound()); } else { throw; } } catch (Exception ex) { ViewBag.Message = new StatusMessage { Message = ex.Message, Type = StatusType.Error }; return(View(accountViewModel)); } return(RedirectToAction(nameof(Index))); } return(View(accountViewModel)); }