From f68c5261f9209aba7d8f19a33f1892d863c3c448 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=BE=20=D0=9A=D0=B0=D1=82?= =?UTF-8?q?=D1=8E=D1=85=D0=B0?= Date: Tue, 28 Nov 2017 10:45:39 +0000 Subject: [PATCH] [FIX] resource: Fix work/leaves hours count methods Purpose ======= Buggy example: I have a 35h working schedule. I work every week day: - From 8h to 12h - From 12h to 16h Let's say that I want to retrieve the working hours from Wednesday 14h03 to Thursday 11h03. The working hours are: - From 14h03 to 16h --> 1h57 - From 8h to 11h03 --> 3h03 ---------------------------- TOTAL 5h Currently the result will be 1.95, i.e. 1h57 because the second day is not taken into account as start time > end time, and thus rrule doesn't manage this correctly and returns only the datetime corresponding to the first day. Specification ============= Force the until parameter of rrule to be set at the end of the day, do avoid skipping the second day. Do this in the methods that compute the work hours and the leave hours. Add a test to ensure the robustness of the fix. Closes #21297 closes odoo/odoo#28920 --- addons/resource/models/resource.py | 4 ++-- addons/resource/tests/test_resource.py | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/addons/resource/models/resource.py b/addons/resource/models/resource.py index ebe4fd2406637..ebb2ab7ba6f4c 100644 --- a/addons/resource/models/resource.py +++ b/addons/resource/models/resource.py @@ -422,7 +422,7 @@ def _iter_work_intervals(self, start_dt, end_dt, resource_id, compute_leaves=Tru for day in rrule.rrule(rrule.DAILY, dtstart=start_dt, - until=end_dt, + until=end_dt.replace(hour=23, minute=59, second=59, microsecond=999999), byweekday=self._get_weekdays()): start_time = datetime.time.min if day.date() == start_dt.date(): @@ -451,7 +451,7 @@ def _iter_leave_intervals(self, start_dt, end_dt, resource_id): for day in rrule.rrule(rrule.DAILY, dtstart=start_dt, - until=end_dt, + until=end_dt.replace(hour=23, minute=59, second=59, microsecond=999999), byweekday=self._get_weekdays()): start_time = datetime.time.min if day.date() == start_dt.date(): diff --git a/addons/resource/tests/test_resource.py b/addons/resource/tests/test_resource.py index 16d6c5a0edeb0..2e8df285e8fbf 100644 --- a/addons/resource/tests/test_resource.py +++ b/addons/resource/tests/test_resource.py @@ -233,6 +233,15 @@ def test_calendar_working_hours(self): compute_leaves=False) self.assertEqual(res, 40.0) + def test_calendar_working_hours_count(self): + calendar = self.env.ref('resource.resource_calendar_std_35h') + res = calendar.get_work_hours_count( + Datetime.from_string('2017-05-03 14:03:00'), # Wednesday (8:00-12:00, 13:00-16:00) + Datetime.from_string('2017-05-04 11:03:00'), # Thursday (8:00-12:00, 13:00-16:00) + resource_id=None, + compute_leaves=False) + self.assertEqual(res, 5.0) + def test_calendar_working_hours_leaves(self): # new API: resource and leaves # res: 2 weeks -> 40 hours - (3+4) leave hours