public ActionResult CreateLoan(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Customer customer = db.Customers.Find(id); if (customer == null) { return(HttpNotFound()); } CreateLoanAccountViewModel model = new CreateLoanAccountViewModel(); model.CustomerID = customer.ID; model.InterestRate = db.AccountConfigurations.First().LoanDebitInterestRate; model.NumberOfYears = 1; ViewBag.TermsOfLoan = string.Empty; ViewBag.BranchID = new SelectList(db.Branches, "ID", "Name"); // for loan accounts only // servicing account, user will input account number, in the post account number is checked to see if it is a savings or current account that belongs to customer //ViewBag.ServicingAccountID = new SelectList(db.CustomerAccounts, "ID", "AccountName"); return(View(model)); }
public ActionResult CreateLoan([Bind(Include = "AccountName,LoanAmount,TermsOfLoan,NumberOfYears,InterestRate,BranchID,CustomerID,ServicingAccountNumber")] CreateLoanAccountViewModel model) { ViewBag.BranchID = new SelectList(db.Branches, "ID", "Name", model.BranchID); if (ModelState.IsValid) { try { // general settings CustomerAccount customerAccount = new CustomerAccount(); customerAccount.AccountName = model.AccountName; customerAccount.AccountType = AccountType.Loan; customerAccount.CustomerID = model.CustomerID; customerAccount.AccountNumber = custActLogic.GenerateCustomerAccountNumber(AccountType.Loan, model.CustomerID); customerAccount.DateCreated = DateTime.Now; customerAccount.BranchID = model.BranchID; customerAccount.AccountStatus = AccountStatus.Closed; customerAccount.AccountBalance = 0; // loan specific settings customerAccount.LoanAmount = model.LoanAmount; customerAccount.TermsOfLoan = model.TermsOfLoan; long actNo = Convert.ToInt64(model.ServicingAccountNumber); var servAct = db.CustomerAccounts.Where(a => a.AccountNumber == actNo).SingleOrDefault(); if (servAct == null) { AddError("Servicing account number does not exist"); return(View(model)); } // check if servicing account number actually belongs to customer and is either savings or current. if (servAct.AccountType == AccountType.Loan || servAct.CustomerID != model.CustomerID) { AddError("Invalid servicing account"); return(View(model)); } if (servAct.AccountStatus == AccountStatus.Closed) { AddError("Servicing account is closed"); return(View(model)); } // get the id customerAccount.ServicingAccountID = servAct.ID; customerAccount.LoanInterestRatePerMonth = Convert.ToDecimal(model.InterestRate); switch (model.TermsOfLoan) { case TermsOfLoan.Fixed: custActLogic.ComputeFixedRepayment(customerAccount, model.NumberOfYears, model.InterestRate); break; case TermsOfLoan.Reducing: custActLogic.ComputeReducingRepayment(customerAccount, model.NumberOfYears, model.InterestRate); break; default: break; } // loan disbursement busLogic.DebitCustomerAccount(customerAccount, customerAccount.LoanAmount); busLogic.CreditCustomerAccount(servAct, customerAccount.LoanAmount); db.Entry(servAct).State = EntityState.Modified; db.CustomerAccounts.Add(customerAccount); db.SaveChanges(); // financial report logging reportLogic.CreateTransaction(customerAccount, customerAccount.LoanAmount, TransactionType.Debit); reportLogic.CreateTransaction(servAct, customerAccount.LoanAmount, TransactionType.Credit); return(RedirectToAction("Index")); } catch (Exception ex) { AddError(ex.ToString()); return(View(model)); } } AddError("Please enter valid data"); return(View(model)); }