diff --git a/fusion_tasks/models/technician_task.py b/fusion_tasks/models/technician_task.py index 484c86a8..61e1c177 100644 --- a/fusion_tasks/models/technician_task.py +++ b/fusion_tasks/models/technician_task.py @@ -781,7 +781,7 @@ class FusionTechnicianTask(models.Model): def _inverse_datetime_start(self): """When datetime_start is changed (e.g. from calendar drag), update date + time.""" import pytz - user_tz = pytz.timezone(self.env.user.tz or 'UTC') + user_tz = self._get_local_tz() for task in self: if task.datetime_start: local_dt = pytz.utc.localize(task.datetime_start).astimezone(user_tz) @@ -791,7 +791,7 @@ class FusionTechnicianTask(models.Model): def _inverse_datetime_end(self): """When datetime_end is changed (e.g. from calendar resize), update time_end.""" import pytz - user_tz = pytz.timezone(self.env.user.tz or 'UTC') + user_tz = self._get_local_tz() for task in self: if task.datetime_end: local_dt = pytz.utc.localize(task.datetime_end).astimezone(user_tz) diff --git a/fusion_tasks/tests/__init__.py b/fusion_tasks/tests/__init__.py new file mode 100644 index 00000000..08755748 --- /dev/null +++ b/fusion_tasks/tests/__init__.py @@ -0,0 +1,2 @@ +# -*- coding: utf-8 -*- +from . import test_task_tz diff --git a/fusion_tasks/tests/test_task_tz.py b/fusion_tasks/tests/test_task_tz.py new file mode 100644 index 00000000..5c8a810d --- /dev/null +++ b/fusion_tasks/tests/test_task_tz.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- +from datetime import date +from odoo.tests.common import TransactionCase, tagged + + +@tagged('post_install', '-at_install') +class TestTaskTz(TransactionCase): + + @classmethod + def setUpClass(cls): + super().setUpClass() + # _compute_datetimes resolves company resource-calendar tz FIRST, then user tz. + # Set BOTH to Toronto so the UTC assertion and the round-trip are deterministic. + cls.env.user.tz = 'America/Toronto' + cal = cls.env.company.resource_calendar_id + if cal: + cal.tz = 'America/Toronto' + # technician_id is required (domain x_fc_is_field_staff=True) -> make a field tech. + cls.tech = cls.env['res.users'].create({ + 'name': 'TZ Test Tech', + 'login': 'tz_test_tech_svcbook', + 'x_fc_is_field_staff': True, + }) + cls.task = cls.env['fusion.technician.task'].create({ + 'technician_id': cls.tech.id, + 'scheduled_date': date(2026, 6, 3), + 'time_start': 9.0, + 'time_end': 10.0, + }) + + def test_local_to_utc_compute(self): + # 9:00 local Toronto (EDT, -4) -> 13:00 UTC stored + self.assertEqual(self.task.datetime_start.hour, 13) + + def test_inverse_round_trips_with_same_tz(self): + # writing datetime_start back recovers the same local time_start + self.task.datetime_start = self.task.datetime_start # force inverse + self.task.flush_recordset(['datetime_start']) + self.assertAlmostEqual(self.task.time_start, 9.0, places=2)