[FIX] Hr_attendance : attendance error report corrected(ref:ach,jvo)
[odoo/odoo.git] / addons / hr_attendance / report / attendance_errors.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
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.
12 #
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.
17 #
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/>.
20 #
21 ##############################################################################
22
23
24 import time
25 from report import report_sxw
26 import pooler
27 import datetime
28
29 class attendance_print(report_sxw.rml_parse):
30     def __init__(self, cr, uid, name, context):
31         super(attendance_print, self).__init__(cr, uid, name, context)
32         self.localcontext.update({
33             'time': time,
34             'lst': self._lst,
35             'total': self._lst_total,
36             'get_employees':self._get_employees,
37         })
38
39     def _get_employees(self, emp_ids):
40         emp_obj_list = self.pool.get('hr.employee').browse(self.cr,self.uid,emp_ids)
41         return emp_obj_list    
42         
43 #    def _sign(self, dt):
44 #        if abs(dt.days) > 1:
45 #            format = '%d day'+(((abs(dt.days)>=2) and 's') or '')+' %H:%M:%S'
46 #        else:
47 #            format = '%H:%M:%S'
48 #        if dt.seconds<0:
49 #            return dt.strftime('- '+format)
50 #        else:
51 #            return dt.strftime(format)
52
53     def _lst(self, employee_id, dt_from, dt_to, max, *args):
54         self.cr.execute("select name as date, create_date, action, create_date-name as delay from hr_attendance where employee_id=%s and to_char(name,'YYYY-mm-dd')<=%s and to_char(name,'YYYY-mm-dd')>=%s and action in (%s,%s) order by name", (employee_id, dt_to, dt_from, 'sign_in', 'sign_out'))
55         res = self.cr.dictfetchall()
56         for r in res:
57             if r['action'] == 'sign_out':
58                 r['delay'] = -r['delay']
59             temp = r['delay'].seconds
60
61 #            r['delay'] = self._sign(r['delay'])
62             r['delay'] = str(r['delay']).split('.')[0]
63             if abs(temp) < max*60:
64                 r['delay2'] = r['delay']
65             else:
66                 r['delay2'] = '/'
67         return res
68
69     def _lst_total(self, employee_id, dt_from, dt_to, max, *args):
70         self.cr.execute("select name as date, create_date, action, create_date-name as delay from hr_attendance where employee_id=%s and to_char(name,'YYYY-mm-dd')<=%s and to_char(name,'YYYY-mm-dd')>=%s and action in (%s,%s) order by name", (employee_id, dt_to, dt_from, 'sign_in', 'sign_out'))
71         res = self.cr.dictfetchall()
72         if not res:
73             return ('/','/')
74         total2 = datetime.timedelta(seconds = 0, minutes = 0, hours = 0)
75         total = datetime.timedelta(seconds = 0, minutes = 0, hours = 0)
76         for r in res:
77             if r['action'] == 'sign_out':
78                 r['delay'] = -r['delay']
79             total += r['delay']
80             if abs(r['delay'].seconds) < max*60:
81                 total2 += r['delay']
82         
83         result_dict = {
84                 'total' : total and str(total).split('.')[0],
85                 'total2' : total2  and  str(total2).split('.')[0]
86                 }
87 #        return (self._sign(total),total2 and self._sign(total2))
88         return [result_dict]
89     
90 report_sxw.report_sxw('report.hr.attendance.error', 'hr.employee', 'addons/hr_attendance/report/attendance_errors.rml',parser=attendance_print, header=2)
91
92
93 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
94