1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
23 from mx import DateTime
24 from mx.DateTime import now
30 from report.interface import report_rml
31 from report.interface import toxml
33 one_day = DateTime.RelativeDateTime(days=1)
34 month2name = [0,'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
38 minutes = int(round((h - hours) * 60, 0))
39 return '%02dh%02d' % (hours, minutes)
41 class report_custom(report_rml):
42 def create_xml(self, cr, uid, ids, datas, context):
43 service = netsvc.LocalService('object_proxy')
45 month = DateTime.DateTime(datas['form']['year'], datas['form']['month'], 1)
47 user_xml = ['<month>%s</month>' % month2name[month.month], '<year>%s</year>' % month.year]
49 for employee_id in ids:
50 emp = service.execute(cr.dbname, uid, 'hr.employee', 'read', [employee_id])[0]
51 stop, days_xml = False, []
57 ''' % (toxml(emp['name']))
58 today, tomor = month, month + one_day
59 while today.month == month.month:
60 #### Work hour calculation
62 select action, att.name
63 from hr_employee as emp inner join hr_attendance as att
64 on emp.id = att.employee_id
65 where att.name between %s and %s and emp.id = %s
68 cr.execute(sql, (today.strftime('%Y-%m-%d %H:%M:%S'), tomor.strftime('%Y-%m-%d %H:%M:%S'), employee_id))
69 attendences = cr.dictfetchall()
71 # Fake sign ins/outs at week ends, to take attendances across week ends into account
72 if attendences and attendences[0]['action'] == 'sign_out':
73 attendences.insert(0, {'name': today.strftime('%Y-%m-%d %H:%M:%S'), 'action':'sign_in'})
74 if attendences and attendences[-1]['action'] == 'sign_in':
75 attendences.append({'name' : tomor.strftime('%Y-%m-%d %H:%M:%S'), 'action':'sign_out'})
76 # sum up the attendances' durations
77 for att in attendences:
78 dt = DateTime.strptime(att['name'], '%Y-%m-%d %H:%M:%S')
79 if att['action'] == 'sign_out':
80 wh += (dt - ldt).hours
83 # Week xml representation
85 today_xml = '<day num="%s"><wh>%s</wh></day>' % ((today - month).days+1, wh)
86 days_xml.append(today_xml)
87 today, tomor = tomor, tomor + one_day
89 user_xml.append(user_repr % '\n'.join(days_xml))
91 xml = '''<?xml version="1.0" encoding="UTF-8" ?>
95 ''' % '\n'.join(user_xml)
99 report_custom('report.hr.attendance.bymonth', 'hr.employee', '', 'addons/hr_attendance/report/bymonth.xsl')
101 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: