This is an automated email from the ASF dual-hosted git repository.

adamsaghy pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/fineract.git


The following commit(s) were added to refs/heads/develop by this push:
     new 393f4398a FINERACT-1981: Fix 2nd disburse on repayment due date 
results wrong EMI
393f4398a is described below

commit 393f4398ac85e4eb48db51805131d2116d058885
Author: Janos Meszaros <[email protected]>
AuthorDate: Thu Oct 24 10:37:43 2024 +0200

    FINERACT-1981: Fix 2nd disburse on repayment due date results wrong EMI
---
 .../loanschedule/data/RepaymentPeriod.java         |  8 ++--
 .../loanproduct/calc/ProgressiveEMICalculator.java | 18 ++++++++-
 .../calc/ProgressiveEMICalculatorTest.java         | 47 ++++++++++++++++++++++
 3 files changed, 68 insertions(+), 5 deletions(-)

diff --git 
a/fineract-progressive-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/loanschedule/data/RepaymentPeriod.java
 
b/fineract-progressive-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/loanschedule/data/RepaymentPeriod.java
index 129433527..3a21b51b3 100644
--- 
a/fineract-progressive-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/loanschedule/data/RepaymentPeriod.java
+++ 
b/fineract-progressive-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/loanschedule/data/RepaymentPeriod.java
@@ -153,10 +153,10 @@ public class RepaymentPeriod {
     public Money getOutstandingLoanBalance() {
         if (outstandingBalanceCalculation == null) {
             outstandingBalanceCalculation = Memo.of(() -> {
-                InterestPeriod lastInstallmentPeriod = 
getInterestPeriods().get(getInterestPeriods().size() - 1);
-                Money calculatedOutStandingLoanBalance = 
lastInstallmentPeriod.getOutstandingLoanBalance() //
-                        
.plus(lastInstallmentPeriod.getBalanceCorrectionAmount(), mc) //
-                        .plus(lastInstallmentPeriod.getDisbursementAmount(), 
mc) //
+                InterestPeriod lastInterestPeriod = 
getInterestPeriods().get(getInterestPeriods().size() - 1);
+                Money calculatedOutStandingLoanBalance = 
lastInterestPeriod.getOutstandingLoanBalance() //
+                        .plus(lastInterestPeriod.getBalanceCorrectionAmount(), 
mc) //
+                        .plus(lastInterestPeriod.getDisbursementAmount(), mc) 
//
                         .minus(getDuePrincipal(), mc)//
                         .plus(getPaidPrincipal(), mc);//
                 return 
MathUtil.negativeToZero(calculatedOutStandingLoanBalance, mc);
diff --git 
a/fineract-progressive-loan/src/main/java/org/apache/fineract/portfolio/loanproduct/calc/ProgressiveEMICalculator.java
 
b/fineract-progressive-loan/src/main/java/org/apache/fineract/portfolio/loanproduct/calc/ProgressiveEMICalculator.java
index c97ef747f..29013b834 100644
--- 
a/fineract-progressive-loan/src/main/java/org/apache/fineract/portfolio/loanproduct/calc/ProgressiveEMICalculator.java
+++ 
b/fineract-progressive-loan/src/main/java/org/apache/fineract/portfolio/loanproduct/calc/ProgressiveEMICalculator.java
@@ -84,7 +84,23 @@ public final class ProgressiveEMICalculator implements 
EMICalculator {
         scheduleModel
                 
.changeOutstandingBalanceAndUpdateInterestPeriods(disbursementDueDate, 
disbursedAmount,
                         Money.zero(disbursedAmount.getCurrency(), 
scheduleModel.mc()))
-                .ifPresent((repaymentPeriod) -> 
calculateEMIValueAndRateFactors(repaymentPeriod.getDueDate(), scheduleModel));
+                .ifPresent((repaymentPeriod) -> 
calculateEMIValueAndRateFactors(
+                        getEffectiveRepaymentDueDate(scheduleModel, 
repaymentPeriod, disbursementDueDate), scheduleModel));
+    }
+
+    private LocalDate getEffectiveRepaymentDueDate(final 
ProgressiveLoanInterestScheduleModel scheduleModel,
+            final RepaymentPeriod changedRepaymentPeriod, final LocalDate 
disbursementDueDate) {
+        final boolean isRelatedToNextRepaymentPeriod = 
changedRepaymentPeriod.getDueDate().isEqual(disbursementDueDate);
+        if (isRelatedToNextRepaymentPeriod) {
+            final Optional<RepaymentPeriod> nextRepaymentPeriod = 
scheduleModel.repaymentPeriods().stream()
+                    .filter(repaymentPeriod -> 
changedRepaymentPeriod.equals(repaymentPeriod.getPrevious().orElse(null))).findFirst();
+            if (nextRepaymentPeriod.isPresent()) {
+                return nextRepaymentPeriod.get().getDueDate();
+            }
+            // Currently N+1 scenario is not supported. Disbursement on Last 
Repayment due date affects the last
+            // repayment period.
+        }
+        return changedRepaymentPeriod.getDueDate();
     }
 
     @Override
diff --git 
a/fineract-progressive-loan/src/test/java/org/apache/fineract/portfolio/loanproduct/calc/ProgressiveEMICalculatorTest.java
 
b/fineract-progressive-loan/src/test/java/org/apache/fineract/portfolio/loanproduct/calc/ProgressiveEMICalculatorTest.java
index 1137b2e68..c129a5e4f 100644
--- 
a/fineract-progressive-loan/src/test/java/org/apache/fineract/portfolio/loanproduct/calc/ProgressiveEMICalculatorTest.java
+++ 
b/fineract-progressive-loan/src/test/java/org/apache/fineract/portfolio/loanproduct/calc/ProgressiveEMICalculatorTest.java
@@ -308,6 +308,53 @@ class ProgressiveEMICalculatorTest {
         checkPeriod(interestSchedule, 5, 0, 17.13, 0.007901833333, 0.13, 
17.00, 0.0);
     }
 
+    @Test
+    public void 
testEMICalculation_multi_disbursedAmt200_2ndOnDueDate_dayInYears360_daysInMonth30_repayEvery1Month()
 {
+
+        final List<LoanScheduleModelRepaymentPeriod> expectedRepaymentPeriods 
= new ArrayList<>();
+
+        expectedRepaymentPeriods.add(repayment(1, LocalDate.of(2024, 1, 1), 
LocalDate.of(2024, 2, 1)));
+        expectedRepaymentPeriods.add(repayment(2, LocalDate.of(2024, 2, 1), 
LocalDate.of(2024, 3, 1)));
+        expectedRepaymentPeriods.add(repayment(3, LocalDate.of(2024, 3, 1), 
LocalDate.of(2024, 4, 1)));
+        expectedRepaymentPeriods.add(repayment(4, LocalDate.of(2024, 4, 1), 
LocalDate.of(2024, 5, 1)));
+        expectedRepaymentPeriods.add(repayment(5, LocalDate.of(2024, 5, 1), 
LocalDate.of(2024, 6, 1)));
+        expectedRepaymentPeriods.add(repayment(6, LocalDate.of(2024, 6, 1), 
LocalDate.of(2024, 7, 1)));
+
+        final BigDecimal interestRate = BigDecimal.valueOf(9.4822);
+        final Integer installmentAmountInMultiplesOf = null;
+
+        
Mockito.when(loanProductRelatedDetail.getAnnualNominalInterestRate()).thenReturn(interestRate);
+        
Mockito.when(loanProductRelatedDetail.getDaysInYearType()).thenReturn(DaysInYearType.DAYS_360.getValue());
+        
Mockito.when(loanProductRelatedDetail.getDaysInMonthType()).thenReturn(DaysInMonthType.DAYS_30.getValue());
+        
Mockito.when(loanProductRelatedDetail.getRepaymentPeriodFrequencyType()).thenReturn(PeriodFrequencyType.MONTHS);
+        Mockito.when(loanProductRelatedDetail.getRepayEvery()).thenReturn(1);
+        
Mockito.when(loanProductRelatedDetail.getCurrency()).thenReturn(monetaryCurrency);
+
+        final ProgressiveLoanInterestScheduleModel interestSchedule = 
emiCalculator.generateInterestScheduleModel(expectedRepaymentPeriods,
+                loanProductRelatedDetail, installmentAmountInMultiplesOf, mc);
+
+        final Money disbursedAmount = toMoney(100.0);
+        emiCalculator.addDisbursement(interestSchedule, LocalDate.of(2024, 1, 
1), disbursedAmount);
+
+        checkPeriod(interestSchedule, 0, 0, 17.13, 0.0, 0.0, 0.79, 16.34, 
83.66);
+        checkPeriod(interestSchedule, 0, 1, 17.13, 0.007901833333, 0.79, 
16.34, 83.66);
+        checkPeriod(interestSchedule, 1, 0, 17.13, 0.007901833333, 0.66, 
16.47, 67.19);
+        checkPeriod(interestSchedule, 2, 0, 17.13, 0.007901833333, 0.53, 
16.60, 50.59);
+        checkPeriod(interestSchedule, 3, 0, 17.13, 0.007901833333, 0.40, 
16.73, 33.86);
+        checkPeriod(interestSchedule, 4, 0, 17.13, 0.007901833333, 0.27, 
16.86, 17.0);
+        checkPeriod(interestSchedule, 5, 0, 17.13, 0.007901833333, 0.13, 
17.00, 0.0);
+
+        emiCalculator.addDisbursement(interestSchedule, LocalDate.of(2024, 3, 
1), disbursedAmount);
+
+        checkPeriod(interestSchedule, 0, 0, 17.13, 0.0, 0.0, 0.79, 16.34, 
83.66);
+        checkPeriod(interestSchedule, 0, 1, 17.13, 0.007901833333, 0.79, 
16.34, 83.66);
+        checkPeriod(interestSchedule, 1, 0, 17.13, 0.007901833333, 0.66, 
16.47, 167.19);
+        checkPeriod(interestSchedule, 2, 0, 42.63, 0.007901833333, 1.32, 1.32, 
41.31, 125.88);
+        checkPeriod(interestSchedule, 3, 0, 42.63, 0.007901833333, 0.99, 
41.64, 84.24);
+        checkPeriod(interestSchedule, 4, 0, 42.63, 0.007901833333, 0.67, 
41.96, 42.28);
+        checkPeriod(interestSchedule, 5, 0, 42.61, 0.007901833333, 0.33, 
42.28, 0.0);
+    }
+
     @Test
     public void 
testEMICalculation_disbursedAmt100_dayInYears360_daysInMonth30_repayEvery1Month_reschedule()
 {
 

Reply via email to