# -*- coding: utf-8 -*- # Copyright 2017 Grant Thornton Spain - Ismael Calvo # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from odoo import api, models, _ from odoo.exceptions import ValidationError from odoo.tools import config class ResPartner(models.Model): _inherit = 'res.partner' @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 results = self.env['res.partner'].search_count([ ('parent_id', '=', False), ('vat', '=', record.vat), ('id', '!=', record.id) ]) if results: raise ValidationError(_( "The VAT %s already exists in another " "partner.") % record.vat)