[MERGE] [FIX] hr_timesheet_sheet: make it timezone tolerant and avoid errors when...
authorMartin Trigaux <mat@openerp.com>
Mon, 28 Apr 2014 15:32:16 +0000 (01:32 +1000)
committerMartin Trigaux <mat@openerp.com>
Mon, 28 Apr 2014 15:32:16 +0000 (01:32 +1000)
The attendance date recieved by the server is in UTC while the user sees it in his timezone. This means that an attendance could be in a timesheet (bounded by dates) for a user but not for the server which would not accept a valid attendance.
The fix will make the check in the user's timezone.

Only the date part of the attendance is kept for comparison as the boundaries are dates objects.

bzr revid: mat@openerp.com-20140428153216-4s6r5hu1ov0p0ofm

1  2 
addons/hr_timesheet_sheet/hr_timesheet_sheet.py

@@@ -391,16 -393,45 +393,45 @@@ class hr_attendance(osv.osv)
              attendance_ids.extend([row[0] for row in cr.fetchall()])
          return attendance_ids
  
+     def _get_attendance_employee_tz(self, cr, uid, employee_id, date, context=None):
+         """ Simulate timesheet in employee timezone
 -        Return the attendance datetime as date in string format in employee
++        Return the attendance date in string format in the employee
+         tz converted from utc timezone as we consider date of employee
+         timesheet is in employee timezone
+         """
+         employee_obj = self.pool['hr.employee']
+         tz = False
+         if employee_id:
+             employee = employee_obj.browse(cr, uid, employee_id, context=context)
+             tz = employee.user_id.partner_id.tz
+         att_tz = timezone(tz or 'utc')
+         attendance_dt = datetime.strptime(date, DEFAULT_SERVER_DATETIME_FORMAT)
+         att_tz_dt = pytz.utc.localize(attendance_dt)
+         att_tz_dt = att_tz_dt.astimezone(att_tz)
+         # We take only the date omiting the hours as we compare with timesheet
+         # date_from which is a date format thus using hours would lead to
+         # be out of scope of timesheet
+         att_tz_date_str = datetime.strftime(att_tz_dt, DEFAULT_SERVER_DATE_FORMAT)
+         return att_tz_date_str
      def _get_current_sheet(self, cr, uid, employee_id, date=False, context=None):
+         sheet_obj = self.pool['hr_timesheet_sheet.sheet']
          if not date:
              date = time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
-         # ending date with no time to avoid timesheet with early date_to
-         date_to = date[0:10]+' 00:00:00'
-         # limit=1 because only one sheet possible for an employee between 2 dates
-         sheet_ids = self.pool.get('hr_timesheet_sheet.sheet').search(cr, uid, [
-             ('date_to', '>=', date_to), ('date_from', '<=', date),
-             ('employee_id', '=', employee_id)
-         ], limit=1, context=context)
+         att_tz_date_str = self._get_attendance_employee_tz(
+                 cr, uid, employee_id,
+                 date=date, context=context)
+         sheet_ids = sheet_obj.search(cr, uid,
+             [('date_from', '<=', att_tz_date_str),
+              ('date_to', '>=', att_tz_date_str),
+              ('employee_id', '=', employee_id)],
 -            context=context)
++            limit=1, context=context)
          return sheet_ids and sheet_ids[0] or False
  
      def _sheet(self, cursor, user, ids, name, args, context=None):