예제 #1
0
        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));
        }
예제 #2
0
        public ActionResult AddAccount(CustomerAccount model, string BranchId, string InterestRate, string NumberOfYears)
        {
            ViewBag.BranchId = new SelectList(branchRepo.GetAll(), "ID", "Name", model.BranchId);

            if (ModelState.IsValid)
            {
                try
                {
                    var customer = custRepo.GetById(model.CustId);
                    if (customer == null)
                    {
                        ViewBag.Msg = "Incorrect customer Id";
                        return(View());
                    }

                    var act = new CustomerAccount();
                    act.AccountName   = model.AccountName;
                    act.AccountType   = model.AccountType;
                    act.Customer      = customer;
                    act.AccountNumber = logic.GenerateCustomerAccountNumber(model.AccountType, customer.ID);
                    act.DateCreated   = DateTime.Now;

                    int bid = 0;
                    if (!int.TryParse(BranchId, out bid))
                    {
                        ViewBag.Msg = "Branch cannot be null";
                        return(View());
                    }
                    var branch = branchRepo.GetById(bid); //db.Branches.Where(b => b.BranchId == bid).SingleOrDefault();
                    if (branch == null)
                    {
                        ViewBag.Msg = "Branch cannot be null";
                        return(View());
                    }
                    act.Branch = branch;

                    if (!(model.AccountType == AccountType.Loan))   //for savings and current
                    {
                        actRepo.Insert(act);
                        ViewBag.Msg = "Account successfully created";
                        return(RedirectToAction("Index"));
                    }
                    else   //   FOR LOAN ACCOUNTS
                    {
                        if (model.LoanAmount < 1000 || model.LoanAmount >= decimal.MaxValue)
                        {
                            ViewBag.Msg = "Loan amount must be between #1,000 and a maximum reasonable amount";
                            return(View(model));
                        }
                        act.LoanAmount  = model.LoanAmount;
                        act.TermsOfLoan = model.TermsOfLoan;
                        var servAct = actRepo.GetByAccountNumber(model.ServicingAccountId);
                        if (servAct == null || servAct.AccountType == AccountType.Loan)
                        {
                            ViewBag.Msg = "Invalid account selected";
                            return(View(model));
                        }
                        act.ServicingAccount = servAct;

                        double interestRate = 0;
                        double nyears       = 0;

                        if (!(double.TryParse(InterestRate, out interestRate) && double.TryParse(NumberOfYears, out nyears)))
                        {
                            ViewBag.Msg = "Number of years or Interest rate value is incorrect";
                            return(View(model));
                        }
                        act.LoanInterestRatePerMonth = Convert.ToDecimal(InterestRate);
                        if (!(interestRate > 0 && nyears > 0 && model.LoanAmount > 0))
                        {
                            ViewBag.Msg = "Please enter positive values";
                            return(View(model));
                        }
                        switch (act.TermsOfLoan)
                        {
                        case TermsOfLoan.Fixed:
                            logic.ComputeFixedRepayment(act, nyears, interestRate);
                            break;

                        case TermsOfLoan.Reducing:
                            logic.ComputeReducingRepayment(act, nyears, interestRate);
                            break;

                        default:
                            break;
                        }

                        busLogic.DebitCustomerAccount(act, (decimal)act.LoanAmount);                   //debit the loan act (An Asset to the Bank)
                        busLogic.CreditCustomerAccount(act.ServicingAccount, (decimal)act.LoanAmount); //credit the loan's servicing account
                        new FinancialReportLogic().CreateTransaction(act, act.LoanAmount, TransactionType.Debit);
                        new FinancialReportLogic().CreateTransaction(act.ServicingAccount, act.LoanAmount, TransactionType.Credit);

                        actRepo.Update(act.ServicingAccount);
                        actRepo.Insert(act);
                        ViewBag.Msg = "Account successfully created";
                        return(RedirectToAction("Index"));
                    }
                }
                catch (Exception)
                {
                    //ErrorLogger.Log("Message= " + ex.Message + "\nInner Exception= " + ex.InnerException + "\n");
                    return(View(model));
                }
            }//end if ModelState
            else
            {
                ViewBag.Msg = "Please enter correct data";
                return(View(model));
            }

            // return View(model);
        }// end addAccount