From 1a7c23760cf272bffca40b207580fd64f0c008ce Mon Sep 17 00:00:00 2001 From: Franco Leyes Date: Fri, 29 Nov 2024 11:53:33 -0300 Subject: [PATCH] [IMP] partner_vat_unique: add configuration parameter for unique VAT constraint - Defined `partner_vat_unique` as a boolean configuration parameter. - Updated `res.partner` to validate VAT uniqueness based on this parameter. - Added a new section in ResConfigSettings for enabling/disabling the constraint. - Updated tests to include scenarios with the configuration parameter. --- partner_vat_unique/__manifest__.py | 5 ++++- partner_vat_unique/models/__init__.py | 1 + partner_vat_unique/models/res_config_settings.py | 13 +++++++++++++ partner_vat_unique/models/res_partner.py | 14 ++++++++------ partner_vat_unique/tests/test_vat_unique.py | 14 +++++++++++--- .../views/res_config_settings_views.xml | 16 ++++++++++++++++ 6 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 partner_vat_unique/models/res_config_settings.py create mode 100644 partner_vat_unique/views/res_config_settings_views.xml diff --git a/partner_vat_unique/__manifest__.py b/partner_vat_unique/__manifest__.py index b1110372aa1..fbd9b8dd91e 100644 --- a/partner_vat_unique/__manifest__.py +++ b/partner_vat_unique/__manifest__.py @@ -11,5 +11,8 @@ "license": "AGPL-3", "application": True, "installable": True, - "depends": ["base"], + "depends": ["base_setup"], + "data": [ + "views/res_config_settings_views.xml", + ], } diff --git a/partner_vat_unique/models/__init__.py b/partner_vat_unique/models/__init__.py index 91fed54d404..0d7a048a3c9 100644 --- a/partner_vat_unique/models/__init__.py +++ b/partner_vat_unique/models/__init__.py @@ -1 +1,2 @@ from . import res_partner +from . import res_config_settings diff --git a/partner_vat_unique/models/res_config_settings.py b/partner_vat_unique/models/res_config_settings.py new file mode 100644 index 00000000000..e29fed4b2bd --- /dev/null +++ b/partner_vat_unique/models/res_config_settings.py @@ -0,0 +1,13 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import fields, models + + +class ResConfigSettings(models.TransientModel): + _inherit = "res.config.settings" + + partner_vat_unique = fields.Boolean( + string="Unique VAT Constraint", + config_parameter="partner_vat_unique.partner_vat_unique", + help="Check this if you want to constrain VAT number to be unique.", + ) diff --git a/partner_vat_unique/models/res_partner.py b/partner_vat_unique/models/res_partner.py index 00e8cdf890d..f068fc068a6 100644 --- a/partner_vat_unique/models/res_partner.py +++ b/partner_vat_unique/models/res_partner.py @@ -4,7 +4,6 @@ from odoo import api, fields, models from odoo.exceptions import ValidationError -from odoo.tools import config class ResPartner(models.Model): @@ -14,14 +13,17 @@ class ResPartner(models.Model): @api.constrains("vat", "parent_id") def _check_vat_unique(self): + if ( + not self.env["ir.config_parameter"] + .sudo() + .get_param("partner_vat_unique.partner_vat_unique", default=False) + ): + return + for record in self: if record.parent_id or not record.vat: continue - test_condition = config["test_enable"] and not self.env.context.get( - "test_vat" - ) - if test_condition: - continue + if record.same_vat_partner_id: raise ValidationError( self.env._( diff --git a/partner_vat_unique/tests/test_vat_unique.py b/partner_vat_unique/tests/test_vat_unique.py index 447d2358ac5..f668c059aa8 100644 --- a/partner_vat_unique/tests/test_vat_unique.py +++ b/partner_vat_unique/tests/test_vat_unique.py @@ -10,15 +10,23 @@ class TestVatUnique(TransactionCase): def setUpClass(cls): super().setUpClass() cls.partner_model = cls.env["res.partner"] + cls.config_parameter = cls.env["ir.config_parameter"] cls.partner = cls.partner_model.create( {"name": "Test partner", "vat": "ESA12345674"} ) def test_duplicated_vat_creation(self): + self.config_parameter.set_param("partner_vat_unique.partner_vat_unique", True) + with self.assertRaises(ValidationError): - self.partner_model.with_context(test_vat=True).create( - {"name": "Second partner", "vat": "ESA12345674"} - ) + self.partner_model.create({"name": "Second partner", "vat": "ESA12345674"}) + + self.config_parameter.set_param("partner_vat_unique.partner_vat_unique", False) + + partner_duplicated = self.partner_model.create( + {"name": "Second partner", "vat": "ESA12345674"} + ) + self.assertEqual(partner_duplicated.vat, "ESA12345674") def test_duplicate_partner(self): partner_copied = self.partner.copy() diff --git a/partner_vat_unique/views/res_config_settings_views.xml b/partner_vat_unique/views/res_config_settings_views.xml new file mode 100644 index 00000000000..f1cd6803516 --- /dev/null +++ b/partner_vat_unique/views/res_config_settings_views.xml @@ -0,0 +1,16 @@ + + + res.config.settings.partner.vat.unique + res.config.settings + + + + + + + + + +