You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
1.1 KiB

  1. # Copyright 2017 Grant Thornton Spain - Ismael Calvo <ismael.calvo@es.gt.com>
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from odoo import _, api, fields, models
  4. from odoo.exceptions import ValidationError
  5. from odoo.tools import config
  6. class ResPartner(models.Model):
  7. _inherit = "res.partner"
  8. vat = fields.Char(copy=False)
  9. @api.constrains("vat")
  10. def _check_vat_unique(self):
  11. for record in self:
  12. if record.parent_id or not record.vat:
  13. continue
  14. test_condition = config["test_enable"] and not self.env.context.get(
  15. "test_vat"
  16. )
  17. if test_condition:
  18. continue
  19. results = self.env["res.partner"].search_count(
  20. [
  21. ("parent_id", "=", False),
  22. ("vat", "=", record.vat),
  23. ("id", "!=", record.id),
  24. ]
  25. )
  26. if results:
  27. raise ValidationError(
  28. _("The VAT %s already exists in another " "partner.") % record.vat
  29. )