[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
This commit is contained in:
Дмитро Катюха
2018-11-21 15:42:48 +00:00
committed by Yannick Tivisse
parent 07d4505759
commit f68c5261f9
2 changed files with 11 additions and 2 deletions
+2 -2
View File
@@ -422,7 +422,7 @@ class ResourceCalendar(models.Model):
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 @@ class ResourceCalendar(models.Model):
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():
+9
View File
@@ -233,6 +233,15 @@ class ResourceWorkingHours(TestResourceCommon):
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