from odoo import models, fields, api class ProductTemplate(models.Model): _inherit = "product.template" is_event_deposit = fields.Boolean( string="Event deposit", compute="_compute_is_event_deposit", inverse="_set_is_event_deposit", search="_search_is_event_deposit", ) balance_variant_id = fields.Many2one( comodel_name="product.product", string="Balance variant", compute="_compute_balance_variant_id", inverse="_set_balance_variant_id", search="_search_balance_variant_id", ) @api.depends("product_variant_ids.is_event_deposit") def _compute_is_event_deposit(self): self.is_event_deposit = False for template in self: variant_count = len(template.product_variant_ids) if variant_count == 1: template.is_event_deposit = ( template.product_variant_ids.is_event_deposit ) elif variant_count == 0: archived_variants = template.with_context( active_test=False ).product_variant_ids if len(archived_variants) == 1: template.is_event_deposit = archived_variants.is_event_deposit def _search_is_event_deposit(self, operator, value): query = self.with_context(active_test=False)._search( [("product_variant_ids.is_event_deposit", operator, value)] ) return [("id", "in", query)] def _set_is_event_deposit(self): variant_count = len(self.product_variant_ids) if variant_count == 1: self.product_variant_ids.is_event_deposit = self.is_event_deposit elif variant_count == 0: archived_variants = self.with_context(active_test=False).product_variant_ids if len(archived_variants) == 1: archived_variants.is_event_deposit = self.is_event_deposit @api.depends("product_variant_ids.balance_variant_id") def _compute_balance_variant_id(self): self.balance_variant_id = False for template in self: variant_count = len(template.product_variant_ids) if variant_count == 1: template.balance_variant_id = ( template.product_variant_ids.balance_variant_id ) elif variant_count == 0: archived_variants = template.with_context( active_test=False ).product_variant_ids if len(archived_variants) == 1: template.balance_variant_id = archived_variants.balance_variant_id def _search_balance_variant_id(self, operator, value): query = self.with_context(active_test=False)._search( [("product_variant_ids.balance_variant_id", operator, value)] ) return [("id", "in", query)] def _set_balance_variant_id(self): variant_count = len(self.product_variant_ids) if variant_count == 1: self.product_variant_ids.balance_variant_id = self.balance_variant_id elif variant_count == 0: archived_variants = self.with_context(active_test=False).product_variant_ids if len(archived_variants) == 1: archived_variants.balance_variant_id = self.balance_variant_id def _get_related_fields_variant_template(self): return super()._get_related_fields_variant_template() + [ "is_event_deposit", "balance_variant_id", ]