This question has been flagged
1 Reply
10923 Views

Hello there,

The new api is great. I become better each day with it.

Here, I have a field to_invoice_hours who is computed an stored each time the timesheet_ids or timesheet_ids.unit_amount fields (in a task) are changed. It works well when we edit a task and we a timesheet line.

But, if the timesheet is changed directly in the account.analytic.line model, in the menu Timesheet, the field is not recomputed...

How to recompute the field to_invoice_hours no matter where is modified the account.analytic.line???

I hope you understand my question.

THanks to help.

  class Task(models.Model):
_inherit = "project.task" 

    @api.depends('timesheet_ids','timesheet_ids.unit_amount')
    def _to_invoice_hours_get(self):
        #_logger.error("_to_invoice_hours_get BEGIN")
        hours = 0
        for task in self.browse(self.ids):
            analytic_line_ids = self.env['account.analytic.line'].search([('task_id', 'in', [task.id]), ('to_invoice', '=', True)])
            for record in analytic_line_ids:
                hours = hours + record.unit_amount
         
        self.to_invoice_hours = hours

    to_invoice_hours = fields.Float(compute='_to_invoice_hours_get', string='Hours To Invoice', store=True, default=0)

Avatar
Discard
Best Answer

HI,

you should try onchange on "account.analytic.line" to recompute "to_invoice_hours". In case of compute field the computation will takes place while rendering the view which the field is defined.

Avatar
Discard
Author

Thanks for your comment. Will give it a try and come back here soon.

Pascal, I'm really interested in how you solve the issue.

To correctly update a field on another model it's best to use @api.onchange instead of @api.depends? In my case I'm trying to trigger a function when a field on another model is changed in two models on the same module.

Author

I'm not ready to answer your question yet. I will come back when I will be sure. sorry

Ok, thanks.

For now I've solved updating the second table using a cursor from api.depends. Definitely not elegant but it's working.

My advantage over you is I can't edit the second model directly (by design), so I'm not suffering the issue you had.