public static void Approve(LoanApplication loanApplication, DateTime today, List<AmortizationScheduleModel> model, DateTime loanReleaseDate)
        {
            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);

            AmortizationSchedule schedule = new AmortizationSchedule();
            schedule.DateGenerated = today;
            schedule.EffectiveDate = today;
            schedule.LoanReleaseDate = today;
            schedule.PaymentStartDate = model.First().ScheduledPaymentDate;
            schedule.LoanAgreement = loanAgreement;
            Context.AmortizationSchedules.AddObject(schedule);

            foreach (var models in model)
            {
                AmortizationScheduleItem item = new AmortizationScheduleItem();
                item.AmortizationSchedule = schedule;
                item.ScheduledPaymentDate = models.ScheduledPaymentDate;
                item.PrincipalPayment = models.PrincipalPayment;
                item.InterestPayment = models.InterestPayment;
                item.PrincipalBalance = models.PrincipalBalance;
                item.TotalLoanBalance = models.TotalLoanBalance;
                item.IsBilledIndicator = false;
                Context.AmortizationScheduleItems.AddObject(item);
            }
            Context.SaveChanges();
        }
        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 btnSaveAmortizationSchedItem_Click(object sender, DirectEventArgs e)
        {
            int selectedLoanId = int.Parse(hdnSelectedLoanId.Text);
            string selectedAmortSchedItemId = hdnSelectedAmortSchedItemId.Text;//CHANGE

            if (selectedAmortSchedItemId != "")
            {
                int asId = int.Parse(selectedAmortSchedItemId);
                //EDIT TOMORROW
                var amortSchedItem = ObjectContext.AmortizationScheduleItems.SingleOrDefault(entity => entity.Id == asId);
                amortSchedItem.ScheduledPaymentDate = dtScheduledPaymentDate.SelectedDate;
                amortSchedItem.PrincipalPayment = decimal.Parse(txtPrincipalPayment.Text);
                amortSchedItem.InterestPayment = decimal.Parse(txtInterestPayment.Text);
                amortSchedItem.PrincipalBalance = decimal.Parse(txtPrincipalBalance.Text);
                amortSchedItem.TotalLoanBalance = decimal.Parse(txtTotalLoanBalance.Text);

                ObjectContext.SaveChanges();
                strAmortSched.DataBind();
                wndAmortizationSchedItem.Hide();
                ClearAmortSchedItemWindow();
                return;
            }

            var financialAccount = ObjectContext.FinancialAccounts.SingleOrDefault(entity => entity.Id == selectedLoanId);
            var agreement = ObjectContext.Agreements.SingleOrDefault(entity => entity.Id == financialAccount.AgreementId);
            var amortizationSchedule = ObjectContext.AmortizationSchedules.SingleOrDefault(entity => entity.AgreementId == agreement.Id);

            AmortizationScheduleItem newAmortSchedItem = new AmortizationScheduleItem();
            newAmortSchedItem.AmortizationSchedule = amortizationSchedule;
            newAmortSchedItem.ScheduledPaymentDate = dtScheduledPaymentDate.SelectedDate;
            newAmortSchedItem.PrincipalPayment = decimal.Parse(txtPrincipalPayment.Text);
            newAmortSchedItem.InterestPayment = decimal.Parse(txtInterestPayment.Text);
             newAmortSchedItem.PrincipalBalance = decimal.Parse(txtPrincipalBalance.Text);
            newAmortSchedItem.TotalLoanBalance = decimal.Parse(txtTotalLoanBalance.Text);

            ObjectContext.AmortizationScheduleItems.AddObject(newAmortSchedItem);
            ObjectContext.SaveChanges();
            wndAmortizationSchedItem.Hide();
            ClearAmortSchedItemWindow();
            strAmortSched.DataBind();
        }
        private AmortizationScheduleItem CreateAmortizationScheduleItem(AmortizationSchedule schedule, DateTime today, AmortizationScheduleModel item)
        {
            AmortizationScheduleItem amortizationItem = new AmortizationScheduleItem();

            amortizationItem.AmortizationSchedule = schedule;
            amortizationItem.InterestPayment = item.InterestPayment;
            amortizationItem.IsBilledIndicator = item.IsBilledIndicator;
            amortizationItem.PrincipalBalance = item.PrincipalBalance;
            amortizationItem.PrincipalPayment = item.PrincipalPayment;
            amortizationItem.ScheduledPaymentDate = item.ScheduledPaymentDate;
            amortizationItem.TotalLoanBalance = item.TotalLoanBalance;

            return amortizationItem;
        }
        public void PrepareForSave(LoanApplication app, AmortizationSchedule sched, AmortizationScheduleModel models)
        {
            if (this.IsNew)
            {
                if (app.LoanTermLength != 0)
                {
                    AmortizationScheduleItem item = new AmortizationScheduleItem();
                    item.AmortizationSchedule = sched;
                    item.ScheduledPaymentDate = models.ScheduledPaymentDate;
                    item.PrincipalPayment = models.PrincipalPayment;
                    item.InterestPayment = models.InterestPayment;
                    item.PrincipalBalance = models.PrincipalBalance;
                    item.TotalLoanBalance = models.TotalLoanBalance;
                    item.IsBilledIndicator = false;
                    Context.AmortizationScheduleItems.AddObject(item);
                }
            }
            else
            {
                if (app.LoanTermLength != 0)
                {
                    var items = Context.AmortizationScheduleItems.Where(entity => entity.AmortizationScheduleId == sched.Id);
                    foreach (var schedule in items)
                    {
                        Context.AmortizationScheduleItems.DeleteObject(schedule);
                    }

                    AmortizationScheduleItem item = new AmortizationScheduleItem();
                    item.AmortizationSchedule = sched;
                    item.ScheduledPaymentDate = models.ScheduledPaymentDate;
                    item.PrincipalPayment = models.PrincipalPayment;
                    item.InterestPayment = models.InterestPayment;
                    item.PrincipalBalance = models.PrincipalBalance;
                    item.TotalLoanBalance = models.TotalLoanBalance;
                    item.IsBilledIndicator = false;
                    Context.AmortizationScheduleItems.AddObject(item);

                    //int count = Convert.ToInt32(models.Counter.ElementAt(models.Counter.Length - 1));
                    //int i = 0;
                    //foreach (var item in items)
                    //{
                    //    if (i != count)
                    //        i++;
                    //    else
                    //    {
                    //        DateTime date = models.ScheduledPaymentDate;
                    //        item.ScheduledPaymentDate = date;
                    //        item.PrincipalPayment = models.PrincipalPayment;
                    //        item.InterestPayment = models.InterestPayment;
                    //        item.PrincipalBalance = models.PrincipalBalance;
                    //        item.TotalLoanBalance = models.TotalLoanBalance;
                    //        item.IsBilledIndicator = false;
                    //        break;
                    //    }
                    //}
                }
            }
        }
 public AmortizationScheduleModel(AmortizationScheduleItem items)
 {
     this.IsNew = false;
     this.ScheduledPaymentDate = items.ScheduledPaymentDate;
     this.PrincipalBalance = items.PrincipalBalance;
     this.InterestPayment = items.InterestPayment;
     this.TotalLoanBalance = items.TotalLoanBalance;
     this.IsBilledIndicator = items.IsBilledIndicator;
     this.PrincipalPayment = items.PrincipalPayment;
 }