Skip to content

Conversation

@budaidev
Copy link
Contributor

Description

Describe the changes made and why they were made.

Ignore if these details are present on the associated Apache Fineract JIRA ticket.

Checklist

Please make sure these boxes are checked before submitting your pull request - thanks!

  • Write the commit message as per https://github.com/apache/fineract/#pull-requests
  • Acknowledge that we will not review PRs that are not passing the build ("green") - it is your responsibility to get a proposed PR to pass the build, not primarily the project's maintainers.
  • Create/update unit or integration tests for verifying the changes made.
  • Follow coding conventions at https://cwiki.apache.org/confluence/display/FINERACT/Coding+Conventions.
  • Add required Swagger annotation and update API documentation at fineract-provider/src/main/resources/static/legacy-docs/apiLive.htm with details of any API changes
  • Submission is not a "code dump". (Large changes can be made "in repository" via a branch. Ask on the developer mailing list for guidance, if required.)

FYI our guidelines for code reviews are at https://cwiki.apache.org/confluence/display/FINERACT/Code+Review+Guide.

@budaidev budaidev force-pushed the FINERACT-2412/schedule-handling-and-calculation branch 2 times, most recently from c7d8a3b to ed3c0ea Compare December 17, 2025 06:47
@budaidev budaidev marked this pull request as ready for review December 17, 2025 06:48
@budaidev budaidev force-pushed the FINERACT-2412/schedule-handling-and-calculation branch 2 times, most recently from 8503394 to b677187 Compare December 17, 2025 11:10
@adamsaghy
Copy link
Contributor

> Task :fineract-progressive-loan-embeddable-schedule-generator:test

org.apache.fineract.portfolio.loanaccount.loanschedule.domain.EmbeddableProgressiveLoanScheduleGeneratorTest

  Test testGenerate() PASSED

SUCCESS: Executed 1 tests in 2.6s

gradle/actions: Writing build results to /home/runner/work/_temp/.gradle-actions/build-results/__run_2-1765970062079.json

[Incubating] Problems report is available at: file:///home/runner/work/fineract/fineract/build/reports/problems/problems-report.html

Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/8.14.3/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.

BUILD SUCCESSFUL in 29s
49 actionable tasks: 1 executed, 48 up-to-date
Error: Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
	at org.apache.fineract.portfolio.loanaccount.loanschedule.domain.ProgressiveLoanScheduleGenerator.<clinit>(ProgressiveLoanScheduleGenerator.java:62)
	at org.apache.fineract.portfolio.loanaccount.loanschedule.domain.EmbeddableProgressiveLoanScheduleGenerator.<init>(EmbeddableProgressiveLoanScheduleGenerator.java:41)
	at Main.main(Main.java:43)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
	... 3 more
Error: Process completed with exit code 1.

