[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.
This commit is contained in:
2
maintenance_equipment_customer/models/__init__.py
Normal file
2
maintenance_equipment_customer/models/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from . import maintenance_equipment
|
||||
from . import res_partner
|
||||
@@ -0,0 +1,33 @@
|
||||
from odoo import api, fields, models
|
||||
from odoo.exceptions import ValidationError
|
||||
|
||||
|
||||
class MaintenanceEquipment(models.Model):
|
||||
"""Link maintenance equipment to the company contact it belongs to."""
|
||||
|
||||
_inherit = "maintenance.equipment"
|
||||
|
||||
customer_id = fields.Many2one(
|
||||
"res.partner",
|
||||
string="Customer Contact",
|
||||
domain=[("is_company", "=", True)],
|
||||
index=True,
|
||||
ondelete="restrict",
|
||||
)
|
||||
|
||||
@api.constrains("customer_id")
|
||||
def _check_customer_is_company(self):
|
||||
"""Reject individual contacts: equipment belongs to a company.
|
||||
|
||||
Raises:
|
||||
ValidationError: when the linked contact is not a company.
|
||||
"""
|
||||
for equipment in self:
|
||||
if equipment.customer_id and not equipment.customer_id.is_company:
|
||||
raise ValidationError(
|
||||
self.env._(
|
||||
"The customer contact of equipment %(equipment)s "
|
||||
"must be a company.",
|
||||
equipment=equipment.display_name,
|
||||
)
|
||||
)
|
||||
116
maintenance_equipment_customer/models/res_partner.py
Normal file
116
maintenance_equipment_customer/models/res_partner.py
Normal 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
|
||||
Reference in New Issue
Block a user