From fd69a22410e9a97bc62023a7e49356aa5118027e Mon Sep 17 00:00:00 2001 From: Philippe Wauthy Date: Thu, 21 Oct 2021 09:57:31 +0000 Subject: [PATCH] [IMP] resource: assign a search_range for calendar_end Assign a search_range for calendar_end in the _adjust_to_calendar method in all conditions. The search_range was only assigned a value if the start and end are on the same date before. The _adjust_to_calendar method receives a start and end from midnight to midnight in the resource timezone. Start and end datetimes can be set on a different day via the Gantt view set in month or year for example. This commit ensures that a search_range is always assigned to find the calendar_end and not only when start and end are on the same day. This allows to find the end of a shift searched over two days if the employee is only working one day. Task-2628876 closes odoo/odoo#78746 Signed-off-by: Yannick Tivisse (yti) --- addons/resource/models/resource.py | 20 ++++++++++---------- addons/resource/tests/test_resource.py | 22 ++++++++++++++++++++-- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/addons/resource/models/resource.py b/addons/resource/models/resource.py index fbf7386033d..92bd32d0dda 100644 --- a/addons/resource/models/resource.py +++ b/addons/resource/models/resource.py @@ -951,16 +951,16 @@ class ResourceResource(models.Model): end, revert_end_tz = make_aware(end) result = {} for resource in self: - calendar_start = resource.calendar_id._get_closest_work_time(start, resource=resource) - search_range = None - tz = timezone(resource.tz) - if calendar_start and start.astimezone(tz).date() == end.astimezone(tz).date(): - # Make sure to only search end after start - search_range = ( - start, - end + relativedelta(days=1, hour=0, minute=0, second=0), - ) - calendar_end = resource.calendar_id._get_closest_work_time(end, match_end=True, resource=resource, search_range=search_range) + resource_tz = timezone(resource.tz) + start, end = start.astimezone(resource_tz), end.astimezone(resource_tz) + search_range = [ + start + relativedelta(hour=0, minute=0, second=0), + end + relativedelta(days=1, hour=0, minute=0, second=0), + ] + calendar_start = resource.calendar_id._get_closest_work_time(start, resource=resource, search_range=search_range) + search_range[0] = start + calendar_end = resource.calendar_id._get_closest_work_time(end if end > start else start, match_end=True, + resource=resource, search_range=search_range) result[resource] = ( calendar_start and revert_start_tz(calendar_start), calendar_end and revert_end_tz(calendar_end), diff --git a/addons/resource/tests/test_resource.py b/addons/resource/tests/test_resource.py index 5ab0fc37b6a..875be5b57f8 100644 --- a/addons/resource/tests/test_resource.py +++ b/addons/resource/tests/test_resource.py @@ -493,7 +493,7 @@ class TestResMixin(TestResourceCommon): ) self.assertEqual(result[self.john], ( datetime_tz(2020, 4, 3, 8, 0, 0, tzinfo=self.john.tz), - None, + datetime_tz(2020, 4, 3, 23, 0, 0, tzinfo=self.john.tz), )) result = self.john._adjust_to_calendar( @@ -501,10 +501,28 @@ class TestResMixin(TestResourceCommon): datetime_tz(2020, 4, 3, 14, 0, 0, tzinfo=self.john.tz), ) self.assertEqual(result[self.john], ( - None, + datetime_tz(2020, 4, 3, 8, 0, 0, tzinfo=self.john.tz), datetime_tz(2020, 4, 3, 13, 0, 0, tzinfo=self.john.tz), )) + result = self.john._adjust_to_calendar( + datetime_tz(2020, 3, 31, 0, 0, 0, tzinfo=self.john.tz), # search between Tuesday and Thursday + datetime_tz(2020, 4, 2, 23, 59, 59, tzinfo=self.john.tz), + ) + self.assertEqual(result[self.john], ( + datetime_tz(2020, 3, 31, 8, 0, tzinfo=self.john.tz), + datetime_tz(2020, 3, 31, 16, 0, tzinfo=self.john.tz), + )) + + result = self.john._adjust_to_calendar( + datetime_tz(2020, 3, 31, 0, 0, 0, tzinfo=self.john.tz), # search between Tuesday and Friday + datetime_tz(2020, 4, 3, 23, 59, 59, tzinfo=self.john.tz), + ) + result = self.john._adjust_to_calendar( + datetime_tz(2020, 3, 31, 8, 0, 0, tzinfo=self.john.tz), + datetime_tz(2020, 4, 3, 23, 0, 0, tzinfo=self.john.tz), + ) + def test_adjust_calendar_timezone_after(self): # Calendar: # Tuesdays 8-16