1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
23 from mx import DateTime
26 from osv import fields, osv
27 from tools.translate import _
29 class hr_action_reason(osv.osv):
30 _name = "hr.action.reason"
31 _description = "Action reason"
33 'name' : fields.char('Reason', size=64, required=True),
34 'action_type' : fields.selection([('sign_in', 'Sign in'), ('sign_out', 'Sign out')], "Action's type"),
37 'action_type' : lambda *a: 'sign_in',
41 def _employee_get(obj,cr,uid,context={}):
42 ids = obj.pool.get('hr.employee').search(cr, uid, [('user_id','=', uid)])
47 class hr_attendance(osv.osv):
48 _name = "hr.attendance"
49 _description = "Attendance"
51 'name' : fields.datetime('Date', required=True),
52 'action' : fields.selection([('sign_in', 'Sign In'), ('sign_out', 'Sign Out'),('action','Action')], 'Action', required=True),
53 'action_desc' : fields.many2one("hr.action.reason", "Action reason", domain="[('action_type', '=', action)]"),
54 'employee_id' : fields.many2one('hr.employee', 'Employee', required=True, select=True),
57 'name' : lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
58 'employee_id' : _employee_get,
61 def _altern_si_so(self, cr, uid, ids):
65 from hr_attendance as att
66 where employee_id = (select employee_id from hr_attendance where id=%s)
67 and action in ('sign_in','sign_out')
68 and name <= (select name from hr_attendance where id=%s)
74 if not ((len(atts)==1 and atts[0][0] == 'sign_in') or (atts[0][0] != atts[1][0] and atts[0][1] != atts[1][1])):
78 _constraints = [(_altern_si_so, 'Error: Sign in (resp. Sign out) must follow Sign out (resp. Sign in)', ['action'])]
82 class hr_employee(osv.osv):
83 _inherit = "hr.employee"
84 _description = "Employee"
86 def _state(self, cr, uid, ids, name, args, context={}):
90 cr.execute('SELECT hr_attendance.action, hr_attendance.employee_id \
92 SELECT MAX(name) AS name, employee_id \
94 WHERE action in (\'sign_in\', \'sign_out\') \
95 GROUP BY employee_id \
97 LEFT JOIN hr_attendance \
98 ON (hr_attendance.employee_id = foo.employee_id \
99 AND hr_attendance.name = foo.name) \
100 WHERE hr_attendance.employee_id \
101 in (' + ','.join([str(x) for x in ids]) + ')')
102 for res in cr.fetchall():
103 result[res[1]] = res[0] == 'sign_in' and 'present' or 'absent'
107 'state': fields.function(_state, method=True, type='selection', selection=[('absent', 'Absent'), ('present', 'Present')], string='Attendance'),
110 def sign_change(self, cr, uid, ids, context={}, dt=False):
111 for emp in self.browse(cr, uid, ids):
112 if not self._action_check(cr, uid, emp.id, dt, context):
113 raise osv.except_osv(_('Warning'), _('You tried to sign with a date anterior to another event !\nTry to contact the administrator to correct attendances.'))
114 res = {'action':'action', 'employee_id':emp.id}
117 att_id = self.pool.get('hr.attendance').create(cr, uid, res, context=context)
120 def sign_out(self, cr, uid, ids, context={}, dt=False, *args):
122 for emp in self.browse(cr, uid, ids):
123 if not self._action_check(cr, uid, emp.id, dt, context):
124 raise osv.except_osv(_('Warning'), _('You tried to sign out with a date anterior to another event !\nTry to contact the administrator to correct attendances.'))
125 res = {'action':'sign_out', 'employee_id':emp.id}
128 att_id = self.pool.get('hr.attendance').create(cr, uid, res, context=context)
132 def _action_check(self, cr, uid, emp_id, dt=False,context={}):
133 cr.execute('select max(name) from hr_attendance where employee_id=%s', (emp_id,))
135 return not (res and (res[0]>=(dt or time.strftime('%Y-%m-%d %H:%M:%S'))))
137 def sign_in(self, cr, uid, ids, context={}, dt=False, *args):
139 for emp in self.browse(cr, uid, ids):
140 if not self._action_check(cr, uid, emp.id, dt, context):
141 raise osv.except_osv(_('Warning'), _('You tried to sign in with a date anterior to another event !\nTry to contact the administrator to correct attendances.'))
142 res = {'action':'sign_in', 'employee_id':emp.id}
145 id = self.pool.get('hr.attendance').create(cr, uid, res, context=context)
150 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: