From fdfdf2d38a3b8351dc71627bc78772be0fb39b15 Mon Sep 17 00:00:00 2001 From: Jose Alberto Hernandez Date: Fri, 30 Aug 2024 08:55:29 -0500 Subject: [PATCH] FINERACT-2081: Apply penalty to overdue loans enhancements --- ...ApplyChargeToOverdueLoansBusinessStep.java | 4 +++- ...ChargeToOverdueLoanInstallmentTasklet.java | 5 +++-- .../LoanChargeWritePlatformServiceImpl.java | 9 +++++++++ .../service/LoanReadPlatformServiceImpl.java | 20 ++++++++++--------- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/fineract-provider/src/main/java/org/apache/fineract/cob/loan/ApplyChargeToOverdueLoansBusinessStep.java b/fineract-provider/src/main/java/org/apache/fineract/cob/loan/ApplyChargeToOverdueLoansBusinessStep.java index 86a26eceb3f..7d157a49fc7 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/cob/loan/ApplyChargeToOverdueLoansBusinessStep.java +++ b/fineract-provider/src/main/java/org/apache/fineract/cob/loan/ApplyChargeToOverdueLoansBusinessStep.java @@ -37,7 +37,9 @@ public class ApplyChargeToOverdueLoansBusinessStep implements LoanCOBBusinessSte public Loan execute(Loan loan) { final Collection overdueLoanScheduleDataList = loanReadPlatformService .retrieveAllOverdueInstallmentsForLoan(loan); - loanChargeWritePlatformService.applyOverdueChargesForLoan(loan.getId(), overdueLoanScheduleDataList); + if (!overdueLoanScheduleDataList.isEmpty()) { + loanChargeWritePlatformService.applyOverdueChargesForLoan(loan.getId(), overdueLoanScheduleDataList); + } return loan; } diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/jobs/applychargetooverdueloaninstallment/ApplyChargeToOverdueLoanInstallmentTasklet.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/jobs/applychargetooverdueloaninstallment/ApplyChargeToOverdueLoanInstallmentTasklet.java index bd09da8fcb3..e04fdf5ba46 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/jobs/applychargetooverdueloaninstallment/ApplyChargeToOverdueLoanInstallmentTasklet.java +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/jobs/applychargetooverdueloaninstallment/ApplyChargeToOverdueLoanInstallmentTasklet.java @@ -68,8 +68,9 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext chunkCon List exceptions = new ArrayList<>(); for (Map.Entry> entry : overdueScheduleData.entrySet()) { try { - loanChargeWritePlatformService.applyOverdueChargesForLoan(entry.getKey(), entry.getValue()); - + if (!entry.getValue().isEmpty()) { + loanChargeWritePlatformService.applyOverdueChargesForLoan(entry.getKey(), entry.getValue()); + } } catch (final PlatformApiDataValidationException e) { final List errors = e.getErrors(); for (final ApiParameterError error : errors) { diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanChargeWritePlatformServiceImpl.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanChargeWritePlatformServiceImpl.java index 12295fd0574..307394a090e 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanChargeWritePlatformServiceImpl.java +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanChargeWritePlatformServiceImpl.java @@ -31,6 +31,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Optional; import java.util.Set; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -757,11 +758,19 @@ public CommandProcessingResult adjustmentForLoanCharge(Long loanId, Long loanCha @Transactional @Override public void applyOverdueChargesForLoan(final Long loanId, Collection overdueLoanScheduleDataList) { + if (overdueLoanScheduleDataList.isEmpty()) { + return; + } Loan loan = this.loanAssembler.assembleFrom(loanId); if (loan.isChargedOff()) { log.warn("Adding charge to Loan: {} is not allowed. Loan Account is Charged-off", loanId); return; } + Optional optPenaltyCharge = loan.getLoanProduct().getCharges().stream() + .filter((e) -> ChargeTimeType.OVERDUE_INSTALLMENT.getValue().equals(e.getChargeTimeType()) && e.isLoanCharge()).findFirst(); + if (optPenaltyCharge.isEmpty()) { + return; + } final List existingTransactionIds = loan.findExistingTransactionIds(); final List existingReversedTransactionIds = loan.findExistingReversedTransactionIds(); boolean runInterestRecalculation = false; diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanReadPlatformServiceImpl.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanReadPlatformServiceImpl.java index 7b745a752ba..4e38213f2b9 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanReadPlatformServiceImpl.java +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanReadPlatformServiceImpl.java @@ -1642,6 +1642,15 @@ public Collection retrieveAllOverdueInstallmentsForLoan if (!loan.isOpen()) { return list; } + + Optional optPenaltyCharge = loan.getLoanProduct().getCharges().stream() + .filter((e) -> ChargeTimeType.OVERDUE_INSTALLMENT.getValue().equals(e.getChargeTimeType()) && e.isLoanCharge()).findFirst(); + + if (optPenaltyCharge.isEmpty()) { + return list; + } + final Charge penaltyCharge = optPenaltyCharge.get(); + final Long penaltyWaitPeriod = configurationDomainService.retrievePenaltyWaitPeriod(); final boolean backdatePenalties = configurationDomainService.isBackdatePenaltiesEnabled(); @@ -1657,16 +1666,9 @@ public Collection retrieveAllOverdueInstallmentsForLoan if (!backdatePenalties && !isDueToday) { continue; } - Optional penaltyCharge = loan.getLoanProduct().getCharges().stream() - .filter((e) -> ChargeTimeType.OVERDUE_INSTALLMENT.getValue().equals(e.getChargeTimeType()) && e.isLoanCharge()) - .findFirst(); - - if (penaltyCharge.isEmpty()) { - continue; - } - list.add(new OverdueLoanScheduleData(loan.getId(), penaltyCharge.get().getId(), - DateUtils.DEFAULT_DATE_FORMATTER.format(installment.getDueDate()), penaltyCharge.get().getAmount(), + list.add(new OverdueLoanScheduleData(loan.getId(), penaltyCharge.getId(), + DateUtils.DEFAULT_DATE_FORMATTER.format(installment.getDueDate()), penaltyCharge.getAmount(), DateUtils.DEFAULT_DATE_FORMAT, Locale.ENGLISH.toLanguageTag(), installment.getPrincipalOutstanding(loan.getCurrency()).getAmount(), installment.getInterestOutstanding(loan.getCurrency()).getAmount(), installment.getInstallmentNumber()));