FINERACT-1981: Interest recalculation - Recalculate Interest For Loan - Business step for COB
This commit is contained in:
+64
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* 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 lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.fineract.infrastructure.core.domain.ActionContext;
|
||||
import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
|
||||
import org.apache.fineract.portfolio.loanaccount.domain.Loan;
|
||||
import org.apache.fineract.portfolio.loanaccount.service.LoanWritePlatformService;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class LoanInterestRecalculationCOBBusinessStep implements LoanCOBBusinessStep {
|
||||
|
||||
private final LoanWritePlatformService loanWritePlatformService;
|
||||
|
||||
@Override
|
||||
public Loan execute(Loan loan) {
|
||||
if (!loan.isInterestBearing() || !loan.getStatus().isActive() || loan.isNpa() || loan.isChargedOff()
|
||||
|| !loan.isInterestRecalculationEnabledForProduct()) {
|
||||
log.debug("Skip processing loan interest recalculation [{}] - reason: not interest bearing loan or not active.", loan.getId());
|
||||
return loan;
|
||||
}
|
||||
|
||||
log.debug("Start processing loan interest recalculation [{}]", loan.getId());
|
||||
|
||||
ThreadLocalContextUtil.setActionContext(ActionContext.DEFAULT);
|
||||
|
||||
loan = loanWritePlatformService.recalculateInterest(loan);
|
||||
|
||||
log.debug("End processing loan interest recalculation [{}]", loan.getId());
|
||||
return loan;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getEnumStyledName() {
|
||||
return "LOAN_INTEREST_RECALCULATION";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHumanReadableName() {
|
||||
return "Loan Interest Recalculation";
|
||||
}
|
||||
|
||||
}
|
||||
+12
-2
@@ -32,8 +32,11 @@ import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.step.builder.StepBuilder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
@@ -52,7 +55,7 @@ public class RecalculateInterestForLoanConfig {
|
||||
private LoanWritePlatformService loanWritePlatformService;
|
||||
|
||||
@Autowired
|
||||
private RecalculateInterestPoster recalculateInterestPoster;
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Autowired
|
||||
private OfficeReadPlatformService officeReadPlatformService;
|
||||
@@ -73,9 +76,16 @@ public class RecalculateInterestForLoanConfig {
|
||||
.incrementer(new RunIdIncrementer()).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Scope("prototype")
|
||||
@ConditionalOnMissingBean(RecalculateInterestPoster.class)
|
||||
public RecalculateInterestPoster recalculateInterestPoster() {
|
||||
return new RecalculateInterestPoster(loanWritePlatformService);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RecalculateInterestForLoanTasklet recalculateInterestForLoanTasklet() {
|
||||
return new RecalculateInterestForLoanTasklet(loanReadPlatformService, loanWritePlatformService, recalculateInterestPoster,
|
||||
return new RecalculateInterestForLoanTasklet(loanReadPlatformService, loanWritePlatformService, applicationContext,
|
||||
officeReadPlatformService, taskExecutor);
|
||||
}
|
||||
}
|
||||
|
||||
+15
-49
@@ -29,7 +29,9 @@ import java.util.concurrent.Future;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.collections4.ListUtils;
|
||||
import org.apache.fineract.infrastructure.core.config.TaskExecutorConstant;
|
||||
import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
|
||||
import org.apache.fineract.infrastructure.jobs.exception.JobExecutionException;
|
||||
import org.apache.fineract.organisation.office.data.OfficeData;
|
||||
import org.apache.fineract.organisation.office.exception.OfficeNotFoundException;
|
||||
@@ -43,6 +45,7 @@ import org.springframework.batch.core.scope.context.ChunkContext;
|
||||
import org.springframework.batch.core.step.tasklet.Tasklet;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
|
||||
@Slf4j
|
||||
@@ -51,7 +54,7 @@ public class RecalculateInterestForLoanTasklet implements Tasklet {
|
||||
|
||||
private final LoanReadPlatformService loanReadPlatformService;
|
||||
private final LoanWritePlatformService loanWritePlatformService;
|
||||
private final RecalculateInterestPoster recalculateInterestPoster;
|
||||
private final ApplicationContext applicationContext;
|
||||
private final OfficeReadPlatformService officeReadPlatformService;
|
||||
@Qualifier(TaskExecutorConstant.CONFIGURABLE_TASK_EXECUTOR_BEAN_NAME)
|
||||
private final ThreadPoolTaskExecutor taskExecutor;
|
||||
@@ -106,67 +109,30 @@ public class RecalculateInterestForLoanTasklet implements Tasklet {
|
||||
do {
|
||||
int totalFilteredRecords = loanIds.size();
|
||||
log.debug("Starting accrual - total filtered records - {}", totalFilteredRecords);
|
||||
recalculateInterest(loanIds, threadPoolSize, batchSize);
|
||||
recalculateInterest(loanIds, threadPoolSize);
|
||||
maxLoanIdInList += pageSize + 1;
|
||||
loanIds = Collections.synchronizedList(
|
||||
this.loanReadPlatformService.fetchLoansForInterestRecalculation(pageSize, maxLoanIdInList, officeHierarchy));
|
||||
} while (!CollectionUtils.isEmpty(loanIds));
|
||||
}
|
||||
|
||||
private void recalculateInterest(List<Long> loanIds, int threadPoolSize, int batchSize) {
|
||||
|
||||
List<Callable<Void>> posters = new ArrayList<>();
|
||||
int fromIndex = 0;
|
||||
int size = loanIds.size();
|
||||
double toGetCeilValue = size / ((double) threadPoolSize);
|
||||
batchSize = (int) Math.ceil(toGetCeilValue);
|
||||
|
||||
if (batchSize == 0) {
|
||||
private void recalculateInterest(List<Long> loanIds, int threadPoolSize) {
|
||||
if (loanIds == null || loanIds.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
int toIndex = (batchSize > size - 1) ? size : batchSize;
|
||||
while (toIndex < size && loanIds.get(toIndex - 1).equals(loanIds.get(toIndex))) {
|
||||
toIndex++;
|
||||
}
|
||||
boolean lastBatch = false;
|
||||
int loopCount = size / batchSize + 1;
|
||||
int actualBatchSize = (int) Math.ceil(loanIds.size() / (double) threadPoolSize);
|
||||
|
||||
for (long i = 0; i < loopCount; i++) {
|
||||
List<Long> subList = safeSubList(loanIds, fromIndex, toIndex);
|
||||
recalculateInterestPoster.setLoanIds(subList);
|
||||
recalculateInterestPoster.setLoanWritePlatformService(loanWritePlatformService);
|
||||
posters.add(recalculateInterestPoster);
|
||||
if (lastBatch) {
|
||||
break;
|
||||
}
|
||||
if (toIndex + batchSize > size - 1) {
|
||||
lastBatch = true;
|
||||
}
|
||||
fromIndex = fromIndex + (toIndex - fromIndex);
|
||||
toIndex = (toIndex + batchSize > size - 1) ? size : toIndex + batchSize;
|
||||
while (toIndex < size && loanIds.get(toIndex - 1).equals(loanIds.get(toIndex))) {
|
||||
toIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
List<Future<Void>> responses = new ArrayList<>();
|
||||
posters.forEach(poster -> responses.add(taskExecutor.submit(poster)));
|
||||
List<Future<Void>> responses = ListUtils.partition(loanIds, actualBatchSize).stream().filter(subList -> !subList.isEmpty())
|
||||
.map(subList -> {
|
||||
RecalculateInterestPoster recalculateInterestPoster = applicationContext.getBean(RecalculateInterestPoster.class);
|
||||
recalculateInterestPoster.setLoanIds(subList);
|
||||
recalculateInterestPoster.setFineractContext(ThreadLocalContextUtil.getContext());
|
||||
return (Callable<Void>) recalculateInterestPoster;
|
||||
}).map(taskExecutor::submit).toList();
|
||||
checkCompletion(responses);
|
||||
}
|
||||
|
||||
private <T> List<T> safeSubList(List<T> list, int fromIndex, int toIndex) {
|
||||
int size = list.size();
|
||||
if (fromIndex >= size || toIndex <= 0 || fromIndex >= toIndex) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
fromIndex = Math.max(0, fromIndex);
|
||||
toIndex = Math.min(size, toIndex);
|
||||
|
||||
return list.subList(fromIndex, toIndex);
|
||||
}
|
||||
|
||||
private void checkCompletion(List<Future<Void>> responses) {
|
||||
try {
|
||||
for (Future<Void> f : responses) {
|
||||
|
||||
+3
-2
@@ -2118,7 +2118,8 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService, Loa
|
||||
sqlBuilder.append(" left join m_client mc on mc.id = ml.client_id ");
|
||||
sqlBuilder.append(" left join m_office o on mc.office_id = o.id ");
|
||||
sqlBuilder.append(" INNER JOIN m_loan_repayment_schedule mr on mr.loan_id = ml.id ");
|
||||
sqlBuilder.append(" LEFT JOIN m_loan_disbursement_detail dd on dd.loan_id=ml.id and dd.disbursedon_date is null ");
|
||||
sqlBuilder.append(
|
||||
" LEFT JOIN m_loan_disbursement_detail dd on dd.loan_id=ml.id and dd.disbursedon_date is null and dd.is_reversed = false ");
|
||||
// For Floating rate changes
|
||||
sqlBuilder.append(
|
||||
" left join m_product_loan_floating_rates pfr on ml.product_id = pfr.loan_product_id and ml.is_floating_interest_rate = true");
|
||||
@@ -2129,7 +2130,7 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService, Loa
|
||||
sqlBuilder.append(" left join m_floating_rates bfr on bfr.is_base_lending_rate = true");
|
||||
sqlBuilder.append(" left join m_floating_rates_periods bfrp on bfr.id = bfrp.floating_rates_id and bfrp.created_date >= ?");
|
||||
sqlBuilder.append(" WHERE ml.loan_status_id = ? ");
|
||||
sqlBuilder.append(" and ml.is_npa = false and ml.is_charged_off = false and dd.is_reversed = false ");
|
||||
sqlBuilder.append(" and ml.is_npa = false and ml.is_charged_off = false ");
|
||||
sqlBuilder.append(" and ((");
|
||||
sqlBuilder.append("ml.interest_recalculation_enabled = true ");
|
||||
sqlBuilder.append(" and (ml.interest_recalcualated_on is null or ml.interest_recalcualated_on <> ? )");
|
||||
|
||||
+7
@@ -2854,6 +2854,12 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
|
||||
@Retry(name = "recalculateInterest", fallbackMethod = "fallbackRecalculateInterest")
|
||||
public void recalculateInterest(final long loanId) {
|
||||
Loan loan = this.loanAssembler.assembleFrom(loanId);
|
||||
recalculateInterest(loan);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public Loan recalculateInterest(Loan loan) {
|
||||
LocalDate recalculateFrom = loan.fetchInterestRecalculateFromDate();
|
||||
businessEventNotifierService.notifyPreBusinessEvent(new LoanInterestRecalculationBusinessEvent(loan));
|
||||
final List<Long> existingTransactionIds = new ArrayList<>();
|
||||
@@ -2883,6 +2889,7 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
|
||||
loanAccrualsProcessingService.processAccrualsForInterestRecalculation(loan,
|
||||
loan.repaymentScheduleDetail().isInterestRecalculationEnabled());
|
||||
businessEventNotifierService.notifyPostBusinessEvent(new LoanInterestRecalculationBusinessEvent(loan));
|
||||
return loan;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
-9
@@ -121,7 +121,6 @@ import org.apache.fineract.portfolio.loanaccount.service.LoanTransactionRelation
|
||||
import org.apache.fineract.portfolio.loanaccount.service.LoanUtilService;
|
||||
import org.apache.fineract.portfolio.loanaccount.service.LoanWritePlatformService;
|
||||
import org.apache.fineract.portfolio.loanaccount.service.LoanWritePlatformServiceJpaRepositoryImpl;
|
||||
import org.apache.fineract.portfolio.loanaccount.service.RecalculateInterestPoster;
|
||||
import org.apache.fineract.portfolio.loanaccount.service.ReplayedTransactionBusinessEventService;
|
||||
import org.apache.fineract.portfolio.loanaccount.service.ReplayedTransactionBusinessEventServiceImpl;
|
||||
import org.apache.fineract.portfolio.loanproduct.domain.LoanProductRepository;
|
||||
@@ -138,7 +137,6 @@ import org.apache.fineract.portfolio.savings.service.GSIMReadPlatformService;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||
|
||||
@@ -380,13 +378,6 @@ public class LoanAccountConfiguration {
|
||||
loanTransactionAssembler, loanAccrualsProcessingService);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Scope("prototype")
|
||||
@ConditionalOnMissingBean(RecalculateInterestPoster.class)
|
||||
public RecalculateInterestPoster recalculateInterestPoster() {
|
||||
return new RecalculateInterestPoster();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(ReplayedTransactionBusinessEventService.class)
|
||||
public ReplayedTransactionBusinessEventService replayedTransactionBusinessEventService(
|
||||
|
||||
Reference in New Issue
Block a user