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.

39 lines
1.4 KiB

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