diff --git a/partner_owner_company/README.rst b/partner_owner_company/README.rst new file mode 100644 index 00000000000..5b2cfeb6f90 --- /dev/null +++ b/partner_owner_company/README.rst @@ -0,0 +1,96 @@ +===================== +Partner Owner Company +===================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:8b7a808a1ac310d9bdc68b9c2666a3fc1650ee036dc4d0afe75fc9ec5c413834 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png + :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html + :alt: License: LGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fpartner--contact-lightgray.png?logo=github + :target: https://github.com/OCA/partner-contact/tree/17.0/partner_owner_company + :alt: OCA/partner-contact +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/partner-contact-17-0/partner-contact-17-0-partner_owner_company + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/partner-contact&target_branch=17.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Add owner_company_id field to contacts as soft alternative to company_id +for tracking company belonging without enforcing access security. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +After installing the module, you'll see a new "Owner Company" field on +contact forms: + +When selecting a contact in a dropdown selection field, only the +contacts owned by the current company, or global, are proposed. Contacts +owned by other companies can still be selected, by extact match of name, +reference code, email, or Tax/VAt number. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Open Source Integrators + +Contributors +------------ + +Open Source Integrators + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +.. |maintainer-dreispt| image:: https://github.com/dreispt.png?size=40px + :target: https://github.com/dreispt + :alt: dreispt + +Current `maintainer `__: + +|maintainer-dreispt| + +This module is part of the `OCA/partner-contact `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/partner_owner_company/__init__.py b/partner_owner_company/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/partner_owner_company/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/partner_owner_company/__manifest__.py b/partner_owner_company/__manifest__.py new file mode 100644 index 00000000000..d10c2cfb007 --- /dev/null +++ b/partner_owner_company/__manifest__.py @@ -0,0 +1,19 @@ +# Copyright 2026 Open Source Integrators +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +{ + "name": "Partner Owner Company", + "summary": "Add owner_company_id field to contacts as soft alternative " + "to company_id", + "version": "17.0.1.0.0", + "category": "Extra Tools", + "website": "https://github.com/OCA/partner-contact", + "author": "Open Source Integrators, Odoo Community Association (OCA)", + "license": "LGPL-3", + "maintainers": ["dreispt"], + "depends": ["contacts"], + "data": [ + "views/res_partner_view.xml", + ], + "installable": True, +} diff --git a/partner_owner_company/i18n/fr_FR.po b/partner_owner_company/i18n/fr_FR.po new file mode 100644 index 00000000000..5c3951db0a8 --- /dev/null +++ b/partner_owner_company/i18n/fr_FR.po @@ -0,0 +1,38 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * partner_owner_company +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 17.0+e\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2026-01-21 12:59+0000\n" +"PO-Revision-Date: 2026-01-21 12:59+0000\n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: partner_owner_company +#: model:ir.model.fields,help:partner_owner_company.field_res_partner__owner_company_id +#: model:ir.model.fields,help:partner_owner_company.field_res_users__owner_company_id +msgid "" +"Company that owns this contact. This is a soft alternative to company_id " +"that doesn't enforce access security but allows tracking company belonging." +msgstr "" +"Société qui possède ce contact. C'est une alternative douce à company_id " +"qui n'applique pas la sécurité d'accès mais permet de suivre l'appartenance à la société." + +#. module: partner_owner_company +#: model:ir.model,name:partner_owner_company.model_res_partner +msgid "Contact" +msgstr "Contact" + +#. module: partner_owner_company +#: model:ir.model.fields,field_description:partner_owner_company.field_res_partner__owner_company_id +#: model:ir.model.fields,field_description:partner_owner_company.field_res_users__owner_company_id +#: model_terms:ir.ui.view,arch_db:partner_owner_company.view_res_partner_filter_owner_company +msgid "Owner Company" +msgstr "Société propriétaire" diff --git a/partner_owner_company/i18n/partner_owner_company.pot b/partner_owner_company/i18n/partner_owner_company.pot new file mode 100644 index 00000000000..e92499d3376 --- /dev/null +++ b/partner_owner_company/i18n/partner_owner_company.pot @@ -0,0 +1,36 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * partner_owner_company +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 17.0+e\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2026-01-21 13:00+0000\n" +"PO-Revision-Date: 2026-01-21 13:00+0000\n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: partner_owner_company +#: model:ir.model.fields,help:partner_owner_company.field_res_partner__owner_company_id +#: model:ir.model.fields,help:partner_owner_company.field_res_users__owner_company_id +msgid "" +"Company that owns this contact. This is a soft alternative to company_id " +"that doesn't enforce access security but allows tracking company belonging." +msgstr "" + +#. module: partner_owner_company +#: model:ir.model,name:partner_owner_company.model_res_partner +msgid "Contact" +msgstr "" + +#. module: partner_owner_company +#: model:ir.model.fields,field_description:partner_owner_company.field_res_partner__owner_company_id +#: model:ir.model.fields,field_description:partner_owner_company.field_res_users__owner_company_id +#: model_terms:ir.ui.view,arch_db:partner_owner_company.view_res_partner_filter_owner_company +msgid "Owner Company" +msgstr "" diff --git a/partner_owner_company/models/__init__.py b/partner_owner_company/models/__init__.py new file mode 100644 index 00000000000..91fed54d404 --- /dev/null +++ b/partner_owner_company/models/__init__.py @@ -0,0 +1 @@ +from . import res_partner diff --git a/partner_owner_company/models/res_partner.py b/partner_owner_company/models/res_partner.py new file mode 100644 index 00000000000..913d84bc353 --- /dev/null +++ b/partner_owner_company/models/res_partner.py @@ -0,0 +1,114 @@ +# Copyright 2026 Open Source Integrators +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from odoo import api, fields, models +from odoo.osv import expression + + +class ResPartner(models.Model): + _inherit = "res.partner" + + owner_company_id = fields.Many2one( + "res.company", + string="Owner Company", + help="Company that owns this contact. This is a soft alternative to company_id " + "that doesn't enforce access security but allows tracking company belonging.", + index=True, + ) + + @api.onchange("parent_id") + def _onchange_owner_company_parent(self): + if self.parent_id: + self.owner_company_id = self.parent_id.owner_company_id + + @api.model_create_multi + def create(self, vals_list): + # Apply ownership inheritance to UI, import, and RPC creations alike. + partners = super().create(vals_list) + partners._sync_owner_company() + return partners + + def write(self, vals): + # A child cannot override the owner inherited from its parent. + result = super().write(vals) + if "parent_id" in vals or "owner_company_id" in vals: + self._sync_owner_company() + return result + + @api.model + def name_search(self, name="", args=None, operator="ilike", limit=100): + """ + Override name_search to prioritize current company contacts unless: + - Full name match + - Exact code/ref match + - Exact email match + """ + args = args or [] + # For partial matches, prioritize current company contacts + # Logic: Show contacts where: + # 1. Contact belongs to current company, OR + # 2. Contact's parent belongs to current company, OR + # 3. Contact has no owner company AND + # (no parent OR parent has no owner company) + current_company = self.env.company.id + belongs_to_company = expression.OR( + [ + [("owner_company_id", "parent_of", [current_company])], + [("parent_id.owner_company_id", "parent_of", [current_company])], + ] + ) + no_company_owner = expression.AND( + [ + [("owner_company_id", "=", False)], + expression.OR( + [ + [("parent_id", "=", False)], # Top-level contact + [ + ("parent_id.owner_company_id", "=", False) + ], # Parent has no owner + ] + ), + ] + ) + owner_args = expression.OR([belongs_to_company, no_company_owner]) + res = super().name_search( + name, expression.AND([owner_args, args]), operator, limit + ) + + # If not result, try exact matches (bypasses company filtering) + # Check if search term matches full name, exact code, or exact email + if name and not res: + exact_match_domain = [ + "|", + ("name", "=ilike", name), + "|", + ("ref", "=ilike", name), + "|", + ("email", "=ilike", name), + ("vat", "=ilike", name), + ] + res = super().name_search(name, exact_match_domain + args, operator, limit) + + return res + + def _sync_owner_company(self): + """Synchronize ownership from the root, one generation at a time.""" + roots = self + if not self.env.context.get("_partner_owner_company_propagating"): + roots = self.browse() + # commercial_partner_id stops at child companies; ownership follows + # the full parent hierarchy, including companies with a parent. + for partner in self: + root = partner + while root.parent_id: + root = root.parent_id + roots |= root + for root in roots: + children = self.with_context(active_test=False).search( + [("parent_id", "=", root.id)] + ) + if children: + # Continue downward without restarting from the root on each write. + children.with_context(_partner_owner_company_propagating=True).write( + {"owner_company_id": root.owner_company_id.id} + ) diff --git a/partner_owner_company/pyproject.toml b/partner_owner_company/pyproject.toml new file mode 100644 index 00000000000..4231d0cccb3 --- /dev/null +++ b/partner_owner_company/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/partner_owner_company/readme/CONTRIBUTORS.md b/partner_owner_company/readme/CONTRIBUTORS.md new file mode 100644 index 00000000000..b480fa88c36 --- /dev/null +++ b/partner_owner_company/readme/CONTRIBUTORS.md @@ -0,0 +1 @@ +Open Source Integrators diff --git a/partner_owner_company/readme/DESCRIPTION.md b/partner_owner_company/readme/DESCRIPTION.md new file mode 100644 index 00000000000..23c7b91f2d0 --- /dev/null +++ b/partner_owner_company/readme/DESCRIPTION.md @@ -0,0 +1 @@ +Add owner_company_id field to contacts as soft alternative to company_id for tracking company belonging without enforcing access security. diff --git a/partner_owner_company/readme/USAGE.md b/partner_owner_company/readme/USAGE.md new file mode 100644 index 00000000000..addf3e7c025 --- /dev/null +++ b/partner_owner_company/readme/USAGE.md @@ -0,0 +1,14 @@ +After installing the module, multi-company users will see a new "Owner Company" field on contact forms. + +Child contacts inherit their parent's owner company automatically, including when they are moved to another parent. +The owner company is read-only on child contacts. Change it on the top-level contact instead; changes also apply to archived descendants. Synchronization starts from the top-level contact, so all branches of its contact hierarchy use the same owner company. + +Clearing a parent's owner company clears it for its descendants. +Removing a contact's parent keeps its last inherited owner company, which can then be edited independently. + +When selecting a contact in a dropdown selection field, partial-name searches propose contacts owned by the current company or global contacts. +If no matching contact is found, contacts owned by other companies can still be selected by an exact match of their name, reference code, email, or Tax/VAT number. + +This filtering applies only to the dropdown's autocomplete suggestions (`name_search`). +The **Search More** dialog and the Contacts list do not apply this owner-company filtering: partial-name searches there can return contacts owned by other companies, subject to the usual access rights and record rules. +The owner company is not an access restriction. diff --git a/partner_owner_company/static/description/index.html b/partner_owner_company/static/description/index.html new file mode 100644 index 00000000000..2451c9fd3b2 --- /dev/null +++ b/partner_owner_company/static/description/index.html @@ -0,0 +1,434 @@ + + + + + +Partner Owner Company + + + +
+

