Samer-Melhem-FOO opened a new pull request, #6646:
URL: https://github.com/apache/fineract/pull/6646

   ## Description
   
   JIRA: https://issues.apache.org/jira/browse/FINERACT-2946
   
   The loan reschedule preview (`GET 
/rescheduleloans/{id}?command=previewLoanReschedule`) for a request with a 
grace on principal returns one of two schedules for the same request, varying 
from call to call. One of them amortises the remaining principal over the grace 
periods too and ends in a balloon last installment, so the preview can disagree 
with what approval applies.
   
   **Root cause:** a grace on principal creates `GRACE_ON_PRINCIPAL` and its 
child `EXTEND_REPAYMENT_PERIOD` with the same applicable date. 
`LoanTermVariationsData#compareTo` returned 0 for that tie, and the preview 
reads the variations from `LoanRescheduleRequest`'s `HashSet` (its element type 
has no `hashCode`), so the stable sort kept an effectively random order. In 
`AbstractCumulativeLoanScheduleGenerator#applyExceptionLoanTermVariations`, 
`EXTEND_REPAYMENT_PERIOD` recalculates the fixed installment from the 
principal-grace periods registered so far; processed before its grace, it 
amortises over too many periods. Approval orders the variations by id 
(`@OrderBy("termApplicableFrom, id")`), so it was always correct.
   
   **Fix:** same-date ties in `compareTo` now order due-date and 
insert-installment variations first (as before), then grace on 
principal/interest, then extend repayment period, then everything else. That is 
the order approval already applies, so preview and approval agree.
   
   **Tests:** new `LoanTermVariationsDataTest` (grace before its extension in 
either input order, both graces before the extensions, other variations after 
the extension, due-date still first, earlier date still wins). On `develop` 4 
of the 5 fail; with the fix all pass.
   
   **Live check** (local PostgreSQL, 12,000 at 1% per month over 12 months, 
declining balance, equal installments, first installment paid, reschedule with 
`graceOnPrincipal: 2`, 20 previews):
   - `develop`: 11 previews at 1,066.18 per installment, 9 at 897.21 with a 
2,665.08 last installment.
   - with the fix: all 20 at 1,066.18 (last installment 1,066.24).
   
   No API change.
   
   ## Checklist
   
   - [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).
   - [ ] 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 (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).
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
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