Skip to content

Commit

Permalink
[IMP] partner_vat_unique: add configuration parameter for unique VAT …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
lef-adhoc committed Nov 29, 2024
1 parent 6e33608 commit 1fce377
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 9 deletions.
5 changes: 4 additions & 1 deletion partner_vat_unique/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
"license": "AGPL-3",
"application": True,
"installable": True,
"depends": ["base"],
"depends": ["base_setup"],
"data": [
"views/res_config_settings_views.xml",
],
}
1 change: 1 addition & 0 deletions partner_vat_unique/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import res_partner
from . import res_config_settings
12 changes: 12 additions & 0 deletions partner_vat_unique/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -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."
)
10 changes: 5 additions & 5 deletions partner_vat_unique/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from odoo import api, fields, models
from odoo.exceptions import ValidationError
from odoo.tools import config


class ResPartner(models.Model):
Expand All @@ -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(
Expand Down
14 changes: 11 additions & 3 deletions partner_vat_unique/tests/test_vat_unique.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 14 additions & 0 deletions partner_vat_unique/views/res_config_settings_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<odoo>
<record id="res_config_settings_view_form" model="ir.ui.view">
<field name="name">res.config.settings.partner.vat.unique</field>
<field name="model">res.config.settings</field>
<field name="inherit_id" ref="base_setup.res_config_settings_view_form" />
<field name="arch" type="xml">
<block name="contacts_setting_container" position="inside">
<setting help="Check this if you want to constrain VAT number to be unique">
<field name="partner_vat_unique"/>
</setting>
</block>
</field>
</record>
</odoo>

0 comments on commit 1fce377

Please sign in to comment.