diff --git a/partner_rank_single/README.rst b/partner_rank_single/README.rst new file mode 100644 index 00000000000..aafcd1dc8b6 --- /dev/null +++ b/partner_rank_single/README.rst @@ -0,0 +1,98 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + +=================== +Partner Rank Single +=================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:e571755a16128c564e35adaa55e41b1c04c097f21659dc0048be298e8f761389 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/license-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fpartner--contact-lightgray.png?logo=github + :target: https://github.com/OCA/partner-contact/tree/19.0/partner_rank_single + :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-19-0/partner-contact-19-0-partner_rank_single + :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=19.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module introduces a constraint to ensure that a contact cannot +simultaneously be both a customer and a supplier. + +**Table of contents** + +.. contents:: + :local: + +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 +------- + +* Camptocamp + +Contributors +------------ + +- Ivan Todorovich +- Maksym Yankin + +Other credits +------------- + +- Camptocamp + +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-ivantodorovich| image:: https://github.com/ivantodorovich.png?size=40px + :target: https://github.com/ivantodorovich + :alt: ivantodorovich +.. |maintainer-yankinmax| image:: https://github.com/yankinmax.png?size=40px + :target: https://github.com/yankinmax + :alt: yankinmax + +Current `maintainers `__: + +|maintainer-ivantodorovich| |maintainer-yankinmax| + +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_rank_single/__init__.py b/partner_rank_single/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/partner_rank_single/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/partner_rank_single/__manifest__.py b/partner_rank_single/__manifest__.py new file mode 100644 index 00000000000..b55bf547b6e --- /dev/null +++ b/partner_rank_single/__manifest__.py @@ -0,0 +1,15 @@ +# Copyright 2025 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +{ + "name": "Partner Rank Single", + "summary": "Introduce single rank for partners.", + "version": "19.0.1.0.0", + "category": "Partner Management", + "website": "https://github.com/OCA/partner-contact", + "author": "Camptocamp, Odoo Community Association (OCA)", + "license": "AGPL-3", + "installable": True, + "maintainers": ["ivantodorovich", "yankinmax"], + "depends": ["account"], +} diff --git a/partner_rank_single/i18n/it.po b/partner_rank_single/i18n/it.po new file mode 100644 index 00000000000..8f4ced184e6 --- /dev/null +++ b/partner_rank_single/i18n/it.po @@ -0,0 +1,28 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * partner_rank_single +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 18.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2025-11-10 10:44+0000\n" +"Last-Translator: mymage \n" +"Language-Team: none\n" +"Language: it\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 5.10.4\n" + +#. module: partner_rank_single +#. odoo-python +#: code:addons/partner_rank_single/models/res_partner.py:0 +msgid "A contact cannot be both a customer and a supplier." +msgstr "Un contatto non può essere sia cliente che fornitore." + +#. module: partner_rank_single +#: model:ir.model,name:partner_rank_single.model_res_partner +msgid "Contact" +msgstr "Contatto" diff --git a/partner_rank_single/i18n/partner_rank_single.pot b/partner_rank_single/i18n/partner_rank_single.pot new file mode 100644 index 00000000000..96e3e1c7cdf --- /dev/null +++ b/partner_rank_single/i18n/partner_rank_single.pot @@ -0,0 +1,25 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * partner_rank_single +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 19.0\n" +"Report-Msgid-Bugs-To: \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_rank_single +#. odoo-python +#: code:addons/partner_rank_single/models/res_partner.py:0 +msgid "A contact cannot be both a customer and a supplier." +msgstr "" + +#. module: partner_rank_single +#: model:ir.model,name:partner_rank_single.model_res_partner +msgid "Contact" +msgstr "" diff --git a/partner_rank_single/models/__init__.py b/partner_rank_single/models/__init__.py new file mode 100644 index 00000000000..91fed54d404 --- /dev/null +++ b/partner_rank_single/models/__init__.py @@ -0,0 +1 @@ +from . import res_partner diff --git a/partner_rank_single/models/res_partner.py b/partner_rank_single/models/res_partner.py new file mode 100644 index 00000000000..f87a8ac992a --- /dev/null +++ b/partner_rank_single/models/res_partner.py @@ -0,0 +1,29 @@ +# Copyright 2025 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import api, models +from odoo.exceptions import ValidationError + + +class Contact(models.Model): + _inherit = "res.partner" + + @api.constrains("customer_rank", "supplier_rank") + def _constrains_single_rank(self): + for record in self: + if record.customer_rank > 0 and record.supplier_rank > 0: + raise ValidationError( + self.env._("A contact cannot be both a customer and a supplier.") + ) + + def _increase_rank(self, field, n=1): + # OVERRIDE: to ignore increasing the rank if the partner is already ranked + # in the opposite field + field_inverses = { + "customer_rank": "supplier_rank", + "supplier_rank": "customer_rank", + } + field_inverse = field_inverses[field] + self = self.filtered(lambda rec: not rec[field_inverse]) + res = super()._increase_rank(field, n=n) + return res diff --git a/partner_rank_single/pyproject.toml b/partner_rank_single/pyproject.toml new file mode 100644 index 00000000000..4231d0cccb3 --- /dev/null +++ b/partner_rank_single/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/partner_rank_single/readme/CONTRIBUTORS.md b/partner_rank_single/readme/CONTRIBUTORS.md new file mode 100644 index 00000000000..4820c77c838 --- /dev/null +++ b/partner_rank_single/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- Ivan Todorovich \<\> +- Maksym Yankin \<\> diff --git a/partner_rank_single/readme/CREDITS.md b/partner_rank_single/readme/CREDITS.md new file mode 100644 index 00000000000..2ddab058cf1 --- /dev/null +++ b/partner_rank_single/readme/CREDITS.md @@ -0,0 +1 @@ +- Camptocamp diff --git a/partner_rank_single/readme/DESCRIPTION.md b/partner_rank_single/readme/DESCRIPTION.md new file mode 100644 index 00000000000..76b8811a137 --- /dev/null +++ b/partner_rank_single/readme/DESCRIPTION.md @@ -0,0 +1 @@ +This module introduces a constraint to ensure that a contact cannot simultaneously be both a customer and a supplier. \ No newline at end of file diff --git a/partner_rank_single/static/description/icon.png b/partner_rank_single/static/description/icon.png new file mode 100644 index 00000000000..1dcc49c24f3 Binary files /dev/null and b/partner_rank_single/static/description/icon.png differ diff --git a/partner_rank_single/static/description/index.html b/partner_rank_single/static/description/index.html new file mode 100644 index 00000000000..ccde5f8a74b --- /dev/null +++ b/partner_rank_single/static/description/index.html @@ -0,0 +1,440 @@ + + + + + +README.rst + + + +
+ + + +Odoo Community Association + +
+

