From 84a650e33aadb29661121410813e3de37a5ad3e9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= <tde@odoo.com>
Date: Fri, 30 Dec 2016 16:11:41 +0100
Subject: [PATCH] [FIX] resource: fix computation of limited attendances

Due to an error introduced when migrating the module at new API
at commit a4e57ea16ce2d021157f7d00a0500f4ab7febed3 limited attendances
having date_from and / or date_to were not correctly taken into
account.

Tests have been added to avoid regression.
---
 addons/resource/models/resource.py     | 15 +++++++-----
 addons/resource/tests/test_resource.py | 32 ++++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/addons/resource/models/resource.py b/addons/resource/models/resource.py
index 9acafc841100..32898e2470e6 100644
--- a/addons/resource/models/resource.py
+++ b/addons/resource/models/resource.py
@@ -151,14 +151,17 @@ class ResourceCalendar(models.Model):
     # --------------------------------------------------
 
     @api.multi
-    def get_attendances_for_weekday(self, date):
-        """ Given a list of weekdays, return matching resource.calendar.attendance"""
+    def get_attendances_for_weekday(self, day_dt):
+        """ Given a day datetime, return matching attendances """
         self.ensure_one()
-        weekday = date.weekday()
-        date = fields.Date.today()
+        weekday = day_dt.weekday()
         attendances = self.env['resource.calendar.attendance']
-        for attendance in self.attendance_ids.filtered(lambda att: int(att.dayofweek) == weekday and
-                    not ((att.date_from and date < att.date_from) or (att.date_to and date > att.date_to))):
+
+        for attendance in self.attendance_ids.filtered(
+            lambda att:
+                int(att.dayofweek) == weekday and
+                not (att.date_from and fields.Date.from_string(att.date_from) > day_dt.date()) and
+                not (att.date_to and fields.Date.from_string(att.date_to) < day_dt.date())):
             attendances |= attendance
         return attendances
 
diff --git a/addons/resource/tests/test_resource.py b/addons/resource/tests/test_resource.py
index eff3fdd6a49b..a01bc7ec63f0 100644
--- a/addons/resource/tests/test_resource.py
+++ b/addons/resource/tests/test_resource.py
@@ -162,6 +162,38 @@ class TestResource(TestResourceCommon):
         self.assertEqual(intervals[1][0], Datetime.from_string('2013-02-19 12:00:00'), 'resource_calendar: wrong working intervals')
         self.assertEqual(intervals[1][1], Datetime.from_string('2013-02-19 15:45:30'), 'resource_calendar: wrong working intervals')
 
+    def test_21_calendar_working_intervals_limited_attendances(self):
+        """ Test attendances limited in time. """
+        self.env['resource.calendar.attendance'].browse(self.att3_id).write({
+            'date_from': self.date2 + relativedelta(days=7),
+            'date_to': False,
+        })
+        intervals = self.calendar.get_working_intervals_of_day(start_dt=self.date2)
+        self.assertEqual(intervals, [(Datetime.from_string('2013-02-15 10:11:12'), Datetime.from_string('2013-02-15 13:00:00'))])
+
+        self.env['resource.calendar.attendance'].browse(self.att3_id).write({
+            'date_from': False,
+            'date_to': self.date2 - relativedelta(days=7),
+        })
+        intervals = self.calendar.get_working_intervals_of_day(start_dt=self.date2)
+        self.assertEqual(intervals, [(Datetime.from_string('2013-02-15 10:11:12'), Datetime.from_string('2013-02-15 13:00:00'))])
+
+        self.env['resource.calendar.attendance'].browse(self.att3_id).write({
+            'date_from': self.date2 + relativedelta(days=7),
+            'date_to': self.date2 - relativedelta(days=7),
+        })
+        intervals = self.calendar.get_working_intervals_of_day(start_dt=self.date2)
+        self.assertEqual(intervals, [(Datetime.from_string('2013-02-15 10:11:12'), Datetime.from_string('2013-02-15 13:00:00'))])
+
+        self.env['resource.calendar.attendance'].browse(self.att3_id).write({
+            'date_from': self.date2,
+            'date_to': self.date2,
+        })
+        intervals = self.calendar.get_working_intervals_of_day(start_dt=self.date2)
+        self.assertEqual(len(intervals), 2)
+        self.assertEqual(intervals[0], (Datetime.from_string('2013-02-15 10:11:12'), Datetime.from_string('2013-02-15 13:00:00')))
+        self.assertEqual(intervals[1], (Datetime.from_string('2013-02-15 16:00:00'), Datetime.from_string('2013-02-15 23:00:00')))
+
     def test_30_calendar_working_days(self):
         """ Testing calendar hours computation on a working day """
 
-- 
GitLab