[ADD] maintenance_equipment_customer: add customer contact link
Some checks failed
tests / test (pull_request) Successful in 1m11s
pre-commit / pre-commit (pull_request) Failing after 1m35s

Store the company contact owning the equipment as a single many2one field,
restricted to company partners. Expose linked equipment and installed
services on the contact form and filters, so contacts can be searched by
service and version.
This commit is contained in:
Stéphan Sainléger
2026-11-17 17:21:11 +01:00
parent 01cf43a97e
commit 1593eb2222
12 changed files with 608 additions and 5 deletions

View File

@@ -0,0 +1,116 @@
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``,
``ilike``, ``not ilike``).
value: A service id, a text value, 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``,
``ilike``, ``not ilike``).
value: A version id, a text value, 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, a text value, a list of record ids, or ``False``.
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.
"""
supported_operators = ("=", "!=", "in", "not in", "ilike", "not ilike")
if operator not in supported_operators:
raise ValueError(
f"Unsupported operator for maintenance filtering: {operator}"
)
if value is False:
instances = self.env["service.instance"].search(
[(instance_field, "!=", False)]
)
partner_ids = instances.equipment_id.customer_id.ids
if operator in ("!=", "not in", "not ilike"):
return [("id", "in", partner_ids)]
return [("id", "not in", partner_ids)]
if operator in ("=", "in", "ilike"):
instances = self.env["service.instance"].search(
[(instance_field, operator, value)]
)
return [("id", "in", instances.equipment_id.customer_id.ids)]
instance_operator = {
"!=": "=",
"not in": "in",
"not ilike": "ilike",
}[operator]
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