FINERACT-1919: Fix monthly loan schedule due dates to maximize accord… (#3109)
* FINERACT-1919: Fix monthly loan schedule due dates to maximize according to seed date and number of days in the month
This commit is contained in:
+7
-30
@@ -22,8 +22,8 @@ import com.google.gson.JsonElement;
|
||||
import java.text.ParseException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.YearMonth;
|
||||
import java.time.ZoneId;
|
||||
import java.time.chrono.IsoChronology;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoField;
|
||||
import java.time.temporal.Temporal;
|
||||
@@ -90,36 +90,13 @@ public final class CalendarUtils {
|
||||
}
|
||||
|
||||
public static Temporal adjustDate(final Temporal date, final Temporal seedDate, final PeriodFrequencyType frequencyType) {
|
||||
Temporal adjustedVal = date;
|
||||
if (frequencyType.isMonthly() && seedDate.get(ChronoField.DAY_OF_MONTH) > 28 && date.get(ChronoField.DAY_OF_MONTH) > 28) {
|
||||
switch (date.get(ChronoField.MONTH_OF_YEAR)) {
|
||||
case 2:
|
||||
if (IsoChronology.INSTANCE.isLeapYear(date.get(ChronoField.YEAR))) {
|
||||
adjustedVal = date.with(ChronoField.DAY_OF_MONTH, 29);
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
case 6:
|
||||
case 9:
|
||||
case 11:
|
||||
if (seedDate.get(ChronoField.DAY_OF_MONTH) > 30) {
|
||||
adjustedVal = date.with(ChronoField.DAY_OF_MONTH, 30);
|
||||
} else {
|
||||
adjustedVal = date.with(ChronoField.DAY_OF_MONTH, seedDate.get(ChronoField.DAY_OF_MONTH));
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
case 3:
|
||||
case 5:
|
||||
case 7:
|
||||
case 8:
|
||||
case 10:
|
||||
case 12:
|
||||
adjustedVal = date.with(ChronoField.DAY_OF_MONTH, seedDate.get(ChronoField.DAY_OF_MONTH));
|
||||
break;
|
||||
}
|
||||
if (frequencyType.isMonthly() && seedDate.get(ChronoField.DAY_OF_MONTH) > 28 && date.get(ChronoField.DAY_OF_MONTH) >= 28) {
|
||||
int noOfDaysInCurrentMonth = YearMonth.from(date).lengthOfMonth();
|
||||
int seedDay = seedDate.get(ChronoField.DAY_OF_MONTH);
|
||||
int adjustedDay = Math.min(noOfDaysInCurrentMonth, seedDay);
|
||||
return date.with(ChronoField.DAY_OF_MONTH, adjustedDay);
|
||||
}
|
||||
return adjustedVal;
|
||||
return date;
|
||||
}
|
||||
|
||||
private static LocalDateTime getNextRecurringDate(final Recur recur, final LocalDateTime seedDate, final LocalDateTime startDate) {
|
||||
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
/**
|
||||
* 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.calendar.service;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.Month;
|
||||
import java.time.temporal.ChronoField;
|
||||
import java.time.temporal.Temporal;
|
||||
import org.apache.fineract.portfolio.common.domain.PeriodFrequencyType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CalendarUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testAdjustDateWithMonthlyFrequencyAndSeedDateBefore28() {
|
||||
// given
|
||||
Temporal date = LocalDate.of(2023, Month.APRIL, 27);
|
||||
Temporal seedDate = LocalDate.of(2023, Month.JANUARY, 27);
|
||||
PeriodFrequencyType frequencyType = PeriodFrequencyType.MONTHS;
|
||||
|
||||
// when
|
||||
Temporal adjustedDate = CalendarUtils.adjustDate(date, seedDate, frequencyType);
|
||||
|
||||
// then
|
||||
assertEquals(27, adjustedDate.get(ChronoField.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdjustDateWithMonthlyFrequencyAndSeedDateAfter28AndCurrentMonthHasLessDays() {
|
||||
// given
|
||||
Temporal date = LocalDate.of(2023, Month.FEBRUARY, 28);
|
||||
Temporal seedDate = LocalDate.of(2023, Month.JANUARY, 31);
|
||||
PeriodFrequencyType frequencyType = PeriodFrequencyType.MONTHS;
|
||||
|
||||
// when
|
||||
Temporal adjustedDate = CalendarUtils.adjustDate(date, seedDate, frequencyType);
|
||||
|
||||
// then
|
||||
assertEquals(28, adjustedDate.get(ChronoField.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdjustDateWithMonthlyFrequencyAndSeedDateAfter28AndCurrentMonthHasEqualDays() {
|
||||
// given
|
||||
Temporal date = LocalDate.of(2023, Month.MARCH, 31);
|
||||
Temporal seedDate = LocalDate.of(2023, Month.JANUARY, 31);
|
||||
PeriodFrequencyType frequencyType = PeriodFrequencyType.MONTHS;
|
||||
|
||||
// when
|
||||
Temporal adjustedDate = CalendarUtils.adjustDate(date, seedDate, frequencyType);
|
||||
|
||||
// then
|
||||
assertEquals(31, adjustedDate.get(ChronoField.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdjustDateWithMonthlyFrequencyAndSeedDateAfter28AndCurrentMonthHasMoreDays() {
|
||||
// given
|
||||
Temporal date = LocalDate.of(2023, Month.MAY, 31);
|
||||
Temporal seedDate = LocalDate.of(2023, Month.APRIL, 30);
|
||||
PeriodFrequencyType frequencyType = PeriodFrequencyType.MONTHS;
|
||||
|
||||
// when
|
||||
Temporal adjustedDate = CalendarUtils.adjustDate(date, seedDate, frequencyType);
|
||||
|
||||
// then
|
||||
assertEquals(30, adjustedDate.get(ChronoField.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdjustDateWithMonthlyFrequencyAndSeedDateAfter28AndCurrentMonthIsMarch() {
|
||||
// given
|
||||
Temporal date = LocalDate.of(2023, Month.MARCH, 28);
|
||||
Temporal seedDate = LocalDate.of(2023, Month.JANUARY, 31);
|
||||
PeriodFrequencyType frequencyType = PeriodFrequencyType.MONTHS;
|
||||
|
||||
// when
|
||||
Temporal adjustedDate = CalendarUtils.adjustDate(date, seedDate, frequencyType);
|
||||
|
||||
// then
|
||||
assertEquals(31, adjustedDate.get(ChronoField.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdjustDateWithMonthlyFrequencyAndSeedDateAfter28AndCurrentMonthIsFebLeapYear() {
|
||||
// given
|
||||
Temporal date = LocalDate.of(2024, Month.FEBRUARY, 29);
|
||||
Temporal seedDate = LocalDate.of(2024, Month.JANUARY, 30);
|
||||
PeriodFrequencyType frequencyType = PeriodFrequencyType.MONTHS;
|
||||
|
||||
// when
|
||||
Temporal adjustedDate = CalendarUtils.adjustDate(date, seedDate, frequencyType);
|
||||
|
||||
// then
|
||||
assertEquals(29, adjustedDate.get(ChronoField.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdjustDateNonMonthlyPeriod() {
|
||||
// given
|
||||
Temporal date = LocalDate.of(2023, Month.APRIL, 30);
|
||||
Temporal seedDate = LocalDate.of(2023, Month.JANUARY, 27);
|
||||
|
||||
// when
|
||||
Temporal adjustedDateWeekly = CalendarUtils.adjustDate(date, seedDate, PeriodFrequencyType.WEEKS);
|
||||
// then
|
||||
assertEquals(30, adjustedDateWeekly.get(ChronoField.DAY_OF_MONTH));
|
||||
|
||||
// when
|
||||
Temporal adjustedDateYearly = CalendarUtils.adjustDate(date, seedDate, PeriodFrequencyType.YEARS);
|
||||
// then
|
||||
assertEquals(30, adjustedDateYearly.get(ChronoField.DAY_OF_MONTH));
|
||||
|
||||
// when
|
||||
Temporal adjustedDateDaily = CalendarUtils.adjustDate(date, seedDate, PeriodFrequencyType.DAYS);
|
||||
// then
|
||||
assertEquals(30, adjustedDateDaily.get(ChronoField.DAY_OF_MONTH));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user