From 78add732782a79a8da1aba1f4bfba4542edbc1a4 Mon Sep 17 00:00:00 2001 From: DragonSlayer_14 Date: Sun, 25 Jan 2026 16:34:14 +0100 Subject: [PATCH] Fix: Passt an, dass bei monatlichen Transaktionen der Tag entsprechend hergenommen wird --- lib/PlatformDependent/Web/date_utils_web.dart | 28 +++++++++++++ lib/Tasks/generate_transactions_task.dart | 40 ++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/lib/PlatformDependent/Web/date_utils_web.dart b/lib/PlatformDependent/Web/date_utils_web.dart index 08e53e3..866dba1 100644 --- a/lib/PlatformDependent/Web/date_utils_web.dart +++ b/lib/PlatformDependent/Web/date_utils_web.dart @@ -16,4 +16,32 @@ class DateUtils { final DateTime monthDate, final int monthsToAdd, ) => DateTime(monthDate.year, monthDate.month + monthsToAdd); + + /// Returns the number of days in a month, according to the proleptic + /// Gregorian calendar. + /// + /// This applies the leap year logic introduced by the Gregorian reforms of + /// 1582. It will not give valid results for dates prior to that time. + static int getDaysInMonth(final int year, final int month) { + if (month == DateTime.february) { + final bool isLeapYear = + (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0); + return isLeapYear ? 29 : 28; + } + const List daysInMonth = [ + 31, + -1, + 31, + 30, + 31, + 30, + 31, + 31, + 30, + 31, + 30, + 31, + ]; + return daysInMonth[month - 1]; + } } diff --git a/lib/Tasks/generate_transactions_task.dart b/lib/Tasks/generate_transactions_task.dart index 0dc8df7..93ba2c9 100644 --- a/lib/Tasks/generate_transactions_task.dart +++ b/lib/Tasks/generate_transactions_task.dart @@ -67,15 +67,51 @@ class GenerateTransactionsTask extends Task { case TimeFrameEnum.weekly: newTransactionDate = lastTransactionDate.add(const Duration(days: 7)); case TimeFrameEnum.monthly: - newTransactionDate = DateUtils.addMonthsToMonthDate( + final DateTime monthDate = DateUtils.addMonthsToMonthDate( lastTransactionDate, 1, ); + + final int day = + recurringTransaction.startDate!.day < + DateUtils.getDaysInMonth( + monthDate.year, + monthDate.day, + ) + ? recurringTransaction.startDate!.day + : DateUtils.getDaysInMonth( + monthDate.year, + monthDate.month, + ); + + newTransactionDate = DateTime( + monthDate.year, + monthDate.month, + day, + ); case TimeFrameEnum.yearly: - newTransactionDate = DateUtils.addMonthsToMonthDate( + final DateTime monthDate = DateUtils.addMonthsToMonthDate( lastTransactionDate, 12, ); + + final int day = + recurringTransaction.startDate!.day < + DateUtils.getDaysInMonth( + monthDate.year, + monthDate.day, + ) + ? recurringTransaction.startDate!.day + : DateUtils.getDaysInMonth( + monthDate.year, + monthDate.month, + ); + + newTransactionDate = DateTime( + monthDate.year, + monthDate.month, + day, + ); } } else { newTransactionDate = recurringTransaction.startDate!;