Skip to content

Commit

Permalink
FINERACT-2107: Interest Refund - Multiple Partial Repayment
Browse files Browse the repository at this point in the history
  • Loading branch information
somasorosdpc committed Oct 31, 2024
1 parent 001d586 commit c6e5451
Show file tree
Hide file tree
Showing 2 changed files with 316 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@
import java.util.concurrent.atomic.AtomicReference;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.fineract.portfolio.loanaccount.domain.ChangedTransactionDetail;
import org.apache.fineract.portfolio.loanaccount.domain.Loan;
import org.apache.fineract.portfolio.loanaccount.domain.LoanRepaymentScheduleInstallment;
import org.apache.fineract.portfolio.loanaccount.domain.LoanTransaction;
import org.apache.fineract.portfolio.loanaccount.domain.LoanTransactionType;
import org.apache.fineract.portfolio.loanaccount.domain.transactionprocessor.impl.AdvancedPaymentScheduleTransactionProcessor;
import org.apache.fineract.portfolio.loanaccount.loanschedule.data.ProgressiveLoanInterestScheduleModel;
import org.apache.fineract.portfolio.loanaccount.starter.AdvancedPaymentScheduleTransactionProcessorCondition;
import org.apache.fineract.portfolio.loanproduct.calc.EMICalculator;
import org.apache.fineract.portfolio.loanproduct.domain.LoanSupportedInterestRefundTypes;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
Expand Down Expand Up @@ -78,15 +82,28 @@ private BigDecimal totalInterest(final Loan loan, BigDecimal refundAmount, Local
BigDecimal payableInterest = BigDecimal.ZERO;
if (loan.getLoanTransactions().stream().anyMatch(LoanTransaction::isDisbursement)) {
List<LoanTransaction> transactionsToReprocess = new ArrayList<>();
loan.getLoanTransactions().stream().filter(lt -> !lt.isReversed()) //
.filter(lt -> !lt.isAccrual() && !lt.isAccrualActivity()) //
List<LoanTransactionType> interestRefundTypes = loan.getLoanProductRelatedDetail().getSupportedInterestRefundTypes().stream()
.map(LoanSupportedInterestRefundTypes::getTransactionType).toList();
// add already interest refunded amounts to refund amount
// it is necessary to avoid multi disbursed refund
loan.getLoanTransactions().stream() //
.filter(lt -> !lt.isReversed()) //
.filter(lt -> interestRefundTypes.contains(lt.getTypeOf())) //
.forEach(t -> refundFinal.set(refundFinal.get().add(t.getAmount()))); //
loan.getLoanTransactions().stream() //
.filter(lt -> !lt.isReversed()) //
.filter(lt -> !lt.isAccrual() && !lt.isAccrualActivity() && !lt.isInterestRefund()) //
.filter(loanTransaction -> !interestRefundTypes.contains(loanTransaction.getTypeOf())) //
.forEach(lt -> simulateRepaymentForDisbursements(lt, refundFinal, transactionsToReprocess)); //

List<LoanRepaymentScheduleInstallment> installmentsToReprocess = new ArrayList<>(
loan.getRepaymentScheduleInstallments().stream().filter(i -> !i.isReAged() && !i.isAdditional()).toList());

ProgressiveLoanInterestScheduleModel modelAfter = processor.reprocessProgressiveLoanTransactions(loan.getDisbursementDate(),
transactionsToReprocess, loan.getCurrency(), installmentsToReprocess, loan.getActiveCharges()).getRight();
Pair<ChangedTransactionDetail, ProgressiveLoanInterestScheduleModel> reprocessResult = processor
.reprocessProgressiveLoanTransactions(loan.getDisbursementDate(), transactionsToReprocess, loan.getCurrency(),
installmentsToReprocess, loan.getActiveCharges());
loan.getLoanTransactions().addAll(reprocessResult.getLeft().getCurrentTransactionToOldId().keySet());
ProgressiveLoanInterestScheduleModel modelAfter = reprocessResult.getRight();

payableInterest = installmentsToReprocess.stream() //
.map(installment -> emiCalculator //
Expand Down
Loading

0 comments on commit c6e5451

Please sign in to comment.