Partner Rank Single

+ +

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

+

This module introduces a constraint to ensure that a contact cannot +simultaneously be both a customer and a supplier.

+

Table of contents

+ +
+

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

+
    +
  • Camptocamp
  • +
+
+ +
+

Other credits

+
    +
  • Camptocamp
  • +
+
+
+

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 maintainers:

+

ivantodorovich yankinmax

+

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_rank_single/tests/__init__.py b/partner_rank_single/tests/__init__.py new file mode 100644 index 00000000000..7238ba75c91 --- /dev/null +++ b/partner_rank_single/tests/__init__.py @@ -0,0 +1 @@ +from . import test_partner_rank_single diff --git a/partner_rank_single/tests/test_partner_rank_single.py b/partner_rank_single/tests/test_partner_rank_single.py new file mode 100644 index 00000000000..bdb55d8a9ec --- /dev/null +++ b/partner_rank_single/tests/test_partner_rank_single.py @@ -0,0 +1,138 @@ +# Copyright 2025 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import Command, fields +from odoo.exceptions import ValidationError +from odoo.tests import tagged + +from odoo.addons.base.tests.common import BaseCommon + + +@tagged("res_partner") +class TestPartnerRankSingle(BaseCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + + cls.customer = cls.env["res.partner"].create( + { + "name": "Customer", + "is_company": True, + } + ) + cls.supplier = cls.env["res.partner"].create( + { + "name": "Supplier", + "is_company": True, + } + ) + cls.table = cls.env["product.product"].create({"name": "Table"}) + + def _create_invoice(self, move_type, date, partner_id, **kwargs): + move = self.env["account.move"].create( + { + "invoice_date": date, + "partner_id": partner_id.id, + **kwargs, + "move_type": move_type, + "date": date, + "invoice_line_ids": [ + Command.create( + { + "product_id": self.table.id, + "price_unit": 120.0, + "tax_ids": [], + **line_kwargs, + } + ) + for line_kwargs in kwargs.get("invoice_line_ids", [{}]) + ], + } + ) + return move.action_post() + + def test_00_customer_rank_single(self): + # No rank yet + self.assertFalse(self.customer.customer_rank) + self.assertFalse(self.customer.supplier_rank) + # Create an invoice as customer + self._create_invoice( + move_type="out_invoice", date=fields.Date.today(), partner_id=self.customer + ) + self.assertEqual(self.customer.customer_rank, 1, "Ranked") + self.assertFalse(self.customer.supplier_rank, "Not ranked") + # Create an invoice as supplier + self._create_invoice( + move_type="in_invoice", + date=fields.Date.today(), + partner_id=self.customer, + ) + self.assertEqual(self.customer.customer_rank, 1, "Rank unchanged") + self.assertFalse(self.customer.supplier_rank, "Not ranked") + # Create another invoice as customer + self._create_invoice( + move_type="out_invoice", + date=fields.Date.today(), + partner_id=self.customer, + ) + self.customer.invalidate_recordset() + # In Odoo 19, rank > 0 deferred to postcommit (not run in tests) + self.assertEqual(self.customer.customer_rank, 1, "Rank deferred to postcommit") + postcommit_data = self.env.cr.postcommit.data.get( + "account.res.partner.increase_rank.customer_rank", {} + ) + self.assertGreaterEqual( + postcommit_data.get(self.customer.id, 0), 1, "Rank increase scheduled" + ) + self.assertFalse(self.customer.supplier_rank, "Not ranked") + + def test_01_supplier_rank_single(self): + # No rank yet + self.assertFalse(self.supplier.customer_rank) + self.assertFalse(self.supplier.supplier_rank) + # Create an invoice as supplier + self._create_invoice( + move_type="in_invoice", + date=fields.Date.today(), + partner_id=self.supplier, + ) + self.assertEqual(self.supplier.supplier_rank, 1, "Ranked") + self.assertFalse(self.supplier.customer_rank, "Not ranked") + # Create an invoice as customer + self._create_invoice( + move_type="out_invoice", + date=fields.Date.today(), + partner_id=self.supplier, + ) + self.assertFalse(self.supplier.customer_rank, "Not ranked") + self.assertEqual(self.supplier.supplier_rank, 1, "Rank unchanged") + # Create another invoice as supplier + self._create_invoice( + move_type="in_invoice", + date=fields.Date.today(), + partner_id=self.supplier, + ) + self.supplier.invalidate_recordset() + # In Odoo 19, rank > 0 deferred to postcommit (not run in tests) + self.assertEqual(self.supplier.supplier_rank, 1, "Rank deferred to postcommit") + postcommit_data = self.env.cr.postcommit.data.get( + "account.res.partner.increase_rank.supplier_rank", {} + ) + self.assertGreaterEqual( + postcommit_data.get(self.supplier.id, 0), 1, "Rank increase scheduled" + ) + self.assertFalse(self.supplier.customer_rank, "Not ranked") + + def test_03_customer_rank_manual(self): + self.customer.customer_rank = 10 + with self.assertRaisesRegex( + ValidationError, "A contact cannot be both a customer and a supplier." + ): + self.customer.supplier_rank = 1 + + def test_04_supplier_rank_manual(self): + self.supplier.supplier_rank = 10 + with self.assertRaisesRegex( + ValidationError, "A contact cannot be both a customer and a supplier." + ): + self.supplier.customer_rank = 1