Skip to content

Commit

Permalink
[FIX] resource: Fix work/leaves hours count methods
Browse files Browse the repository at this point in the history
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 #28920
  • Loading branch information
katyukha authored and tivisse committed Nov 21, 2018
1 parent 07d4505 commit f68c526
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions addons/resource/models/resource.py
Expand Up @@ -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():
Expand Down Expand Up @@ -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():
Expand Down
9 changes: 9 additions & 0 deletions addons/resource/tests/test_resource.py
Expand Up @@ -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
Expand Down

0 comments on commit f68c526

Please sign in to comment.