From 1e5c7d259f4dfd9cd22fa559d4ac4e5b939dce72 Mon Sep 17 00:00:00 2001 From: leander19961 Date: Wed, 9 Jul 2025 14:59:37 +0200 Subject: [PATCH] Fix crash if person hours doesn't fit into standard pattern --- .../Service/TimesheetService.cs | 21 +++++++++++++--- LeanderShiftPlannerV2/Service/ViewService.cs | 7 ++++++ .../View/ErrorView/GeneralError.axaml | 12 ++++++++++ .../View/ErrorView/GeneralError.axaml.cs | 24 +++++++++++++++++++ .../LeanderShiftPlannerMainWindow.axaml.cs | 2 +- 5 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 LeanderShiftPlannerV2/View/ErrorView/GeneralError.axaml create mode 100644 LeanderShiftPlannerV2/View/ErrorView/GeneralError.axaml.cs diff --git a/LeanderShiftPlannerV2/Service/TimesheetService.cs b/LeanderShiftPlannerV2/Service/TimesheetService.cs index 1dca027..6281a49 100644 --- a/LeanderShiftPlannerV2/Service/TimesheetService.cs +++ b/LeanderShiftPlannerV2/Service/TimesheetService.cs @@ -28,9 +28,10 @@ public class TimesheetService foreach (Person person in persons) { List pattern = GetPattern(person); + if (pattern.Count == 0) continue; List holidays = await GetHolidays(); List weekends = GetWeekends(); - + int monthHours = person.Hours * 4; foreach (string month in Constants.Months) { @@ -40,6 +41,7 @@ public class TimesheetService int currentPersonHours = 0; int currentPatternIndex = 0; int dayOfMonth = 1; + int lastDayOfMonth = new DateTime(DateTime.Now.Year, Constants.Months.IndexOf(month), dayOfMonth).AddMonths(1).AddDays(-1).Day; while (currentPersonHours != monthHours) { DateTime currentDay = new DateTime(DateTime.Now.Year, Constants.Months.IndexOf(month), dayOfMonth); @@ -75,9 +77,18 @@ public class TimesheetService currentPersonHours += 2; break; } + case 1: + { + DateTime dayStart = new DateTime(currentDay.Year, currentDay.Month, currentDay.Day, 9, 0, 0); + DateTime dayEnd = new DateTime(currentDay.Year, currentDay.Month, currentDay.Day, 10, 0, 0); + timesheetValue.Add((dayStart, dayEnd, false)); + currentPersonHours += 1; + break; + } } dayOfMonth++; + if (dayOfMonth > lastDayOfMonth) throw new Exception("Too many hours!"); currentPatternIndex = (currentPatternIndex + 1) % pattern.Count; } @@ -158,10 +169,14 @@ public class TimesheetService pattern.Add(2); hours += 2; } + else if (hours + 1 <= person.Hours) + { + pattern.Add(1); + hours += 1; + } else if (hours + 2 > person.Hours) { - // Error! Could not find pattern! - return null; + return new List(); } } diff --git a/LeanderShiftPlannerV2/Service/ViewService.cs b/LeanderShiftPlannerV2/Service/ViewService.cs index 5541377..ae6e3fa 100644 --- a/LeanderShiftPlannerV2/Service/ViewService.cs +++ b/LeanderShiftPlannerV2/Service/ViewService.cs @@ -82,5 +82,12 @@ namespace LeanderShiftPlannerV2.Service areYouSure.Show(sender); return areYouSure; } + + public static GeneralError ShowGeneralError(Window sender) + { + GeneralError generalError = new GeneralError(); + generalError.Show(sender); + return generalError; + } } } \ No newline at end of file diff --git a/LeanderShiftPlannerV2/View/ErrorView/GeneralError.axaml b/LeanderShiftPlannerV2/View/ErrorView/GeneralError.axaml new file mode 100644 index 0000000..4dff45e --- /dev/null +++ b/LeanderShiftPlannerV2/View/ErrorView/GeneralError.axaml @@ -0,0 +1,12 @@ + + + +