public AccruedInterestEvent GetAccruedInterestEvent(DateTime currentDate) { AccruedInterestEvent accruedInterestEvent = new AccruedInterestEvent { User = User.CurrentUser, Date = TimeProvider.Now }; OCurrency accruedAmount = 0; DateTime lastDateOfPayment = StartDate; foreach (Installment installment in InstallmentList) { if (installment.ExpectedDate <= currentDate) { accruedAmount += installment.InterestsRepayment; if (installment.IsRepaid || installment.IsPartiallyRepaid) { lastDateOfPayment = installment.ExpectedDate; if (installment.PaidDate.HasValue) { if (((DateTime) installment.PaidDate).Date == TimeProvider.Now.Date && installment.ExpectedDate < TimeProvider.Now) { lastDateOfPayment = installment.PaidDate.Value; } } } } DateTime date = installment.Number == 1 ? StartDate : GetInstallment(installment.Number - 2).ExpectedDate; if (!installment.IsRepaid && installment.ExpectedDate > currentDate && date <= currentDate) { int days = (currentDate - date).Days; accruedAmount += days >= DateTime.DaysInMonth(date.Year, date.Month) ? installment.InterestsRepayment : installment.InterestsRepayment*(double) days / (double) DateTime.DaysInMonth(date.Year, date.Month); accruedInterestEvent.InstallmentNumber = installment.Number - 1; } } foreach (RepaymentEvent repaymentEvent in Events.GetRepaymentEvents()) { if (!repaymentEvent.Deleted && currentDate >= repaymentEvent.Date) accruedAmount -= repaymentEvent.Interests; } foreach (AccruedInterestEvent accruedEvent in Events.GetAccruedInterestEvents()) { if (!accruedEvent.Deleted && currentDate >= accruedEvent.Date && accruedEvent.Date > lastDateOfPayment) accruedAmount -= accruedEvent.AccruedInterest; } accruedInterestEvent.InterestPrepayment = accruedAmount < 0 ? -1.0m * accruedAmount : 0; accruedAmount = accruedAmount < 0 ? 0 : accruedAmount; OCurrency diffDays = (currentDate - lastDateOfPayment).Days <= _generalSettings.CeaseLateDays ? (currentDate - lastDateOfPayment).Days : _generalSettings.CeaseLateDays; int diffBetweenDays = (currentDate - lastDateOfPayment).Days; accruedAmount = diffBetweenDays != 0 ? accruedAmount * diffDays / diffBetweenDays : 0; accruedAmount = UseCents ? Math.Round(accruedAmount.Value, 2) : Math.Round(accruedAmount.Value, 0); accruedInterestEvent.AccruedInterest = accruedAmount; accruedInterestEvent.Date = currentDate; if (accruedInterestEvent.AccruedInterest > 0) { Events.Add(accruedInterestEvent); return accruedInterestEvent; } return null; }
public AccruedInterestEvent CreateLoanInterestAccruingEvent(DateTime today) { OCurrency accruedAmount = 0; AccruedInterestEvent accruedInterestEvent = new AccruedInterestEvent {ClientType = _clientType}; foreach (Installment installment in _installmentList) { if (installment.IsRepaid) { accruedAmount += installment.InterestsRepayment; } else { DateTime date = installment.Number == 1 ? _startDate : GetInstallment(installment.Number - 2).ExpectedDate; int days = (today - date).Days; accruedAmount += days >= DateTime.DaysInMonth(date.Year, date.Month) ? installment.InterestsRepayment : installment.InterestsRepayment * (double)days / (double)DateTime.DaysInMonth(date.Year, date.Month); accruedAmount = UseCents ? Math.Round(accruedAmount.Value, 2) : Math.Round(accruedAmount.Value, 0); break; } } OCurrency alreadyAccrued = 0; foreach (AccruedInterestEvent loanInterestAccruingEvent in Events.GetEventsByType(typeof(AccruedInterestEvent))) { alreadyAccrued += loanInterestAccruingEvent.AccruedInterest + loanInterestAccruingEvent.InterestPrepayment; } accruedAmount -= alreadyAccrued; accruedInterestEvent.InterestPrepayment = accruedAmount; accruedInterestEvent.AccruedInterest = accruedAmount; accruedInterestEvent.Rescheduled = _rescheduled; accruedInterestEvent.Date = today; return accruedInterestEvent; }