Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.portfolio.savings.domain;

import java.time.LocalDate;
Expand All @@ -24,6 +6,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.apache.fineract.infrastructure.core.domain.LocalDateInterval;
import org.apache.fineract.infrastructure.core.service.DateUtils;
import org.apache.fineract.organisation.monetary.domain.MonetaryCurrency;
Expand All @@ -40,96 +23,128 @@ public final class SavingsHelper {

private final AccountTransfersReadPlatformService accountTransfersReadPlatformService;

private static final CompoundInterestHelper COMPOUND_INTEREST_HELPER = new CompoundInterestHelper();

@Autowired
public SavingsHelper(AccountTransfersReadPlatformService accountTransfersReadPlatformService) {
this.accountTransfersReadPlatformService = accountTransfersReadPlatformService;
}

private static final CompoundInterestHelper COMPOUND_INTEREST_HELPER = new CompoundInterestHelper();

// FIXED METHOD (FINERACT-1266)


public List<LocalDateInterval> determineInterestPostingPeriods(final LocalDate startInterestCalculationLocalDate,
final LocalDate interestPostingUpToDate, final SavingsPostingInterestPeriodType postingPeriodType,
final Integer financialYearBeginningMonth, List<LocalDate> postInterestAsOn) {
public List<LocalDateInterval> determineInterestPostingPeriods(
final LocalDate startInterestCalculationLocalDate,
final LocalDate interestPostingUpToDate,
final SavingsPostingInterestPeriodType postingPeriodType,
final Integer financialYearBeginningMonth,
List<LocalDate> postInterestAsOn) {

final List<LocalDateInterval> postingPeriods = new ArrayList<>();

if (startInterestCalculationLocalDate == null || interestPostingUpToDate == null) {
return postingPeriods;
}

// 🔐 Null-safe list
if (postInterestAsOn == null) {
postInterestAsOn = Collections.emptyList();
}

// 🔐 Null-safe posting period
final SavingsPostingInterestPeriodType safePostingPeriodType =
postingPeriodType != null ? postingPeriodType : SavingsPostingInterestPeriodType.INVALID;

LocalDate periodStartDate = startInterestCalculationLocalDate;
LocalDate periodEndDate = periodStartDate;
LocalDate actualPeriodStartDate = periodStartDate;

while (!DateUtils.isAfter(periodStartDate, interestPostingUpToDate) && !DateUtils.isAfter(periodEndDate, interestPostingUpToDate)) {
final LocalDate interestPostingLocalDate = determineInterestPostingPeriodEndDateFrom(periodStartDate, postingPeriodType,
interestPostingUpToDate, financialYearBeginningMonth);
while (!DateUtils.isAfter(periodStartDate, interestPostingUpToDate)
&& !DateUtils.isAfter(periodEndDate, interestPostingUpToDate)) {

final LocalDate interestPostingLocalDate =
determineInterestPostingPeriodEndDateFrom(
periodStartDate,
safePostingPeriodType,
interestPostingUpToDate,
financialYearBeginningMonth);

periodEndDate = interestPostingLocalDate.minusDays(1);

if (!postInterestAsOn.isEmpty()) {
for (LocalDate transactiondate : postInterestAsOn) {
if (DateUtils.isAfter(transactiondate, periodStartDate) && !DateUtils.isAfter(transactiondate, periodEndDate)) {
periodEndDate = transactiondate.minusDays(1);
actualPeriodStartDate = periodEndDate;
break;
}
for (LocalDate transactionDate : postInterestAsOn) {
if (DateUtils.isAfter(transactionDate, periodStartDate)
&& !DateUtils.isAfter(transactionDate, periodEndDate)) {
periodEndDate = transactionDate.minusDays(1);
actualPeriodStartDate = periodEndDate;
break;
}
}

postingPeriods.add(LocalDateInterval.create(periodStartDate, periodEndDate));

if (DateUtils.isEqual(actualPeriodStartDate, periodEndDate)) {
periodEndDate = actualPeriodStartDate.plusDays(1);
periodStartDate = actualPeriodStartDate.plusDays(1);
periodEndDate = actualPeriodStartDate.plusDays(1);
} else {
periodEndDate = interestPostingLocalDate;
periodStartDate = interestPostingLocalDate;
periodEndDate = interestPostingLocalDate;
}
}

return postingPeriods;
}

private LocalDate determineInterestPostingPeriodEndDateFrom(final LocalDate periodStartDate,
final SavingsPostingInterestPeriodType interestPostingPeriodType, final LocalDate interestPostingUpToDate,

// FIXED HELPER METHOD


private LocalDate determineInterestPostingPeriodEndDateFrom(
final LocalDate periodStartDate,
final SavingsPostingInterestPeriodType interestPostingPeriodType,
final LocalDate interestPostingUpToDate,
Integer financialYearBeginningMonth) {

// 🔐 Null-safe fallback
if (interestPostingPeriodType == null || financialYearBeginningMonth == null) {
return interestPostingUpToDate.plusDays(1);
}

LocalDate periodEndDate = interestPostingUpToDate;
final Integer monthOfYear = periodStartDate.getMonthValue();

financialYearBeginningMonth--;
if (financialYearBeginningMonth == 0) {
financialYearBeginningMonth = 12;
}

final ArrayList<LocalDate> quarterlyDates = new ArrayList<>();
quarterlyDates.add(periodStartDate.withMonth(financialYearBeginningMonth).with(TemporalAdjusters.lastDayOfMonth()));
quarterlyDates.add(periodStartDate.withMonth(financialYearBeginningMonth).plusMonths(3).withYear(periodStartDate.getYear())
.with(TemporalAdjusters.lastDayOfMonth()));
quarterlyDates.add(periodStartDate.withMonth(financialYearBeginningMonth).plusMonths(6).withYear(periodStartDate.getYear())
.with(TemporalAdjusters.lastDayOfMonth()));
quarterlyDates.add(periodStartDate.withMonth(financialYearBeginningMonth).plusMonths(9).withYear(periodStartDate.getYear())
.with(TemporalAdjusters.lastDayOfMonth()));
quarterlyDates.add(periodStartDate.withMonth(financialYearBeginningMonth).plusMonths(3)
.withYear(periodStartDate.getYear()).with(TemporalAdjusters.lastDayOfMonth()));
quarterlyDates.add(periodStartDate.withMonth(financialYearBeginningMonth).plusMonths(6)
.withYear(periodStartDate.getYear()).with(TemporalAdjusters.lastDayOfMonth()));
quarterlyDates.add(periodStartDate.withMonth(financialYearBeginningMonth).plusMonths(9)
.withYear(periodStartDate.getYear()).with(TemporalAdjusters.lastDayOfMonth()));
Collections.sort(quarterlyDates);

final ArrayList<LocalDate> biannualDates = new ArrayList<>();
biannualDates.add(periodStartDate.withMonth(financialYearBeginningMonth).with(TemporalAdjusters.lastDayOfMonth()));
biannualDates.add(periodStartDate.withMonth(financialYearBeginningMonth).plusMonths(6).withYear(periodStartDate.getYear())
.with(TemporalAdjusters.lastDayOfMonth()));
biannualDates.add(periodStartDate.withMonth(financialYearBeginningMonth).plusMonths(6)
.withYear(periodStartDate.getYear()).with(TemporalAdjusters.lastDayOfMonth()));
Collections.sort(biannualDates);

boolean isEndDateSet = false;

switch (interestPostingPeriodType) {
case INVALID:
break;
case DAILY:
// produce period end date on current day
periodEndDate = periodStartDate;
break;
break;

case MONTHLY:
// produce period end date on last day of current month
periodEndDate = periodStartDate.with(TemporalAdjusters.lastDayOfMonth());
break;
break;

case QUATERLY:
for (LocalDate quarterlyDate : quarterlyDates) {
if (DateUtils.isAfter(quarterlyDate, periodStartDate)) {
Expand All @@ -138,11 +153,11 @@ private LocalDate determineInterestPostingPeriodEndDateFrom(final LocalDate peri
break;
}
}

if (!isEndDateSet) {
periodEndDate = quarterlyDates.get(0).plusYears(1).with(TemporalAdjusters.lastDayOfMonth());
periodEndDate = quarterlyDates.get(0).plusYears(1);
}
break;
break;

case BIANNUAL:
for (LocalDate biannualDate : biannualDates) {
if (DateUtils.isAfter(biannualDate, periodStartDate)) {
Expand All @@ -151,38 +166,47 @@ private LocalDate determineInterestPostingPeriodEndDateFrom(final LocalDate peri
break;
}
}

if (!isEndDateSet) {
periodEndDate = biannualDates.get(0).plusYears(1).with(TemporalAdjusters.lastDayOfMonth());
periodEndDate = biannualDates.get(0).plusYears(1);
}
break;
break;

case ANNUAL:
if (financialYearBeginningMonth < monthOfYear) {
periodEndDate = periodStartDate.withMonth(financialYearBeginningMonth);
periodEndDate = periodEndDate.plusYears(1);
periodEndDate = periodStartDate.withMonth(financialYearBeginningMonth).plusYears(1);
} else {
periodEndDate = periodStartDate.withMonth(financialYearBeginningMonth);
}
periodEndDate = periodEndDate.with(TemporalAdjusters.lastDayOfMonth());
break;
break;

case INVALID:
default:
periodEndDate = interestPostingUpToDate;
}
// interest posting always occurs on next day after the period end date.
periodEndDate = periodEndDate.plusDays(1);
return periodEndDate;

// Interest posting happens next day
return periodEndDate.plusDays(1);
}

public Money calculateInterestForAllPostingPeriods(final MonetaryCurrency currency, final List<PostingPeriod> allPeriods,
LocalDate accountLockedUntil, Boolean immediateWithdrawalOfInterest) {
return COMPOUND_INTEREST_HELPER.calculateInterestForAllPostingPeriods(currency, allPeriods, accountLockedUntil,
immediateWithdrawalOfInterest);
// --------------------------------------------------

public Money calculateInterestForAllPostingPeriods(
final MonetaryCurrency currency,
final List<PostingPeriod> allPeriods,
LocalDate accountLockedUntil,
Boolean immediateWithdrawalOfInterest) {

return COMPOUND_INTEREST_HELPER.calculateInterestForAllPostingPeriods(
currency, allPeriods, accountLockedUntil, immediateWithdrawalOfInterest);
}

public Collection<Long> fetchPostInterestTransactionIds(Long accountId) {
return this.accountTransfersReadPlatformService.fetchPostInterestTransactionIds(accountId);
}

public Collection<Long> fetchPostInterestTransactionIds(Long accountId, LocalDate pivotDate) {
return this.accountTransfersReadPlatformService.fetchPostInterestTransactionIdsWithPivotDate(accountId, pivotDate);
return this.accountTransfersReadPlatformService
.fetchPostInterestTransactionIdsWithPivotDate(accountId, pivotDate);
}

}
Loading