document: fix regressions at storage and node_descriptor
[odoo/odoo.git] / addons / hr_attendance / wizard / hr_attendance_sign_in_out.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 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 import time
22
23 from osv import osv, fields
24 from tools.translate import _
25
26 class hr_si_so_ask(osv.osv_memory):
27     _name = 'hr.sign.in.out.ask'
28     _description = 'Ask for Sign In Out'
29     _columns = {
30         'name': fields.char('Employees name', size=32, required=True, readonly=True),
31         'last_time': fields.datetime('Your last sign out', required=True),
32         'emp_id': fields.char('Empoyee ID', size=32, required=True, readonly=True),
33         }
34
35     def _get_empname(self, cr, uid, context=None):
36         if context is None:
37             context = {}
38         emp_id = self.pool.get('hr.employee').search(cr, uid, [('user_id', '=', uid)], context=context)
39         if emp_id:
40             employee = self.pool.get('hr.employee').browse(cr, uid, emp_id, context=context)[0].name
41             return employee
42         return ''
43
44     def _get_empid(self, cr, uid, context=None):
45         emp_id = self.pool.get('hr.employee').search(cr, uid, [('user_id', '=', uid)], context=context)
46         if emp_id:
47             return emp_id[0]
48         return False
49
50     _defaults = {
51          'name': _get_empname,
52          'emp_id': _get_empid,
53                  }
54
55     def sign_in(self, cr, uid, ids, context=None):
56         if context is None:
57             context = {}
58         data = self.read(cr, uid, ids, [], context=context)[0]
59         return self.pool.get('hr.sign.in.out').sign_in(cr, uid, data, context)
60
61     def sign_out(self, cr, uid, ids, context=None):
62         if context is None:
63             context = {}
64         data = self.read(cr, uid, ids, [], context=context)[0]
65         return self.pool.get('hr.sign.in.out').sign_out(cr, uid, data, context)
66
67 hr_si_so_ask()
68
69 class hr_sign_in_out(osv.osv_memory):
70
71     _name = 'hr.sign.in.out'
72     _description = 'Sign In Sign Out'
73
74     _columns = {
75         'name': fields.char('Employees name', size=32, required=True, readonly=True),
76         'state': fields.char('Current state', size=32, required=True, readonly=True),
77         'emp_id': fields.char('Empoyee ID', size=32, required=True, readonly=True),
78                 }
79
80     def _get_empid(self, cr, uid, context=None):
81         if context is None:
82             context = {}
83         emp_id = self.pool.get('hr.employee').search(cr, uid, [('user_id', '=', uid)], context=context)
84         if emp_id:
85             employee = self.pool.get('hr.employee').browse(cr, uid, emp_id, context=context)[0]
86             return {'name': employee.name, 'state': employee.state, 'emp_id': emp_id[0]}
87         return {}
88
89     def default_get(self, cr, uid, fields_list, context=None):
90         if context is None:
91             context = {}
92         res = super(hr_sign_in_out, self).default_get(cr, uid, fields_list, context=context)
93         res_emp = self._get_empid(cr, uid, context=context)
94         res.update(res_emp)
95         return res
96
97     def si_check(self, cr, uid, ids, context=None):
98         obj_model = self.pool.get('ir.model.data')
99         att_obj = self.pool.get('hr.attendance')
100         if context is None:
101             context = {}
102         data = self.read(cr, uid, ids, [], context=context)[0]
103         emp_id = data['emp_id']
104         att_id = att_obj.search(cr, uid, [('employee_id', '=', emp_id)], limit=1, order='name desc')
105         last_att = att_obj.browse(cr, uid, att_id, context=context)
106         if last_att:
107             last_att = last_att[0]
108         cond = not last_att or last_att.action == 'sign_out'
109         if cond:
110             return self.sign_in(cr, uid, data, context)
111         else:
112             model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','view_hr_attendance_so_ask')], context=context)
113             resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'], context=context)[0]['res_id']
114             return {
115                 'name': _('Sign in / Sign out'),
116                 'view_type': 'form',
117                 'view_mode': 'tree,form',
118                 'res_model': 'hr.sign.in.out.ask',
119                 'views': [(resource_id,'form')],
120                 'type': 'ir.actions.act_window',
121                 'target': 'new',
122             }
123
124     def so_check(self, cr, uid, ids, context=None):
125         obj_model = self.pool.get('ir.model.data')
126         att_obj = self.pool.get('hr.attendance')
127         if context is None:
128             context = {}
129         data = self.read(cr, uid, ids, [], context=context)[0]
130         emp_id = data['emp_id']
131         att_id = att_obj.search(cr, uid, [('employee_id', '=', emp_id),('action', '!=', 'action')], limit=1, order='name desc')
132         last_att = att_obj.browse(cr, uid, att_id, context=context)
133         if last_att:
134             last_att = last_att[0]
135         if not att_id and not last_att:
136             model_data_ids = obj_model.search(cr, uid, [('model','=','ir.ui.view'),('name','=','view_hr_attendance_message')], context=context)
137             resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'], context=context)[0]['res_id']
138             return {
139                 'name': _('Sign in / Sign out'),
140                 'view_type': 'form',
141                 'view_mode': 'tree,form',
142                 'res_model': 'hr.sign.in.out',
143                 'views': [(resource_id,'form')],
144                 'type': 'ir.actions.act_window',
145                 'target': 'new',
146             }
147
148         cond = last_att and last_att['action'] == 'sign_in'
149         if cond:
150             return self.sign_out(cr, uid, data, context)
151         else:
152             model_data_ids = obj_model.search(cr, uid, [('model','=','ir.ui.view'),('name','=','view_hr_attendance_si_ask')], context=context)
153             resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'], context=context)[0]['res_id']
154             return {
155                 'name': _('Sign in / Sign out'),
156                 'view_type': 'form',
157                 'view_mode': 'tree,form',
158                 'res_model': 'hr.sign.in.out.ask',
159                 'views': [(resource_id,'form')],
160                 'type': 'ir.actions.act_window',
161                 'target': 'new',
162             }
163
164     def sign_in(self, cr, uid, data, context=None):
165         if context is None:
166             context = {}
167         emp_id = data['emp_id']
168         if 'last_time' in data:
169             if data['last_time'] > time.strftime('%Y-%m-%d %H:%M:%S'):
170                 raise osv.except_osv(_('UserError'), _('The sign-out date must be in the past'))
171             self.pool.get('hr.attendance').create(cr, uid, {'name': data['last_time'], 'action': 'sign_out',
172                 'employee_id': emp_id}, context=context)
173         try:
174             success = self.pool.get('hr.employee').attendance_action_change(cr, uid, [emp_id], 'sign_in')
175         except:
176             raise osv.except_osv(_('UserError'), _('A sign-in must be right after a sign-out !'))
177         return {} # To do: Return Success message
178
179     def sign_out(self, cr, uid, data, context=None):
180         if context is None:
181             context = {}
182         emp_id = data['emp_id']
183         if 'last_time' in data:
184             if data['last_time'] > time.strftime('%Y-%m-%d %H:%M:%S'):
185                 raise osv.except_osv(_('UserError'), _('The Sign-in date must be in the past'))
186             self.pool.get('hr.attendance').create(cr, uid, {'name':data['last_time'], 'action':'sign_in',  'employee_id':emp_id}, context=context)
187         try:
188             success = self.pool.get('hr.employee').attendance_action_change(cr, uid, [emp_id], 'sign_out')
189         except:
190             raise osv.except_osv(_('UserError'), _('A sign-out must be right after a sign-in !'))
191         return {} # To do: Return Success message
192
193 hr_sign_in_out()
194
195 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: