FINERACT-1981: Fix 2nd disburse on repayment due date results wrong EMI

This commit is contained in:
Janos Meszaros
2024-10-24 13:44:31 +02:00
committed by Adam Saghy
parent 2469f22280
commit 393f4398ac
3 changed files with 68 additions and 5 deletions
@@ -153,10 +153,10 @@ public class RepaymentPeriod {
public Money getOutstandingLoanBalance() {
if (outstandingBalanceCalculation == null) {
outstandingBalanceCalculation = Memo.of(() -> {
InterestPeriod lastInstallmentPeriod = getInterestPeriods().get(getInterestPeriods().size() - 1);
Money calculatedOutStandingLoanBalance = lastInstallmentPeriod.getOutstandingLoanBalance() //
.plus(lastInstallmentPeriod.getBalanceCorrectionAmount(), mc) //
.plus(lastInstallmentPeriod.getDisbursementAmount(), mc) //
InterestPeriod lastInterestPeriod = getInterestPeriods().get(getInterestPeriods().size() - 1);
Money calculatedOutStandingLoanBalance = lastInterestPeriod.getOutstandingLoanBalance() //
.plus(lastInterestPeriod.getBalanceCorrectionAmount(), mc) //
.plus(lastInterestPeriod.getDisbursementAmount(), mc) //
.minus(getDuePrincipal(), mc)//
.plus(getPaidPrincipal(), mc);//
return MathUtil.negativeToZero(calculatedOutStandingLoanBalance, mc);
@@ -84,7 +84,23 @@ public final class ProgressiveEMICalculator implements EMICalculator {
scheduleModel
.changeOutstandingBalanceAndUpdateInterestPeriods(disbursementDueDate, disbursedAmount,
Money.zero(disbursedAmount.getCurrency(), scheduleModel.mc()))
.ifPresent((repaymentPeriod) -> calculateEMIValueAndRateFactors(repaymentPeriod.getDueDate(), scheduleModel));
.ifPresent((repaymentPeriod) -> calculateEMIValueAndRateFactors(
getEffectiveRepaymentDueDate(scheduleModel, repaymentPeriod, disbursementDueDate), scheduleModel));
}
private LocalDate getEffectiveRepaymentDueDate(final ProgressiveLoanInterestScheduleModel scheduleModel,
final RepaymentPeriod changedRepaymentPeriod, final LocalDate disbursementDueDate) {
final boolean isRelatedToNextRepaymentPeriod = changedRepaymentPeriod.getDueDate().isEqual(disbursementDueDate);
if (isRelatedToNextRepaymentPeriod) {
final Optional<RepaymentPeriod> nextRepaymentPeriod = scheduleModel.repaymentPeriods().stream()
.filter(repaymentPeriod -> changedRepaymentPeriod.equals(repaymentPeriod.getPrevious().orElse(null))).findFirst();
if (nextRepaymentPeriod.isPresent()) {
return nextRepaymentPeriod.get().getDueDate();
}
// Currently N+1 scenario is not supported. Disbursement on Last Repayment due date affects the last
// repayment period.
}
return changedRepaymentPeriod.getDueDate();
}
@Override