1# -*- coding: utf-8 -*-
2from odoo import api, fields, models, _
3from odoo.exceptions import ValidationError
4
5
6class AccountAccountTag(models.Model):
7    _name = 'account.account.tag'
8    _description = 'Account Tag'
9
10    name = fields.Char('Tag Name', required=True)
11    applicability = fields.Selection([('accounts', 'Accounts'), ('taxes', 'Taxes')], required=True, default='accounts')
12    color = fields.Integer('Color Index')
13    active = fields.Boolean(default=True, help="Set active to false to hide the Account Tag without removing it.")
14    tax_report_line_ids = fields.Many2many(string="Tax Report Lines", comodel_name='account.tax.report.line', relation='account_tax_report_line_tags_rel', help="The tax report lines using this tag")
15    tax_negate = fields.Boolean(string="Negate Tax Balance", help="Check this box to negate the absolute value of the balance of the lines associated with this tag in tax report computation.")
16    country_id = fields.Many2one(string="Country", comodel_name='res.country', help="Country for which this tag is available, when applied on taxes.")
17
18    @api.model
19    def _get_tax_tags(self, tag_name, country_id):
20        """ Returns all the tax tags corresponding to the tag name given in parameter
21        in the specified country.
22        """
23        escaped_tag_name = tag_name.replace('\\', '\\\\').replace('%', '\%').replace('_', '\_')
24        return self.env['account.account.tag'].search([('name', '=like', '_' + escaped_tag_name), ('country_id', '=', country_id), ('applicability', '=', 'taxes')])
25
26    @api.constrains('country_id', 'applicability')
27    def _validate_tag_country(self):
28        for record in self:
29            if record.applicability == 'taxes' and not record.country_id:
30                raise ValidationError(_("A tag defined to be used on taxes must always have a country set."))
31