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.

31 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
  15. not self.env.context.get('test_vat'))
  16. if test_condition:
  17. continue
  18. results = self.env['res.partner'].search_count([
  19. ('parent_id', '=', False),
  20. ('vat', '=', record.vat),
  21. ('id', '!=', record.id)
  22. ])
  23. if results:
  24. raise ValidationError(_(
  25. "The VAT %s already exists in another "
  26. "partner.") % record.vat)