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.

30 lines
1.0 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 Grant Thornton Spain - Ismael Calvo <ismael.calvo@es.gt.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import api, models, _
  5. from odoo.exceptions import ValidationError
  6. from odoo.tools import config
  7. class ResPartner(models.Model):
  8. _inherit = 'res.partner'
  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)