[merge]
[odoo/odoo.git] / addons / hr_attendance / hr_attendance.py
index 8c5648a..08323ce 100644 (file)
@@ -56,7 +56,7 @@ 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
@@ -64,18 +64,22 @@ class hr_attendance(osv.osv):
     }
 
     def _altern_si_so(self, cr, uid, ids, context=None):
-        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 (len(atts)==2 and atts[0][0] != atts[1][0] and atts[0][1] != atts[1][1])):
+        """ 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
 
@@ -110,7 +114,7 @@ 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):