From 9bd5f53675b67dd4d153f2da83afd6949f5fa6a3 Mon Sep 17 00:00:00 2001 From: mariomitte Date: Mon, 10 Aug 2026 08:17:49 +0200 Subject: [PATCH] fix: normalize task datetimes before travel expense min/max Prevent 500 errors on travel-expenses-table when work-hour entries mix naive and timezone-aware datetimes. Normalize all candidate datetimes to a consistent timezone before min/max comparisons. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- backend/modules/fleet/views.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/backend/modules/fleet/views.py b/backend/modules/fleet/views.py index c91e7f2..ad2db2e 100644 --- a/backend/modules/fleet/views.py +++ b/backend/modules/fleet/views.py @@ -304,6 +304,14 @@ def _format_decimal_fixed(value, *, default='0.00', places=2): return format(normalized.quantize(quantizer), 'f') +def _normalize_datetime_for_compare(value): + if not isinstance(value, datetime): + return value + if timezone.is_naive(value): + return timezone.make_aware(value, timezone.get_current_timezone()) + return timezone.localtime(value) + + def _work_order_travel_expenses_context(work_order): related_tasks = list(_work_order_related_tasks_queryset(work_order)) trip_entries = [] @@ -318,8 +326,16 @@ def _work_order_travel_expenses_context(work_order): if trip_entries: entry_dates = [entry['date'] for entry in trip_entries if entry.get('date')] - start_candidates = [entry['start_dt'] for entry in trip_entries if entry.get('start_dt')] - end_candidates = [entry['end_dt'] for entry in trip_entries if entry.get('end_dt')] + start_candidates = [ + _normalize_datetime_for_compare(entry.get('start_dt')) + for entry in trip_entries + if entry.get('start_dt') + ] + end_candidates = [ + _normalize_datetime_for_compare(entry.get('end_dt')) + for entry in trip_entries + if entry.get('end_dt') + ] if entry_dates: trip_start_date = min(entry_dates) trip_end_date = max(entry_dates)