diff options
| author | Christophe Simonis <christophe@tinyerp.com> | 2008-07-22 17:11:28 +0200 |
|---|---|---|
| committer | Christophe Simonis <christophe@tinyerp.com> | 2008-07-22 17:11:28 +0200 |
| commit | d2ff95f66505842995786872924507f077c714dd (patch) | |
| tree | d4b4c59ee9ba927a2869522cad75e1f8cd5d2df2 /addons/hr/report | |
| parent | 2dd9723b8b3d38df8c8962300ab67afffa5cdaaa (diff) | |
replace tabs with 4 spaces
bzr revid: christophe@tinyerp.com-20080722151128-f5zy75tsn8pgtndd
Diffstat (limited to 'addons/hr/report')
| -rw-r--r-- | addons/hr/report/attendance_errors.py | 86 | ||||
| -rw-r--r-- | addons/hr/report/bymonth.py | 232 | ||||
| -rw-r--r-- | addons/hr/report/timesheet.py | 272 |
3 files changed, 295 insertions, 295 deletions
diff --git a/addons/hr/report/attendance_errors.py b/addons/hr/report/attendance_errors.py index 4f6cd57ce63..a6549ce5f5c 100644 --- a/addons/hr/report/attendance_errors.py +++ b/addons/hr/report/attendance_errors.py @@ -32,53 +32,53 @@ import time from report import report_sxw class attendance_print(report_sxw.rml_parse): - def __init__(self, cr, uid, name, context): - super(attendance_print, self).__init__(cr, uid, name, context) - self.localcontext.update({ - 'time': time, - 'lst': self._lst, - 'total': self._lst_total, - }) + def __init__(self, cr, uid, name, context): + super(attendance_print, self).__init__(cr, uid, name, context) + self.localcontext.update({ + 'time': time, + 'lst': self._lst, + 'total': self._lst_total, + }) - def _sign(self, dt): - if abs(dt.days)>1: - format = '%d day'+(((abs(dt.days)>=2) and 's') or '')+' %H:%M:%S' - else: - format = '%H:%M:%S' - if dt.seconds<0: - return dt.strftime('- '+format) - else: - return dt.strftime(format) + def _sign(self, dt): + if abs(dt.days)>1: + format = '%d day'+(((abs(dt.days)>=2) and 's') or '')+' %H:%M:%S' + else: + format = '%H:%M:%S' + if dt.seconds<0: + return dt.strftime('- '+format) + else: + return dt.strftime(format) - def _lst(self, employee_id, dt_from, dt_to, max, *args): - self.cr.execute('select name as date, create_date, action, create_date-name as delay from hr_attendance where employee_id=%d and name<=%s and name>=%s and action in (%s,%s) order by name', (employee_id, dt_to, dt_from, 'sign_in', 'sign_out')) - res = self.cr.dictfetchall() - for r in res: - if r['action']=='sign_out': - r['delay'] = - r['delay'] - temp = r['delay'].seconds - r['delay'] = self._sign(r['delay']) - if abs(temp) < max*60: - r['delay2'] = r['delay'] - else: - r['delay2'] = '/' - return res + def _lst(self, employee_id, dt_from, dt_to, max, *args): + self.cr.execute('select name as date, create_date, action, create_date-name as delay from hr_attendance where employee_id=%d and name<=%s and name>=%s and action in (%s,%s) order by name', (employee_id, dt_to, dt_from, 'sign_in', 'sign_out')) + res = self.cr.dictfetchall() + for r in res: + if r['action']=='sign_out': + r['delay'] = - r['delay'] + temp = r['delay'].seconds + r['delay'] = self._sign(r['delay']) + if abs(temp) < max*60: + r['delay2'] = r['delay'] + else: + r['delay2'] = '/' + return res - def _lst_total(self, employee_id, dt_from, dt_to, max, *args): - self.cr.execute('select name as date, create_date, action, create_date-name as delay from hr_attendance where employee_id=%d and name<=%s and name>=%s and action in (%s,%s)', (employee_id, dt_to, dt_from, 'sign_in', 'sign_out')) - res = self.cr.dictfetchall() - if not res: - return ('/','/') - total = 0 - total2 = 0 - for r in res: - if r['action']=='sign_out': - r['delay'] = - r['delay'] - total += r['delay'] - if abs(r['delay'].seconds) < max*60: - total2 += r['delay'] + def _lst_total(self, employee_id, dt_from, dt_to, max, *args): + self.cr.execute('select name as date, create_date, action, create_date-name as delay from hr_attendance where employee_id=%d and name<=%s and name>=%s and action in (%s,%s)', (employee_id, dt_to, dt_from, 'sign_in', 'sign_out')) + res = self.cr.dictfetchall() + if not res: + return ('/','/') + total = 0 + total2 = 0 + for r in res: + if r['action']=='sign_out': + r['delay'] = - r['delay'] + total += r['delay'] + if abs(r['delay'].seconds) < max*60: + total2 += r['delay'] - return (self._sign(total),total2 and self._sign(total2)) + return (self._sign(total),total2 and self._sign(total2)) report_sxw.report_sxw('report.hr.timesheet.attendance.error', 'hr.employee', 'addons/hr/report/attendance_errors.rml',parser=attendance_print) diff --git a/addons/hr/report/bymonth.py b/addons/hr/report/bymonth.py index 2c3c9e3751d..52cda6820a3 100644 --- a/addons/hr/report/bymonth.py +++ b/addons/hr/report/bymonth.py @@ -41,128 +41,128 @@ one_day = DateTime.RelativeDateTime(days=1) month2name = [0,'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'] def hour2str(h): - hours = int(h) - minutes = int(round((h - hours) * 60, 0)) - return '%02dh%02d' % (hours, minutes) + hours = int(h) + minutes = int(round((h - hours) * 60, 0)) + return '%02dh%02d' % (hours, minutes) class report_custom(report_rml): - def create_xml(self, cr, uid, ids, datas, context): - service = netsvc.LocalService('object_proxy') + def create_xml(self, cr, uid, ids, datas, context): + service = netsvc.LocalService('object_proxy') - month = DateTime.DateTime(datas['form']['year'], datas['form']['month'], 1) - - user_xml = ['<month>%s</month>' % month2name[month.month], '<year>%s</year>' % month.year] - - # Public holidays - jf_sql = """select hol.date_from, hol.date_to from hr_holidays as hol, hr_holidays_status as stat - where hol.holiday_status = stat.id and stat.name = 'Public holidays' """ - cr.execute(jf_sql) - jfs = [] - jfs = [(DateTime.strptime(l['date_from'], '%Y-%m-%d %H:%M:%S'), DateTime.strptime(l['date_to'], '%Y-%m-%d %H:%M:%S')) for l in cr.dictfetchall()] - - for employee_id in ids: - emp = service.execute(cr.dbname, uid, 'hr.employee', 'read', [employee_id])[0] - stop, days_xml = False, [] - user_repr = ''' - <user> - <name>%s</name> - <regime>%s</regime> - <holiday>%s</holiday> - %%s - </user> - ''' % (toxml(emp['name']),emp['regime'],emp['holiday_max']) - today, tomor = month, month + one_day - while today.month == month.month: - #### Work hour calculation - sql = ''' - select action, att.name - from hr_employee as emp inner join hr_attendance as att - on emp.id = att.employee_id - where att.name between '%s' and '%s' and emp.id = %s - order by att.name - ''' - cr.execute(sql, (today, tomor, employee_id)) - attendences = cr.dictfetchall() - wh = 0 - if attendences and attendences[0]['action'] == 'sign_out': - attendences.insert(0, {'name': today.strftime('%Y-%m-%d %H:%M:%S'), 'action':'sign_in'}) - if attendences and attendences[-1]['action'] == 'sign_in': - attendences.append({'name' : tomor.strftime('%Y-%m-%d %H:%M:%S'), 'action':'sign_out'}) - for att in attendences: - dt = DateTime.strptime(att['name'], '%Y-%m-%d %H:%M:%S') - if att['action'] == 'sign_out': - wh += (dt - ldt).hours - ldt = dt + month = DateTime.DateTime(datas['form']['year'], datas['form']['month'], 1) + + user_xml = ['<month>%s</month>' % month2name[month.month], '<year>%s</year>' % month.year] + + # Public holidays + jf_sql = """select hol.date_from, hol.date_to from hr_holidays as hol, hr_holidays_status as stat + where hol.holiday_status = stat.id and stat.name = 'Public holidays' """ + cr.execute(jf_sql) + jfs = [] + jfs = [(DateTime.strptime(l['date_from'], '%Y-%m-%d %H:%M:%S'), DateTime.strptime(l['date_to'], '%Y-%m-%d %H:%M:%S')) for l in cr.dictfetchall()] + + for employee_id in ids: + emp = service.execute(cr.dbname, uid, 'hr.employee', 'read', [employee_id])[0] + stop, days_xml = False, [] + user_repr = ''' + <user> + <name>%s</name> + <regime>%s</regime> + <holiday>%s</holiday> + %%s + </user> + ''' % (toxml(emp['name']),emp['regime'],emp['holiday_max']) + today, tomor = month, month + one_day + while today.month == month.month: + #### Work hour calculation + sql = ''' + select action, att.name + from hr_employee as emp inner join hr_attendance as att + on emp.id = att.employee_id + where att.name between '%s' and '%s' and emp.id = %s + order by att.name + ''' + cr.execute(sql, (today, tomor, employee_id)) + attendences = cr.dictfetchall() + wh = 0 + if attendences and attendences[0]['action'] == 'sign_out': + attendences.insert(0, {'name': today.strftime('%Y-%m-%d %H:%M:%S'), 'action':'sign_in'}) + if attendences and attendences[-1]['action'] == 'sign_in': + attendences.append({'name' : tomor.strftime('%Y-%m-%d %H:%M:%S'), 'action':'sign_out'}) + for att in attendences: + dt = DateTime.strptime(att['name'], '%Y-%m-%d %H:%M:%S') + if att['action'] == 'sign_out': + wh += (dt - ldt).hours + ldt = dt - #### Theoretical workhour calculation - sql = ''' - select t.hour_from, t.hour_to - from hr_timesheet as t - inner join (hr_timesheet_group as g inner join hr_timesheet_employee_rel as rel - on rel.tgroup_id = g.id and rel.emp_id = %s) - on t.tgroup_id = g.id - where dayofweek = %s - and date_from = (select max(date_from) - from hr_timesheet inner join (hr_timesheet_employee_rel - inner join hr_timesheet_group - on hr_timesheet_group.id = hr_timesheet_employee_rel.tgroup_id - and hr_timesheet_employee_rel.emp_id = %s) - on hr_timesheet.tgroup_id = hr_timesheet_group.id - where dayofweek = %s and date_from <= '%s') - order by date_from desc - ''' - isPH = False - for jf_start, jf_end in jfs: - if jf_start <= today <= jf_end: - isPH = True - break - if isPH: - twh = 0 - else: - cr.execute(sql, (emp['id'], today.day_of_week, emp['id'], today.day_of_week, today)) - ths = cr.dictfetchall() - twh = reduce(lambda x,y:x+(DateTime.strptime(y['hour_to'], '%H:%M:%S') - DateTime.strptime(y['hour_from'], '%H:%M:%S')).hours,ths, 0) + #### Theoretical workhour calculation + sql = ''' + select t.hour_from, t.hour_to + from hr_timesheet as t + inner join (hr_timesheet_group as g inner join hr_timesheet_employee_rel as rel + on rel.tgroup_id = g.id and rel.emp_id = %s) + on t.tgroup_id = g.id + where dayofweek = %s + and date_from = (select max(date_from) + from hr_timesheet inner join (hr_timesheet_employee_rel + inner join hr_timesheet_group + on hr_timesheet_group.id = hr_timesheet_employee_rel.tgroup_id + and hr_timesheet_employee_rel.emp_id = %s) + on hr_timesheet.tgroup_id = hr_timesheet_group.id + where dayofweek = %s and date_from <= '%s') + order by date_from desc + ''' + isPH = False + for jf_start, jf_end in jfs: + if jf_start <= today <= jf_end: + isPH = True + break + if isPH: + twh = 0 + else: + cr.execute(sql, (emp['id'], today.day_of_week, emp['id'], today.day_of_week, today)) + ths = cr.dictfetchall() + twh = reduce(lambda x,y:x+(DateTime.strptime(y['hour_to'], '%H:%M:%S') - DateTime.strptime(y['hour_from'], '%H:%M:%S')).hours,ths, 0) - #### Holiday calculation - hh = 0 - sql = ''' - select hol.date_from, hol.date_to, stat.name as status - from hr_employee as emp - inner join (hr_holidays as hol left join hr_holidays_status as stat - on hol.holiday_status = stat.id) - on emp.id = hol.employee_id - where ((hol.date_from <= '%s' and hol.date_to >= '%s') - or (hol.date_from < '%s' and hol.date_to >= '%s') - or (hol.date_from > '%s' and hol.date_to < '%s')) - and emp.id = %s - order by hol.date_from - ''' - cr.execute(sql, (today, today, tomor, tomor, today, tomor, employee_id)) - holidays = cr.dictfetchall() - for hol in holidays: - df = DateTime.strptime(hol['date_from'], '%Y-%m-%d %H:%M:%S') - dt = DateTime.strptime(hol['date_to'], '%Y-%m-%d %H:%M:%S') - if (df.year, df.month, df.day) <= (today.year, today.month, today.day) <= (dt.year, dt.month, dt.day): - if (df.year, df.month, df.day) == (dt.year, dt.month, dt.day): - hh += (dt - df).hours - else: - hh = twh - - # Week xml representation - twh, wh, hh = map(hour2str, (twh, wh, hh)) - today_xml = '<day num="%s"><th>%s</th><wh>%s</wh><hh>%s</hh></day>' % ((today - month).days+1, twh, wh, hh) - days_xml.append(today_xml) - today, tomor = tomor, tomor + one_day - - user_xml.append(user_repr % '\n'.join(days_xml)) - - xml = '''<?xml version="1.0" encoding="UTF-8" ?> - <report> - %s - </report> - ''' % '\n'.join(user_xml) + #### Holiday calculation + hh = 0 + sql = ''' + select hol.date_from, hol.date_to, stat.name as status + from hr_employee as emp + inner join (hr_holidays as hol left join hr_holidays_status as stat + on hol.holiday_status = stat.id) + on emp.id = hol.employee_id + where ((hol.date_from <= '%s' and hol.date_to >= '%s') + or (hol.date_from < '%s' and hol.date_to >= '%s') + or (hol.date_from > '%s' and hol.date_to < '%s')) + and emp.id = %s + order by hol.date_from + ''' + cr.execute(sql, (today, today, tomor, tomor, today, tomor, employee_id)) + holidays = cr.dictfetchall() + for hol in holidays: + df = DateTime.strptime(hol['date_from'], '%Y-%m-%d %H:%M:%S') + dt = DateTime.strptime(hol['date_to'], '%Y-%m-%d %H:%M:%S') + if (df.year, df.month, df.day) <= (today.year, today.month, today.day) <= (dt.year, dt.month, dt.day): + if (df.year, df.month, df.day) == (dt.year, dt.month, dt.day): + hh += (dt - df).hours + else: + hh = twh + + # Week xml representation + twh, wh, hh = map(hour2str, (twh, wh, hh)) + today_xml = '<day num="%s"><th>%s</th><wh>%s</wh><hh>%s</hh></day>' % ((today - month).days+1, twh, wh, hh) + days_xml.append(today_xml) + today, tomor = tomor, tomor + one_day + + user_xml.append(user_repr % '\n'.join(days_xml)) + + xml = '''<?xml version="1.0" encoding="UTF-8" ?> + <report> + %s + </report> + ''' % '\n'.join(user_xml) - return xml + return xml report_custom('report.hr.timesheet.bymonth', 'hr.employee', '', 'addons/hr/report/bymonth.xsl') # vim:noexpandtab:tw=0 diff --git a/addons/hr/report/timesheet.py b/addons/hr/report/timesheet.py index c1c18f7f24d..ec1f93d3006 100644 --- a/addons/hr/report/timesheet.py +++ b/addons/hr/report/timesheet.py @@ -40,149 +40,149 @@ one_week = DateTime.RelativeDateTime(days=7) num2day = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] def to_hour(h): - return int(h), int(round((h - int(h)) * 60, 0)) + return int(h), int(round((h - int(h)) * 60, 0)) class report_custom(report_rml): - def create_xml(self, cr, uid, ids, datas, context): - service = netsvc.LocalService('object_proxy') + def create_xml(self, cr, uid, ids, datas, context): + service = netsvc.LocalService('object_proxy') - start_date = DateTime.strptime(datas['form']['init_date'], '%Y-%m-%d') - end_date = DateTime.strptime(datas['form']['end_date'], '%Y-%m-%d') - first_monday = start_date - DateTime.RelativeDateTime(days=start_date.day_of_week) - last_monday = end_date + DateTime.RelativeDateTime(days=7 - end_date.day_of_week) + start_date = DateTime.strptime(datas['form']['init_date'], '%Y-%m-%d') + end_date = DateTime.strptime(datas['form']['end_date'], '%Y-%m-%d') + first_monday = start_date - DateTime.RelativeDateTime(days=start_date.day_of_week) + last_monday = end_date + DateTime.RelativeDateTime(days=7 - end_date.day_of_week) - if last_monday < first_monday: - first_monday, last_monday = last_monday, first_monday + if last_monday < first_monday: + first_monday, last_monday = last_monday, first_monday - user_xml = [] - - jf_sql = """select hol.date_from, hol.date_to from hr_holidays as hol, hr_holidays_status as stat - where hol.holiday_status = stat.id and stat.name = 'Public holidays' """ - cr.execute(jf_sql) - jfs = [] - jfs = [(DateTime.strptime(l['date_from'], '%Y-%m-%d %H:%M:%S'), DateTime.strptime(l['date_to'], '%Y-%m-%d %H:%M:%S')) for l in cr.dictfetchall()] - - for employee_id in ids: - emp = service.execute(cr.dbname, uid, 'hr.employee', 'read', [employee_id], ['id', 'name'])[0] - monday, n_monday = first_monday, first_monday + one_week - stop, week_xml = False, [] - user_repr = ''' - <user> - <name>%s</name> - %%s - </user> - ''' % toxml(emp['name']) - while monday != last_monday: - #### Work hour calculation - sql = ''' - select action, att.name - from hr_employee as emp inner join hr_attendance as att - on emp.id = att.employee_id - where att.name between '%s' and '%s' and emp.id = %s - order by att.name - ''' - for idx in range(7): - cr.execute(sql, (monday, monday + DateTime.RelativeDateTime(days=idx+1), employee_id)) - attendences = cr.dictfetchall() - week_wh = {} - if attendences and attendences[0]['action'] == 'sign_out': - attendences.insert(0, {'name': monday.strftime('%Y-%m-%d %H:%M:%S'), 'action':'sign_in'}) - if attendences and attendences[-1]['action'] == 'sign_in': - attendences.append({'name' : n_monday.strftime('%Y-%m-%d %H:%M:%S'), 'action':'sign_out'}) - for att in attendences: - dt = DateTime.strptime(att['name'], '%Y-%m-%d %H:%M:%S') - if att['action'] == 'sign_out': - week_wh[ldt.day_of_week] = week_wh.get(ldt.day_of_week, 0) + (dt - ldt).hours - ldt = dt + user_xml = [] + + jf_sql = """select hol.date_from, hol.date_to from hr_holidays as hol, hr_holidays_status as stat + where hol.holiday_status = stat.id and stat.name = 'Public holidays' """ + cr.execute(jf_sql) + jfs = [] + jfs = [(DateTime.strptime(l['date_from'], '%Y-%m-%d %H:%M:%S'), DateTime.strptime(l['date_to'], '%Y-%m-%d %H:%M:%S')) for l in cr.dictfetchall()] + + for employee_id in ids: + emp = service.execute(cr.dbname, uid, 'hr.employee', 'read', [employee_id], ['id', 'name'])[0] + monday, n_monday = first_monday, first_monday + one_week + stop, week_xml = False, [] + user_repr = ''' + <user> + <name>%s</name> + %%s + </user> + ''' % toxml(emp['name']) + while monday != last_monday: + #### Work hour calculation + sql = ''' + select action, att.name + from hr_employee as emp inner join hr_attendance as att + on emp.id = att.employee_id + where att.name between '%s' and '%s' and emp.id = %s + order by att.name + ''' + for idx in range(7): + cr.execute(sql, (monday, monday + DateTime.RelativeDateTime(days=idx+1), employee_id)) + attendences = cr.dictfetchall() + week_wh = {} + if attendences and attendences[0]['action'] == 'sign_out': + attendences.insert(0, {'name': monday.strftime('%Y-%m-%d %H:%M:%S'), 'action':'sign_in'}) + if attendences and attendences[-1]['action'] == 'sign_in': + attendences.append({'name' : n_monday.strftime('%Y-%m-%d %H:%M:%S'), 'action':'sign_out'}) + for att in attendences: + dt = DateTime.strptime(att['name'], '%Y-%m-%d %H:%M:%S') + if att['action'] == 'sign_out': + week_wh[ldt.day_of_week] = week_wh.get(ldt.day_of_week, 0) + (dt - ldt).hours + ldt = dt - #### Theoretical workhour calculation - week_twh = {} - sql = ''' - select t.hour_from, t.hour_to - from hr_timesheet as t - inner join (hr_timesheet_group as g inner join hr_timesheet_employee_rel as rel - on rel.tgroup_id = g.id and rel.emp_id = %s) - on t.tgroup_id = g.id - where dayofweek = %s - and date_from = (select max(date_from) - from hr_timesheet inner join (hr_timesheet_employee_rel - inner join hr_timesheet_group - on hr_timesheet_group.id = hr_timesheet_employee_rel.tgroup_id - and hr_timesheet_employee_rel.emp_id = %s) - on hr_timesheet.tgroup_id = hr_timesheet_group.id - where dayofweek = %s and date_from <= '%s') - order by date_from desc - ''' - for idx in range(7): - day = monday + DateTime.RelativeDateTime(days=idx+1) - # Is this a public holiday ? - isPH = False - for jf_start, jf_end in jfs: - if jf_start <= day < jf_end: - isPH = True - break - if isPH: - week_twh[idx] = 0 - else: - cr.execute(sql, (emp['id'], day.day_of_week, emp['id'], day.day_of_week, day)) - dhs = cr.dictfetchall() - week_twh[idx] = reduce(lambda x,y:x+(DateTime.strptime(y['hour_to'], '%H:%M:%S') - DateTime.strptime(y['hour_from'], '%H:%M:%S')).hours,dhs, 0) + #### Theoretical workhour calculation + week_twh = {} + sql = ''' + select t.hour_from, t.hour_to + from hr_timesheet as t + inner join (hr_timesheet_group as g inner join hr_timesheet_employee_rel as rel + on rel.tgroup_id = g.id and rel.emp_id = %s) + on t.tgroup_id = g.id + where dayofweek = %s + and date_from = (select max(date_from) + from hr_timesheet inner join (hr_timesheet_employee_rel + inner join hr_timesheet_group + on hr_timesheet_group.id = hr_timesheet_employee_rel.tgroup_id + and hr_timesheet_employee_rel.emp_id = %s) + on hr_timesheet.tgroup_id = hr_timesheet_group.id + where dayofweek = %s and date_from <= '%s') + order by date_from desc + ''' + for idx in range(7): + day = monday + DateTime.RelativeDateTime(days=idx+1) + # Is this a public holiday ? + isPH = False + for jf_start, jf_end in jfs: + if jf_start <= day < jf_end: + isPH = True + break + if isPH: + week_twh[idx] = 0 + else: + cr.execute(sql, (emp['id'], day.day_of_week, emp['id'], day.day_of_week, day)) + dhs = cr.dictfetchall() + week_twh[idx] = reduce(lambda x,y:x+(DateTime.strptime(y['hour_to'], '%H:%M:%S') - DateTime.strptime(y['hour_from'], '%H:%M:%S')).hours,dhs, 0) - #### Holiday calculation - sql = ''' - select hol.date_from, hol.date_to, stat.name as status - from hr_employee as emp - inner join (hr_holidays as hol left join hr_holidays_status as stat - on hol.holiday_status = stat.id) - on emp.id = hol.employee_id - where ((hol.date_from <= '%s' and hol.date_to >= '%s') - or (hol.date_from < '%s' and hol.date_to >= '%s') - or (hol.date_from > '%s' and hol.date_to < '%s') - and stat.name != 'Public holidays') and emp.id = %s - order by hol.date_from - ''' - cr.execute(sql, (monday, monday, n_monday, n_monday, monday, n_monday, employee_id)) - holidays = cr.dictfetchall() - week_hol = {} - for hol in holidays: - df = DateTime.strptime(hol['date_from'], '%Y-%m-%d %H:%M:%S') - dt = DateTime.strptime(hol['date_to'], '%Y-%m-%d %H:%M:%S') - for idx in range(7): - day = monday + DateTime.RelativeDateTime(days=idx+1) - if (df.year, df.month, df.day) <= (day.year, day.month, day.day) <= (dt.year, dt.month, dt.day): - if (df.year, df.month, df.day) == (dt.year, dt.month, dt.day): - week_hol[idx] = {'status' : hol['status'], 'hours' : (dt - df).hours} - else: - week_hol[idx] = {'status' : hol['status'], 'hours' : week_twh[idx]} - - # Week xml representation - week_repr = ['<week>', '<weekstart>%s</weekstart>' % monday.strftime('%Y-%m-%d'), '<weekend>%s</weekend>' % n_monday.strftime('%Y-%m-%d')] - for idx in range(7): - week_repr.append('<%s>' % num2day[idx]) - week_repr.append('<theoretical>%sh%02d</theoretical>' % to_hour(week_twh[idx])) - if idx in week_wh: - week_repr.append('<workhours>%sh%02d</workhours>' % to_hour(week_wh[idx])) - if idx in week_hol and week_hol[idx]['hours']: - week_repr.append('<holidayhours type="%(status)s">%(hours)s</holidayhours>' % week_hol[idx]) - week_repr.append('</%s>' % num2day[idx]) - week_repr.append('<total>') - week_repr.append('<theoretical>%sh%02d</theoretical>' % to_hour(reduce(lambda x,y:x+y, week_twh.values(), 0))) - week_repr.append('<worked>%sh%02d</worked>' % to_hour(reduce(lambda x,y:x+y, week_wh.values(), 0))) - week_repr.append('<holiday>%sh%02d</holiday>' % to_hour(reduce(lambda x,y:x+y, [day['hours'] for day in week_hol.values()], 0))) - week_repr.append('</total>') - week_repr.append('</week>') - if len(week_repr) > 30: # 30 = minimal length of week_repr - week_xml.append('\n'.join(week_repr)) - - monday, n_monday = n_monday, n_monday + one_week - user_xml.append(user_repr % '\n'.join(week_xml)) - - xml = '''<?xml version="1.0" encoding="UTF-8" ?> - <report> - %s - </report> - ''' % '\n'.join(user_xml) - return self.post_process_xml_data(cr, uid, xml, context) + #### Holiday calculation + sql = ''' + select hol.date_from, hol.date_to, stat.name as status + from hr_employee as emp + inner join (hr_holidays as hol left join hr_holidays_status as stat + on hol.holiday_status = stat.id) + on emp.id = hol.employee_id + where ((hol.date_from <= '%s' and hol.date_to >= '%s') + or (hol.date_from < '%s' and hol.date_to >= '%s') + or (hol.date_from > '%s' and hol.date_to < '%s') + and stat.name != 'Public holidays') and emp.id = %s + order by hol.date_from + ''' + cr.execute(sql, (monday, monday, n_monday, n_monday, monday, n_monday, employee_id)) + holidays = cr.dictfetchall() + week_hol = {} + for hol in holidays: + df = DateTime.strptime(hol['date_from'], '%Y-%m-%d %H:%M:%S') + dt = DateTime.strptime(hol['date_to'], '%Y-%m-%d %H:%M:%S') + for idx in range(7): + day = monday + DateTime.RelativeDateTime(days=idx+1) + if (df.year, df.month, df.day) <= (day.year, day.month, day.day) <= (dt.year, dt.month, dt.day): + if (df.year, df.month, df.day) == (dt.year, dt.month, dt.day): + week_hol[idx] = {'status' : hol['status'], 'hours' : (dt - df).hours} + else: + week_hol[idx] = {'status' : hol['status'], 'hours' : week_twh[idx]} + + # Week xml representation + week_repr = ['<week>', '<weekstart>%s</weekstart>' % monday.strftime('%Y-%m-%d'), '<weekend>%s</weekend>' % n_monday.strftime('%Y-%m-%d')] + for idx in range(7): + week_repr.append('<%s>' % num2day[idx]) + week_repr.append('<theoretical>%sh%02d</theoretical>' % to_hour(week_twh[idx])) + if idx in week_wh: + week_repr.append('<workhours>%sh%02d</workhours>' % to_hour(week_wh[idx])) + if idx in week_hol and week_hol[idx]['hours']: + week_repr.append('<holidayhours type="%(status)s">%(hours)s</holidayhours>' % week_hol[idx]) + week_repr.append('</%s>' % num2day[idx]) + week_repr.append('<total>') + week_repr.append('<theoretical>%sh%02d</theoretical>' % to_hour(reduce(lambda x,y:x+y, week_twh.values(), 0))) + week_repr.append('<worked>%sh%02d</worked>' % to_hour(reduce(lambda x,y:x+y, week_wh.values(), 0))) + week_repr.append('<holiday>%sh%02d</holiday>' % to_hour(reduce(lambda x,y:x+y, [day['hours'] for day in week_hol.values()], 0))) + week_repr.append('</total>') + week_repr.append('</week>') + if len(week_repr) > 30: # 30 = minimal length of week_repr + week_xml.append('\n'.join(week_repr)) + + monday, n_monday = n_monday, n_monday + one_week + user_xml.append(user_repr % '\n'.join(week_xml)) + + xml = '''<?xml version="1.0" encoding="UTF-8" ?> + <report> + %s + </report> + ''' % '\n'.join(user_xml) + return self.post_process_xml_data(cr, uid, xml, context) report_custom('report.hr.timesheet.allweeks', 'hr.employee', '', 'addons/hr/report/timesheet.xsl') # vim:noexpandtab:tw=0 |
