[FIX] resource: allow to plan 0 hours

The method `plan_hours` does nothing if the `hours` parameter is equal to 0.
This commit adds the 0 case in the logic, and add an optional `resource` in the
parameters.

Example: A workorder works each days between 8am and 4pm. If we call
`plan_hours(0)` at 9pm, we want to get 8am the next day. Currently, it
sends back 9pm.

Before this commit, a hack existed consisting to send a very small value to
`plan_hours`. The holidays and non-working time were taken into account and the
result was rounded to the second.  We can now send explicitely 0 and the
non-working hours will be skipped.
This commit is contained in:
William Henrotin
2018-08-22 16:02:52 +02:00
parent 5fdee3ddbb
commit 701d0f0e1a
2 changed files with 9 additions and 11 deletions
+4 -8
View File
@@ -295,7 +295,7 @@ class ResourceCalendar(models.Model):
)
@api.multi
def plan_hours(self, hours, day_dt, compute_leaves=False, domain=None):
def plan_hours(self, hours, day_dt, compute_leaves=False, domain=None, resource=None):
"""
`compute_leaves` controls whether or not this method is taking into
account the global leaves.
@@ -309,11 +309,11 @@ class ResourceCalendar(models.Model):
# which method to use for retrieving intervals
if compute_leaves:
get_intervals = partial(self._work_intervals, domain=domain)
get_intervals = partial(self._work_intervals, domain=domain, resource=resource)
else:
get_intervals = self._attendance_intervals
if hours > 0:
if hours >= 0:
delta = timedelta(days=14)
for n in range(100):
dt = day_dt + delta * n
@@ -323,8 +323,7 @@ class ResourceCalendar(models.Model):
return revert(start + timedelta(hours=hours))
hours -= interval_hours
return False
elif hours < 0:
else:
hours = abs(hours)
delta = timedelta(days=14)
for n in range(100):
@@ -336,9 +335,6 @@ class ResourceCalendar(models.Model):
hours -= interval_hours
return False
else:
return revert(day_dt)
@api.multi
def plan_days(self, days, day_dt, compute_leaves=False, domain=None):
"""
+5 -3
View File
@@ -260,9 +260,11 @@ class TestCalendar(TestResourceCommon):
time = self.calendar_jean.plan_hours(-10, datetime_tz(2018, 4, 10, 0, 0, 0, tzinfo=self.jean.tz), compute_leaves=True)
self.assertEqual(time, datetime_tz(2018, 4, 6, 14, 0, 0, tzinfo=self.jean.tz))
# zero planning
time = self.calendar_jean.plan_hours(0, datetime_tz(2018, 4, 10, 0, 0, 0, tzinfo=self.jean.tz), compute_leaves=True)
self.assertEqual(time, datetime_tz(2018, 4, 10, 0, 0, 0, tzinfo=self.jean.tz))
# zero planning with holidays
time = self.calendar_jean.plan_hours(0, datetime_tz(2018, 4, 11, 0, 0, 0, tzinfo=self.jean.tz), compute_leaves=True)
self.assertEqual(time, datetime_tz(2018, 4, 12, 8, 0, 0, tzinfo=self.jean.tz))
time = self.calendar_jean.plan_hours(0, datetime_tz(2018, 4, 10, 0, 0, 0, tzinfo=self.jean.tz), compute_leaves=False)
self.assertEqual(time, datetime_tz(2018, 4, 10, 8, 0, 0, tzinfo=self.jean.tz))
# very small planning
time = self.calendar_jean.plan_hours(0.0002, datetime_tz(2018, 4, 10, 0, 0, 0, tzinfo=self.jean.tz), compute_leaves=True)