From e2b7cd17fcec0e9f73d460f329ab223bf64525b4 Mon Sep 17 00:00:00 2001 From: Yannick Vaucher Date: Tue, 23 Jul 2013 20:43:41 +0200 Subject: [PATCH 1/5] [FIX] hr_timesheet_sheet - search sheet_id for attendance using date in users timezone. As attendances depends on timezone but timesheet have from and to date instead of datetime we have to change the attendance to the same timezone expected for timesheet. And as a Timesheet belongs to a user, we can expect this timesheet to be in the same timezone as its owner. Maybe a timezone field should be added on timesheet. In anycase it make no sense to have all timesheet forced with start and end date as UTC. lp bug: https://launchpad.net/bugs/1204224 fixed bzr revid: yannick.vaucher@camptocamp.com-20130723184341-zowaw71f0mpghgrx --- .../hr_timesheet_sheet/hr_timesheet_sheet.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/addons/hr_timesheet_sheet/hr_timesheet_sheet.py b/addons/hr_timesheet_sheet/hr_timesheet_sheet.py index 768d85860df..b02bd7edcac 100644 --- a/addons/hr_timesheet_sheet/hr_timesheet_sheet.py +++ b/addons/hr_timesheet_sheet/hr_timesheet_sheet.py @@ -22,10 +22,13 @@ import time from datetime import datetime, timedelta from dateutil.relativedelta import relativedelta +from pytz import timezone +import pytz from openerp.osv import fields, osv from openerp.tools.translate import _ from openerp import netsvc +from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT class hr_timesheet_sheet(osv.osv): _name = "hr_timesheet_sheet.sheet" @@ -364,11 +367,20 @@ class hr_attendance(osv.osv): def _sheet(self, cursor, user, ids, name, args, context=None): sheet_obj = self.pool.get('hr_timesheet_sheet.sheet') + utc_tz = pytz.utc res = {}.fromkeys(ids, False) for attendance in self.browse(cursor, user, ids, context=context): - date_to = datetime.strftime(datetime.strptime(attendance.name[0:10], '%Y-%m-%d'), '%Y-%m-%d %H:%M:%S') + + # Simulate timesheet in employee timezone + att_tz = timezone(attendance.employee_id.user_id.partner_id.tz) + + attendance_dt = datetime.strptime(attendance.name, DEFAULT_SERVER_DATETIME_FORMAT) + att_tz_dt = pytz.utc.localize(attendance_dt) + att_tz_dt = att_tz_dt.astimezone(att_tz) + date_from = datetime.strftime(att_tz_dt, DEFAULT_SERVER_DATETIME_FORMAT) + date_to = datetime.strftime(att_tz_dt.date(), DEFAULT_SERVER_DATETIME_FORMAT) sheet_ids = sheet_obj.search(cursor, user, - [('date_to', '>=', date_to), ('date_from', '<=', attendance.name), + [('date_to', '>=', date_to), ('date_from', '<=', date_from), ('employee_id', '=', attendance.employee_id.id)], context=context) if sheet_ids: @@ -488,7 +500,7 @@ class hr_timesheet_sheet_sheet_day(osv.osv): a.name::date as name, s.id as sheet_id, 0.0 as total_timesheet, - SUM(((EXTRACT(hour FROM a.name) * 60) + EXTRACT(minute FROM a.name)) * (CASE WHEN a.action = 'sign_in' THEN -1 ELSE 1 END)) as total_attendance + SUM(((EXTRACT(hour FROM a.name AT TIME ZONE 'UTC') * 60) + EXTRACT(minute FROM a.name AT TIME ZONE 'UTC')) * (CASE WHEN a.action = 'sign_in' THEN -1 ELSE 1 END)) as total_attendance from hr_attendance a LEFT JOIN hr_timesheet_sheet_sheet s From 372f3d131a05d9a3e78076e28af9941df0aa2eb7 Mon Sep 17 00:00:00 2001 From: Yannick Vaucher Date: Mon, 9 Sep 2013 12:36:54 +0200 Subject: [PATCH 2/5] [IMP] hr_timesheet_sheet - remove unused variable bzr revid: yannick.vaucher@camptocamp.com-20130909103654-wwkpyaaoqpwq1mqu --- addons/hr_timesheet_sheet/hr_timesheet_sheet.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/addons/hr_timesheet_sheet/hr_timesheet_sheet.py b/addons/hr_timesheet_sheet/hr_timesheet_sheet.py index b02bd7edcac..c10aa4905eb 100644 --- a/addons/hr_timesheet_sheet/hr_timesheet_sheet.py +++ b/addons/hr_timesheet_sheet/hr_timesheet_sheet.py @@ -20,7 +20,7 @@ ############################################################################## import time -from datetime import datetime, timedelta +from datetime import datetime from dateutil.relativedelta import relativedelta from pytz import timezone import pytz @@ -367,7 +367,6 @@ class hr_attendance(osv.osv): def _sheet(self, cursor, user, ids, name, args, context=None): sheet_obj = self.pool.get('hr_timesheet_sheet.sheet') - utc_tz = pytz.utc res = {}.fromkeys(ids, False) for attendance in self.browse(cursor, user, ids, context=context): From 0ed7f5ef3bc8a8696ceb96ce0efb75cff736f1b1 Mon Sep 17 00:00:00 2001 From: Yannick Vaucher Date: Mon, 9 Sep 2013 12:37:24 +0200 Subject: [PATCH 3/5] [IMP] hr_timesheet_sheet - simplify and improve readability in search for sheet_ids bzr revid: yannick.vaucher@camptocamp.com-20130909103724-4w5tkx2bfms07s91 --- addons/hr_timesheet_sheet/hr_timesheet_sheet.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/addons/hr_timesheet_sheet/hr_timesheet_sheet.py b/addons/hr_timesheet_sheet/hr_timesheet_sheet.py index c10aa4905eb..5f51e8e3df1 100644 --- a/addons/hr_timesheet_sheet/hr_timesheet_sheet.py +++ b/addons/hr_timesheet_sheet/hr_timesheet_sheet.py @@ -28,7 +28,7 @@ import pytz from openerp.osv import fields, osv from openerp.tools.translate import _ from openerp import netsvc -from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT +from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT class hr_timesheet_sheet(osv.osv): _name = "hr_timesheet_sheet.sheet" @@ -376,10 +376,13 @@ class hr_attendance(osv.osv): attendance_dt = datetime.strptime(attendance.name, DEFAULT_SERVER_DATETIME_FORMAT) att_tz_dt = pytz.utc.localize(attendance_dt) att_tz_dt = att_tz_dt.astimezone(att_tz) - date_from = datetime.strftime(att_tz_dt, DEFAULT_SERVER_DATETIME_FORMAT) - date_to = datetime.strftime(att_tz_dt.date(), DEFAULT_SERVER_DATETIME_FORMAT) + # 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) sheet_ids = sheet_obj.search(cursor, user, - [('date_to', '>=', date_to), ('date_from', '<=', date_from), + [('date_from', '<=', att_tz_date_str), + ('date_to', '>=', att_tz_date_str), ('employee_id', '=', attendance.employee_id.id)], context=context) if sheet_ids: From 76a0d0acf61ea042fbf49efaa5e5591e35f46744 Mon Sep 17 00:00:00 2001 From: Yannick Vaucher Date: Mon, 9 Sep 2013 12:37:49 +0200 Subject: [PATCH 4/5] [IMP] hr_timesheet_sheet - set default timezone as UTC if employee has none bzr revid: yannick.vaucher@camptocamp.com-20130909103749-sc1ia08hna4o6q8h --- addons/hr_timesheet_sheet/hr_timesheet_sheet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/hr_timesheet_sheet/hr_timesheet_sheet.py b/addons/hr_timesheet_sheet/hr_timesheet_sheet.py index 5f51e8e3df1..76e6a90d292 100644 --- a/addons/hr_timesheet_sheet/hr_timesheet_sheet.py +++ b/addons/hr_timesheet_sheet/hr_timesheet_sheet.py @@ -371,7 +371,7 @@ class hr_attendance(osv.osv): for attendance in self.browse(cursor, user, ids, context=context): # Simulate timesheet in employee timezone - att_tz = timezone(attendance.employee_id.user_id.partner_id.tz) + att_tz = timezone(attendance.employee_id.user_id.partner_id.tz or 'utc') attendance_dt = datetime.strptime(attendance.name, DEFAULT_SERVER_DATETIME_FORMAT) att_tz_dt = pytz.utc.localize(attendance_dt) From c59b97b358526563a7ad32a101ec778c35f2a891 Mon Sep 17 00:00:00 2001 From: Yannick Vaucher Date: Mon, 9 Sep 2013 12:38:19 +0200 Subject: [PATCH 5/5] [FIX] hr_timesheet_sheet - remove useless call to AT TIME ZONE in psql request here it isn't current_date but an utc 'timestamp' date. Need a strong tea even if it isn't utc tea time. bzr revid: yannick.vaucher@camptocamp.com-20130909103819-i3143f6zenrf8hi9 --- addons/hr_timesheet_sheet/hr_timesheet_sheet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/hr_timesheet_sheet/hr_timesheet_sheet.py b/addons/hr_timesheet_sheet/hr_timesheet_sheet.py index 76e6a90d292..4f91d79c041 100644 --- a/addons/hr_timesheet_sheet/hr_timesheet_sheet.py +++ b/addons/hr_timesheet_sheet/hr_timesheet_sheet.py @@ -502,7 +502,7 @@ class hr_timesheet_sheet_sheet_day(osv.osv): a.name::date as name, s.id as sheet_id, 0.0 as total_timesheet, - SUM(((EXTRACT(hour FROM a.name AT TIME ZONE 'UTC') * 60) + EXTRACT(minute FROM a.name AT TIME ZONE 'UTC')) * (CASE WHEN a.action = 'sign_in' THEN -1 ELSE 1 END)) as total_attendance + SUM(((EXTRACT(hour FROM a.name) * 60) + EXTRACT(minute FROM a.name)) * (CASE WHEN a.action = 'sign_in' THEN -1 ELSE 1 END)) as total_attendance from hr_attendance a LEFT JOIN hr_timesheet_sheet_sheet s