diff --git a/maintenance_equipment_customer/README.md b/maintenance_equipment_customer/README.md new file mode 100644 index 0000000..046e231 --- /dev/null +++ b/maintenance_equipment_customer/README.md @@ -0,0 +1,78 @@ +# =================================== maintenance_equipment_customer + +This module links maintenance equipment to the company contact that owns it. It also +exposes the equipment's installed services on the contact, so contacts can be filtered +by service and service version. + +A single company contact is allowed per equipment. Individual contacts are rejected +because an equipment belongs to a structure, not to a person attached to that structure. + +Features: + +- **Customer contact on equipment**: a _Customer Contact_ field is available under the + equipment name and is restricted to company contacts. +- **Equipment on contacts**: a stat button on the contact form opens the equipment + linked to that contact. +- **Service-based contact filtering**: contacts can be filtered by maintenance service + and service version (e.g. contacts having an Odoo 14 instance). + +# Installation + +Use Odoo normal module installation procedure to install +`maintenance_equipment_customer`. + +This module depends on: + +- `maintenance` +- `maintenance_server_data` + +# Configuration + +No specific configuration is required. After installation, the new fields are available +on the maintenance equipment and contact forms. + +# Usage + +## Linking equipment to a customer + +1. Go to _Maintenance > Equipments_. +2. Open an equipment record. +3. Fill in the **Customer Contact** field under the equipment name. +4. Only company contacts can be selected. + +## Viewing equipment from a contact + +1. Go to _Contacts_. +2. Open a company contact. +3. Click the **Equipment** stat button to see its maintenance equipment. + +## Filtering contacts by service + +1. Go to _Contacts_. +2. In the search bar, select **Maintenance Service** and/or **Maintenance Service + Version**. +3. For example, select service _Odoo_ and version _14.0_ to find contacts with an Odoo + 14 instance. + +# Bug Tracker + +Bugs are tracked on +[our issues website](https://github.com/elabore-coop/maintenance-tools/issues). In case +of trouble, please check there if your issue has already been reported. If you spotted +it first, help us smashing it by providing a detailed and welcomed feedback. + +# Credits + +## Contributors + +- Stéphan Sainléger + +## Funders + +The development of this module has been financially supported by: + +- Elabore (https://elabore.coop) + +## Maintainer + +This module is maintained by Elabore. diff --git a/maintenance_equipment_customer/__init__.py b/maintenance_equipment_customer/__init__.py new file mode 100644 index 0000000..0650744 --- /dev/null +++ b/maintenance_equipment_customer/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/maintenance_equipment_customer/__manifest__.py b/maintenance_equipment_customer/__manifest__.py new file mode 100644 index 0000000..6231bdd --- /dev/null +++ b/maintenance_equipment_customer/__manifest__.py @@ -0,0 +1,25 @@ +# Copyright 2026 Stéphan Sainléger (Elabore) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "maintenance_equipment_customer", + "version": "18.0.1.0.0", + "author": "Elabore", + "website": "https://git.elabore.coop/elabore/maintenance-tools", + "maintainer": "Stéphan Sainléger", + "license": "AGPL-3", + "category": "Tools", + "summary": "Link maintenance equipment to customer company contacts.", + "depends": [ + "base", + "maintenance", + "maintenance_server_data", + ], + "data": [ + "views/maintenance_equipment.xml", + "views/res_partner.xml", + ], + "installable": True, + "auto_install": False, + "application": False, +} diff --git a/maintenance_equipment_customer/i18n/fr.po b/maintenance_equipment_customer/i18n/fr.po new file mode 100644 index 0000000..37f89ab --- /dev/null +++ b/maintenance_equipment_customer/i18n/fr.po @@ -0,0 +1,79 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * maintenance_equipment_customer +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 18.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2026-11-17 16:10+0000\n" +"PO-Revision-Date: 2026-11-17 16:10+0000\n" +"Last-Translator: \n" +"Language-Team: \n" +"Language: fr_FR\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: maintenance_equipment_customer +#: model:ir.model,name:maintenance_equipment_customer.model_res_partner +msgid "Contact" +msgstr "Contact" + +#. module: maintenance_equipment_customer +#: model:ir.model.fields,field_description:maintenance_equipment_customer.field_maintenance_equipment__customer_id +#: model_terms:ir.ui.view,arch_db:maintenance_equipment_customer.equipment_view_search_customer_inherit +msgid "Customer Contact" +msgstr "Contact client" + +#. module: maintenance_equipment_customer +#: model_terms:ir.ui.view,arch_db:maintenance_equipment_customer.partner_view_form_maintenance_equipment +msgid "Equipment" +msgstr "Équipement" + +#. module: maintenance_equipment_customer +#: model:ir.model,name:maintenance_equipment_customer.model_maintenance_equipment +#: model:ir.model.fields,field_description:maintenance_equipment_customer.field_res_partner__maintenance_equipment_ids +#: model:ir.model.fields,field_description:maintenance_equipment_customer.field_res_users__maintenance_equipment_ids +msgid "Maintenance Equipment" +msgstr "Équipement de maintenance" + +#. module: maintenance_equipment_customer +#: model:ir.model.fields,field_description:maintenance_equipment_customer.field_res_partner__maintenance_equipment_count +#: model:ir.model.fields,field_description:maintenance_equipment_customer.field_res_users__maintenance_equipment_count +msgid "Maintenance Equipment Count" +msgstr "Nombre d'équipements de maintenance" + +#. module: maintenance_equipment_customer +#: model_terms:ir.ui.view,arch_db:maintenance_equipment_customer.partner_view_search_maintenance_equipment +msgid "Maintenance Service" +msgstr "Service de maintenance" + +#. module: maintenance_equipment_customer +#: model_terms:ir.ui.view,arch_db:maintenance_equipment_customer.partner_view_search_maintenance_equipment +msgid "Maintenance Service Version" +msgstr "Version de service de maintenance" + +#. module: maintenance_equipment_customer +#: model:ir.model.fields,field_description:maintenance_equipment_customer.field_res_partner__maintenance_service_version_ids +#: model:ir.model.fields,field_description:maintenance_equipment_customer.field_res_users__maintenance_service_version_ids +msgid "Maintenance Service Versions" +msgstr "Versions de services de maintenance" + +#. module: maintenance_equipment_customer +#: model:ir.model.fields,field_description:maintenance_equipment_customer.field_res_partner__maintenance_service_ids +#: model:ir.model.fields,field_description:maintenance_equipment_customer.field_res_users__maintenance_service_ids +msgid "Maintenance Services" +msgstr "Services de maintenance" + +#. module: maintenance_equipment_customer +#. odoo-python +#: code:addons/maintenance_equipment_customer/models/maintenance_equipment.py:0 +msgid "The customer contact of equipment %(equipment)s must be a company." +msgstr "Le contact client de l'équipement %(equipment)s doit être une société." + +#. module: maintenance_equipment_customer +#: model_terms:ir.ui.view,arch_db:maintenance_equipment_customer.partner_view_search_maintenance_equipment +msgid "With Maintenance Equipment" +msgstr "Avec équipement de maintenance" diff --git a/maintenance_equipment_customer/models/__init__.py b/maintenance_equipment_customer/models/__init__.py new file mode 100644 index 0000000..dfb05eb --- /dev/null +++ b/maintenance_equipment_customer/models/__init__.py @@ -0,0 +1,2 @@ +from . import maintenance_equipment +from . import res_partner diff --git a/maintenance_equipment_customer/models/maintenance_equipment.py b/maintenance_equipment_customer/models/maintenance_equipment.py new file mode 100644 index 0000000..88020d4 --- /dev/null +++ b/maintenance_equipment_customer/models/maintenance_equipment.py @@ -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, + ) + ) diff --git a/maintenance_equipment_customer/models/res_partner.py b/maintenance_equipment_customer/models/res_partner.py new file mode 100644 index 0000000..6b83945 --- /dev/null +++ b/maintenance_equipment_customer/models/res_partner.py @@ -0,0 +1,114 @@ +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 diff --git a/maintenance_equipment_customer/tests/__init__.py b/maintenance_equipment_customer/tests/__init__.py new file mode 100644 index 0000000..9dde182 --- /dev/null +++ b/maintenance_equipment_customer/tests/__init__.py @@ -0,0 +1 @@ +from . import test_maintenance_equipment_customer diff --git a/maintenance_equipment_customer/tests/test_maintenance_equipment_customer.py b/maintenance_equipment_customer/tests/test_maintenance_equipment_customer.py new file mode 100644 index 0000000..ee7b12f --- /dev/null +++ b/maintenance_equipment_customer/tests/test_maintenance_equipment_customer.py @@ -0,0 +1,115 @@ +from odoo.exceptions import ValidationError +from odoo.tests.common import TransactionCase + + +class TestMaintenanceEquipmentCustomer(TransactionCase): + """Validate the equipment-to-customer link and service filtering.""" + + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.company = cls.env["res.partner"].create( + {"name": "Customer Company", "is_company": True} + ) + cls.individual = cls.env["res.partner"].create( + {"name": "Individual Customer", "is_company": False} + ) + cls.service_odoo = cls.env["service"].create({"name": "Odoo"}) + cls.version_14 = cls.env["service.version"].create( + {"service_id": cls.service_odoo.id, "name": "14.0"} + ) + cls.service_postgres = cls.env["service"].create({"name": "PostgreSQL"}) + cls.version_16 = cls.env["service.version"].create( + {"service_id": cls.service_postgres.id, "name": "16"} + ) + cls.equipment = cls.env["maintenance.equipment"].create( + {"name": "Customer Server", "customer_id": cls.company.id} + ) + cls.service_instance = cls.env["service.instance"].create( + { + "equipment_id": cls.equipment.id, + "service_id": cls.service_odoo.id, + "version_id": cls.version_14.id, + } + ) + + def test_customer_link_is_visible_from_partner(self): + self.assertEqual(self.equipment.customer_id, self.company) + self.assertIn(self.equipment, self.company.maintenance_equipment_ids) + self.assertEqual(self.company.maintenance_equipment_count, 1) + + def test_customer_must_be_a_company(self): + with self.assertRaises(ValidationError): + self.env["maintenance.equipment"].create( + {"name": "Individual Equipment", "customer_id": self.individual.id} + ) + + def test_partners_can_be_filtered_by_service_and_version(self): + partners = self.env["res.partner"].search( + [ + ("maintenance_service_ids", "in", self.service_odoo.ids), + ( + "maintenance_service_version_ids", + "in", + self.version_14.ids, + ), + ] + ) + self.assertIn(self.company, partners) + self.assertNotIn(self.individual, partners) + + def test_service_filter_excludes_other_services(self): + partners = self.env["res.partner"].search( + [("maintenance_service_ids", "in", self.service_postgres.ids)] + ) + self.assertNotIn(self.company, partners) + + def test_service_change_updates_partner_filters(self): + self.service_instance.write( + { + "service_id": self.service_postgres.id, + "version_id": self.version_16.id, + } + ) + partners = self.env["res.partner"].search( + [ + ("maintenance_service_ids", "in", self.service_postgres.ids), + ( + "maintenance_service_version_ids", + "in", + self.version_16.ids, + ), + ] + ) + self.assertIn(self.company, partners) + partners = self.env["res.partner"].search( + [ + ("maintenance_service_ids", "in", self.service_odoo.ids), + ( + "maintenance_service_version_ids", + "in", + self.version_14.ids, + ), + ] + ) + self.assertNotIn(self.company, partners) + + def test_transfer_equipment_to_another_company_updates_partners(self): + other_company = self.env["res.partner"].create( + {"name": "Other Customer Company", "is_company": True} + ) + self.equipment.write({"customer_id": other_company.id}) + self.assertFalse(self.company.maintenance_equipment_ids) + self.assertEqual(self.company.maintenance_equipment_count, 0) + self.assertIn(self.equipment, other_company.maintenance_equipment_ids) + partners = self.env["res.partner"].search( + [("maintenance_service_ids", "in", self.service_odoo.ids)] + ) + self.assertIn(other_company, partners) + self.assertNotIn(self.company, partners) + + def test_action_view_maintenance_equipment(self): + action = self.company.action_view_maintenance_equipment() + self.assertEqual(action["res_model"], "maintenance.equipment") + self.assertEqual(action["domain"], [("customer_id", "=", self.company.id)]) + self.assertEqual(action["context"], {"default_customer_id": self.company.id}) diff --git a/maintenance_equipment_customer/views/maintenance_equipment.xml b/maintenance_equipment_customer/views/maintenance_equipment.xml new file mode 100644 index 0000000..35976a3 --- /dev/null +++ b/maintenance_equipment_customer/views/maintenance_equipment.xml @@ -0,0 +1,44 @@ + + + + maintenance.equipment.form.customer + maintenance.equipment + + + + + + + + + + + + maintenance.equipment.list.customer + maintenance.equipment + + + + + + + + + + maintenance.equipment.search.customer + maintenance.equipment + + + + + + + + + + + diff --git a/maintenance_equipment_customer/views/res_partner.xml b/maintenance_equipment_customer/views/res_partner.xml new file mode 100644 index 0000000..8a00d5e --- /dev/null +++ b/maintenance_equipment_customer/views/res_partner.xml @@ -0,0 +1,51 @@ + + + + res.partner.form.maintenance.equipment + res.partner + + + + + + + + + + res.partner.search.maintenance.equipment + res.partner + + + + + + + + + + + + +