forked from apache/fineract
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FINERACT-2104: Accrual Activity Posting Job
- Loading branch information
1 parent
38d9944
commit 905a733
Showing
11 changed files
with
430 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
.../java/org/apache/fineract/portfolio/loanaccount/domain/LoanAccrualActivityRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* 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.domain; | ||
|
||
import java.time.LocalDate; | ||
import java.util.Set; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.Repository; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
public interface LoanAccrualActivityRepository extends Repository<Loan, Long> { | ||
|
||
@Query("select loan.id from Loan loan left join LoanTransaction lt on lt.loan = loan and lt.typeOf = 32 and lt.reversed = false and lt.dateOf = :currentDate inner join LoanRepaymentScheduleInstallment rs on rs.loan = loan and rs.isDownPayment = false and rs.additional = false and rs.dueDate = :currentDate where loan.loanRepaymentScheduleDetail.enableAccrualActivityPosting = true and loan.loanStatus = 300 and lt.id is null ") | ||
Set<Long> fetchLoanIdsForAccrualActivityPosting(@Param("currentDate") LocalDate currentDate); | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...g/apache/fineract/portfolio/loanaccount/service/LoanAccrualActivityProcessingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* 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.service; | ||
|
||
import java.time.LocalDate; | ||
import org.apache.fineract.portfolio.loanaccount.domain.Loan; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
public interface LoanAccrualActivityProcessingService { | ||
|
||
@Transactional | ||
void makeAccrualActivityTransaction(Long loanId, LocalDate currentDate); | ||
|
||
Loan makeAccrualActivityTransaction(Loan loan, LocalDate currentDate); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...eract/portfolio/loanaccount/jobs/accrualactivityposting/AccrualActivityPostingConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* 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.accrualactivityposting; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.apache.fineract.infrastructure.jobs.service.JobName; | ||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.Step; | ||
import org.springframework.batch.core.job.builder.JobBuilder; | ||
import org.springframework.batch.core.launch.support.RunIdIncrementer; | ||
import org.springframework.batch.core.repository.JobRepository; | ||
import org.springframework.batch.core.step.builder.StepBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class AccrualActivityPostingConfig { | ||
|
||
private final JobRepository jobRepository; | ||
private final PlatformTransactionManager transactionManager; | ||
|
||
@Bean | ||
protected Step accrualActivityPostingStep(AccrualActivityPostingTasklet accrualActivityPostingTasklet) { | ||
return new StepBuilder(JobName.ACCRUAL_ACTIVITY_POSTING.name(), jobRepository) | ||
.tasklet(accrualActivityPostingTasklet, transactionManager).build(); | ||
} | ||
|
||
@Bean | ||
public Job accrualActivityPostingJob(AccrualActivityPostingTasklet accrualActivityPosting) { | ||
return new JobBuilder(JobName.ACCRUAL_ACTIVITY_POSTING.name(), jobRepository) | ||
.start(accrualActivityPostingStep(accrualActivityPosting)).incrementer(new RunIdIncrementer()).build(); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...ract/portfolio/loanaccount/jobs/accrualactivityposting/AccrualActivityPostingTasklet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.portfolio.loanaccount.jobs.accrualactivityposting; | ||
|
||
import java.time.LocalDate; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.fineract.infrastructure.core.service.DateUtils; | ||
import org.apache.fineract.infrastructure.jobs.exception.JobExecutionException; | ||
import org.apache.fineract.portfolio.loanaccount.domain.LoanAccrualActivityRepository; | ||
import org.apache.fineract.portfolio.loanaccount.service.LoanAccrualActivityProcessingService; | ||
import org.springframework.batch.core.StepContribution; | ||
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.stereotype.Component; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Component | ||
public class AccrualActivityPostingTasklet implements Tasklet { | ||
|
||
private final LoanAccrualActivityProcessingService loanAccrualActivityProcessingService; | ||
private final LoanAccrualActivityRepository loanAccrualActivityRepository; | ||
|
||
@Override | ||
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { | ||
final LocalDate yesterday = DateUtils.getBusinessLocalDate().minusDays(1); | ||
List<Throwable> errors = new ArrayList<>(); | ||
Set<Long> loanAccounts = loanAccrualActivityRepository.fetchLoanIdsForAccrualActivityPosting(yesterday); | ||
for (Long accountId : loanAccounts) { | ||
try { | ||
loanAccrualActivityProcessingService.makeAccrualActivityTransaction(accountId, yesterday); | ||
} catch (Exception e) { | ||
log.error("Failed to add accrual activity transaction for loan {}", accountId, e); | ||
errors.add(e); | ||
} | ||
} | ||
if (!errors.isEmpty()) { | ||
throw new JobExecutionException(errors); | ||
} | ||
|
||
return RepeatStatus.FINISHED; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...ache/fineract/portfolio/loanaccount/service/LoanAccrualActivityProcessingServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* 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.service; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.fineract.portfolio.loanaccount.domain.Loan; | ||
import org.apache.fineract.portfolio.loanaccount.domain.LoanRepaymentScheduleInstallment; | ||
import org.apache.fineract.portfolio.loanaccount.domain.LoanRepositoryWrapper; | ||
import org.apache.fineract.portfolio.loanaccount.domain.LoanTransaction; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Propagation; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class LoanAccrualActivityProcessingServiceImpl implements LoanAccrualActivityProcessingService { | ||
|
||
private final LoanRepositoryWrapper loanRepositoryWrapper; | ||
private final LoanWritePlatformService loanWritePlatformService; | ||
|
||
@Override | ||
@Transactional(propagation = Propagation.REQUIRES_NEW) | ||
public void makeAccrualActivityTransaction(Long loanId, final LocalDate currentDate) { | ||
Loan loan = loanRepositoryWrapper.findOneWithNotFoundDetection(loanId, true); | ||
makeAccrualActivityTransaction(loan, currentDate); | ||
} | ||
|
||
@Override | ||
public Loan makeAccrualActivityTransaction(Loan loan, final LocalDate currentDate) { | ||
if (loan.getLoanProductRelatedDetail().isEnableAccrualActivityPosting()) { | ||
// check if loan has installment due on business day | ||
Optional<LoanRepaymentScheduleInstallment> first = loan.getRepaymentScheduleInstallments().stream() | ||
.filter(loanRepaymentScheduleInstallment -> loanRepaymentScheduleInstallment.getDueDate().isEqual(currentDate)) | ||
.findFirst(); | ||
if (first.isPresent()) { | ||
final LoanRepaymentScheduleInstallment installment = first.get(); | ||
// check if there is no not-replayed-accrual-activity related to business date | ||
List<LoanTransaction> loanTransactions = loan.getLoanTransactions(loanTransaction -> loanTransaction.isNotReversed() | ||
&& loanTransaction.isAccrualActivity() && loanTransaction.getTransactionDate().isEqual(currentDate)); | ||
if (loanTransactions.isEmpty()) { | ||
loan = loanWritePlatformService.makeAccrualActivityTransaction(loan, installment, currentDate); | ||
} | ||
} | ||
} | ||
return loan; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...er/src/main/resources/db/changelog/tenant/parts/0143_add_accrual_activity_posting_job.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
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. | ||
--> | ||
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.1.xsd"> | ||
<changeSet author="fineract" id="1"> | ||
<insert tableName="job"> | ||
<column name="name" value="Accrual Activity Posting"/> | ||
<column name="display_name" value="Accrual Activity Posting"/> | ||
<column name="cron_expression" value="0 0 1 * * ?"/> | ||
<column name="create_time" valueDate="${current_datetime}"/> | ||
<column name="task_priority" valueNumeric="5"/> | ||
<column name="group_name"/> | ||
<column name="previous_run_start_time"/> | ||
<column name="job_key" value="Accrual Activity Posting1 _ DEFAULT"/> | ||
<column name="initializing_errorlog"/> | ||
<column name="is_active" valueBoolean="false"/> | ||
<column name="currently_running" valueBoolean="false"/> | ||
<column name="updates_allowed" valueBoolean="true"/> | ||
<column name="scheduler_group" valueNumeric="0"/> | ||
<column name="is_misfired" valueBoolean="false"/> | ||
<column name="node_id" valueNumeric="1"/> | ||
<column name="is_mismatched_job" valueBoolean="true"/> | ||
</insert> | ||
</changeSet> | ||
</databaseChangeLog> |
Oops, something went wrong.