adamsaghy commented on code in PR #5053:
URL: https://github.com/apache/fineract/pull/5053#discussion_r2432359549
##########
fineract-progressive-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/transactionprocessor/impl/AdvancedPaymentScheduleTransactionProcessor.java:
##########
@@ -2890,71 +2899,362 @@ private void handleReAge(LoanTransaction
loanTransaction, TransactionCtx ctx) {
MonetaryCurrency currency = ctx.getCurrency();
List<LoanRepaymentScheduleInstallment> installments =
ctx.getInstallments();
- AtomicReference<Money> outstandingPrincipalBalance = new
AtomicReference<>(Money.zero(currency));
+ // re-aging logic for interest-bearing loans
+ if (ctx instanceof ProgressiveTransactionCtx progressiveTransactionCtx
+ &&
loanTransaction.getLoan().isInterestBearingAndInterestRecalculationEnabled()) {
+ handleReAgeWithInterestRecalculationEnabled(loanTransaction,
progressiveTransactionCtx);
+ } else if (loanTransaction.getLoan().isInterestBearing() &&
!loanTransaction.getLoan().isInterestRecalculationEnabled()) {
+ // TODO: implement interestRecalculation = false logic
+ throw new NotImplementedException(
+ "Logic for re-aging when interest bearing loan has
interestRecalculation disabled is not implemented");
+ } else {
+ AtomicReference<Money> outstandingPrincipalBalance = new
AtomicReference<>(Money.zero(currency));
+ installments.forEach(i -> {
+ Money principalOutstanding =
i.getPrincipalOutstanding(currency);
+ if (principalOutstanding.isGreaterThanZero()) {
+
outstandingPrincipalBalance.set(outstandingPrincipalBalance.get().add(principalOutstanding));
+ i.addToPrincipal(loanTransaction.getTransactionDate(),
principalOutstanding.negated());
+ }
+ });
+
+
loanTransaction.updateComponentsAndTotal(outstandingPrincipalBalance.get(),
Money.zero(currency), Money.zero(currency),
+ Money.zero(currency));
+
+ Money calculatedPrincipal = Money.zero(currency);
+ Money adjustCalculatedPrincipal = Money.zero(currency);
+ if (outstandingPrincipalBalance.get().isGreaterThanZero()) {
+ calculatedPrincipal = outstandingPrincipalBalance.get()
+
.dividedBy(loanTransaction.getLoanReAgeParameter().getNumberOfInstallments(),
MoneyHelper.getMathContext());
+ Integer installmentAmountInMultiplesOf =
loanTransaction.getLoan().getLoanProductRelatedDetail()
+ .getInstallmentAmountInMultiplesOf();
+ if (installmentAmountInMultiplesOf != null) {
+ calculatedPrincipal =
Money.roundToMultiplesOf(calculatedPrincipal, installmentAmountInMultiplesOf);
+ }
+ adjustCalculatedPrincipal = outstandingPrincipalBalance.get()
+
.minus(calculatedPrincipal.multipliedBy(loanTransaction.getLoanReAgeParameter().getNumberOfInstallments()));
+ }
+
+ Optional<LoanRepaymentScheduleInstallment>
lastNormalInstallmentOptional = installments.stream().filter(i ->
!i.isDownPayment())
+ .filter(i ->
i.getDueDate().isBefore(loanTransaction.getTransactionDate())).reduce((first,
second) -> second);
+
+ int reAgedInstallmentNumber;
+ LocalDate fromDate;
+ Loan loan;
+ if (lastNormalInstallmentOptional.isEmpty()) {
+ LoanRepaymentScheduleInstallment firstNormalInstallment =
installments.stream().filter(i -> !i.isDownPayment())
+
.min(Comparator.comparing(LoanRepaymentScheduleInstallment::getDueDate)).orElseThrow();
+ reAgedInstallmentNumber =
firstNormalInstallment.getInstallmentNumber();
+ fromDate = firstNormalInstallment.getFromDate();
+ loan = firstNormalInstallment.getLoan();
+ } else {
+ LoanRepaymentScheduleInstallment lastNormalInstallment =
lastNormalInstallmentOptional.get();
+ reAgedInstallmentNumber =
lastNormalInstallment.getInstallmentNumber() + 1;
+ fromDate = lastNormalInstallment.getDueDate();
+ loan = lastNormalInstallment.getLoan();
+ }
+
+ LoanRepaymentScheduleInstallment reAgedInstallment =
LoanRepaymentScheduleInstallment.newReAgedInstallment(loan,
+ reAgedInstallmentNumber, fromDate,
loanTransaction.getLoanReAgeParameter().getStartDate(),
+ calculatedPrincipal.getAmount());
+ insertOrReplaceRelatedInstallment(installments, reAgedInstallment,
currency, loanTransaction.getTransactionDate());
+
+ for (int i = 1; i <
loanTransaction.getLoanReAgeParameter().getNumberOfInstallments(); i++) {
+ LocalDate calculatedDueDate =
calculateReAgedInstallmentDueDate(loanTransaction.getLoanReAgeParameter(),
+ reAgedInstallment.getDueDate());
+ int nextReAgedInstallmentNumber =
reAgedInstallment.getInstallmentNumber() + 1;
+ reAgedInstallment =
LoanRepaymentScheduleInstallment.newReAgedInstallment(reAgedInstallment.getLoan(),
+ nextReAgedInstallmentNumber,
reAgedInstallment.getDueDate(), calculatedDueDate,
calculatedPrincipal.getAmount());
+ if (i + 1 ==
loanTransaction.getLoanReAgeParameter().getNumberOfInstallments()) {
+
reAgedInstallment.addToPrincipal(loanTransaction.getTransactionDate(),
adjustCalculatedPrincipal);
+ }
+ insertOrReplaceRelatedInstallment(installments,
reAgedInstallment, currency, loanTransaction.getTransactionDate());
+ }
+ int lastReAgedInstallmentNumber =
reAgedInstallment.getInstallmentNumber();
+ List<LoanRepaymentScheduleInstallment> toRemove =
installments.stream().filter(i -> i != null && !i.isAdditional()
+ && i.getInstallmentNumber() != null &&
i.getInstallmentNumber() > lastReAgedInstallmentNumber).toList();
+ toRemove.forEach(installments::remove);
+ reprocessInstallments(installments);
+ }
+ }
+
+ private void handleReAgeWithInterestRecalculationEnabled(final
LoanTransaction loanTransaction, final ProgressiveTransactionCtx ctx) {
+ final MonetaryCurrency currency = ctx.getCurrency();
+ final Loan loan = loanTransaction.getLoan();
+ final MathContext mc = MoneyHelper.getMathContext();
+ final LocalDate transactionDate = loanTransaction.getTransactionDate();
+ final List<LoanRepaymentScheduleInstallment> installments =
ctx.getInstallments();
+ final List<RepaymentPeriod> repaymentPeriods =
ctx.getModel().repaymentPeriods();
+ final LocalDate reAgingStartDate =
loanTransaction.getLoanReAgeParameter().getStartDate();
+
+ final Optional<LoanTransaction> chargeOffTransaction =
ctx.getAlreadyProcessedTransactions().stream()
+ .filter(t -> ((t.isChargeOff() &&
(loan.hasAccelerateChargeOffStrategy() ||
loan.hasZeroInterestChargeOffStrategy()))
+ || t.isContractTermination()) && !t.isReversed() &&
t.getTransactionDate().isBefore(reAgingStartDate))
+ .findFirst();
Review Comment:
Its 2 types of transactions: chargeoff and contract termination
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]