[merge]
[odoo/odoo.git] / addons / hr_attendance / hr_attendance.py
index c4466dc..08323ce 100644 (file)
@@ -29,7 +29,7 @@ class hr_action_reason(osv.osv):
     _description = "Action Reason"
     _columns = {
         'name': fields.char('Reason', size=64, required=True, help='Specifies the reason for Signing In/Signing Out.'),
-        'action_type': fields.selection([('sign_in', 'Sign in'), ('sign_out', 'Sign out')], "Action's type"),
+        'action_type': fields.selection([('sign_in', 'Sign in'), ('sign_out', 'Sign out')], "Action Type"),
     }
     _defaults = {
         'action_type': 'sign_in',
@@ -38,8 +38,6 @@ class hr_action_reason(osv.osv):
 hr_action_reason()
 
 def _employee_get(obj, cr, uid, context=None):
-    if context is None:
-        context = {}
     ids = obj.pool.get('hr.employee').search(cr, uid, [('user_id', '=', uid)], context=context)
     return ids and ids[0] or False
 
@@ -48,8 +46,6 @@ class hr_attendance(osv.osv):
     _description = "Attendance"
 
     def _day_compute(self, cr, uid, ids, fieldnames, args, context=None):
-        if context is None:
-            context = {}
         res = dict.fromkeys(ids, '')
         for obj in self.browse(cr, uid, ids, context=context):
             res[obj.id] = time.strftime('%Y-%m-%d', time.strptime(obj.name, '%Y-%m-%d %H:%M:%S'))
@@ -60,30 +56,34 @@ class hr_attendance(osv.osv):
         'action': fields.selection([('sign_in', 'Sign In'), ('sign_out', 'Sign Out'), ('action','Action')], 'Action', required=True),
         '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.'),
         'employee_id': fields.many2one('hr.employee', "Employee's Name", required=True, select=True),
-        'day': fields.function(_day_compute, method=True, type='char', string='Day', store=True, select=1, size=32),
+        'day': fields.function(_day_compute, type='char', string='Day', store=True, select=1, size=32),
     }
     _defaults = {
         'name': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'), #please don't remove the lambda, if you remove it then the current time will not change
         'employee_id': _employee_get,
     }
 
-    def _altern_si_so(self, cr, uid, ids):
-        for id in ids:
-            sql = '''
-            SELECT action, name
-            FROM hr_attendance AS att
-            WHERE employee_id = (SELECT employee_id FROM hr_attendance WHERE id=%s)
-            AND action IN ('sign_in','sign_out')
-            AND name <= (SELECT name FROM hr_attendance WHERE id=%s)
-            ORDER BY name DESC
-            LIMIT 2 '''
-            cr.execute(sql,(id,id))
-            atts = cr.fetchall()
-            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])):
+    def _altern_si_so(self, cr, uid, ids, context=None):
+        """ Alternance sign_in/sign_out check.
+            Previous (if exists) must be of opposite action.
+            Next (if exists) must be of opposite action.
+        """
+        for att in self.browse(cr, uid, ids, context=context):
+            # search and browse for first previous and first next records
+            prev_att_ids = self.search(cr, uid, [('employee_id', '=', att.employee_id.id), ('name', '<', att.name), ('action', 'in', ('sign_in', 'sign_out'))], limit=1, order='name DESC')
+            next_add_ids = self.search(cr, uid, [('employee_id', '=', att.employee_id.id), ('name', '>', att.name), ('action', 'in', ('sign_in', 'sign_out'))], limit=1, order='name ASC')
+            prev_atts = self.browse(cr, uid, prev_att_ids, context=context)
+            next_atts = self.browse(cr, uid, next_add_ids, context=context)
+            # check for alternance, return False if at least one condition is not satisfied
+            if prev_atts and prev_atts[0].action == att.action: # previous exists and is same action
+                return False
+            if next_atts and next_atts[0].action == att.action: # next exists and is same action
+                return False
+            if (not prev_atts) and (not next_atts) and att.action != 'sign_in': # first attendance must be sign_in
                 return False
         return True
 
-    _constraints = [(_altern_si_so, _('Error: Sign in (resp. Sign out) must follow Sign out (resp. Sign in)'), ['action'])]
+    _constraints = [(_altern_si_so, 'Error: Sign in (resp. Sign out) must follow Sign out (resp. Sign in)', ['action'])]
     _order = 'name desc'
 
 hr_attendance()
@@ -93,8 +93,6 @@ class hr_employee(osv.osv):
     _description = "Employee"
 
     def _state(self, cr, uid, ids, name, args, context=None):
-        if context is None:
-            context = {}
         result = {}
         if not ids:
             return result
@@ -116,20 +114,16 @@ class hr_employee(osv.osv):
         return result
 
     _columns = {
-       'state': fields.function(_state, method=True, type='selection', selection=[('absent', 'Absent'), ('present', 'Present')], string='Attendance'),
+       'state': fields.function(_state, type='selection', selection=[('absent', 'Absent'), ('present', 'Present')], string='Attendance'),
     }
 
     def _action_check(self, cr, uid, emp_id, dt=False, context=None):
-        if context is None:
-            context = {}
         cr.execute('SELECT MAX(name) FROM hr_attendance WHERE employee_id=%s', (emp_id,))
         res = cr.fetchone()
         return not (res and (res[0]>=(dt or time.strftime('%Y-%m-%d %H:%M:%S'))))
 
     def attendance_action_change(self, cr, uid, ids, type='action', context=None, dt=False, *args):
         obj_attendance = self.pool.get('hr.attendance')
-        if context is None:
-            context = {}
         id = False
         warning_sign = 'sign'
         res = {}
@@ -156,4 +150,4 @@ class hr_employee(osv.osv):
 
 hr_employee()
 
-# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
\ No newline at end of file
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: