I am planning to pick up FINERACT-3172 (Move calculations out of the Loan
entity in fineract-loan/.../portfolio/loanaccount/domain/Loan.java).
Because Loan.java is currently ~1,890 lines and is referenced across
fineract-loan, fineract-provider, fineract-progressive-loan, and
fineract-investor, migrating all
derived calculations in a single PR would be large and risky to review. I
would like to propose executing this in four focused, incremental phases,
preserving existing
behavior and adding unit tests for each extracted component (similar to
the SavingsAccountInterestCalculator pattern used in FINERACT-3174).
Guiding Principles
1. Module Placement (fineract-loan): New calculators and domain services
will live in fineract-loan (for example, alongside
LoanSummaryBalancesRepository,
LoanTransactionService, and LoanScheduleService) so both internal
fineract-loan callers and downstream modules can consume them cleanly.
2. Preserve Entity Cohesion (Avoid an Anemic Model): Loan will retain its
state, state-changing mutators, and trivial single-field/enum predicates
(such as isApproved(),
isClosed(), isForeclosure(), isProgressiveSchedule(), etc.). Extraction
will focus on multi-step aggregations, cross-collection traversals,
schedule/tranche math, and
checks that currently couple the JPA entity to
DateUtils.getBusinessLocalDate().
3. Zero Behavior Change: Every phase will preserve existing logic,
validation error codes/messages, and be backed by unit tests.
Proposed Phased Plan
• Phase 1 — Balance, Charge & Derived Amount Calculations
• Extract derived balance and amount calculations (e.g.,
deriveSumTotalOfChargesDueAtDisbursement, getTotalInterest,
getTotalWrittenOff, getDisbursedAmount,
getDisburseAmountForTemplate, getTotalPaidInRepayments,
getTotalPrincipalOutstandingUntil, getTotalInterestOutstandingOnLoan,
getPrincipalAmountForRepaymentSchedule,
retriveLastEmiAmount, getTotalOverpaidAsMoney).
• Clean up dead private code (getTotalInterestOverdueOnLoan,
currently unused).
• Phase 2 — Schedule, Installment & Disbursal Date Derivations
• Move maturity date, disbursement date, and installment count/lookup
derivations (e.g., determineExpectedMaturityDate,
getLastRepaymentPeriodDueDate,
getDisbursementDate, getExpectedDisbursedOnLocalDateForTemplate,
getActualDisbursementDate(LoanCharge),
fetchNumberOfInstallmentsAfterExceptions, getTermsCount,
getNextPossibleRepaymentDateForRescheduling, fetchUndisbursedDetail,
and installment range lookups).
• Phase 3 — Transaction, Refund & Monetary Activity Derivations
• Align transaction filtering, last-transaction queries, and
backdated/monetary activity checks with LoanTransactionService (e.g.,
isUserTransaction,
getLastUserTransaction, getLastUserTransactionDate,
getLastUserTransactionDateExcludingAmortization, getLastRepaymentDate,
getLatestTransactionDate,
possibleNextRefundDate, hasMonetaryActivityAfter,
hasChargesAffectedByBackdatedRepaymentLikeTransaction, isChargeOffOnDate,
isTerminatedEarlyOnDate).
• Phase 4 — Interest Recalculation, Disbursement Rules & Complex Status
Derivations
• Move interest recalculation checks and multi-condition
disbursement/tranche status rules (isInterestBearing,
isInterestBearingAndInterestRecalculationEnabled,
fetchInterestRecalculateFromDate,
isFeeCompoundingEnabledForInterestRecalculation, isDisbursementMissed,
isDisbursementAllowed, atLeastOnceDisbursed,
isAllTranchesNotDisbursed, isChargesAdditionAllowed,
isActualDisbursedOnDateEarlierOrLaterThanExpected).
Design Questions for Feedback
Before starting Phase 1, I would appreciate input on three design points:
1. Spring Services vs. Stateless Calculators: For methods currently
called inside Loan state mutators (such as
updateLoanScheduleDependentDerivedFields() calling
determineExpectedMaturityDate(), and adjustNetDisbursalAmount() calling
deriveSumTotalOfChargesDueAtDisbursement()), would you prefer:
• Passing the derived values in from orchestrating services, or
• Having Spring-injected calculators (@Component / @Service) used by
services while keeping Loan free of service dependencies?
2. Reuse of Existing Services: For Phase 3, should transaction
query/classification helpers be consolidated into the existing
LoanTransactionService, or placed in a
dedicated read/calculator class?
3. Caller Migration Strategy: Would you prefer each phase's PR to update
all callers directly and remove the method from Loan immediately, rather
than leaving temporary
delegating methods on Loan?
Looking forward to your thoughts and feedback on this plan!
┌── karthik-kulkarni@fedora:~$ whoami
│ Karthik Kulkarni — Senior Software Engineer @ HCLTech
├── stack: [Java, Spring Boot, Docker, MongoDb]
└── links: *github.com/kartik1502
<http://github.com/kartik1502> | linkedin.com/in/
<http://linkedin.com/in/>**kartikulkarni
<https://www.linkedin.com/in/karthikulkarni/> | KARTHIK. SYS
<https://kartik1502.github.io/portfolio/>*