@budaidev budaidev force-pushed the FINERACT-2412/schedule-handling-and-calculation branch 5 times, most recently from 4327678 to a05daaa Compare December 19, 2025 05:42
@budaidev budaidev marked this pull request as draft December 19, 2025 06:07
@budaidev budaidev marked this pull request as ready for review December 19, 2025 08:49
@budaidev budaidev force-pushed the FINERACT-2412/schedule-handling-and-calculation branch from a05daaa to 65e7bac Compare December 19, 2025 11:26
@budaidev budaidev force-pushed the FINERACT-2412/schedule-handling-and-calculation branch from 65e7bac to 5a2503b Compare December 19, 2025 12:05
@adamsaghy
Copy link
Contributor

    (see https://errorprone.info/bugpattern/NonApiType)
/home/runner/work/fineract/fineract/fineract-progressive-loan/src/main/java/org/apache/fineract/portfolio/loanproduct/calc/ProgressiveEMICalculator.java:1672: error: [UnusedMethod] Method 'calculateFnResultFromDate' is never used.
    private BigDecimal calculateFnResultFromDate(final List<RepaymentPeriod> periods, final LocalDate fromDate, final MathContext mc) {
                       ^
    (see https://errorprone.info/bugpattern/UnusedMethod)
  Did you mean to remove this line?

@budaidev budaidev force-pushed the FINERACT-2412/schedule-handling-and-calculation branch from 5a2503b to b4ad7ae Compare December 19, 2025 14:12
public BigDecimal getRateFactorPlus1FromDate(LocalDate fromDate) {
return interestPeriods.stream().filter(ip -> !ip.getFromDate().isBefore(fromDate)).map(InterestPeriod::getRateFactor)
.reduce(BigDecimal.ONE, BigDecimal::add);
}
Copy link
Contributor

@Aman-Mittal Aman-Mittal Dec 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getRateFactorPlus1FromDate
has implementation .reduce(BigDecimal.ONE, BigDecimal::add);

1 + rate1 + rate2 + rate3 ...

But the Javadoc says:

sum of (Rate Factor + 1) from interest periods
similar to.
(rate1 + 1) + (rate2 + 1) + ...

Needs some more clarification in this javadoc, it is ambiguous

* the date from which to include interest periods
* @return rate factor plus 1 for filtered interest periods
*/
public BigDecimal getRateFactorPlus1FromDate(LocalDate fromDate) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure that interestPeriods never be null?

@adamsaghy adamsaghy self-requested a review December 23, 2025 11:43
final Integer installmentAmountInMultiplesOf = loan.getLoanProductRelatedDetail().getInstallmentAmountInMultiplesOf();
ProgressiveLoanInterestScheduleModel scheduleModel = emiCalculator.generateInstallmentInterestScheduleModel(installments,
LoanConfigurationDetailsMapper.map(loan), installmentAmountInMultiplesOf, overpaymentHolder.getMoneyObject().getMc());

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole thing can be handled as part of emiCalculator.generateInstallmentInterestScheduleModel

expectedRepaymentPeriods, loanApplicationTerms.toLoanConfigurationDetails(),
loanApplicationTerms.getInstallmentAmountInMultiplesOf(), mc);

interestScheduleModel.allowFullTermForTranche(loanApplicationTerms.isAllowFullTermForTranche());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allowFullTermForTranche can be stored on the model as part of loanProductRelatedDetail.
originalNumberOfRepayments we don t need this. Its already there on the loanProductRelatedDetail

emiCalculator.addRepaymentPeriods(interestScheduleModel, extensionResult.disbursementDate,
extensionResult.additionalPeriods, List.of());
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think we need this at all. When a new disbursement added to the loan, we can handle the additional periods to be added.

final List<LoanScheduleModelPeriod> periods, final BigDecimal chargesDueAtTimeOfDisbursement,
final boolean includeDisbursementsAfterMaturityDate, final MathContext mc) {

// Check if any disbursement has actually occurred (only relevant for Full Term Tranche)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this?

Comment on lines +532 to +611

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);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

private LocalDate lastOverdueBalanceChange;

@Setter
private boolean allowFullTermForTranche = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be part of loanProductRelatedDetail

private boolean allowFullTermForTranche = false;

@Setter
private int originalNumberOfRepayments;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This already available on the loanProductRelatedDetail

* the date from which to include interest periods
* @return rate factor plus 1 for filtered interest periods
*/
public BigDecimal getRateFactorPlus1FromDate(LocalDate fromDate) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We dont need this. We are already supporting mid period disbursements.

}
}

private void addFullTermTrancheDisbursement(final ProgressiveLoanInterestScheduleModel scheduleModel,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think we need this. When we are disbursing, we are simply checking the "on the fly" period should use how many periods. If the allowFullTermForTranche is true, we should just simply provide to the on-the-fly" model the number of installments from the loan product related details.

* Term Tranche calculations where a disbursement occurs mid-period, this ensures only interest periods from the
* disbursement date are included.
*/
private BigDecimal calculateRateFactorPlus1NFromDate(final List<RepaymentPeriod> periods, final LocalDate fromDate,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think we need this either. The "addDisbursement" action should handle all.

.ifPresent((repaymentPeriod) -> calculateEMIValueAndRateFactors(
getEffectiveRepaymentDueDate(scheduleModel, repaymentPeriod, operation.getSubmittedOnDate()), scheduleModel,
operation));
if (scheduleModel.allowFullTermForTranche() && scheduleModel.originalNumberOfRepayments() > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We dont need a separate logic for this.

Copy link
Contributor

@adamsaghy adamsaghy Jan 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add

//TODO: If the allow full tranche is TRUE, we should generate and the new repayment periods to the `relatedRepaymentPeriods` 

to the org.apache.fineract.portfolio.loanproduct.calc.ProgressiveEMICalculator#calculateEMIValueAndRateFactorsForDecliningBalanceInterestMethod

// Process disbursement data
DisbursementData data = (DisbursementData) dataItem.getData();
periodData = createLoanSchedulePeriodData(data, disbursementChargeAmount, waivedChargeAmount);
// For FTT: only add disbursed tranches to cumulative balance
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need this?

@budaidev budaidev closed this Jan 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants