Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
910 Zobrazení

Hello, I have this code in models:

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


class ResPartner(models.Model):
_inherit = "res.partner"

vat = fields.Char(copy=False)

@api.constrains("vat")
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:
continue
if record.same_vat_partner_id:
raise ValidationError(
_("The VAT %s already exists in another partner.") % record.vat
)

And I have this code in the test folder: 

# Copyright 2017 Grant Thornton Spain - Ismael Calvo 
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

from odoo.exceptions import ValidationError
from odoo.tests.common import SavepointCase


class TestVatUnique(SavepointCase):
@classmethod
def setUpClass(cls):
super(TestVatUnique, cls).setUpClass()
cls.partner_model = cls.env["res.partner"]
cls.partner = cls.partner_model.create(
{"name": "Test partner", "vat": "ESA12345674"}
)

def test_duplicated_vat_creation(self):
with self.assertRaises(ValidationError):
self.partner_model.with_context(test_vat=True).create(
{"name": "Second partner", "vat": "ESA12345674"}
)

def test_duplicate_partner(self):
partner_copied = self.partner.copy()
self.assertFalse(partner_copied.vat)

Basically what this module does is that it prevents the duplication of the VAT field (that the user cannot enter the same VAT in two different contacts), so far so good, but I have the following problem that I cannot solve: If before installing the module there are already two contacts with the same VAT, and I want to MERGE THEM, how can I avoid validation ONLY WHEN MERGING CONTACTS? Because when installing the module and trying to merge two contacts with the same VAT, the validation of The VAT %s already exists in another partner skips me, and I want to avoid that only when merging contacts.

Sorry for the inconvenience, could you help me? Thank you!

Odoo 13 community

Avatar
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
0
srp 24
1075
2
srp 24
2467
2
úno 24
1502
1
bře 22
2805
1
říj 20
2431