diff --git a/projectforge-business/src/main/kotlin/org/projectforge/birthdaybutler/BirthdayButlerService.kt b/projectforge-business/src/main/kotlin/org/projectforge/birthdaybutler/BirthdayButlerService.kt index 3762dbb27c..e4d42cd55d 100644 --- a/projectforge-business/src/main/kotlin/org/projectforge/birthdaybutler/BirthdayButlerService.kt +++ b/projectforge-business/src/main/kotlin/org/projectforge/birthdaybutler/BirthdayButlerService.kt @@ -133,7 +133,7 @@ class BirthdayButlerService { month, locale ) - }_${LocalDateTime.now().year}.docx" + }_${getYear(month)}.docx" } /** @@ -206,7 +206,7 @@ class BirthdayButlerService { if (birthdayList.isNotEmpty()) { val variables = Variables() variables.put("table", "") // Marker for finding table (should be removed). - variables.put("year", LocalDateTime.now().year) + variables.put("year", getYear(month)) variables.put("month", translateMonth(month, locale = locale)) val birthdayButlerTemplate = configurationService.getOfficeTemplateFile("BirthdayButlerTemplate.docx") check(birthdayButlerTemplate != null) { "BirthdayButlerTemplate.docx not found" } @@ -320,6 +320,25 @@ class BirthdayButlerService { return StringHelper.format2DigitNumber(number) } + /** + * Returns the year for the given month. If the month more than 2 months is in the past, the next year is returned. + */ + private fun getYear(month: Month): Int { + return getYear(month, LocalDateTime.now().month) + } + + /** + * Only for testing purposes + */ + internal fun getYear(month: Month, currentMonth: Month): Int { + return if (month.ordinal + 2 < currentMonth.ordinal) { + // At the end of the year, the next year is meant. + LocalDateTime.now().year + 1 + } else { + return LocalDateTime.now().year + } + } + private val months = arrayOf( "calendar.month.january", "calendar.month.february", diff --git a/projectforge-business/src/test/kotlin/org/projectforge/birthdaybutler/BithdayButlerServiceTest.kt b/projectforge-business/src/test/kotlin/org/projectforge/birthdaybutler/BithdayButlerServiceTest.kt new file mode 100644 index 0000000000..f166134c93 --- /dev/null +++ b/projectforge-business/src/test/kotlin/org/projectforge/birthdaybutler/BithdayButlerServiceTest.kt @@ -0,0 +1,55 @@ +///////////////////////////////////////////////////////////////////////////// +// +// Project ProjectForge Community Edition +// www.projectforge.org +// +// Copyright (C) 2001-2024 Micromata GmbH, Germany (www.micromata.com) +// +// ProjectForge is dual-licensed. +// +// This community edition is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as published +// by the Free Software Foundation; version 3 of the License. +// +// This community edition is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General +// Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, see http://www.gnu.org/licenses/. +// +///////////////////////////////////////////////////////////////////////////// + +package org.projectforge.birthdaybutler + +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test +import java.time.LocalDateTime +import java.time.Month + +class BithdayButlerServiceTest { + @Test + fun testGetYear() { + val year = LocalDateTime.now().year + testGetYear(currentMonth = Month.JANUARY, month = Month.JANUARY, year) + testGetYear(currentMonth = Month.JANUARY, month = Month.DECEMBER, year) + testGetYear(currentMonth = Month.FEBRUARY, month = Month.JANUARY, year) + testGetYear(currentMonth = Month.FEBRUARY, month = Month.FEBRUARY, year) + testGetYear(currentMonth = Month.FEBRUARY, month = Month.DECEMBER, year) + testGetYear(currentMonth = Month.JUNE, month = Month.JANUARY, year + 1) + testGetYear(currentMonth = Month.JUNE, month = Month.MARCH, year + 1) + testGetYear(currentMonth = Month.JUNE, month = Month.APRIL, year) + testGetYear(currentMonth = Month.JUNE, month = Month.MAY, year) + testGetYear(currentMonth = Month.JUNE, month = Month.DECEMBER, year) + testGetYear(currentMonth = Month.DECEMBER, month = Month.JANUARY, year + 1) + testGetYear(currentMonth = Month.DECEMBER, month = Month.SEPTEMBER, year + 1) + testGetYear(currentMonth = Month.DECEMBER, month = Month.OCTOBER, year) + testGetYear(currentMonth = Month.DECEMBER, month = Month.NOVEMBER, year) + testGetYear(currentMonth = Month.DECEMBER, month = Month.DECEMBER, year) + } + + private fun testGetYear(currentMonth: Month, month: Month, expectedYear: Int) { + Assertions.assertEquals(expectedYear, BirthdayButlerService.getYear(month, currentMonth)) + } +}