Ejemplo n.º 1
0
        public ActionResult CreateSavings([Bind(Include = "AccountName,BranchID,CustomerID")] CreateAccountViewModel model)
        {
            ViewBag.BranchID = new SelectList(db.Branches, "ID", "Name", model.BranchID);

            if (ModelState.IsValid)
            {
                try
                {
                    CustomerAccount customerAccount = new CustomerAccount();
                    customerAccount.AccountName    = model.AccountName;
                    customerAccount.AccountType    = AccountType.Savings;
                    customerAccount.CustomerID     = model.CustomerID;
                    customerAccount.AccountNumber  = custActLogic.GenerateCustomerAccountNumber(AccountType.Savings, model.CustomerID);
                    customerAccount.DateCreated    = DateTime.Now;
                    customerAccount.BranchID       = model.BranchID;
                    customerAccount.AccountStatus  = AccountStatus.Closed;
                    customerAccount.AccountBalance = 0;

                    db.CustomerAccounts.Add(customerAccount);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                catch (Exception ex)
                {
                    AddError(ex.ToString());
                    return(View(model));
                }
            }
            AddError("Please enter valid data");
            return(View(model));
        }
Ejemplo n.º 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