Skip to Content
Menu
This question has been flagged
1 Reply
551 Views

I have an inherited crm_lead model as follows:

class CrmLead(models.Model):

    _inherit = 'crm.lead'

​linked_user_ids = fields.One2many('crm.deal.value', 'lead_id', string='Consultants', index=True, tracking=True)

​total_value = fields.Monetary(string='Total Value', currency_field='company_currency', compute='_compute_total_value', store=True, tracking=True)


    @api.depends('linked_user_ids', 'linked_user_ids.value')

    def _compute_total_value(self):

        for lead in self:

            lead.total_value = sum(user.value or 0.0 for user in lead.linked_user_ids) or 0.0


​@api.constrains('total_value','expected_revenue')

​    def _check_total_value(self):

​        for lead in self:

​            if lead.total_value != lead.expected_revenue:

​                raise ValidationError("Total contribution value should be equal to the opportunity's expected revenue.")

I have the following deal_value model

class DealValue(models.Model):

    _name='crm.deal.value'

    _inherit = ['mail.thread', 'mail.activity.mixin']


​lead_id = fields.Many2one('crm.lead', string='Lead', ondelete='cascade', readonly=True)

​value = fields.Monetary('Contribution Value', currency_field = 'currency_id', tracking=True)

​currency_id = fields.Many2one('res.currency', string='Currency', readonly=True, required=True, default= lambda self: self.env.company.currency_id)

  • When I delete a deal value record and modify other deal value records associated with the lead such that total_value = expected_revenue, I get a validation error saying that these values are not equal. Why does this happen?
  • Currently, the only workaround is first setting the deal value to 0, save the changes, and then delete the deal value record.
  • The compute method works correctly.

[Odoo 18 community edition]

Avatar
Discard
Related Posts Replies Views Activity
2
Aug 25
1143
1
Jun 25
1094
0
May 25
649
4
Jul 25
4768
3
Mar 25
2006