79c103c79cf4660c2b0419cbcb9791c26781be80
[odoo/odoo.git] / addons / hr_attendance / wizard / print_attendance_error.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
19 #
20 ##############################################################################
21
22 import wizard
23 import time
24 import pooler
25
26 _date_form = '''<?xml version="1.0"?>
27 <form string="Select a time span">
28     <separator string="Analysis Information" colspan="4"/>
29     <field name="init_date"/>
30     <field name="end_date"/>
31     <field name="max_delay"/>
32     <label string="Bellow this delay, the error is considered to be voluntary" colspan="2"/>
33 </form>'''
34
35 _date_fields = {
36     'init_date': {'string':'Starting Date', 'type':'date', 'default':lambda *a: time.strftime('%Y-%m-%d'), 'required':True},
37     'end_date': {'string':'Ending Date', 'type':'date', 'default':lambda *a: time.strftime('%Y-%m-%d'), 'required':True},
38     'max_delay': {'string':'Max. Delay (Min)', 'type':'integer', 'default':lambda *a: 120, 'required':True},
39 }
40
41 def _check_data(self, cr, uid, data, *args):
42     date_from = data['form']['init_date']
43     date_to = data['form']['end_date']
44     emp_ids = (','.join([str(x) for x in data['ids']]))
45                   
46     cr.execute("select id from hr_attendance where employee_id in (%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" %(emp_ids, date_to, date_from, 'sign_in', 'sign_out'))
47     attendance_ids = [x[0] for x in cr.fetchall()]
48     if not attendance_ids:
49         raise wizard.except_wizard(_('No Data Available'), _('No records found for your selection!'))    
50     
51     attendance_records = pooler.get_pool(cr.dbname).get('hr.attendance').browse(cr,uid,attendance_ids)
52     emp_ids = []
53     for rec in attendance_records:
54         if rec.employee_id.id not in emp_ids:
55             emp_ids.append(rec.employee_id.id)
56     
57     data['form']['emp_ids'] = emp_ids
58     
59     return data['form']
60
61
62 class wiz_attendance(wizard.interface):
63     states = {
64         'init': {
65             'actions': [],
66             'result': {'type': 'form', 'arch':_date_form, 'fields':_date_fields, 'state':[('print','Print Attendance Report'),('end','Cancel') ]}
67         },
68         'print': {
69             'actions': [_check_data],
70             'result': {'type': 'print', 'report': 'hr.attendance.error', 'state':'end'}
71         }
72     }
73 wiz_attendance('hr.attendance.report')
74
75
76 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
77