ImalkaPerera opened a new pull request, #6664: URL: https://github.com/apache/fineract/pull/6664
## Description JIRA: https://issues.apache.org/jira/browse/FINERACT-2954 ### Problem For flat-rate loans, `LoanApplicationTerms.calculateTotalPrincipalPerPeriodWithoutGrace` spreads the difference between the computed principal and a `PRINCIPAL_AMOUNT` term variation's fixed principal over the remaining installments by dividing by `actualNumberOfRepayments - periodNumber`. When the variation applies to the last installment (`periodNumber == actualNumberOfRepayments`) the divisor is `0`. `Money.dividedBy(long, MathContext)` only short-circuits on `1`, so `BigDecimal.divide` throws an `ArithmeticException` and schedule generation fails. Call path: `CumulativeFlatInterestLoanScheduleGenerator` -> `calculateTotalPrincipalForPeriod` (FLAT) -> `calculateTotalPrincipalPerPeriodWithoutGrace`. ### Fix Only spread the adjustment when `remainingPeriods > 0`. With no installments left to absorb a difference, the adjustment is skipped and the fixed principal is still applied to the installment. Behaviour for all other periods is unchanged. ### Tests New `LoanApplicationTermsTest` (fineract-loan): - `flatLoan_principalVariationOnLastInstallment_doesNotThrow` fails before the fix with `ArithmeticException`, passes after. - `flatLoan_principalVariationOnMiddleInstallment_spreadsAdjustment` guards the existing spreading behaviour. Verified locally: `spotlessJavaCheck`, `compileJava`/`compileTestJava` (no warnings) and the new tests pass. ## Checklist Please make sure these boxes are checked before submitting your pull request - thanks! - [x] Write the commit message as per [our guidelines](https://github.com/apache/fineract/blob/develop/CONTRIBUTING.md#pull-requests) - [x] 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. - [x] Create/update [unit or integration tests](https://fineract.apache.org/docs/current/#_testing) for verifying the changes made. - [x] Follow our [coding conventions](https://cwiki.apache.org/confluence/display/FINERACT/Coding+Conventions). - [x] 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 (N/A, no API change) - [x] [This PR must not be a "code dump"](https://cwiki.apache.org/confluence/display/FINERACT/Pull+Request+Size+Limit). Large changes can be made in a branch, with assistance. Ask for help on the [developer mailing list](https://fineract.apache.org/#contribute). - [x] If merging this PR resolves a JIRA issue, I will mark that issue as resolved and set "Fix Version/s" appropriately. - [x] I followed the [AI Policy](https://github.com/apache/fineract/blob/develop/CONTRIBUTING.md#ai-policy). Your assigned reviewer(s) will follow our [guidelines for code reviews](https://cwiki.apache.org/confluence/display/FINERACT/Code+Review+Guide). -- 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]
