<commit_before># Copyright (C) 2020  <NAME> - Akretion <<EMAIL>>
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError

from erpbrasil.base import fiscal


class DocumentRelated(models.Model):
    _name = 'l10n_br_fiscal.document.related'
    _description = 'Fiscal Document Related'

    fiscal_document_id = fields.Many2one(
        comodel_name='l10n_br_fiscal.document',
        string='Fiscal Document',
        index=True)

    document_related_id = fields.Many2one(
        comodel_name='l10n_br_fiscal.document',
        string='Fiscal Document Related',
        index=True)

    document_type_id = fields.Many2one(
        comodel_name='l10n_br_fiscal.document.type',
        string='Fiscal Document Type')

    document_type_code = fields.Char(
        related='document_type_id.code')

    document_key = fields.Char(
        string='Document Key',
        size=44)

    serie = fields.Char(
        string='Serie',
        size=12)

    number = fields.Char(
        string='Number',
        size=32)

    state_id = fields.Many2one(
        comodel_name='res.country.state',
        string='State',
        domain=[('country_id.code', '=', 'BR')])

    cnpj_cpf = fields.Char(
        string='CNPJ/CPF',
        size=18)

    cpfcnpj_type = fields.Selection(
        selection=[
            ('cpf', 'CPF'),
            ('cnpj', 'CNPJ')],
        string='Type Doc Number',
        default='cnpj')

    inscr_est = fields.Char(
        string='Inscr. Estadual/RG',
        size=16)

    date = fields.Date(
        string='Data')

    @api.multi
    @api.constrains('cnpj_cpf')
    def _check_cnpj_cpf(self):
        result = True
        for record in self:
            disable_cnpj_ie_validation = record.env[
                'ir.config_parameter'].sudo().get_param(
                    'l10n_br_base.disable_cpf_cnpj_validation',
                    default=False)

            if not disable_cnpj_ie_validation:
                if record.cpfcnpj_type == 'cnpj':
                    if not fiscal.cnpj_cpf.validar(record.cnpj_cpf):
                        result = False
                        document = "CNPJ"
                elif record.cpfcnpj_type == 'cpf':
                    result = False
                    document = "CPF"

                if not result:
                    raise ValidationError(_("{} Invalid!").format(document))

    @api.multi
    @api.constrains('inscr_est', 'state_id')
    def _check_ie(self):<commit_msg>[FIX] Just try to validate cnpj on related documents if filled<commit_after># Copyright (C) 2020  <NAME> - Akretion <<EMAIL>>
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError

from erpbrasil.base import fiscal


class DocumentRelated(models.Model):
    _name = 'l10n_br_fiscal.document.related'
    _description = 'Fiscal Document Related'

    fiscal_document_id = fields.Many2one(
        comodel_name='l10n_br_fiscal.document',
        string='Fiscal Document',
        index=True)

    document_related_id = fields.Many2one(
        comodel_name='l10n_br_fiscal.document',
        string='Fiscal Document Related',
        index=True)

    document_type_id = fields.Many2one(
        comodel_name='l10n_br_fiscal.document.type',
        string='Fiscal Document Type')

    document_type_code = fields.Char(
        related='document_type_id.code')

    document_key = fields.Char(
        string='Document Key',
        size=44)

    serie = fields.Char(
        string='Serie',
        size=12)

    number = fields.Char(
        string='Number',
        size=32)

    state_id = fields.Many2one(
        comodel_name='res.country.state',
        string='State',
        domain=[('country_id.code', '=', 'BR')])

    cnpj_cpf = fields.Char(
        string='CNPJ/CPF',
        size=18)

    cpfcnpj_type = fields.Selection(
        selection=[
            ('cpf', 'CPF'),
            ('cnpj', 'CNPJ')],
        string='Type Doc Number',
        default='cnpj')

    inscr_est = fields.Char(
        string='Inscr. Estadual/RG',
        size=16)

    date = fields.Date(
        string='Data')

    @api.multi
    @api.constrains('cnpj_cpf')
    def _check_cnpj_cpf(self):
        result = True
        for record in self:
            if record.cnpj_cpf:
                disable_cnpj_ie_validation = record.env[
                    'ir.config_parameter'].sudo().get_param(
                        'l10n_br_base.disable_cpf_cnpj_validation',
                        default=False)

                if not disable_cnpj_ie_validation:
                    if record.cpfcnpj_type == 'cnpj':
                        if not fiscal.cnpj_cpf.validar(record.cnpj_cpf):
                            result = False
                            document = "CNPJ"
                    elif record.cpfcnpj_type == 'cpf':
                        result = False
                        document = "CPF"

                    if not result:
                        raise ValidationError(
                            _("{} Invalid!").format(document)
                        )

    @api.multi
    @api.constrains('inscr_est', 'state_id')
    def _check_ie(self):
{'fraction_non_alphanumeric': 0.11067044381491974, 'fraction_numerical': 0.011709159584513693, 'mean_word_length': 1.9520624303232998, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1446744', 'n_tokens_mistral': 1743, 'n_tokens_neox': 1640, 'n_words': 297}