Skip to content

Commit

Permalink
FINERACT-1971: N plus one installment loan reschedule
Browse files Browse the repository at this point in the history
  • Loading branch information
ruchiD authored and adamsaghy committed Jan 18, 2024
1 parent 7d7669f commit 89c3dc3
Show file tree
Hide file tree
Showing 3 changed files with 385 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.apache.fineract.portfolio.loanaccount.domain.transactionprocessor.impl.CreocoreLoanRepaymentScheduleTransactionProcessor;
import org.apache.fineract.portfolio.loanaccount.domain.transactionprocessor.impl.HeavensFamilyLoanRepaymentScheduleTransactionProcessor;
import org.apache.fineract.portfolio.loanaccount.domain.transactionprocessor.impl.InterestPrincipalPenaltyFeesOrderLoanRepaymentScheduleTransactionProcessor;
import org.springframework.util.CollectionUtils;

/**
* Abstract implementation of {@link LoanRepaymentScheduleTransactionProcessor} which is more convenient for concrete
Expand Down Expand Up @@ -78,6 +79,7 @@ public ChangedTransactionDetail reprocessLoanTransactions(final LocalDate disbur
}
}
}
addChargeOnlyRepaymentInstallmentIfRequired(charges, installments);

for (final LoanRepaymentScheduleInstallment currentInstallment : installments) {
currentInstallment.resetDerivedComponents();
Expand Down Expand Up @@ -871,4 +873,44 @@ private LoanCharge findLatestPaidChargeFromUnOrderedSet(final Set<LoanCharge> ch

return latestPaidCharge;
}

protected void addChargeOnlyRepaymentInstallmentIfRequired(Set<LoanCharge> charges,
List<LoanRepaymentScheduleInstallment> installments) {
if (!CollectionUtils.isEmpty(charges) && !CollectionUtils.isEmpty(installments)) {
LoanRepaymentScheduleInstallment latestRepaymentScheduleInstalment = installments.get(installments.size() - 1);
LocalDate installmentDueDate = null;

LoanCharge latestCharge = getLatestLoanChargeWithSpecificDueDate(charges);
if (latestCharge != null
&& DateUtils.isAfter(latestCharge.getEffectiveDueDate(), latestRepaymentScheduleInstalment.getDueDate())) {
installmentDueDate = latestCharge.getEffectiveDueDate();
}

if (installmentDueDate != null) {
if (latestRepaymentScheduleInstalment.isAdditional()) {
latestRepaymentScheduleInstalment.updateDueDate(installmentDueDate);
} else {
Loan loan = latestCharge.getLoan();
final LoanRepaymentScheduleInstallment installment = new LoanRepaymentScheduleInstallment(loan,
(installments.size() + 1), latestRepaymentScheduleInstalment.getDueDate(), installmentDueDate, BigDecimal.ZERO,
BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, false, null);
installment.markAsAdditional();
loan.addLoanRepaymentScheduleInstallment(installment);

}
}
}
}

private LoanCharge getLatestLoanChargeWithSpecificDueDate(Set<LoanCharge> charges) {
LoanCharge latestCharge = null;
List<LoanCharge> chargesWithSpecificDueDate = new ArrayList<>();
chargesWithSpecificDueDate.addAll(charges.stream().filter(charge -> charge.isSpecifiedDueDate()).toList());
if (!CollectionUtils.isEmpty(chargesWithSpecificDueDate)) {
Collections.sort(chargesWithSpecificDueDate,
(charge1, charge2) -> DateUtils.compare(charge1.getEffectiveDueDate(), charge2.getEffectiveDueDate()));
latestCharge = chargesWithSpecificDueDate.get(chargesWithSpecificDueDate.size() - 1);
}
return latestCharge;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ public ChangedTransactionDetail reprocessLoanTransactions(LocalDate disbursement
}
}

addChargeOnlyRepaymentInstallmentIfRequired(charges, installments);

for (final LoanRepaymentScheduleInstallment currentInstallment : installments) {
currentInstallment.resetBalances();
currentInstallment.updateDerivedFields(currency, disbursementDate);
Expand Down
Loading

0 comments on commit 89c3dc3

Please sign in to comment.