-
Notifications
You must be signed in to change notification settings - Fork 2.5k
FINERACT-2455: Working Capital - Transaction Type - Discount Fee Amortization #5850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
budaidev
wants to merge
1
commit into
apache:develop
Choose a base branch
from
openMF:FINERACT-2455/WC-discount-fee-amortization
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
61 changes: 61 additions & 0 deletions
61
...che/fineract/cob/workingcapitalloan/businessstep/DiscountFeeAmortizationBusinessStep.java
This file contains hidden or 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,61 @@ | ||
| /** | ||
| * 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.workingcapitalloan.businessstep; | ||
|
|
||
| import java.time.LocalDate; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.apache.fineract.infrastructure.core.service.DateUtils; | ||
| import org.apache.fineract.infrastructure.core.service.MathUtil; | ||
| import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoan; | ||
| import org.apache.fineract.portfolio.workingcapitalloan.service.WorkingCapitalLoanDiscountFeeAmortizationService; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Slf4j | ||
| @RequiredArgsConstructor | ||
| @Component | ||
| public class DiscountFeeAmortizationBusinessStep extends WorkingCapitalLoanCOBBusinessStep { | ||
|
|
||
| private final WorkingCapitalLoanDiscountFeeAmortizationService discountFeeAmortizationService; | ||
|
|
||
| @Override | ||
| public WorkingCapitalLoan execute(final WorkingCapitalLoan input) { | ||
| if (input.getLoanProductRelatedDetails() == null | ||
| || !MathUtil.isGreaterThanZero(input.getLoanProductRelatedDetails().getDiscount())) { | ||
| log.debug("Skipping discount fee amortization for WC loan {} - no discount fee", input.getId()); | ||
| return input; | ||
| } | ||
|
|
||
| final LocalDate businessDate = DateUtils.getBusinessLocalDate(); | ||
|
|
||
| discountFeeAmortizationService.processDiscountFeeAmortization(input, businessDate); | ||
|
|
||
| return input; | ||
| } | ||
|
|
||
| @Override | ||
| public String getEnumStyledName() { | ||
| return "WC_DISCOUNT_FEE_AMORTIZATION"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getHumanReadableName() { | ||
| return "WC Discount Fee Amortization"; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
27 changes: 27 additions & 0 deletions
27
...ortfolio/workingcapitalloan/service/WorkingCapitalLoanDiscountFeeAmortizationService.java
This file contains hidden or 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,27 @@ | ||
| /** | ||
| * 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.workingcapitalloan.service; | ||
|
|
||
| import java.time.LocalDate; | ||
| import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoan; | ||
|
|
||
| public interface WorkingCapitalLoanDiscountFeeAmortizationService { | ||
|
|
||
| void processDiscountFeeAmortization(WorkingCapitalLoan loan, LocalDate transactionDate); | ||
| } |
83 changes: 83 additions & 0 deletions
83
...olio/workingcapitalloan/service/WorkingCapitalLoanDiscountFeeAmortizationServiceImpl.java
This file contains hidden or 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,83 @@ | ||
| /** | ||
| * 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.workingcapitalloan.service; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.time.LocalDate; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.apache.fineract.infrastructure.core.service.ExternalIdFactory; | ||
| import org.apache.fineract.infrastructure.core.service.MathUtil; | ||
| import org.apache.fineract.portfolio.loanaccount.domain.LoanTransactionType; | ||
| import org.apache.fineract.portfolio.workingcapitalloan.accounting.WorkingCapitalLoanAccountingProcessor; | ||
| import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoan; | ||
| import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoanBalance; | ||
| import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoanTransaction; | ||
| import org.apache.fineract.portfolio.workingcapitalloan.repository.WorkingCapitalLoanTransactionRepository; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| public class WorkingCapitalLoanDiscountFeeAmortizationServiceImpl implements WorkingCapitalLoanDiscountFeeAmortizationService { | ||
|
|
||
| private final WorkingCapitalLoanTransactionRepository transactionRepository; | ||
| private final WorkingCapitalLoanAccountingProcessor accountingProcessor; | ||
| private final ExternalIdFactory externalIdFactory; | ||
|
|
||
| @Override | ||
| @Transactional | ||
| public void processDiscountFeeAmortization(final WorkingCapitalLoan loan, final LocalDate transactionDate) { | ||
| final WorkingCapitalLoanBalance balance = loan.getBalance(); | ||
| if (balance == null) { | ||
| log.debug("Skipping discount fee amortization for WC loan [{}] - no balance", loan.getId()); | ||
| return; | ||
| } | ||
|
|
||
| final BigDecimal currentRealizedIncome = MathUtil.nullToZero(balance.getRealizedIncome()); | ||
| if (MathUtil.isZero(currentRealizedIncome)) { | ||
| log.debug("Skipping discount fee amortization for WC loan [{}] - no realized income", loan.getId()); | ||
| return; | ||
| } | ||
|
|
||
| final BigDecimal alreadyAmortized = calculateAlreadyAmortizedAmount(loan); | ||
| final BigDecimal amortizationAmount = currentRealizedIncome.subtract(alreadyAmortized); | ||
|
|
||
| if (!MathUtil.isGreaterThanZero(amortizationAmount)) { | ||
| log.debug("Skipping discount fee amortization for WC loan [{}] - no new amount to amortize", loan.getId()); | ||
| return; | ||
| } | ||
|
|
||
| final WorkingCapitalLoanTransaction amortizationTxn = WorkingCapitalLoanTransaction.discountFeeAmortization(loan, | ||
| amortizationAmount, transactionDate, externalIdFactory.create()); | ||
| transactionRepository.saveAndFlush(amortizationTxn); | ||
| loan.getTransactions().add(amortizationTxn); | ||
|
|
||
| accountingProcessor.postJournalEntriesForDiscountFeeAmortization(loan, amortizationTxn, false); | ||
|
|
||
| log.debug("Posted discount fee amortization of {} for WC loan [{}]", amortizationAmount, loan.getId()); | ||
| } | ||
|
|
||
| private BigDecimal calculateAlreadyAmortizedAmount(final WorkingCapitalLoan loan) { | ||
| return loan.getTransactions().stream() | ||
| .filter(txn -> txn.getTypeOf() == LoanTransactionType.DISCOUNT_FEE_AMORTIZATION && !txn.isReversed()) | ||
| .map(WorkingCapitalLoanTransaction::getTransactionAmount).reduce(BigDecimal.ZERO, BigDecimal::add); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
39 changes: 39 additions & 0 deletions
39
...g/tenant/module/workingcapitalloan/parts/0037_discount_fee_amortization_business_step.xml
This file contains hidden or 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,39 @@ | ||
| <?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.3.xsd"> | ||
|
|
||
| <changeSet author="fineract" id="wcl-0037-1"> | ||
| <insert tableName="m_batch_business_steps"> | ||
| <column name="job_name" value="WORKING_CAPITAL_LOAN_CLOSE_OF_BUSINESS"/> | ||
| <column name="step_name" value="WC_DISCOUNT_FEE_AMORTIZATION"/> | ||
| <column name="step_order" value="5"/> | ||
| </insert> | ||
| <rollback> | ||
| <delete tableName="m_batch_business_steps"> | ||
| <where>step_name='WC_DISCOUNT_FEE_AMORTIZATION'</where> | ||
| </delete> | ||
| </rollback> | ||
| </changeSet> | ||
|
|
||
| </databaseChangeLog> |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use the amortization schedule to calculate this:
That balance should be compared with (total amount of discount fee amortization - total amount of discount fee amortization adjustment):
Once we have these, we should update the balance table with the current balances: