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