from odoo import api, fields, models class ResPartner(models.Model): """Expose maintenance equipment and installed services on contacts.""" _inherit = "res.partner" maintenance_equipment_ids = fields.One2many( "maintenance.equipment", "customer_id", ) maintenance_equipment_count = fields.Integer( compute="_compute_maintenance_equipment_count", ) maintenance_service_ids = fields.Many2many( "service", string="Maintenance Services", store=False, search="_search_maintenance_service_ids", ) maintenance_service_version_ids = fields.Many2many( "service.version", string="Maintenance Service Versions", store=False, search="_search_maintenance_service_version_ids", ) @api.depends("maintenance_equipment_ids") def _compute_maintenance_equipment_count(self): """Count the equipment records linked to this contact.""" for partner in self: partner.maintenance_equipment_count = len(partner.maintenance_equipment_ids) def _search_maintenance_service_ids(self, operator, value): """Filter contacts by services installed on their equipment. Args: operator: Odoo search operator (``=``, ``!=``, ``in``, ``not in``). value: A service id or a list of service ids. Returns: list: A domain selecting matching contacts. """ return self._search_maintenance_instances(operator, value, "service_id") def _search_maintenance_service_version_ids(self, operator, value): """Filter contacts by service versions installed on their equipment. Args: operator: Odoo search operator (``=``, ``!=``, ``in``, ``not in``). value: A version id or a list of version ids. Returns: list: A domain selecting matching contacts. """ return self._search_maintenance_instances(operator, value, "version_id") def _search_maintenance_instances(self, operator, value, instance_field): """Translate a service instance filter into a contact domain. Args: operator: Odoo search operator applied to ``instance_field``. value: A record id or a list of record ids. instance_field: ``service_id`` or ``version_id`` on service instances. Returns: list: A domain selecting contacts with (or without) matching service instances. Raises: ValueError: when the operator is not supported. """ if operator not in ("=", "!=", "in", "not in"): raise ValueError( f"Unsupported operator for maintenance filtering: {operator}" ) if operator in ("=", "in"): instance_operator = "in" if isinstance(value, list | tuple) else "=" instances = self.env["service.instance"].search( [(instance_field, instance_operator, value)] ) return [ ( "id", "in", instances.equipment_id.customer_id.ids, ) ] instance_operator = "in" if operator == "not in" else "=" instances = self.env["service.instance"].search( [(instance_field, instance_operator, value)] ) return [ ( "id", "not in", instances.equipment_id.customer_id.ids, ) ] def action_view_maintenance_equipment(self): """Open the maintenance equipment list filtered on this contact. Returns: dict: window action restricted to the contact's equipment. """ self.ensure_one() action = self.env["ir.actions.act_window"]._for_xml_id( "maintenance.hr_equipment_action" ) action["domain"] = [("customer_id", "=", self.id)] action["context"] = {"default_customer_id": self.id} return action