adamsaghy commented on code in PR #5219:
URL: https://github.com/apache/fineract/pull/5219#discussion_r2661188792


##########
fineract-progressive-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/loanschedule/domain/ProgressiveLoanScheduleGenerator.java:
##########
@@ -502,4 +529,84 @@ private Set<LoanCharge> 
separateTotalCompoundingPercentageCharges(final Set<Loan
         }
         return interestCharges;
     }
+
+    private ScheduleExtensionResult 
calculateAdditionalPeriodsForFullTermTranches(final List<DisbursementData> 
disbursementDataList,
+            final List<LoanScheduleModelRepaymentPeriod> existingPeriods, 
final LoanApplicationTerms loanApplicationTerms) {
+        if (disbursementDataList.size() <= 1) {
+            return new ScheduleExtensionResult(0, null);
+        }
+
+        int maxAdditionalPeriods = 0;
+        LocalDate maxDisbursementDate = null;
+        final int numberOfRepayments = 
loanApplicationTerms.getNumberOfRepayments();
+        final int currentPeriodCount = existingPeriods.size();
+
+        // For each subsequent tranche, calculate how many additional periods 
are needed
+        // Each tranche needs 'numberOfRepayments' periods starting from its 
disbursement period
+        for (int i = 1; i < disbursementDataList.size(); i++) {
+            DisbursementData disbursementData = disbursementDataList.get(i);
+            // Skip expected disbursements that haven't actually been 
disbursed yet
+            if (!disbursementData.isDisbursed()) {
+                continue;
+            }
+            LocalDate disbursementDate = disbursementData.disbursementDate();
+            int periodIndex = findPeriodIndexForDate(disbursementDate, 
existingPeriods);
+            int lastRequiredPeriodIndex = periodIndex + numberOfRepayments - 1;
+            int additionalPeriodsForThisTranche = Math.max(0, 
lastRequiredPeriodIndex - currentPeriodCount + 1);
+            if (additionalPeriodsForThisTranche > maxAdditionalPeriods) {
+                maxAdditionalPeriods = additionalPeriodsForThisTranche;
+                maxDisbursementDate = disbursementDate;
+            }
+        }
+
+        return new ScheduleExtensionResult(maxAdditionalPeriods, 
maxDisbursementDate);
+    }
+
+    private record ScheduleExtensionResult(int additionalPeriods, LocalDate 
disbursementDate) {
+    }
+
+    private int findPeriodIndexForDate(final LocalDate date, final 
List<LoanScheduleModelRepaymentPeriod> periods) {
+        for (int i = 0; i < periods.size(); i++) {
+            LoanScheduleModelRepaymentPeriod period = periods.get(i);
+            if (!date.isBefore(period.getFromDate()) && 
date.isBefore(period.getDueDate())) {
+                return i;
+            }
+        }
+        return periods.size() - 1;
+    }
+
+    private List<LoanScheduleModelRepaymentPeriod> 
generateAdditionalPeriods(final MathContext mc, final int additionalPeriods,
+            final List<LoanScheduleModelRepaymentPeriod> existingPeriods, 
final LoanApplicationTerms loanApplicationTerms,
+            final HolidayDetailDTO holidayDetailDTO) {
+        final Money zeroAmount = 
Money.zero(loanApplicationTerms.getCurrency(), mc);
+        final List<LoanScheduleModelRepaymentPeriod> extensionPeriods = new 
ArrayList<>(additionalPeriods);
+
+        LoanScheduleModelRepaymentPeriod lastPeriod = 
existingPeriods.get(existingPeriods.size() - 1);
+        LocalDate lastRepaymentDate = lastPeriod.getDueDate();
+        int startingPeriodNumber = existingPeriods.size() + 1;
+
+        for (int i = 0; i < additionalPeriods; i++) {
+            LocalDate nextRepaymentDate = 
generateNextRepaymentDate(lastRepaymentDate, loanApplicationTerms, false);
+
+            if (i == additionalPeriods - 1) {
+                nextRepaymentDate = adjustRepaymentDate(nextRepaymentDate, 
loanApplicationTerms, holidayDetailDTO).getChangedScheduleDate();
+            }
+
+            
extensionPeriods.add(LoanScheduleModelRepaymentPeriod.repayment(startingPeriodNumber
 + i, lastRepaymentDate, nextRepaymentDate,
+                    zeroAmount, zeroAmount, zeroAmount, zeroAmount, 
zeroAmount, zeroAmount, false, mc));
+            lastRepaymentDate = nextRepaymentDate;
+        }
+
+        return extensionPeriods;
+    }
+
+    private LocalDate generateNextRepaymentDate(final LocalDate 
lastRepaymentDate, final LoanApplicationTerms loanApplicationTerms,
+            final boolean isFirstRepayment) {
+        return 
scheduledDateGenerator.generateNextRepaymentDate(lastRepaymentDate, 
loanApplicationTerms, isFirstRepayment);
+    }
+
+    private AdjustedDateDetailsDTO adjustRepaymentDate(final LocalDate 
repaymentDate, final LoanApplicationTerms loanApplicationTerms,
+            final HolidayDetailDTO holidayDetailDTO) {
+        return scheduledDateGenerator.adjustRepaymentDate(repaymentDate, 
loanApplicationTerms, holidayDetailDTO);
+    }

Review Comment:
   I dont think we need any of these... its not part of the generator to handle 
it, but the EmiCalculator "addDisbursement". ProgressiveLoanScheduleGenerator 
should NOT modifiy directly the ProgressiveLoanScheduleModel. Its not its 
responsibility!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to