Skip to content

Commit

Permalink
FINERACT-2081: Code quality, eliminate duplicated methods, use utility
Browse files Browse the repository at this point in the history
  • Loading branch information
Marta Jankovics committed Oct 18, 2024
1 parent c8e6adb commit 51f0fca
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 321 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -357,17 +357,29 @@ public static String format(LocalDateTime dateTime, String format, Locale locale
*
* @param targetDate
* the date to be checked
* @param startDate
* @param fromDate
* the start date of the range
* @param endDate
* @param toDate
* the end date of the range
* @return true if targetDate is within range or equal to start/end dates, otherwise false
*/
public static boolean isDateWithinRange(LocalDate targetDate, LocalDate startDate, LocalDate endDate) {
if (targetDate == null || startDate == null || endDate == null) {
public static boolean isDateWithinRange(LocalDate targetDate, LocalDate fromDate, LocalDate toDate) {
if (targetDate == null || fromDate == null || toDate == null) {
throw new IllegalArgumentException("Dates must not be null");
}
return !targetDate.isBefore(startDate) && !targetDate.isAfter(endDate);
return isDateInRangeInclusive(targetDate, fromDate, toDate);
}

public static boolean isDateInRangeInclusive(LocalDate targetDate, LocalDate fromDate, LocalDate toDate) {
return fromDate != null && !DateUtils.isBefore(targetDate, fromDate) && !DateUtils.isAfter(targetDate, toDate);
}

public static boolean isDateInRangeExclusive(LocalDate targetDate, LocalDate fromDate, LocalDate toDate) {
return fromDate != null && DateUtils.isAfter(targetDate, fromDate) && DateUtils.isBefore(targetDate, toDate);
}

public static boolean isDateInRangeFromExclusiveToInclusive(LocalDate targetDate, LocalDate fromDate, LocalDate toDate) {
return fromDate != null && DateUtils.isAfter(targetDate, fromDate) && !DateUtils.isAfter(targetDate, toDate);
}

@NotNull
Expand All @@ -393,14 +405,4 @@ private static DateTimeFormatter getDateTimeFormatter(String format, Locale loca
}
return formatter;
}

public static boolean occursOnDayFromExclusiveAndUpToAndIncluding(final LocalDate fromNotInclusive, final LocalDate upToAndInclusive,
final LocalDate target) {
return DateUtils.isAfter(target, fromNotInclusive) && !DateUtils.isAfter(target, upToAndInclusive);
}

public static boolean occursOnDayFromAndUpToAndIncluding(final LocalDate fromAndInclusive, final LocalDate upToAndInclusive,
final LocalDate target) {
return target != null && !DateUtils.isBefore(target, fromAndInclusive) && !DateUtils.isAfter(target, upToAndInclusive);
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -616,14 +616,8 @@ public boolean hasLoanIdentifiedBy(final Long loanId) {
return this.loan.hasIdentifyOf(loanId);
}

public boolean isDueForCollectionFromAndUpToAndIncluding(final LocalDate fromNotInclusive, final LocalDate upToAndInclusive) {
final LocalDate dueDate = getDueLocalDate();
return DateUtils.occursOnDayFromExclusiveAndUpToAndIncluding(fromNotInclusive, upToAndInclusive, dueDate);
}

public boolean isDueForCollectionFromIncludingAndUpToAndIncluding(final LocalDate fromAndInclusive, final LocalDate upToAndInclusive) {
final LocalDate dueDate = getDueLocalDate();
return DateUtils.occursOnDayFromAndUpToAndIncluding(fromAndInclusive, upToAndInclusive, dueDate);
public boolean isDueInPeriod(final LocalDate fromDate, final LocalDate toDate, boolean isFirstPeriod) {
return LoanRepaymentScheduleProcessingWrapper.isInPeriod(getDueLocalDate(), fromDate, toDate, isFirstPeriod);
}

public boolean isFeeCharge() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.time.LocalDate;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import org.apache.fineract.infrastructure.core.service.DateUtils;
Expand Down Expand Up @@ -87,7 +88,7 @@ private Money cumulativeFeeChargesDueWithin(final LocalDate periodStart, final L
Money cumulative = Money.zero(monetaryCurrency);
for (final LoanCharge loanCharge : loanCharges) {
if (loanCharge.isFeeCharge() && !loanCharge.isDueAtDisbursement()) {
boolean isDue = loanChargeIsDue(periodStart, periodEnd, isFirstPeriod, loanCharge);
boolean isDue = loanCharge.isDueInPeriod(periodStart, periodEnd, isFirstPeriod);
if (loanCharge.isInstalmentFee() && isInstallmentChargeApplicable) {
cumulative = cumulative.plus(getInstallmentFee(monetaryCurrency, period, loanCharge));
} else if (loanCharge.isOverdueInstallmentCharge() && isDue && loanCharge.getChargeCalculation().isPercentageBased()) {
Expand Down Expand Up @@ -133,7 +134,7 @@ private Money cumulativeChargesWaivedWithin(final LocalDate periodStart, final L

for (final LoanCharge loanCharge : loanCharges) {
if (predicate.test(loanCharge)) {
boolean isDue = loanChargeIsDue(periodStart, periodEnd, isFirstPeriod, loanCharge);
boolean isDue = loanCharge.isDueInPeriod(periodStart, periodEnd, isFirstPeriod);
if (loanCharge.isInstalmentFee() && isInstallmentChargeApplicable) {
LoanInstallmentCharge loanChargePerInstallment = loanCharge.getInstallmentLoanCharge(periodEnd);
if (loanChargePerInstallment != null) {
Expand All @@ -156,7 +157,7 @@ private Money cumulativeChargesWrittenOffWithin(final LocalDate periodStart, fin

for (final LoanCharge loanCharge : loanCharges) {
if (chargePredicate.test(loanCharge)) {
boolean isDue = loanChargeIsDue(periodStart, periodEnd, isFirstPeriod, loanCharge);
boolean isDue = loanCharge.isDueInPeriod(periodStart, periodEnd, isFirstPeriod);
if (loanCharge.isInstalmentFee() && isInstallmentChargeApplicable) {
LoanInstallmentCharge loanChargePerInstallment = loanCharge.getInstallmentLoanCharge(periodEnd);
if (loanChargePerInstallment != null) {
Expand All @@ -175,11 +176,6 @@ private Predicate<LoanCharge> feeCharge() {
return loanCharge -> loanCharge.isFeeCharge() && !loanCharge.isDueAtDisbursement();
}

private boolean loanChargeIsDue(LocalDate periodStart, LocalDate periodEnd, boolean isFirstPeriod, LoanCharge loanCharge) {
return isFirstPeriod ? loanCharge.isDueForCollectionFromIncludingAndUpToAndIncluding(periodStart, periodEnd)
: loanCharge.isDueForCollectionFromAndUpToAndIncluding(periodStart, periodEnd);
}

private Money cumulativePenaltyChargesDueWithin(final LocalDate periodStart, final LocalDate periodEnd,
final Set<LoanCharge> loanCharges, final MonetaryCurrency currency, LoanRepaymentScheduleInstallment period,
final Money totalPrincipal, final Money totalInterest, boolean isInstallmentChargeApplicable, boolean isFirstPeriod) {
Expand All @@ -188,7 +184,7 @@ private Money cumulativePenaltyChargesDueWithin(final LocalDate periodStart, fin

for (final LoanCharge loanCharge : loanCharges) {
if (loanCharge.isPenaltyCharge()) {
boolean isDue = loanChargeIsDue(periodStart, periodEnd, isFirstPeriod, loanCharge);
boolean isDue = loanCharge.isDueInPeriod(periodStart, periodEnd, isFirstPeriod);
if (loanCharge.isInstalmentFee() && isInstallmentChargeApplicable) {
cumulative = cumulative.plus(getInstallmentFee(currency, period, loanCharge));
} else if (loanCharge.isOverdueInstallmentCharge() && isDue && loanCharge.getChargeCalculation().isPercentageBased()) {
Expand Down Expand Up @@ -242,17 +238,24 @@ public static int fetchFirstNormalInstallmentNumber(List<LoanRepaymentScheduleIn
.filter(repaymentPeriod -> !repaymentPeriod.isDownPayment()).findFirst().orElseThrow().getInstallmentNumber();
}

public static boolean isInPeriod(LocalDate transactionDate, LoanRepaymentScheduleInstallment targetInstallment,
public static boolean isInPeriod(LocalDate targetDate, LoanRepaymentScheduleInstallment targetInstallment,
List<LoanRepaymentScheduleInstallment> installments) {
int firstPeriod = fetchFirstNormalInstallmentNumber(installments);
return isInPeriod(transactionDate, targetInstallment, targetInstallment.getInstallmentNumber().equals(firstPeriod));
return isInPeriod(targetDate, targetInstallment, targetInstallment.getInstallmentNumber().equals(firstPeriod));
}

public static boolean isInPeriod(LocalDate targetDate, LoanRepaymentScheduleInstallment installment, boolean isFirstPeriod) {
return isInPeriod(targetDate, installment.getFromDate(), installment.getDueDate(), isFirstPeriod);
}

private static boolean isInPeriod(LocalDate transactionDate, LoanRepaymentScheduleInstallment targetInstallment,
boolean isFirstPeriod) {
LocalDate fromDate = targetInstallment.getFromDate();
LocalDate dueDate = targetInstallment.getDueDate();
return isFirstPeriod ? DateUtils.occursOnDayFromAndUpToAndIncluding(fromDate, dueDate, transactionDate)
: DateUtils.occursOnDayFromExclusiveAndUpToAndIncluding(fromDate, dueDate, transactionDate);
public static boolean isInPeriod(LocalDate targetDate, LocalDate fromDate, LocalDate toDate, boolean isFirstPeriod) {
return isFirstPeriod ? DateUtils.isDateInRangeInclusive(targetDate, fromDate, toDate)
: DateUtils.isDateInRangeFromExclusiveToInclusive(targetDate, fromDate, toDate);
}

public static Optional<LoanRepaymentScheduleInstallment> findInPeriod(LocalDate targetDate,
List<LoanRepaymentScheduleInstallment> installments) {
int firstNumber = fetchFirstNormalInstallmentNumber(installments);
return installments.stream().filter(e -> isInPeriod(targetDate, e, e.getInstallmentNumber().equals(firstNumber))).findFirst();
}
}
Loading

0 comments on commit 51f0fca

Please sign in to comment.