=== modified file 'hr_attendance/hr_attendance.py' --- hr_attendance/hr_attendance.py 2012-01-31 13:36:57 +0000 +++ hr_attendance/hr_attendance.py 2012-10-25 11:21:58 +0000 @@ -20,6 +20,8 @@ ############################################################################## import time +from datetime import * +import pytz from osv import fields, osv from tools.translate import _ @@ -46,9 +48,23 @@ _description = "Attendance" def _day_compute(self, cr, uid, ids, fieldnames, args, context=None): + if context is None: + context = {} + + users_obj = self.pool.get('res.users') + user = users_obj.browse(cr, uid, uid, context=context) + utc = pytz.timezone('UTC') + + if user.context_tz: + context_tz = pytz.timezone(user.context_tz) + else: + context_tz = utc + res = dict.fromkeys(ids, '') for obj in self.browse(cr, uid, ids, context=context): - res[obj.id] = time.strftime('%Y-%m-%d', time.strptime(obj.name, '%Y-%m-%d %H:%M:%S')) + db_time = utc.localize(datetime.strptime(obj.name, "%Y-%m-%d %H:%M:%S")) + user_time = db_time.astimezone(context_tz) + res[obj.id] = datetime.strftime(user_time, "%Y-%m-%d") return res _columns = { === modified file 'hr_timesheet_sheet/hr_timesheet_sheet.py' --- hr_timesheet_sheet/hr_timesheet_sheet.py 2012-08-31 10:45:03 +0000 +++ hr_timesheet_sheet/hr_timesheet_sheet.py 2012-10-30 08:12:48 +0000 @@ -20,6 +20,8 @@ ############################################################################## import time +import pytz +import datetime from datetime import datetime, timedelta from dateutil.relativedelta import relativedelta @@ -34,6 +36,19 @@ if values is None: values = {} + + utc = pytz.timezone('UTC') + + if user: + users_obj = obj.pool.get('res.users') + current_user = users_obj.browse(cr, user, user, context=context) + else: + current_user = None + + if current_user and current_user.context_tz: + context_tz = pytz.timezone(current_user.context_tz) + else: + context_tz = utc # res6 = {id: date_current, ...} res6 = dict([(rec['id'], rec['date_current']) @@ -42,12 +57,15 @@ dom = [] for c, id in enumerate(ids): if id in res6: + start_time = context_tz.localize(datetime.strptime(res6[id] + ' 00:00:00', "%Y-%m-%d %H:%M:%S")).astimezone(utc) + end_time = context_tz.localize(datetime.strptime(res6[id] + ' 23:59:59', "%Y-%m-%d %H:%M:%S")).astimezone(utc) + if c: # skip first dom.insert(0 ,'|') dom.append('&') dom.append('&') - dom.append(('name', '>=', res6[id])) - dom.append(('name', '<=', res6[id])) + dom.append(('name', '>=', datetime.strftime(start_time, "%Y-%m-%d %H:%M:%S"))) + dom.append(('name', '<=', datetime.strftime(end_time, "%Y-%m-%d %H:%M:%S"))) dom.append(('sheet_id', '=', id)) ids2 = obj.pool.get(self._obj).search(cr, user, dom, limit=self._limit) @@ -117,6 +135,16 @@ } """ context = context or {} + + users_obj = self.pool.get('res.users') + user = users_obj.browse(cr, uid, uid, context=context) + utc = pytz.timezone('UTC') + + if user.context_tz: + context_tz = pytz.timezone(user.context_tz) + else: + context_tz = utc + attendance_obj = self.pool.get('hr.attendance') res = {} for sheet_id in ids: @@ -129,11 +157,14 @@ total_attendance = {} for attendance in [att for att in attendances if att.action in ('sign_in', 'sign_out')]: - day = attendance.name[:10] + + attendance_in_time = utc.localize(datetime.strptime(attendance.name, '%Y-%m-%d %H:%M:%S')) + attendance_in_time = attendance_in_time.astimezone(context_tz) + day = datetime.strftime(attendance_in_time, '%Y-%m-%d') + if not total_attendance.get(day, False): total_attendance[day] = timedelta(seconds=0) - attendance_in_time = datetime.strptime(attendance.name, '%Y-%m-%d %H:%M:%S') attendance_interval = timedelta(hours=attendance_in_time.hour, minutes=attendance_in_time.minute, seconds=attendance_in_time.second) @@ -663,6 +694,54 @@ 'total_attendance': fields.float('Attendance', readonly=True), 'total_difference': fields.float('Difference', readonly=True), } + + def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'): + + if not context: + context = {} + + utc = pytz.timezone('UTC') + + if uid: + users_obj = self.pool.get('res.users') + current_user = users_obj.browse(cr, uid, uid, context=context) + else: + current_user = None + + if current_user and current_user.context_tz: + context_tz = pytz.timezone(current_user.context_tz) + else: + context_tz = utc + + cr.execute("set time zone '" + context_tz.zone + "'") + try: + return super(hr_timesheet_sheet_sheet_day, self).read(cr, uid, ids, fields=fields, context=context, load=load) + finally: + cr.execute("set time zone 'localtime'") + + def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False): + + if not context: + context = {} + + utc = pytz.timezone('UTC') + + if uid: + users_obj = self.pool.get('res.users') + current_user = users_obj.browse(cr, uid, uid, context=context) + else: + current_user = None + + if current_user and current_user.context_tz: + context_tz = pytz.timezone(current_user.context_tz) + else: + context_tz = utc + + cr.execute("set time zone '" + context_tz.zone + "'") + try: + return super(hr_timesheet_sheet_sheet_day, self).search(cr, uid, args, offset=offset, limit=limit, order=order, context=context, count=count) + finally: + cr.execute("set time zone 'localtime'") def init(self, cr): cr.execute("""create or replace view hr_timesheet_sheet_sheet_day as @@ -709,7 +788,7 @@ ) union ( select -min(a.id) as id, - a.name::date as name, + (a.name at time zone 'UTC')::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 @@ -724,7 +803,7 @@ AND s.date_to >= date_trunc('day',a.name) AND s.date_from <= a.name) WHERE action in ('sign_in', 'sign_out') - group by a.name::date, s.id + group by (a.name at time zone 'UTC')::date, s.id )) AS foo GROUP BY name, sheet_id )) AS bar""")