1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
23 from osv import osv, fields
24 from tools.translate import _
26 class hr_si_so_ask(osv.osv_memory):
27 _name = 'hr.sign.in.out.ask'
28 _description = 'Ask for Sign In Out'
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),
35 def _get_empname(self, cr, uid, context=None):
38 emp_id = self.pool.get('hr.employee').search(cr, uid, [('user_id', '=', uid)], context=context)
40 employee = self.pool.get('hr.employee').browse(cr, uid, emp_id, context=context)[0].name
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)
55 def sign_in(self, cr, uid, ids, context=None):
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)
61 def sign_out(self, cr, uid, ids, context=None):
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)
69 class hr_sign_in_out(osv.osv_memory):
71 _name = 'hr.sign.in.out'
72 _description = 'Sign In Sign Out'
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),
80 def _get_empid(self, cr, uid, context=None):
83 emp_id = self.pool.get('hr.employee').search(cr, uid, [('user_id', '=', uid)], context=context)
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]}
89 def default_get(self, cr, uid, fields_list, context=None):
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)
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')
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)
107 last_att = last_att[0]
108 cond = not last_att or last_att.action == 'sign_out'
110 return self.sign_in(cr, uid, data, context)
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']
115 'name': _('Sign in / Sign out'),
117 'view_mode': 'tree,form',
118 'res_model': 'hr.sign.in.out.ask',
119 'views': [(resource_id,'form')],
120 'type': 'ir.actions.act_window',
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')
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)
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']
139 'name': _('Sign in / Sign out'),
141 'view_mode': 'tree,form',
142 'res_model': 'hr.sign.in.out',
143 'views': [(resource_id,'form')],
144 'type': 'ir.actions.act_window',
148 cond = last_att and last_att['action'] == 'sign_in'
150 return self.sign_out(cr, uid, data, context)
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']
155 'name': _('Sign in / Sign out'),
157 'view_mode': 'tree,form',
158 'res_model': 'hr.sign.in.out.ask',
159 'views': [(resource_id,'form')],
160 'type': 'ir.actions.act_window',
164 def sign_in(self, cr, uid, data, context=None):
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)
174 success = self.pool.get('hr.employee').attendance_action_change(cr, uid, [emp_id], 'sign_in')
176 raise osv.except_osv(_('UserError'), _('A sign-in must be right after a sign-out !'))
177 return {} # To do: Return Success message
179 def sign_out(self, cr, uid, data, context=None):
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)
188 success = self.pool.get('hr.employee').attendance_action_change(cr, uid, [emp_id], 'sign_out')
190 raise osv.except_osv(_('UserError'), _('A sign-out must be right after a sign-in !'))
191 return {} # To do: Return Success message
195 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: