Abhishek6122008 commented on code in PR #6782:
URL: https://github.com/apache/fineract/pull/6782#discussion_r4418257183
##########
fineract-core/src/main/java/org/apache/fineract/portfolio/savings/domain/interest/CompoundInterestHelper.java:
##########
@@ -48,23 +48,42 @@ public Money calculateInterestForAllPostingPeriods(final
MonetaryCurrency curren
BigDecimal compoundedInterest = BigDecimal.ZERO;
BigDecimal unCompoundedInterest = BigDecimal.ZERO;
final CompoundInterestValues compoundInterestValues = new
CompoundInterestValues(compoundedInterest, unCompoundedInterest);
- for (final PostingPeriod postingPeriod : allPeriods) {
-
- final BigDecimal interestEarnedThisPeriod =
postingPeriod.calculateInterest(compoundInterestValues);
+ int index = 0;
+ while (index < allPeriods.size()) {
+ final PostingPeriod postingPeriod = allPeriods.get(index);
+ // the overdraft split builds two periods for one posting
interval, which are calculated together
+ final PostingPeriod otherPeriodOfInterval = index + 1 <
allPeriods.size()
+ && hasSameInterval(postingPeriod, allPeriods.get(index +
1)) ? allPeriods.get(index + 1) : null;
+ final List<PostingPeriod> periodsOfInterval;
+ if (otherPeriodOfInterval == null) {
+ postingPeriod.calculateInterest(compoundInterestValues);
+ periodsOfInterval = List.of(postingPeriod);
+ } else {
+ PostingPeriod.calculateInterest(postingPeriod,
otherPeriodOfInterval, compoundInterestValues);
+ periodsOfInterval = List.of(postingPeriod,
otherPeriodOfInterval);
+ }
- final Money moneyToBePostedForPeriod = Money.of(currency,
interestEarnedThisPeriod);
+ for (final PostingPeriod period : periodsOfInterval) {
+ final Money moneyToBePostedForPeriod = Money.of(currency,
period.getInterestEarnedUnrounded());
- interestEarned = interestEarned.plus(moneyToBePostedForPeriod);
- // these checks are for fixed deposit account for not include
- // interest for accounts which has post interest to linked savings
- // account and if already transfered then it includes in interest
- // calculation.
- if (!(postingPeriod.isInterestTransfered() ||
!interestTransferEnabled
- || (lockUntil != null &&
!DateUtils.isAfter(postingPeriod.dateOfPostingTransaction(), lockUntil)))) {
- compoundInterestValues.setCompoundedInterest(BigDecimal.ZERO);
+ interestEarned = interestEarned.plus(moneyToBePostedForPeriod);
+ // these checks are for fixed deposit account for not include
+ // interest for accounts which has post interest to linked
savings
+ // account and if already transfered then it includes in
interest
+ // calculation.
+ if (!(period.isInterestTransfered() || !interestTransferEnabled
+ || (lockUntil != null &&
!DateUtils.isAfter(period.dateOfPostingTransaction(), lockUntil)))) {
+
compoundInterestValues.setCompoundedInterest(BigDecimal.ZERO);
+ }
}
+ index += periodsOfInterval.size();
}
return interestEarned;
}
+
+ private static boolean hasSameInterval(final PostingPeriod postingPeriod,
final PostingPeriod other) {
+ return
postingPeriod.getPeriodInterval().startDate().equals(other.getPeriodInterval().startDate())
+ &&
postingPeriod.getPeriodInterval().endDate().equals(other.getPeriodInterval().endDate());
+ }
Review Comment:
calculateInterestUsing adds the two periods of an interval one right after
the other, so they're always next to each other. An assertion wouldn't fit,
since a period with no pair is normal (no split, or one part is empty and gets
skipped). I added a comment stating the assumption instead.
--
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]