public static List<AmortizationScheduleModel> CreateScheduleItems(LoanApplication loanApplication, AmortizationSchedule schedule)
        {
            var today = DateTime.Now;
            LoanCalculatorOptions options = new LoanCalculatorOptions();
            options.LoanReleaseDate = schedule.LoanReleaseDate;
            options.PaymentStartDate = schedule.PaymentStartDate;
            options.LoanAmount = loanApplication.LoanAmount;
            options.LoanTerm = loanApplication.LoanTermLength;
            options.LoanTermId = loanApplication.LoanTermUomId;
            var paymentMode = UnitOfMeasure.GetByID(loanApplication.PaymentModeUomId);
            options.PaymentMode = paymentMode.Name;
            options.PaymentModeId = paymentMode.Id;

            var aiInterestComputationMode = ApplicationItem.GetFirstActive(loanApplication.Application, ProductFeatureCategory.InterestComputationModeType);
            int interestComputation = 0;
            if (aiInterestComputationMode != null)
                interestComputation = aiInterestComputationMode.ProductFeatureApplicability.ProductFeatureId;
            var interestComputationMode = ProductFeature.GetById(interestComputation);

            var interestRate = ProductFeature.GetByName(loanApplication.InterestRateDescription);
            options.InterestComputationMode = interestComputationMode.Name;
            options.InterestRateDescription = interestRate.Name;
            options.InterestRate = loanApplication.InterestRate ?? 0;
            options.InterestRateDescriptionId = interestRate.Id;

            LoanCalculator calculator = new LoanCalculator();
            var items = calculator.GenerateLoanAmortization(options);

            return items;
        }
        public static void Approve(LoanApplication loanApplication, DateTime today, DateTime loanReleaseDate, DateTime paymentStartDate)
        {
            RoleType borrowerRoleType = RoleType.BorrowerApplicationType;
            PartyRole borrowerPartyRole = PartyRole.GetPartyRoleFromLoanApplication(loanApplication, borrowerRoleType);
            PartyRole customerPartyRole = PartyRole.GetByPartyIdAndRole(borrowerPartyRole.PartyId, RoleType.CustomerType);

            LoanApplicationStatu loanStatus = LoanApplicationStatu.CreateOrUpdateCurrent(loanApplication, LoanApplicationStatusType.ApprovedType, today);

            var agreement = Agreement.Create(loanApplication, AgreementType.LoanAgreementType, today);

            //Create new Loan Agreement Record
            LoanAgreement loanAgreement = new LoanAgreement();
            loanAgreement.Agreement = agreement;
            Context.LoanAgreements.AddObject(loanAgreement);

            var agreementItem = Create(loanApplication, agreement);
            Context.AgreementItems.AddObject(agreementItem);

            var borrower = RoleType.BorrowerAgreementType;
            var newBorrowerPartyRole = PartyRole.Create(borrower, customerPartyRole.Party, today);
            Context.PartyRoles.AddObject(newBorrowerPartyRole);

            var newBorrowerAgreementRole = CreateAgreementRole(agreement, newBorrowerPartyRole);
            Context.AgreementRoles.AddObject(newBorrowerAgreementRole);

            CreateAgreementForCoBorrowers(loanApplication, agreement, today);
            CreateAgreementForGuarantor(loanApplication, agreement, today);

            var pendingdvst = DisbursementVcrStatusEnum.PendingType;
            var disbursement = LoanDisbursementVcr.Create(loanApplication, agreement, today);
            DisbursementVcrStatu.CreateOrUpdateCurrent(disbursement, pendingdvst, today);

            //TODO :: Generate Amortization Schedule....
            AmortizationSchedule schedule = new AmortizationSchedule();
            schedule.DateGenerated = today;
            schedule.EffectiveDate = today;
            schedule.LoanReleaseDate = today;
            schedule.PaymentStartDate = paymentStartDate;
            schedule.LoanAgreement = loanAgreement;
            Context.AmortizationSchedules.AddObject(schedule);

            LoanCalculatorOptions options = new LoanCalculatorOptions();
            var aiInterestComputationMode = ApplicationItem.GetFirstActive(loanApplication.Application, ProductFeatureCategory.InterestComputationModeType);
            options.InterestComputationMode = aiInterestComputationMode.ProductFeatureApplicability.ProductFeature.Name;
            options.LoanAmount = loanApplication.LoanAmount;
            options.LoanTerm = loanApplication.LoanTermLength;
            options.LoanTermId = loanApplication.LoanTermUomId;
            options.InterestRate = loanApplication.InterestRate ?? 0;
            options.InterestRateDescription = loanApplication.InterestRateDescription;
            options.InterestRateDescriptionId = Context.ProductFeatures.SingleOrDefault(entity => entity.Name == loanApplication.InterestRateDescription).Id;
            options.PaymentModeId = loanApplication.PaymentModeUomId;
            options.PaymentMode = UnitOfMeasure.GetByID(options.PaymentModeId).Name;
            options.PaymentStartDate = paymentStartDate;
            options.LoanReleaseDate = loanReleaseDate;

            LoanCalculator loanCalculator = new LoanCalculator();
            var models = loanCalculator.GenerateLoanAmortization(options);
            foreach (var model in models)
            {
                AmortizationScheduleItem item = new AmortizationScheduleItem();
                item.AmortizationSchedule = schedule;
                item.ScheduledPaymentDate = model.ScheduledPaymentDate;
                item.PrincipalPayment = model.PrincipalPayment;
                item.InterestPayment = model.InterestPayment;
                item.PrincipalBalance = model.PrincipalBalance;
                item.TotalLoanBalance = model.TotalLoanBalance;
                item.IsBilledIndicator = false;
                Context.AmortizationScheduleItems.AddObject(item);
            }
            Context.SaveChanges();
        }
        protected void btnGenerate_Click(object sender, DirectEventArgs e)
        {
            var today = DateTime.Now;

            LoanCalculatorOptions options = new LoanCalculatorOptions();
            options.LoanReleaseDate = datLoanReleaseDate.SelectedDate;
            options.PaymentStartDate = datPaymentStartDate.SelectedDate;
            options.LoanAmount = Convert.ToDecimal(this.nfLoanAmount.Value);
            options.LoanTerm = (int)nfLoanTerm.Number;
            options.LoanTermId = int.Parse(hiddenLoanTermTimeUnitId.Text);
            options.PaymentMode = cmbPaymentMode.SelectedItem.Text;
            options.PaymentModeId = int.Parse(cmbPaymentMode.SelectedItem.Value);
            options.InterestComputationMode = cmbInterestComputationMode.SelectedItem.Text;
            options.InterestRateDescription = cmbInterestRate.SelectedItem.Text;
            options.InterestRate = (decimal)nfInterestRate.Number;
            options.InterestRateDescriptionId = int.Parse(cmbInterestRate.SelectedItem.Value);

            LoanCalculator calculator = new LoanCalculator();
            var models = calculator.GenerateLoanAmortization(options);

            storeAmortizationSchedule.DataSource = models;
            storeAmortizationSchedule.DataBind();
        }
        protected void btnGenerate_Click(object sender, DirectEventArgs e)
        {
            LoanApplicationForm form = this.CreateOrRetrieve<LoanApplicationForm>();
            form.AmortizationSchedules.Clear();
            form.Cheques.Clear();

            var today = DateTime.Now;

            LoanCalculatorOptions options = new LoanCalculatorOptions();
            options.LoanReleaseDate = datLoanReleaseDate.SelectedDate;
            options.PaymentStartDate = datPaymentStartDate.SelectedDate;
            options.LoanAmount = Convert.ToDecimal(this.nfLoanAmount.Value);
            options.LoanTerm = (int)nfLoanTerm.Number;
            options.LoanTermId = int.Parse(hiddenLoanTermTimeUnitId.Text);
            options.PaymentMode = cmbPaymentMode.SelectedItem.Text;
            options.PaymentModeId = int.Parse(cmbPaymentMode.SelectedItem.Value);
            options.InterestComputationMode = cmbInterestComputationMode.SelectedItem.Text;
            options.InterestRateDescription = cmbInterestRate.SelectedItem.Text;
            options.InterestRate = (decimal)nfInterestRate.Number;
            options.InterestRateDescriptionId = int.Parse(cmbInterestRate.SelectedItem.Value);
            options.PaymentStartDate = datPaymentStartDate.SelectedDate;
            options.LoanReleaseDate = datLoanReleaseDate.SelectedDate;
            options.TermOption = txtTermOption.Text;

            LoanCalculator calculator = new LoanCalculator();
            var models = calculator.GenerateLoanAmortization(options);

            storeAmortizationSchedule.DataSource = models;
            storeAmortizationSchedule.DataBind();

            List<AmortizationScheduleModel> sched = new List<AmortizationScheduleModel>();
            foreach (var item in models)
            {
                form.AddSchedule(item);
                sched.Add(item);
            }

            if (chckCheck.Checked)
            {
                List<ChequeModel> cheque = new List<ChequeModel>();

                foreach (AmortizationScheduleModel model in models)
                {
                    ChequeModel cheques = new ChequeModel();
                    cheques.Amount = model.TotalPayment;
                    cheques.ChequeDate = model.ScheduledPaymentDate;
                    form.AddCheque(cheques);
                    cheque.Add(cheques);
                }

                storeCheques.DataSource = cheque;
                storeCheques.DataBind();
            }
            else if (chckCheck.Checked == false)
            {
                this.SelectionModelCheque.SelectAll();
                grdpnlCheque.DeleteSelected();
                form.Cheques.Clear();
            }

            hdnOnChangeDates.Value = "0";
        }