Partner Owner Company

+ + +

Beta License: LGPL-3 OCA/partner-contact Translate me on Weblate Try me on Runboat

+

Add owner_company_id field to contacts as soft alternative to company_id +for tracking company belonging without enforcing access security.

+

Table of contents

+ +
+

Usage

+

After installing the module, you’ll see a new “Owner Company” field on +contact forms:

+

When selecting a contact in a dropdown selection field, only the +contacts owned by the current company, or global, are proposed. Contacts +owned by other companies can still be selected, by extact match of name, +reference code, email, or Tax/VAt number.

+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Open Source Integrators
  • +
+
+
+

Contributors

+

Open Source Integrators

+
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

Current maintainer:

+

dreispt

+

This module is part of the OCA/partner-contact project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/partner_owner_company/tests/__init__.py b/partner_owner_company/tests/__init__.py new file mode 100644 index 00000000000..a0e83992ba2 --- /dev/null +++ b/partner_owner_company/tests/__init__.py @@ -0,0 +1 @@ +from . import test_owner_company_sync diff --git a/partner_owner_company/tests/test_owner_company_sync.py b/partner_owner_company/tests/test_owner_company_sync.py new file mode 100644 index 00000000000..b63a85ac494 --- /dev/null +++ b/partner_owner_company/tests/test_owner_company_sync.py @@ -0,0 +1,178 @@ +# Copyright 2026 Open Source Integrators +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from odoo.tests import TransactionCase, tagged + + +@tagged("post_install", "-at_install") +class TestOwnerCompanySync(TransactionCase): + """Contacts below a parent contact follow the parent's owner company.""" + + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.company_a = cls.env.company + cls.company_b = cls.env["res.company"].create({"name": "Owner Sync B"}) + cls.parent_a = cls.env["res.partner"].create( + { + "name": "Owner Sync Parent A", + "owner_company_id": cls.company_a.id, + } + ) + cls.parent_b = cls.env["res.partner"].create( + { + "name": "Owner Sync Parent B", + "owner_company_id": cls.company_b.id, + } + ) + + def test_children_inherit_owner_on_creation(self): + """Contacts created under a parent adopt the parent's owner company, + even company contacts or contacts asking for another owner.""" + children = self.env["res.partner"].create( + [ + {"name": "Owner Sync Child", "parent_id": self.parent_a.id}, + { + "name": "Owner Sync Child Conflicting", + "parent_id": self.parent_a.id, + "owner_company_id": self.company_b.id, + }, + { + "name": "Owner Sync Child Company", + "parent_id": self.parent_a.id, + "is_company": True, + }, + ] + ) + self.assertEqual(children.mapped("owner_company_id"), self.company_a) + for child in children: + self.assertEqual(child.owner_company_id, self.company_a) + + def test_owner_changes_reach_archived_descendants(self): + """Updating a parent's owner company propagates through the whole + descendant tree, archived records included.""" + child = self.env["res.partner"].create( + { + "name": "Owner Sync Child", + "parent_id": self.parent_a.id, + "owner_company_id": self.company_a.id, + } + ) + grandchild = self.env["res.partner"].create( + { + "name": "Owner Sync Grandchild", + "parent_id": child.id, + "owner_company_id": self.company_a.id, + } + ) + grandchild.write({"active": False}) + self.assertEqual(child.owner_company_id, self.company_a) + self.assertEqual(grandchild.owner_company_id, self.company_a) + self.parent_a.write({"owner_company_id": self.company_b.id}) + self.assertEqual(child.owner_company_id, self.company_b) + self.assertEqual(grandchild.owner_company_id, self.company_b) + self.parent_a.write({"owner_company_id": False}) + self.assertFalse(child.owner_company_id) + self.assertFalse(grandchild.owner_company_id) + self.assertEqual(self.parent_b.owner_company_id, self.company_b) + + def test_reparenting_and_detaching(self): + """Reparenting moves a contact's whole subtree to the new parent's + owner company; detaching keeps the last inherited owner and turns + the contact back into an independent root.""" + child = self.env["res.partner"].create( + {"name": "Owner Sync Child", "parent_id": self.parent_a.id} + ) + grandchild = self.env["res.partner"].create( + {"name": "Owner Sync Grandchild", "parent_id": child.id} + ) + child.write( + { + "parent_id": self.parent_b.id, + "owner_company_id": self.company_a.id, + } + ) + self.assertEqual(child.owner_company_id, self.company_b) + self.assertEqual(grandchild.owner_company_id, self.company_b) + child.write({"parent_id": False}) + self.assertEqual(child.owner_company_id, self.company_b) + self.assertEqual(grandchild.owner_company_id, self.company_b) + child.write({"owner_company_id": self.company_a.id}) + self.assertEqual(child.owner_company_id, self.company_a) + self.assertEqual(grandchild.owner_company_id, self.company_a) + + def test_child_cannot_override_parent_owner(self): + """A contact with a parent always takes the parent's owner company, + no matter which owner is written on the contact itself.""" + child = self.env["res.partner"].create( + {"name": "Owner Sync Child", "parent_id": self.parent_a.id} + ) + child.write({"owner_company_id": self.company_b.id}) + self.assertEqual(child.owner_company_id, self.company_a) + self.assertEqual(self.parent_a.owner_company_id, self.company_a) + self.parent_a.write({"owner_company_id": False}) + child.write({"owner_company_id": self.company_b.id}) + self.assertFalse(child.owner_company_id) + + def test_batch_updates_preserve_each_parent_owner(self): + """Writing an owner company on several children at once resolves + each child against its own parent.""" + child_a = self.env["res.partner"].create( + {"name": "Owner Sync Child A", "parent_id": self.parent_a.id} + ) + child_b = self.env["res.partner"].create( + {"name": "Owner Sync Child B", "parent_id": self.parent_b.id} + ) + (child_a | child_b).write({"owner_company_id": False}) + self.assertEqual(child_a.owner_company_id, self.company_a) + self.assertEqual(child_b.owner_company_id, self.company_b) + + def test_dropdown_follows_reparented_child(self): + """Partial-name dropdown searches propose a reparented contact + only under its new parent's company.""" + child = self.env["res.partner"].create( + { + "name": "Ownership Synchronization Contact", + "parent_id": self.parent_a.id, + } + ) + child.write({"parent_id": self.parent_b.id}) + Partner = self.env["res.partner"].with_context( + allowed_company_ids=(self.company_a | self.company_b).ids + ) + company_a_results = Partner.with_company(self.company_a).name_search( + "Ownership Synchronization" + ) + company_b_results = Partner.with_company(self.company_b).name_search( + "Ownership Synchronization" + ) + company_a_ids = {result[0] for result in company_a_results} + company_b_ids = {result[0] for result in company_b_results} + self.assertNotIn(child.id, company_a_ids) + self.assertIn(child.id, company_b_ids) + + def test_sync_from_grandchild_uses_top_level_owner(self): + """Editing a grandchild repairs legacy ownership throughout its root's tree.""" + Partner = self.env["res.partner"] + middle = Partner.create( + {"name": "Owner Sync Middle", "parent_id": self.parent_a.id} + ) + grandchild = Partner.create( + {"name": "Owner Sync Grandchild", "parent_id": middle.id} + ) + sibling = Partner.create( + {"name": "Owner Sync Sibling", "parent_id": self.parent_a.id} + ) + sibling.write({"active": False}) + legacy_contacts = middle | grandchild | sibling + # Represent inconsistent ownership saved before synchronization existed. + self.env.flush_all() + self.env.cr.execute( + "UPDATE res_partner SET owner_company_id = %s WHERE id IN %s", + [self.company_b.id, tuple(legacy_contacts.ids)], + ) + legacy_contacts.invalidate_recordset(["owner_company_id"]) + grandchild.write({"owner_company_id": False}) + for contact in self.parent_a | legacy_contacts: + self.assertEqual(contact.owner_company_id, self.company_a) + self.assertEqual(self.parent_b.owner_company_id, self.company_b) diff --git a/partner_owner_company/views/res_partner_view.xml b/partner_owner_company/views/res_partner_view.xml new file mode 100644 index 00000000000..89d095eb1f1 --- /dev/null +++ b/partner_owner_company/views/res_partner_view.xml @@ -0,0 +1,56 @@ + + + + + + res.partner.form.owner_company + res.partner + + + + + + + + + + + res.partner.tree.owner_company + res.partner + + + + + + + + + + + res.partner.search.owner_company + res.partner + + + + + + + + + + +