Loan Delinquency Classification job

This commit is contained in:
Jose Alberto Hernandez
2022-09-03 15:24:01 +02:00
committed by Arnold Gálovics
parent 24a199458f
commit 0980aa8abf
12 changed files with 74 additions and 44 deletions
@@ -33,7 +33,7 @@ public class LoanDelinquencyTagHistoryData implements Serializable {
private Long id;
private Long loanId;
private String delinquencyRange;
private DelinquencyRangeData delinquencyRange;
private LocalDate addedOnDate;
private LocalDate liftedOnDate;
@@ -21,7 +21,6 @@ package org.apache.fineract.portfolio.delinquency.domain;
import java.time.LocalDate;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@@ -39,11 +38,11 @@ import org.apache.fineract.portfolio.loanaccount.domain.Loan;
@Table(name = "m_loan_delinquency_tag_history")
public class LoanDelinquencyTagHistory extends AbstractAuditableWithUTCDateTimeCustom {
@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne
@JoinColumn(name = "delinquency_range_id", nullable = false)
private DelinquencyRange delinquencyRange;
@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne
@JoinColumn(name = "loan_id", nullable = false)
private Loan loan;
@@ -23,15 +23,15 @@ import org.apache.fineract.portfolio.delinquency.data.LoanDelinquencyTagHistoryD
import org.apache.fineract.portfolio.delinquency.domain.LoanDelinquencyTagHistory;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
@Mapper(componentModel = "spring")
@Mapper(componentModel = "spring", uses = DelinquencyRangeMapper.class)
public interface LoanDelinquencyTagMapper {
@Mappings({ @Mapping(target = "id", source = "source.id"), @Mapping(target = "loanId", source = "source.loan.id"),
@Mapping(target = "delinquencyRange", source = "source.delinquencyRange.classification"),
@Mapping(target = "addedOnDate", source = "source.addedOnDate"),
@Mapping(target = "liftedOnDate", source = "source.liftedOnDate") })
@Mapping(target = "id", source = "source.id")
@Mapping(target = "loanId", source = "source.loan.id")
@Mapping(target = "delinquencyRange", source = "source.delinquencyRange")
@Mapping(target = "addedOnDate", source = "source.addedOnDate")
@Mapping(target = "liftedOnDate", source = "source.liftedOnDate")
LoanDelinquencyTagHistoryData map(LoanDelinquencyTagHistory source);
List<LoanDelinquencyTagHistoryData> map(List<LoanDelinquencyTagHistory> sources);
@@ -35,8 +35,7 @@ import org.apache.fineract.portfolio.delinquency.mapper.DelinquencyBucketMapper;
import org.apache.fineract.portfolio.delinquency.mapper.DelinquencyRangeMapper;
import org.apache.fineract.portfolio.delinquency.mapper.LoanDelinquencyTagMapper;
import org.apache.fineract.portfolio.loanaccount.domain.Loan;
import org.apache.fineract.portfolio.loanaccount.service.LoanAssembler;
import org.springframework.jdbc.core.JdbcTemplate;
import org.apache.fineract.portfolio.loanaccount.domain.LoanRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -45,14 +44,13 @@ import org.springframework.transaction.annotation.Transactional;
@Transactional(readOnly = true)
public class DelinquencyReadPlatformServiceImpl implements DelinquencyReadPlatformService {
private final JdbcTemplate jdbcTemplate;
private final DelinquencyRangeRepository repositoryRange;
private final DelinquencyBucketRepository repositoryBucket;
private final LoanDelinquencyTagHistoryRepository repositoryLoanDelinquencyTagHistory;
private final DelinquencyRangeMapper mapperRange;
private final DelinquencyBucketMapper mapperBucket;
private final LoanDelinquencyTagMapper mapperLoanDelinquencyTagHistory;
private final LoanAssembler loanAssembler;
private final LoanRepository loanRepository;
@Override
public Collection<DelinquencyRangeData> retrieveAllDelinquencyRanges() {
@@ -82,7 +80,7 @@ public class DelinquencyReadPlatformServiceImpl implements DelinquencyReadPlatfo
@Override
public DelinquencyRangeData retrieveCurrentDelinquencyTag(Long loanId) {
final Loan loan = this.loanAssembler.assembleFrom(loanId);
final Loan loan = this.loanRepository.getReferenceById(loanId);
Optional<LoanDelinquencyTagHistory> optLoanDelinquencyTag = this.repositoryLoanDelinquencyTagHistory.findByLoanAndLiftedOnDate(loan,
null);
if (optLoanDelinquencyTag.isPresent()) {
@@ -93,7 +91,7 @@ public class DelinquencyReadPlatformServiceImpl implements DelinquencyReadPlatfo
@Override
public Collection<LoanDelinquencyTagHistoryData> retrieveDelinquencyRangeHistory(Long loanId) {
final Loan loan = this.loanAssembler.assembleFrom(loanId);
final Loan loan = this.loanRepository.getReferenceById(loanId);
final List<LoanDelinquencyTagHistory> loanDelinquencyTagData = this.repositoryLoanDelinquencyTagHistory
.findByLoanOrderByAddedOnDateDesc(loan);
return mapperLoanDelinquencyTagHistory.map(loanDelinquencyTagData);
@@ -287,10 +287,10 @@ public class DelinquencyWritePlatformServiceImpl implements DelinquencyWritePlat
} else {
// Sort the ranges based on the minAgeDays
List<DelinquencyRange> ranges = sortDelinquencyRangesByMinAge(delinquencyBucket.getRanges());
final List<DelinquencyRange> ranges = sortDelinquencyRangesByMinAge(delinquencyBucket.getRanges());
for (DelinquencyRange delinquencyRange : ranges) {
if (delinquencyRange.getMaximumAgeDays() == null) {
for (final DelinquencyRange delinquencyRange : ranges) {
if (delinquencyRange.getMaximumAgeDays() == null) { // Last Range in the Bucket
if (delinquencyRange.getMinimumAgeDays() <= ageDays) {
log.debug("Loan {} with delinquency range {} with {} days", loan.getId(), delinquencyRange.getClassification(),
ageDays);
@@ -983,8 +983,7 @@ public class LoansApiResource {
context.authenticatedUser().validateHasReadPermission("DELINQUENCY_TAGS");
final Collection<LoanDelinquencyTagHistoryData> loanDelinquencyTagHistoryData = this.delinquencyReadPlatformService
.retrieveDelinquencyRangeHistory(loanId);
ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper.process(uriInfo.getQueryParameters());
return this.jsonSerializerTagHistory.serialize(settings, loanDelinquencyTagHistoryData);
return this.jsonSerializerTagHistory.serialize(loanDelinquencyTagHistoryData);
}
}
@@ -30,7 +30,6 @@ public class LoanScheduleDelinquencyData implements Serializable {
private final Long loanId;
private final Long productId;
private final LocalDate dueDate;
private final LocalDate fromDate;
private final Long ageDays;
}
@@ -22,7 +22,6 @@ import lombok.AllArgsConstructor;
import org.apache.fineract.infrastructure.jobs.service.JobName;
import org.apache.fineract.portfolio.delinquency.service.DelinquencyWritePlatformService;
import org.apache.fineract.portfolio.loanaccount.service.LoanReadPlatformService;
import org.apache.fineract.portfolio.loanproduct.domain.LoanProductRepository;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
@@ -38,7 +37,6 @@ public class SetLoanDelinquencyTagsConfig {
private JobBuilderFactory jobs;
private StepBuilderFactory steps;
private LoanProductRepository loanProductRepository;
private DelinquencyWritePlatformService delinquencyWritePlatformService;
private LoanReadPlatformService loanReadPlatformService;
@@ -55,7 +53,7 @@ public class SetLoanDelinquencyTagsConfig {
@Bean
public SetLoanDelinquencyTagsTasklet setLoanDelinquencyTagsTasklet() {
return new SetLoanDelinquencyTagsTasklet(loanProductRepository, delinquencyWritePlatformService, loanReadPlatformService);
return new SetLoanDelinquencyTagsTasklet(delinquencyWritePlatformService, loanReadPlatformService);
}
}
@@ -26,7 +26,6 @@ import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
import org.apache.fineract.portfolio.delinquency.service.DelinquencyWritePlatformService;
import org.apache.fineract.portfolio.loanaccount.data.LoanScheduleDelinquencyData;
import org.apache.fineract.portfolio.loanaccount.service.LoanReadPlatformService;
import org.apache.fineract.portfolio.loanproduct.domain.LoanProductRepository;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
@@ -36,16 +35,18 @@ import org.springframework.batch.repeat.RepeatStatus;
@RequiredArgsConstructor
public class SetLoanDelinquencyTagsTasklet implements Tasklet {
private final LoanProductRepository loanProductRepository;
private final DelinquencyWritePlatformService delinquencyWritePlatformService;
private final LoanReadPlatformService loanReadPlatformService;
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
log.debug("Run job for date {}", DateUtils.getBusinessLocalDate());
Collection<LoanScheduleDelinquencyData> loanScheduleDelinquencyData = this.loanReadPlatformService
.retrieveScheduleDelinquencyData(DateUtils.getBusinessLocalDate());
log.debug("Were found {} items", loanScheduleDelinquencyData.size());
for (LoanScheduleDelinquencyData loanDelinquencyData : loanScheduleDelinquencyData) {
log.debug("Processing Loan {} with due date {} and {} overdue days", loanDelinquencyData.getLoanId(),
loanDelinquencyData.getDueDate(), loanDelinquencyData.getAgeDays());
this.delinquencyWritePlatformService.applyDelinquencyTagToLoan(loanDelinquencyData.getLoanId(),
loanDelinquencyData.getAgeDays());
}
@@ -1696,7 +1696,6 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService {
return new LoanTermVariationsData(id, LoanEnumerations.loanVariationType(LoanTermVariationType.EMI_AMOUNT),
variationApplicableFrom, decimalValue, dateValue, isSpecificToInstallment);
}
}
@Override
@@ -1704,10 +1703,13 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService {
LoanScheduleDelinquencyMapper mapper = new LoanScheduleDelinquencyMapper(DateUtils.getBusinessLocalDate());
final StringBuilder sqlBuilder = new StringBuilder(400);
sqlBuilder.append("select ").append(mapper.schema())
// Just get the overdued installments
.append(" where loan.loan_status_id=:active and ls.duedate <= :businessLocalDate and ")
.append(" mpl.delinquency_bucket_id is not null "); // Jusr the Loan Product linked to delinquency
// bucket
sqlBuilder.append(" order by loan.id, ls.duedate ");
// Just get the unpaid installments using the completed_derived flag
.append(" ls.completed_derived = false and ")
// Just the Loan Product linked to delinquency bucket
.append(" mpl.delinquency_bucket_id is not null ");
sqlBuilder.append(" group by ls.loan_id, loan.product_id ");
Map<String, Object> paramMap = new HashMap<>(3);
paramMap.put("active", LoanStatus.ACTIVE.getValue());
paramMap.put("businessLocalDate", businessLocalDate);
@@ -1894,7 +1896,7 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService {
public String schema() {
final StringBuilder sqlBuilder = new StringBuilder(400);
sqlBuilder.append(" loan.id as loanId, ls.duedate as duedate, ls.fromdate as fromdate, loan.product_id as productId ")
sqlBuilder.append(" ls.loan_id as loanId, loan.product_id as productId, min(ls.duedate) as duedate ")
.append(" from m_loan_repayment_schedule ls left join m_loan loan on loan.id=ls.loan_id ")
.append(" left join m_product_loan mpl on mpl.id = loan.product_id ");
return sqlBuilder.toString();
@@ -1906,9 +1908,8 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService {
final Long loanId = rs.getLong("loanId");
final Long productId = rs.getLong("productId");
final LocalDate dueDate = JdbcSupport.getLocalDate(rs, "duedate");
final LocalDate fromDate = JdbcSupport.getLocalDate(rs, "fromdate");
final long ageDays = DateUtils.getDifferenceInDays(dueDate, businessDate);
return new LoanScheduleDelinquencyData(loanId, productId, dueDate, fromDate, ageDays);
return new LoanScheduleDelinquencyData(loanId, productId, dueDate, ageDays);
}
}
@@ -277,8 +277,8 @@ public final class LoanProductDataValidator {
if (this.fromApiJsonHelper.parameterExists(LoanProductConstants.DELINQUENCY_BUCKET_PARAM_NAME, element)) {
final Long delinquencyBucketId = this.fromApiJsonHelper.extractLongNamed(LoanProductConstants.DELINQUENCY_BUCKET_PARAM_NAME,
element);
baseDataValidator.reset().parameter(LoanProductConstants.DELINQUENCY_BUCKET_PARAM_NAME).value(delinquencyBucketId)
.ignoreIfNull().integerGreaterThanZero();
baseDataValidator.reset().parameter(LoanProductConstants.DELINQUENCY_BUCKET_PARAM_NAME).value(delinquencyBucketId).notNull()
.integerGreaterThanZero();
}
// grace validation
@@ -21,6 +21,7 @@ package org.apache.fineract.integrationtests;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.builder.ResponseSpecBuilder;
@@ -36,12 +37,17 @@ import org.apache.fineract.client.models.DeleteDelinquencyRangeResponse;
import org.apache.fineract.client.models.GetDelinquencyBucketsResponse;
import org.apache.fineract.client.models.GetDelinquencyRangesResponse;
import org.apache.fineract.client.models.GetLoanProductsProductIdResponse;
import org.apache.fineract.client.models.GetLoansLoanIdRepaymentPeriod;
import org.apache.fineract.client.models.GetLoansLoanIdRepaymentSchedule;
import org.apache.fineract.client.models.GetLoansLoanIdResponse;
import org.apache.fineract.client.models.PostDelinquencyBucketResponse;
import org.apache.fineract.client.models.PostDelinquencyRangeResponse;
import org.apache.fineract.client.models.PutDelinquencyBucketResponse;
import org.apache.fineract.client.models.PutDelinquencyRangeResponse;
import org.apache.fineract.infrastructure.businessdate.domain.BusinessDateType;
import org.apache.fineract.integrationtests.common.BusinessDateHelper;
import org.apache.fineract.integrationtests.common.ClientHelper;
import org.apache.fineract.integrationtests.common.GlobalConfigurationHelper;
import org.apache.fineract.integrationtests.common.SchedulerJobHelper;
import org.apache.fineract.integrationtests.common.Utils;
import org.apache.fineract.integrationtests.common.loans.LoanApplicationTestBuilder;
@@ -221,6 +227,13 @@ public class DelinquencyBucketsIntegrationTest {
@Test
public void testLoanClassificationJob() {
GlobalConfigurationHelper.updateIsBusinessDateEnabled(requestSpec, responseSpec, Boolean.TRUE);
LocalDate businessDate = Utils.getLocalDateOfTenant();
businessDate = businessDate.minusDays(57);
log.info("Current date {}", businessDate);
BusinessDateHelper.updateBusinessDate(requestSpec, responseSpec, BusinessDateType.BUSINESS_DATE, businessDate);
// Given
final LoanTransactionHelper loanTransactionHelper = new LoanTransactionHelper(this.requestSpec, this.responseSpec);
final SchedulerJobHelper schedulerJobHelper = new SchedulerJobHelper(requestSpec);
@@ -253,12 +266,10 @@ public class DelinquencyBucketsIntegrationTest {
final GetLoanProductsProductIdResponse getLoanProductsProductResponse = loanTransactionHelper.getLoanProduct(loanProductId);
log.info("Loan Product Bucket Name: {}", getLoanProductsProductResponse.getDelinquencyBucket().getName());
LocalDate todaysDate = Utils.getLocalDateOfTenant();
todaysDate = todaysDate.minusDays(40);
final String operationDate = Utils.dateFormatter.format(todaysDate);
final String operationDate = Utils.dateFormatter.format(businessDate);
final String principalAmount = "10000";
final String loanApplicationJSON = new LoanApplicationTestBuilder().withPrincipal(principalAmount).withLoanTermFrequency("12")
String loanApplicationJSON = new LoanApplicationTestBuilder().withPrincipal(principalAmount).withLoanTermFrequency("12")
.withLoanTermFrequencyAsMonths().withNumberOfRepayments("12").withRepaymentEveryAfter("1")
.withRepaymentFrequencyTypeAsMonths() //
.withInterestRatePerPeriod("2") //
@@ -271,19 +282,43 @@ public class DelinquencyBucketsIntegrationTest {
loanTransactionHelper.disburseLoanWithNetDisbursalAmount(operationDate, loanId, principalAmount);
// When
// Run first time the Job
final String jobName = "Loan Delinquency Classification";
schedulerJobHelper.executeAndAwaitJob(jobName);
final GetLoansLoanIdResponse getLoansLoanIdResponse = loanTransactionHelper.getLoan(requestSpec, responseSpec, loanId);
log.info("Loan Delinquency Range {}", getLoansLoanIdResponse.getDelinquencyRange().getClassification());
// Get loan details expecting to have not a delinquency classification
GetLoansLoanIdResponse getLoansLoanIdResponse = loanTransactionHelper.getLoan(requestSpec, responseSpec, loanId);
final GetDelinquencyRangesResponse firstTestCase = getLoansLoanIdResponse.getDelinquencyRange();
log.info("Loan Delinquency Range is null {}", (firstTestCase == null));
GetLoansLoanIdRepaymentSchedule getLoanRepaymentSchedule = getLoansLoanIdResponse.getRepaymentSchedule();
if (getLoanRepaymentSchedule != null) {
log.info("Loan with {} periods", getLoanRepaymentSchedule.getPeriods().size());
for (GetLoansLoanIdRepaymentPeriod period : getLoanRepaymentSchedule.getPeriods()) {
log.info("Period number {} for due date {}", period.getPeriod(), period.getDueDate());
}
}
// Move the Business date to get older the loan and to have an overdue loan
businessDate = businessDate.plusDays(40);
log.info("Current date {}", businessDate);
BusinessDateHelper.updateBusinessDate(requestSpec, responseSpec, BusinessDateType.BUSINESS_DATE, businessDate);
// Run Second time the Job
schedulerJobHelper.executeAndAwaitJob(jobName);
// Get loan details expecting to have a delinquency classification
getLoansLoanIdResponse = loanTransactionHelper.getLoan(requestSpec, responseSpec, loanId);
final GetDelinquencyRangesResponse secondTestCase = getLoansLoanIdResponse.getDelinquencyRange();
log.info("Loan Delinquency Range is {}", secondTestCase.getClassification());
// Then
assertNotNull(delinquencyBucketResponse);
assertNotNull(getLoanProductsProductResponse);
assertNull(firstTestCase);
assertEquals(getLoanProductsProductResponse.getDelinquencyBucket().getName(), delinquencyBucket.getName());
assertNotNull(getLoansLoanIdResponse);
assertEquals(getLoansLoanIdResponse.getDelinquencyRange().getClassification(), classificationExpected);
assertNotNull(secondTestCase);
assertEquals(secondTestCase.getClassification(), classificationExpected);
GlobalConfigurationHelper.updateIsBusinessDateEnabled(requestSpec, responseSpec, Boolean.FALSE);
}
}