Esempio n. 1
0
        public Loan CreateLoanContract(Customer customer, LoanApplication application, string employeeId)
        {
            var today       = GetCurrentDate();
            var bankAccount = GetBankAccount(application.Currency);
            var schedule    = PaymentScheduleCalculator.Calculate(application);
            var accountsSet = _unitOfWork.GetDbSet <Account>();
            var entrySet    = _unitOfWork.GetDbSet <Entry>();

            var accounts = new List <Account>(LoanAccountTypes
                                              .Select(accountType =>
            {
                var account        = accountsSet.Create();
                account.Currency   = application.Currency;
                account.Type       = accountType;
                account.DateOpened = today;
                account.Number     = CreateAccountNumber(accountType);
                account.Entries    = new Collection <Entry>();
                return(account);
            }));
            var generalDebtAcc = accounts.Single(a => a.Type == AccountType.GeneralDebt);
            var entryDate      = GetCurrentDate();
            var initialEntry   = entrySet.Create();

            initialEntry.Amount   = application.LoanAmount;
            initialEntry.Currency = application.Currency;
            initialEntry.Date     = entryDate;
            initialEntry.Type     = EntryType.Transfer;
            initialEntry.SubType  = EntrySubType.GeneralDebt;
            application.Status    = LoanApplicationStatus.ContractPrinted;

            var bankEntry = entrySet.Create();

            Entry.GetOppositeFor(initialEntry, bankEntry);
            bankEntry.Type    = EntryType.Transfer;
            bankEntry.SubType = EntrySubType.BankLoanIssued;
            AddEntry(generalDebtAcc, initialEntry);
            AddEntry(bankAccount, bankEntry);

            var loan = new Loan
            {
                CustomerId       = customer.Id,
                Application      = application,
                IsClosed         = false,
                PaymentSchedule  = schedule,
                Accounts         = accounts,
                IsContractSigned = false,
                EmployeeId       = employeeId
            };

            loan.Application.TimeContracted = GetCurrentDate();
            // TODO: check if entries are saved!
            UpsertLoan(loan);
            return(loan);
        }
        public ActionResult Index(LoanCalculatorModel loanCalculatorModel, string btnToApplication)
        {
            if (btnToApplication != null && btnToApplication == "To Application")
            {
                if (ModelState.IsValid)
                {
                    var loanApplication = new LoanApplication
                    {
                        LoanAmount = loanCalculatorModel.LoanAmount,
                        Term       = loanCalculatorModel.Term,
                        TariffId   = loanCalculatorModel.TariffId
                    };
                    if (_cellPhone != null)
                    {
                        loanApplication.CellPhone = _cellPhone;
                    }

                    TempData.Add("loanApplication", loanApplication);
                    return(RedirectToAction((User.IsInRole("Consultant") ? "Fill" : "Create"), "LoanApplication"));
                }
            }

            var tariffs = Service.GetTariffs().Where(t => t.IsActive).ToList();

            ViewBag.TariffId = new SelectList(tariffs, "Id", "Name");
            var tariff = tariffs.FirstOrDefault(t => t.Id == loanCalculatorModel.TariffId);

            if (ModelState.IsValid)
            {
                try
                {
                    var paymentSchedule =
                        PaymentScheduleCalculator.Calculate(loanCalculatorModel.LoanAmount, tariff,
                                                            loanCalculatorModel.Term);
                    loanCalculatorModel.Payments = paymentSchedule.Payments;
                }
                catch (ArgumentException e)
                {
                    ModelState.AddModelError("TariffId", e.Message);
                    return(View(loanCalculatorModel));
                }
            }
            return(View(loanCalculatorModel));
        }