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.many2one('hr.employee', 'Empoyee ID', readonly=True),
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))
38 employee = self.pool.get('hr.employee').browse(cr, uid, emp_id, context=context)[0].name
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))
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)
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)
65 class hr_sign_in_out(osv.osv_memory):
66 _name = 'hr.sign.in.out'
67 _description = 'Sign In Sign Out'
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),
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))
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]}
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)
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)
97 last_att = last_att[0]
98 cond = not last_att or last_att.action == 'sign_out'
100 return self.sign_in(cr, uid, data, context)
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']
105 'name': _('Sign in / Sign out'),
107 'view_mode': 'tree,form',
108 'res_model': 'hr.sign.in.out.ask',
109 'views': [(resource_id,'form')],
110 'type': 'ir.actions.act_window',
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)
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']
129 'name': _('Sign in / Sign out'),
131 'view_mode': 'tree,form',
132 'res_model': 'hr.sign.in.out',
133 'views': [(resource_id,'form')],
134 'type': 'ir.actions.act_window',
139 cond = last_att and last_att['action'] == 'sign_in'
141 return self.sign_out(cr, uid, data, context)
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']
146 'name': _('Sign in / Sign out'),
148 'view_mode': 'tree,form',
149 'res_model': 'hr.sign.in.out.ask',
150 'views': [(resource_id,'form')],
151 'type': 'ir.actions.act_window',
155 def sign_in(self, cr, uid, data, context=None):
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)
165 self.pool.get('hr.employee').attendance_action_change(cr, uid, [emp_id], 'sign_in')
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
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)
177 self.pool.get('hr.employee').attendance_action_change(cr, uid, [emp_id], 'sign_out')
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
184 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: