public async Task <IEnumerable <LoanRepaymentCashFlowItem> > GenerateCashFlow(LoanApplication loanApplication, int numberOfYears)
        {
            var internalLoans = (await existingLoanRepository.GetInternalActiveLoan(loanApplication.Id)).Where(x => x.IsForRenewal == false);

            var agreementIds = internalLoans.Select(x => x.AgreementId).ToArray();

            var repaymentSchedules = await existingLoanRepaymentScheduleRepository.GetRepaymentSchedules(agreementIds);

            return(internalLoans.Select(x =>
            {
                var loanRepaymentSchedules = repaymentSchedules.Where(xx => xx.Id == x.AgreementId);

                var annualAmortizations = new List <decimal>();

                for (int i = 0; i < numberOfYears; i++)
                {
                    annualAmortizations.Add(
                        GenerateAnnualAmortization(loanRepaymentSchedules, loanApplication.RecastDate.AddYears(i))
                        );
                }

                return new LoanRepaymentCashFlowItem
                {
                    Name = x.Name,
                    AnnualRepayments = annualAmortizations
                };
            }));
        }
        public async Task <IEnumerable <LoanAnnualAmortization> > GetAnnualAmortizations(long loanApplicationId, DateTime startDate)
        {
            var endDate = startDate.AddMonths(12);

            var existingLoans = (await existingLoanRepository.GetInternalActiveLoan(loanApplicationId))
                                .Where(x => x.IsForRenewal == false);

            var agreementIds = existingLoans.Select(x => x.AgreementId).ToArray();

            var repaymentSchedules = await existingLoanRepaymentScheduleRepository.GetRepaymentSchedules(agreementIds);

            var filteredRepaymentSchedules = repaymentSchedules.Where(x => x.InstallmentDate > startDate && x.InstallmentDate <= endDate).ToList();


            return(existingLoans.Select(x => new LoanAnnualAmortization
            {
                Name = x.Name,
                EstimatedAmortization = filteredRepaymentSchedules.Where(xx => xx.Id == x.AgreementId).Sum(y => y.InstallmentAmount)
            }));
        }