FINERACT-1971: RepaymentOverDueBusinessEvent should not be sent when balance is 0

This commit is contained in:
Jose Alberto Hernandez
2024-04-15 11:27:54 +02:00
committed by Adam Saghy
parent 35bdbccd2e
commit 99ba46386b
3 changed files with 151 additions and 24 deletions
@@ -18,7 +18,9 @@
*/
package org.apache.fineract.cob.loan;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@@ -28,6 +30,7 @@ import org.apache.fineract.infrastructure.event.business.domain.loan.repayment.L
import org.apache.fineract.infrastructure.event.business.service.BusinessEventNotifierService;
import org.apache.fineract.portfolio.loanaccount.domain.Loan;
import org.apache.fineract.portfolio.loanaccount.domain.LoanRepaymentScheduleInstallment;
import org.apache.fineract.portfolio.loanaccount.domain.LoanStatus;
import org.springframework.stereotype.Component;
@Slf4j
@@ -40,25 +43,31 @@ public class CheckLoanRepaymentOverdueBusinessStep implements LoanCOBBusinessSte
@Override
public Loan execute(Loan loan) {
log.debug("start processing loan repayment overdue business step for loan with Id [{}]", loan.getId());
Long numberOfDaysAfterDueDateToRaiseEvent = configurationDomainService.retrieveRepaymentOverdueDays();
if (loan.getLoanProduct().getOverDueDaysForRepaymentEvent() != null) {
if (loan.getLoanProduct().getOverDueDaysForRepaymentEvent() > 0) {
numberOfDaysAfterDueDateToRaiseEvent = loan.getLoanProduct().getOverDueDaysForRepaymentEvent().longValue();
}
}
final LocalDate currentDate = DateUtils.getBusinessLocalDate();
final List<LoanRepaymentScheduleInstallment> loanRepaymentScheduleInstallments = loan.getRepaymentScheduleInstallments();
for (LoanRepaymentScheduleInstallment repaymentSchedule : loanRepaymentScheduleInstallments) {
if (!repaymentSchedule.isObligationsMet()) {
LocalDate installmentDueDate = repaymentSchedule.getDueDate();
if (installmentDueDate.plusDays(numberOfDaysAfterDueDateToRaiseEvent).equals(currentDate)) {
businessEventNotifierService.notifyPostBusinessEvent(new LoanRepaymentOverdueBusinessEvent(repaymentSchedule));
break;
List<LoanStatus> nonDisbursedStatuses = Arrays.asList(LoanStatus.INVALID, LoanStatus.SUBMITTED_AND_PENDING_APPROVAL,
LoanStatus.APPROVED);
if (!nonDisbursedStatuses.contains(loan.getStatus())
&& loan.getLoanSummary().getTotalOutstanding().compareTo(BigDecimal.ZERO) > 0) {
log.debug("start processing loan repayment overdue business step for loan with Id [{}]", loan.getId());
Long numberOfDaysAfterDueDateToRaiseEvent = configurationDomainService.retrieveRepaymentOverdueDays();
if (loan.getLoanProduct().getOverDueDaysForRepaymentEvent() != null) {
if (loan.getLoanProduct().getOverDueDaysForRepaymentEvent() > 0) {
numberOfDaysAfterDueDateToRaiseEvent = loan.getLoanProduct().getOverDueDaysForRepaymentEvent().longValue();
}
}
final LocalDate currentDate = DateUtils.getBusinessLocalDate();
final List<LoanRepaymentScheduleInstallment> loanRepaymentScheduleInstallments = loan.getRepaymentScheduleInstallments();
for (LoanRepaymentScheduleInstallment repaymentSchedule : loanRepaymentScheduleInstallments) {
if (!repaymentSchedule.isObligationsMet()) {
LocalDate installmentDueDate = repaymentSchedule.getDueDate();
if (isOverDueEventNeededToBeSent(loan, numberOfDaysAfterDueDateToRaiseEvent, currentDate, repaymentSchedule,
installmentDueDate)) {
businessEventNotifierService.notifyPostBusinessEvent(new LoanRepaymentOverdueBusinessEvent(repaymentSchedule));
break;
}
}
}
log.debug("end processing loan repayment overdue business step for loan with Id [{}]", loan.getId());
}
log.debug("end processing loan repayment overdue business step for loan with Id [{}]", loan.getId());
return loan;
}
@@ -71,4 +80,11 @@ public class CheckLoanRepaymentOverdueBusinessStep implements LoanCOBBusinessSte
public String getHumanReadableName() {
return "Check loan repayment overdue";
}
private static boolean isOverDueEventNeededToBeSent(Loan loan, Long numberOfDaysBeforeDueDateToRaiseEvent, LocalDate currentDate,
LoanRepaymentScheduleInstallment repaymentScheduleInstallment, LocalDate repaymentDate) {
return repaymentDate.plusDays(numberOfDaysBeforeDueDateToRaiseEvent).equals(currentDate)
&& repaymentScheduleInstallment.getTotalOutstanding(loan.getCurrency()).isGreaterThanZero();
}
}