Files
maintenance-tools/maintenance_equipment_customer/models/res_partner.py
Stéphan Sainléger 56951f779d
Some checks failed
tests / test (pull_request) Successful in 1m9s
pre-commit / pre-commit (pull_request) Failing after 1m34s
[ADD] maintenance_equipment_customer: add customer contact link
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.
2026-11-17 17:21:11 +01:00

115 lines
4.0 KiB
Python

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