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..169e63f5c60 --- /dev/null +++ b/partner_vat_unique/models/res_config_settings.py @@ -0,0 +1,12 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import models, fields + +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..ccd57f4de4e 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): @@ -17,10 +16,11 @@ def _check_vat_unique(self): 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: + if ( + not self.env["ir.config_parameter"] + .sudo() + .get_param("partner_vat_unique.partner_vat_unique", default=False) + ): continue if record.same_vat_partner_id: raise ValidationError( 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..b671877c1f2 --- /dev/null +++ b/partner_vat_unique/views/res_config_settings_views.xml @@ -0,0 +1,14 @@ + + + res.config.settings.partner.vat.unique + res.config.settings + + + + + + + + + +