FINERACT-2081: Skip loan processing if no overdue

This commit is contained in:
Andrii Kulminskyi
2024-09-26 10:31:24 +02:00
committed by Adam Saghy
parent a655707e0a
commit b68149c6cd
9 changed files with 219 additions and 86 deletions
@@ -37,7 +37,9 @@ public class ApplyChargeToOverdueLoansBusinessStep implements LoanCOBBusinessSte
public Loan execute(Loan loan) {
final Collection<OverdueLoanScheduleData> overdueLoanScheduleDataList = loanReadPlatformService
.retrieveAllOverdueInstallmentsForLoan(loan);
loanChargeWritePlatformService.applyOverdueChargesForLoan(loan.getId(), overdueLoanScheduleDataList);
if (!overdueLoanScheduleDataList.isEmpty()) {
loanChargeWritePlatformService.applyOverdueChargesForLoan(loan.getId(), overdueLoanScheduleDataList);
}
return loan;
}
@@ -68,7 +68,9 @@ public class ApplyChargeToOverdueLoanInstallmentTasklet implements Tasklet {
List<Throwable> exceptions = new ArrayList<>();
for (Map.Entry<Long, Collection<OverdueLoanScheduleData>> 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<ApiParameterError> errors = e.getErrors();
@@ -31,6 +31,7 @@ import java.util.LinkedHashMap;
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;
@@ -764,11 +765,19 @@ public class LoanChargeWritePlatformServiceImpl implements LoanChargeWritePlatfo
@Transactional
@Override
public void applyOverdueChargesForLoan(final Long loanId, Collection<OverdueLoanScheduleData> 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<Charge> optPenaltyCharge = loan.getLoanProduct().getCharges().stream()
.filter((e) -> ChargeTimeType.OVERDUE_INSTALLMENT.getValue().equals(e.getChargeTimeType()) && e.isLoanCharge()).findFirst();
if (optPenaltyCharge.isEmpty()) {
return;
}
final List<Long> existingTransactionIds = loan.findExistingTransactionIds();
final List<Long> existingReversedTransactionIds = loan.findExistingReversedTransactionIds();
boolean runInterestRecalculation = false;
@@ -1642,6 +1642,15 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService, Loa
if (!loan.isOpen()) {
return list;
}
Optional<Charge> 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 class LoanReadPlatformServiceImpl implements LoanReadPlatformService, Loa
if (!backdatePenalties && !isDueToday) {
continue;
}
Optional<Charge> 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()));
@@ -0,0 +1,80 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.cob.loan;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.anyCollection;
import static org.mockito.Mockito.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Collections;
import java.util.List;
import org.apache.fineract.portfolio.loanaccount.domain.Loan;
import org.apache.fineract.portfolio.loanaccount.loanschedule.data.OverdueLoanScheduleData;
import org.apache.fineract.portfolio.loanaccount.service.LoanChargeWritePlatformService;
import org.apache.fineract.portfolio.loanaccount.service.LoanReadPlatformService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class ApplyChargeToOverdueLoansBusinessStepTest {
private LoanReadPlatformService loanReadPlatformService;
private LoanChargeWritePlatformService loanChargeWritePlatformService;
private ApplyChargeToOverdueLoansBusinessStep applyChargeToOverdueLoansBusinessStep;
@BeforeEach
public void setUp() {
loanReadPlatformService = mock(LoanReadPlatformService.class);
loanChargeWritePlatformService = mock(LoanChargeWritePlatformService.class);
applyChargeToOverdueLoansBusinessStep = new ApplyChargeToOverdueLoansBusinessStep(loanReadPlatformService,
loanChargeWritePlatformService);
}
@Test
public void testExecute_WhenNoOverdueInstallments_ShouldNotApplyCharges() {
Loan loan = mock(Loan.class);
when(loanReadPlatformService.retrieveAllOverdueInstallmentsForLoan(loan)).thenReturn(Collections.emptyList());
Loan result = applyChargeToOverdueLoansBusinessStep.execute(loan);
assertNotNull(result);
verify(loanChargeWritePlatformService, never()).applyOverdueChargesForLoan(anyLong(), anyCollection());
}
@Test
public void testExecute_WhenOverdueInstallmentsExist_ShouldApplyCharges() {
Long testId = 1L;
Loan loan = mock(Loan.class);
OverdueLoanScheduleData overdueData = mock(OverdueLoanScheduleData.class);
List<OverdueLoanScheduleData> overdueList = Collections.singletonList(overdueData);
when(loan.getId()).thenReturn(testId);
when(loanReadPlatformService.retrieveAllOverdueInstallmentsForLoan(loan)).thenReturn(overdueList);
Loan result = applyChargeToOverdueLoansBusinessStep.execute(loan);
assertNotNull(result);
verify(loanChargeWritePlatformService, times(1)).applyOverdueChargesForLoan(testId, overdueList);
}
}
@@ -0,0 +1,86 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.portfolio.loanaccount.jobs.applychargetooverdueloaninstallment;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.anyBoolean;
import static org.mockito.Mockito.anyCollection;
import static org.mockito.Mockito.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Collections;
import org.apache.fineract.infrastructure.configuration.domain.ConfigurationDomainService;
import org.apache.fineract.portfolio.loanaccount.loanschedule.data.OverdueLoanScheduleData;
import org.apache.fineract.portfolio.loanaccount.service.LoanChargeWritePlatformService;
import org.apache.fineract.portfolio.loanaccount.service.LoanReadPlatformService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.repeat.RepeatStatus;
public class ApplyChargeToOverdueLoanInstallmentTaskletTest {
private LoanReadPlatformService loanReadPlatformService;
private LoanChargeWritePlatformService loanChargeWritePlatformService;
private StepContribution contribution;
private ChunkContext chunkContext;
private ApplyChargeToOverdueLoanInstallmentTasklet tasklet;
@BeforeEach
public void setUp() {
loanReadPlatformService = mock(LoanReadPlatformService.class);
loanChargeWritePlatformService = mock(LoanChargeWritePlatformService.class);
ConfigurationDomainService configurationDomainService = mock(ConfigurationDomainService.class);
contribution = mock(StepContribution.class);
chunkContext = mock(ChunkContext.class);
tasklet = new ApplyChargeToOverdueLoanInstallmentTasklet(configurationDomainService, loanReadPlatformService,
loanChargeWritePlatformService);
}
@Test
public void testExecute_WhenNoOverdueInstallments_ShouldNotApplyCharges() throws Exception {
when(loanReadPlatformService.retrieveAllLoansWithOverdueInstallments(anyLong(), anyBoolean()))
.thenReturn(Collections.emptyList());
RepeatStatus status = tasklet.execute(contribution, chunkContext);
assertEquals(RepeatStatus.FINISHED, status);
verify(loanChargeWritePlatformService, never()).applyOverdueChargesForLoan(anyLong(), anyCollection());
}
@Test
public void testExecute_WhenOverdueInstallmentsExist_ShouldApplyCharges() throws Exception {
OverdueLoanScheduleData overdueData = mock(OverdueLoanScheduleData.class);
when(loanReadPlatformService.retrieveAllLoansWithOverdueInstallments(anyLong(), anyBoolean()))
.thenReturn(Collections.singletonList(overdueData));
RepeatStatus status = tasklet.execute(contribution, chunkContext);
assertEquals(RepeatStatus.FINISHED, status);
verify(loanChargeWritePlatformService, times(1)).applyOverdueChargesForLoan(anyLong(), anyCollection());
}
}