Merge lp:~camptocamp/openobject-addons/7.0-fix-hr_timesheet_sheet-timezone-20130723 into lp:openobject-addons/7.0

Proposed by Yannick Vaucher @ Camptocamp
Status: Merged
Merged at revision: 10019
Proposed branch: lp:~camptocamp/openobject-addons/7.0-fix-hr_timesheet_sheet-timezone-20130723
Merge into: lp:openobject-addons/7.0
Diff against target: 98 lines (+46/-11)
1 file modified
hr_timesheet_sheet/hr_timesheet_sheet.py (+46/-11)
To merge this branch: bzr merge lp:~camptocamp/openobject-addons/7.0-fix-hr_timesheet_sheet-timezone-20130723
Reviewer Review Type Date Requested Status
Martin Trigaux (OpenERP) (community) Approve
Review via email: mp+176459@code.launchpad.net

Description of the change

Fix timezone issue between attendances and timesheets

This MP depends on https://code.launchpad.net/~openerp-dev/openobject-addons/7.0-opw-592846-msh/+merge/165106

To post a comment you must log in.
Revision history for this message
Yannick Vaucher @ Camptocamp (yvaucher-c2c) wrote :

MP on openobject-addons was approved but is not merged yet

Revision history for this message
Yannick Vaucher @ Camptocamp (yvaucher-c2c) wrote :
Revision history for this message
Martin Trigaux (OpenERP) (mat-openerp) wrote :

Thanks for the patch, it was merged in 7.0

revno: 10019 [merge]
revision-id: <email address hidden>

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hr_timesheet_sheet/hr_timesheet_sheet.py'
2--- hr_timesheet_sheet/hr_timesheet_sheet.py 2014-04-25 10:08:56 +0000
3+++ hr_timesheet_sheet/hr_timesheet_sheet.py 2014-04-28 14:24:48 +0000
4@@ -22,9 +22,11 @@
5 import time
6 from datetime import datetime
7 from dateutil.relativedelta import relativedelta
8+from pytz import timezone
9+import pytz
10
11 from openerp.osv import fields, osv
12-from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
13+from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT
14 from openerp.tools.translate import _
15 from openerp import netsvc
16
17@@ -391,22 +393,53 @@
18 attendance_ids.extend([row[0] for row in cr.fetchall()])
19 return attendance_ids
20
21+ def _get_attendance_employee_tz(self, cr, uid, employee_id, date, context=None):
22+ """ Simulate timesheet in employee timezone
23+
24+ Return the attendance datetime as date in string format in employee
25+ tz converted from utc timezone as we consider date of employee
26+ timesheet is in employee timezone
27+ """
28+ employee_obj = self.pool['hr.employee']
29+
30+ tz = False
31+ if employee_id:
32+ employee = employee_obj.browse(cr, uid, employee_id, context=context)
33+ tz = employee.user_id.partner_id.tz
34+
35+ att_tz = timezone(tz or 'utc')
36+
37+ attendance_dt = datetime.strptime(date, DEFAULT_SERVER_DATETIME_FORMAT)
38+ att_tz_dt = pytz.utc.localize(attendance_dt)
39+ att_tz_dt = att_tz_dt.astimezone(att_tz)
40+ # We take only the date omiting the hours as we compare with timesheet
41+ # date_from which is a date format thus using hours would lead to
42+ # be out of scope of timesheet
43+ att_tz_date_str = datetime.strftime(att_tz_dt, DEFAULT_SERVER_DATE_FORMAT)
44+ return att_tz_date_str
45+
46 def _get_current_sheet(self, cr, uid, employee_id, date=False, context=None):
47+
48+ sheet_obj = self.pool['hr_timesheet_sheet.sheet']
49 if not date:
50 date = time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
51- # ending date with no time to avoid timesheet with early date_to
52- date_to = date[0:10]+' 00:00:00'
53- # limit=1 because only one sheet possible for an employee between 2 dates
54- sheet_ids = self.pool.get('hr_timesheet_sheet.sheet').search(cr, uid, [
55- ('date_to', '>=', date_to), ('date_from', '<=', date),
56- ('employee_id', '=', employee_id)
57- ], limit=1, context=context)
58+
59+ att_tz_date_str = self._get_attendance_employee_tz(
60+ cr, uid, employee_id,
61+ date=date, context=context)
62+ sheet_ids = sheet_obj.search(cr, uid,
63+ [('date_from', '<=', att_tz_date_str),
64+ ('date_to', '>=', att_tz_date_str),
65+ ('employee_id', '=', employee_id)],
66+ context=context)
67 return sheet_ids and sheet_ids[0] or False
68
69 def _sheet(self, cursor, user, ids, name, args, context=None):
70 res = {}.fromkeys(ids, False)
71 for attendance in self.browse(cursor, user, ids, context=context):
72- res[attendance.id] = self._get_current_sheet(cursor, user, attendance.employee_id.id, attendance.name, context=context)
73+ res[attendance.id] = self._get_current_sheet(
74+ cursor, user, attendance.employee_id.id, attendance.name,
75+ context=context)
76 return res
77
78 _columns = {
79@@ -428,10 +461,13 @@
80
81 sheet_id = context.get('sheet_id') or self._get_current_sheet(cr, uid, vals.get('employee_id'), vals.get('name'), context=context)
82 if sheet_id:
83+ att_tz_date_str = self._get_attendance_employee_tz(
84+ cr, uid, vals.get('employee_id'),
85+ date=vals.get('name'), context=context)
86 ts = self.pool.get('hr_timesheet_sheet.sheet').browse(cr, uid, sheet_id, context=context)
87 if ts.state not in ('draft', 'new'):
88 raise osv.except_osv(_('Error!'), _('You can not enter an attendance in a submitted timesheet. Ask your manager to reset it before adding attendance.'))
89- elif ts.date_from > vals.get('name') or ts.date_to < vals.get('name'):
90+ elif ts.date_from > att_tz_date_str or ts.date_to < att_tz_date_str:
91 raise osv.except_osv(_('User Error!'), _('You can not enter an attendance date outside the current timesheet dates.'))
92 return super(hr_attendance,self).create(cr, uid, vals, context=context)
93
94@@ -587,4 +623,3 @@
95 res_company()
96
97 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
98-