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;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (X.IsAjaxRequest == false && this.IsPostBack == false)
            {
                int customerId = int.Parse(Request.QueryString["customerPartyRoleId"]);
                this.ParentResourceGuid = Request.QueryString["ResourceGuid"];
                LoanApplicationForm form = this.Retrieve<LoanApplicationForm>(ParentResourceGuid);
                CustomerDetailsModel model = new CustomerDetailsModel(form.PartyRoleId);
                this.lblCustomerName.Text = model.Name;
                this.lblDateGenerated.Text = String.Format("{0:MMMM dd, yyyy}", DateTime.Today);

                var loanApplication = LoanApplication.GetById(form.LoanApplicationId);
                //HEADER LENDER INFORMATION
                var partyRole = ObjectContext.PartyRoles.FirstOrDefault(x => x.RoleTypeId == RoleType.LendingInstitutionType.Id);
                var party = partyRole.Party;
                FillLenderInformation(party);

                if (form.AvailableSchedule.Count() != 0)
                {
                    storeAmortizationSchedule.DataSource = form.AmortizationSchedules;
                    storeAmortizationSchedule.DataBind();
                }
                else
                {
                    var agr = ObjectContext.Agreements.SingleOrDefault(entity => entity.ApplicationId == loanApplication.ApplicationId);
                    var schedule = ObjectContext.AmortizationSchedules.SingleOrDefault(entity => entity.AgreementId == agr.Id);
                    var items = ObjectContext.AmortizationScheduleItems.Where(entity => entity.AmortizationScheduleId == schedule.Id);
                    var scheduleItems = LoanApplication.CreateScheduleItems(loanApplication, schedule);

                    if (items.Count() == 0)
                    {
                        storeAmortizationSchedule.DataSource = scheduleItems;
                        storeAmortizationSchedule.DataBind();
                    }
                    else
                    {
                        var type = UnitOfMeasure.GetByID(loanApplication.PaymentModeUomId).Name;
                        int i = 0;
                        foreach (var amModel in items)
                        {
                            AmortizationScheduleModel newModel = new AmortizationScheduleModel();
                            newModel.Counter = GetItemType(type) + " " + (i + 1).ToString();
                            newModel.InterestPayment = amModel.InterestPayment;
                            newModel.IsBilledIndicator = amModel.IsBilledIndicator;
                            newModel.PrincipalBalance = amModel.PrincipalBalance;
                            newModel.PrincipalPayment = amModel.PrincipalPayment;
                            newModel.ScheduledPaymentDate = amModel.ScheduledPaymentDate;
                            newModel.TotalLoanBalance = amModel.TotalLoanBalance;
                            newModel.TotalPayment = amModel.PrincipalPayment + amModel.InterestPayment;
                            i++;

                            form.AddAmortizationModel(newModel);
                        }

                        storeAmortizationSchedule.DataSource = form.AmortizationSchedules;
                        storeAmortizationSchedule.DataBind();
                    }
                }
            }
        }
 public AmortizationScheduleModel(AmortizationScheduleModel items)
 {
     this.IsNew = false;
     this.Counter = items.Counter;
     this.ScheduledPaymentDate = items.ScheduledPaymentDate;
     this.PrincipalBalance = items.PrincipalBalance;
     this.InterestPayment = items.InterestPayment;
     this.TotalLoanBalance = items.TotalLoanBalance;
     this.IsBilledIndicator = items.IsBilledIndicator;
     this.PrincipalPayment = items.PrincipalPayment;
     this.TotalPayment = items.PrincipalPayment + items.InterestPayment;
 }
        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 void Fill(int loanApplicationId)
        {
            LoanApplicationForm form = this.CreateOrRetrieve<LoanApplicationForm>();
            this.hiddenResourceGuid.Value = this.ResourceGuid;
            form.Retrieve(loanApplicationId);

            this.FillCustomerDetails(form.PartyRoleId);
            this.FillFinancialProductDetails(form.FinancialProductId);

            form.SetSelectedOutstandingLoansToPayoff(loanApplicationId, form.RetrieveOutstandingLoans());
            var ids = from ol in form.SelectedLoansToPayOff
                      select new SelectedRow { RecordID = ol.LoanId.ToString() };

            RowSelectionOutstandingLoans.SelectedRows.AddRange(ids);
            RowSelectionOutstandingLoans.UpdateSelection();
            this.txtStatusComment.Text = form.StatusComment;

            ProductFeatureApplicability pfa = ProductFeatureApplicability.GetById(form.PFAInterestRateId);
            cmbInterestRate.Value = pfa.ProductFeatureId;
            nfInterestRate.Number = (double)form.InterestRate;

            hiddenPastDueInterestRate.Text = form.PFAPastDueInterestRateId.ToString();
            txtPastDueDescription.Text = form.PastDueInterestRateDescription;
            nfPastDueRate.Number = (double)form.PastDueInterestRate;

            txtStatusComment.Text = form.StatusComment;
            dtApplicationDate.SelectedDate = form.LoanApplicationDate ;
            nfLoanAmount.Text = ((double)form.LoanAmount).ToString("N");
            var termOption = ProductFeatureApplicability.GetById(form.TermOptionId).ProductFeature.Name;
            txtTermOption.Text = termOption;
            if (termOption == ProductFeature.NoTermType.Name)
            {
                btnGenerate.Disabled = false;
                nfLoanTerm.Disabled = true;
                nfLoanTerm.Number = 0;
            }
            else
            {
                nfLoanTerm.Number = (double)form.LoanTerm;
            }
            //if (form.WithLoanTermIndicator == true)
            //{
            //    chckWithTerm.Checked = true;
            //    nfLoanTerm.Number = (double)form.LoanTerm;
            //}
            //else
            //{
            //    chckWithTerm.Checked = false;
            //    btnGenerate.Disabled = false;
            //    nfLoanTerm.Disabled = true;
            //    nfLoanTerm.Number = 0;
            //}
            txtLoanPurpose.Text = form.LoanPurpose;

            cmbCollateralRequirement.Value = form.CollateralRequirementId;
            cmbInterestComputationMode.Value = form.InterestComputationModeId;
            cmbPaymentMode.Value = form.PaymentModeUomId;
            cmbMethodOfChargingInterest.Value = form.MethodOfChargingInterestId;
            chkPayOutstandingLoan.Checked = form.PayOutstandingLoanIndicator;

            if (form.WithCheckIndicator == true)
                chckCheck.Checked = true;
            else
                chckCheck.Checked = false;

            datLoanReleaseDate.SelectedDate = DateTime.Parse(form.LoanReleaseDate);
            datPaymentStartDate.SelectedDate = DateTime.Parse(form.PaymentStartDate);

            StoreFee.DataSource = form.AvailableFees;
            StoreFee.DataBind();

            StoreCoBorrower.DataSource = form.AvailableCoBorrowers;
            StoreCoBorrower.DataBind();

            StoreGuarantor.DataSource = form.AvailableGuarantors;
            StoreGuarantor.DataBind();

            StoreCollaterals.DataSource = form.AvailableCollaterals;
            StoreCollaterals.DataBind();

            StoreSubmittedDocuments.DataSource = form.AvailableSubmittedDocuments;
            StoreSubmittedDocuments.DataBind();

            storeCheques.DataSource = form.AvailableCheques;
            storeCheques.DataBind();

            LoanApplication loanApplication = LoanApplication.GetById(loanApplicationId);

            var agr = ObjectContext.Agreements.SingleOrDefault(entity => entity.ApplicationId == loanApplicationId);
            var schedule = ObjectContext.AmortizationSchedules.SingleOrDefault(entity => entity.AgreementId == agr.Id);
            var items = ObjectContext.AmortizationScheduleItems.Where(entity => entity.AmortizationScheduleId == schedule.Id);
            var scheduleItems = LoanApplication.CreateScheduleItems(loanApplication, schedule);

            if (items.Count() == 0)
            {
                storeAmortizationSchedule.DataSource = scheduleItems;
                storeAmortizationSchedule.DataBind();
            }
            else
            {
                var type = UnitOfMeasure.GetByID(loanApplication.PaymentModeUomId).Name;
                int i = 0;
                foreach (var model in items)
                {
                    AmortizationScheduleModel newModel = new AmortizationScheduleModel();
                    newModel.Counter = GetItemType(type) + " " + (i + 1).ToString();
                    newModel.InterestPayment = model.InterestPayment;
                    newModel.IsBilledIndicator = model.IsBilledIndicator;
                    newModel.PrincipalBalance = model.PrincipalBalance;
                    newModel.PrincipalPayment = model.PrincipalPayment;
                    newModel.ScheduledPaymentDate = model.ScheduledPaymentDate;
                    newModel.TotalLoanBalance = model.TotalLoanBalance;
                    newModel.TotalPayment = model.PrincipalPayment + model.InterestPayment;
                    i++;

                    form.AddAmortizationModel(newModel);
                }

                storeAmortizationSchedule.DataSource = form.AmortizationSchedules;
                storeAmortizationSchedule.DataBind();
            }

            EnableValidActivity(loanApplication);
        }