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>
This commit is contained in:
@@ -304,6 +304,14 @@ def _format_decimal_fixed(value, *, default='0.00', places=2):
|
|||||||
return format(normalized.quantize(quantizer), 'f')
|
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):
|
def _work_order_travel_expenses_context(work_order):
|
||||||
related_tasks = list(_work_order_related_tasks_queryset(work_order))
|
related_tasks = list(_work_order_related_tasks_queryset(work_order))
|
||||||
trip_entries = []
|
trip_entries = []
|
||||||
@@ -318,8 +326,16 @@ def _work_order_travel_expenses_context(work_order):
|
|||||||
|
|
||||||
if trip_entries:
|
if trip_entries:
|
||||||
entry_dates = [entry['date'] for entry in trip_entries if entry.get('date')]
|
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')]
|
start_candidates = [
|
||||||
end_candidates = [entry['end_dt'] for entry in trip_entries if entry.get('end_dt')]
|
_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:
|
if entry_dates:
|
||||||
trip_start_date = min(entry_dates)
|
trip_start_date = min(entry_dates)
|
||||||
trip_end_date = max(entry_dates)
|
trip_end_date = max(entry_dates)
|
||||||
|
|||||||
Reference in New Issue
Block a user