public LoanRepaymentCashFlowItem GenerateCashFlow(ExistingLoan existingLoan, LoanApplication loanApplication, int numberOfYears) { var annualRepayments = new List <decimal>(); for (int i = 0; i < numberOfYears; i++) { int currentYear = loanApplication.RecastDate.Year + i; int nextYear = loanApplication.RecastDate.Year + i + 1; DateTime startDate = new DateTime(currentYear, loanApplication.RecastDate.Month, loanApplication.RecastDate.Day); DateTime endDate = new DateTime(nextYear, loanApplication.RecastDate.Month, loanApplication.RecastDate.Day); var isNextYearGreaterThanMaturity = (DateTime.Compare(endDate, existingLoan.MaturityDate)) > 0; bool isLastYear = i == numberOfYears - 1; if (isLastYear || isNextYearGreaterThanMaturity) { endDate = existingLoan.MaturityDate; } var isFirstYear = i == 0; if (isFirstYear) { startDate = loanApplication.RecastDate; } var remainingMonths = GetTotalRemainingMonths(startDate, endDate, isNextYearGreaterThanMaturity); int totalRemainingInstallments = GetTotalRemainingInstallments(remainingMonths, existingLoan.PaymentFrequency); decimal remainingInstallments = totalRemainingInstallments; decimal amortization = GetAccumulatedAmortization(existingLoan.Amortization, remainingInstallments); string debug = $"{existingLoan.Name}; months: {remainingMonths}; installments: {remainingInstallments}; frequency: {existingLoan.PaymentFrequency.ToString()}; amort: {amortization}"; annualRepayments.Add(amortization); } return(new LoanRepaymentCashFlowItem { Name = $"Repayment For - {existingLoan.Name}", AnnualRepayments = annualRepayments }); }
public decimal CalculateAnnualAmortization(ExistingLoan existingLoan, DateTime startDate) { DateTime endDate = startDate.AddMonths(12); if (DateTime.Compare(existingLoan.MaturityDate, endDate) < 0) { endDate = existingLoan.MaturityDate; } int remaingMonths = ((endDate.Year - startDate.Year) * 12) + endDate.Month - startDate.Month; int remainingPayments = (int)Math.Ceiling((decimal)remaingMonths / (int)existingLoan.PaymentFrequency); return(remainingPayments * existingLoan.Amortization); }