I have change the validation of odoo so that the wrong record will show as in red colour in tree view of odoo. Here is the code
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:
return self.write(cr, uid, ids, {'state': True})
if next_atts and next_atts[0].action == att.action: # next exists and is same action
return self.write(cr, uid, ids, {'state': True})
if (not prev_atts) and (not next_atts) and att.action != 'sign_in': # first attendance must be sign_in
return self.write(cr, uid, ids, {'state': True})
else:
return self.write(cr, uid, ids, {'state': False})
return True
_constraints = [
(_altern_si_so, 'Error ! Sign in (resp. Sign out) must follow Sign out (resp. Sign in)', ['action'])]
The view code is
<tree string="Employee attendances" colors="red:state == True;black:state == False">
<field name="employee_id"/>
In this case I will change the wrong record to correct action so that the color will be changed to black.
Now the problem is if the action is 1)sign in 2)sign in 3)sign in 4)sign out
In this case the 2 and 3 record shows in red color since there are two consecutive sign in for the same employee it is an error. Now I change the 2 record to sign out so the 2 and 3 record are correct since there are sign in and sign out. So the record 2 color changes to black but the 3 record still remains in red color. I want the 3 record to be black when I change the wrong. How can I achieve this?
Aucun commentaire:
Enregistrer un commentaire