Browse Source

[IMP] partner_vat_unique: More rigorous check

Two cases are not covered:

- If the partner with duplicated VAT is inactive
- If there are specific contact record rules that hides some of the results for
  that user.

We cover both with this modification.
12.0
Pedro M. Baeza 4 years ago
parent
commit
f937e2c7e7
  1. 2
      partner_vat_unique/__manifest__.py
  2. 18
      partner_vat_unique/models/res_partner.py
  3. 8
      partner_vat_unique/tests/test_vat_unique.py

2
partner_vat_unique/__manifest__.py

@ -4,7 +4,7 @@
"name": "Partner VAT Unique", "name": "Partner VAT Unique",
"summary": "summary":
"Module to make the VAT number unique for customers and suppliers.", "Module to make the VAT number unique for customers and suppliers.",
"version": "12.0.1.0.1",
"version": "12.0.1.0.2",
"category": "Customer Relationship Management", "category": "Customer Relationship Management",
"website": "https://github.com/OCA/partner-contact", "website": "https://github.com/OCA/partner-contact",
"author": "Grant Thornton S.L.P, Odoo Community Association (OCA)", "author": "Grant Thornton S.L.P, Odoo Community Association (OCA)",

18
partner_vat_unique/models/res_partner.py

@ -1,4 +1,5 @@
# Copyright 2017 Grant Thornton Spain - Ismael Calvo <ismael.calvo@es.gt.com> # Copyright 2017 Grant Thornton Spain - Ismael Calvo <ismael.calvo@es.gt.com>
# Copyright 2020 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import _, api, fields, models from odoo import _, api, fields, models
@ -20,12 +21,19 @@ class ResPartner(models.Model):
not self.env.context.get('test_vat')) not self.env.context.get('test_vat'))
if test_condition: if test_condition:
continue continue
results = self.env['res.partner'].search_count([
if self.env['res.partner'].sudo().with_context(
active_test=False,
).search_count([
('parent_id', '=', False), ('parent_id', '=', False),
('vat', '=', record.vat), ('vat', '=', record.vat),
('id', '!=', record.id)
])
if results:
('id', '!=', record.id),
"|",
("company_id", "=", False),
("company_id", "=", record.company_id.id),
]):
raise ValidationError(_( raise ValidationError(_(
"The VAT %s already exists in another " "The VAT %s already exists in another "
"partner.") % record.vat)
"partner."
) + " " + _(
"NOTE: This partner may be archived."
) % record.vat)

8
partner_vat_unique/tests/test_vat_unique.py

@ -22,6 +22,14 @@ class TestVatUnique(SavepointCase):
'vat': 'ESA12345674' 'vat': 'ESA12345674'
}) })
def test_duplicated_vat_creation_inactive(self):
self.partner.active = False
with self.assertRaises(ValidationError):
self.env['res.partner'].with_context(test_vat=True).create({
'name': 'Second partner',
'vat': 'ESA12345674'
})
def test_duplicate_partner(self): def test_duplicate_partner(self):
partner_copied = self.partner.copy() partner_copied = self.partner.copy()
self.assertFalse(partner_copied.vat) self.assertFalse(partner_copied.vat)
Loading…
Cancel
Save