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 ##############################################################################
24 from osv import fields, osv
25 from tools.translate import _
27 class hr_action_reason(osv.osv):
28 _name = "hr.action.reason"
29 _description = "Action Reason"
31 'name': fields.char('Reason', size=64, required=True, help='Specifies the reason for Signing In/Signing Out.'),
32 'action_type': fields.selection([('sign_in', 'Sign in'), ('sign_out', 'Sign out')], "Action's type"),
35 'action_type': 'sign_in',
40 def _employee_get(obj, cr, uid, context=None):
43 ids = obj.pool.get('hr.employee').search(cr, uid, [('user_id', '=', uid)], context=context)
48 class hr_attendance(osv.osv):
49 _name = "hr.attendance"
50 _description = "Attendance"
52 def _day_compute(self, cr, uid, ids, fieldnames, args, context=None):
55 res = dict.fromkeys(ids, '')
56 for obj in self.browse(cr, uid, ids, context=context):
57 res[obj.id] = time.strftime('%Y-%m-%d', time.strptime(obj.name, '%Y-%m-%d %H:%M:%S'))
61 'name': fields.datetime('Date', required=True, select=1),
62 'action': fields.selection([('sign_in', 'Sign In'), ('sign_out', 'Sign Out'), ('action','Action')], 'Action', required=True),
63 'action_desc': fields.many2one("hr.action.reason", "Action Reason", domain="[('action_type', '=', action)]", help='Specifies the reason for Signing In/Signing Out in case of extra hours.'),
64 'employee_id': fields.many2one('hr.employee', "Employee's Name", required=True, select=True),
65 'day': fields.function(_day_compute, method=True, type='char', string='Day', store=True, select=1, size=32),
68 'name': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'), #Don't remove the lambda, if you remove it then the current time will not change
69 'employee_id': _employee_get,
72 def _altern_si_so(self, cr, uid, ids):
76 FROM hr_attendance AS att
77 WHERE employee_id = (SELECT employee_id FROM hr_attendance WHERE id=%s)
78 AND action IN ('sign_in','sign_out')
79 AND name <= (SELECT name FROM hr_attendance WHERE id=%s)
82 cr.execute(sql,(id,id))
84 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])):
88 _constraints = [(_altern_si_so, 'Error: Sign in (resp. Sign out) must follow Sign out (resp. Sign in)', ['action'])]
93 class hr_employee(osv.osv):
94 _inherit = "hr.employee"
95 _description = "Employee"
97 def _state(self, cr, uid, ids, name, args, context=None):
102 result[id] = 'absent'
103 cr.execute('SELECT hr_attendance.action, hr_attendance.employee_id \
105 SELECT MAX(name) AS name, employee_id \
107 WHERE action in (\'sign_in\', \'sign_out\') \
108 GROUP BY employee_id \
110 LEFT JOIN hr_attendance \
111 ON (hr_attendance.employee_id = foo.employee_id \
112 AND hr_attendance.name = foo.name) \
113 WHERE hr_attendance.employee_id IN %s',(tuple(ids),))
114 for res in cr.fetchall():
115 result[res[1]] = res[0] == 'sign_in' and 'present' or 'absent'
119 'state': fields.function(_state, method=True, type='selection', selection=[('absent', 'Absent'), ('present', 'Present')], string='Attendance'),
122 def _action_check(self, cr, uid, emp_id, dt=False, context=None):
125 cr.execute('SELECT MAX(name) FROM hr_attendance WHERE employee_id=%s', (emp_id,))
127 return not (res and (res[0]>=(dt or time.strftime('%Y-%m-%d %H:%M:%S'))))
129 def attendance_action_change(self, cr, uid, ids, type='action', context=None, dt=False, *args):
130 obj_attendance = self.pool.get('hr.attendance')
134 warning_sign = 'sign'
137 #Special case when button calls this method :type=context
138 if isinstance(type, dict):
139 type = type.get('type','action')
140 if type == 'sign_in':
141 warning_sign = "Sign In"
142 elif type == 'sign_out':
143 warning_sign = "Sign Out"
144 for emp in self.read(cr, uid, ids, ['id'], context=context):
145 if not self._action_check(cr, uid, emp['id'], dt, context):
146 raise osv.except_osv(_('Warning'), _('You tried to %s with a date anterior to another event !\nTry to contact the administrator to correct attendances.')%(warning_sign,))
148 res = {'action': type, 'employee_id': emp['id']}
151 id = obj_attendance.create(cr, uid, res, context=context)
159 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: