This question has been flagged
1 Reply
3565 Views

I have an object with field state (draft and done), and field compteur, i want to increment compteur until the the field change the state to done,

The problem is when i valid the state, the field compteur become zero, and i want to stay in last value

this is my code :

def my_function(self, cr, uid, ids, field_name, arg, context):
    res = {}
    for cons in self.browse(cr, uid, ids, context=context):
        if cons.state == 'draft' :
                compteur_mtn = 0
        date_creation_repertoire= cons.date_creation_repertoire
        date_mtn= time.strftime('%Y-%m-%d')
        date_creation_repertoire = date_creation_repertoire.split('-')
        date_mtn= date_mtn.split('-')
        jours1 = 0
        jours2 = 0
        jours1 = ((int(date_mtn[0]) * 365) + (int(date_mtn[1]) * 30) + int((date_mtn[2])))
        print jours1
        jours2 = ((int(date_creation_repertoire[0]) * 365) + (int(date_creation_repertoire[1]) * 30) + int((date_creation_repertoire[2])))
        print jours2
        compteur = (jours1 - jours2)
        print compteur
        res[cons.id] = compteur
        else : 
        res[cons.id] = False
    return res



    'compteur': fields.function(my_function, type="float", string='Délai en j'),
Avatar
Discard
Best Answer

I would use a 2nd field:

'compteur_store': fields.float("not used"),

Then I would rewrite the function:

def my_function(self, cr, uid, ids, field_name, arg, context):
    res = {}
    for cons in self.browse(cr, uid, ids, context=context):
        if cons.compteur_store:
            res[cons.id] = cons.compteur_store
        else:
            original function ....

Additonally you have to write the field compteur_store when the state is set to done:

def write(self, cr, uid, ids, vals, context=None):
    if vals.get('state', '') == 'done':
        vals['compteur_store'] = self.read(cr, uid, ids[0], ['compteur'])[0]['compteur']
    res = super(my_class, self).write(cr, uid, ids, vals, context=context)
    return res

The write() function is only an example. You should use a loop over all ids. But I hope that the suggested principle is clear.

Avatar
Discard