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.
34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
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,
|
|
)
|
|
)
|