<commit_before># Copyright 2019 KMEE INFORMATICA LTDA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import base64
import logging

from erpbrasil.assinatura import certificado as cert
from erpbrasil.base import misc
from erpbrasil.edoc.provedores.cidades import NFSeFactory
from erpbrasil.transmissao import TransmissaoSOAP
from requests import Session

from odoo import fields, models

from odoo.addons.l10n_br_fiscal.constants.fiscal import (
    EVENT_ENV_HML,
    EVENT_ENV_PROD,
    MODELO_FISCAL_NFSE,
    PROCESSADOR_OCA,
    TAX_FRAMEWORK_SIMPLES_ALL,
)

from ..constants.nfse import (
    NFSE_ENVIRONMENTS,
    OPERATION_NATURE,
    RPS_TYPE,
    TAXATION_SPECIAL_REGIME,
)

_logger = logging.getLogger(__name__)


def filter_processador_edoc_nfse(record):
    if record.processador_edoc == PROCESSADOR_OCA and record.document_type_id.code in [
        MODELO_FISCAL_NFSE,
    ]:
        return True
    return False


class Document(models.Model):

    _inherit = "l10n_br_fiscal.document"

    edoc_error_message = fields.Text(
        readonly=True,
        copy=False,
    )

    rps_type = fields.Selection(
        string="RPS Type",
        selection=RPS_TYPE,
        default="1",
    )
    operation_nature = fields.Selection(
        string="Operation Nature",
        selection=OPERATION_NATURE,
        default="1",
    )
    taxation_special_regime = fields.Selection(
        string="Taxation Special Regime",
        selection=TAXATION_SPECIAL_REGIME,
        default="1",
    )
    verify_code = fields.Char(
        string="Verify Code",
        readonly=True,
        copy=False,
    )
    nfse_environment = fields.Selection(
        selection=NFSE_ENVIRONMENTS,
        string="NFSe Environment",
        default=lambda self: self.env.company.nfse_environment,
    )

    def _document_date(self):
        super()._document_date()
        for record in self.filtered(filter_processador_edoc_nfse):
            if not record.date_in_out:
                record.date_in_out = fields.Datetime.now()

    def make_pdf(self):
        if not self.filtered(filter_processador_edoc_nfse):
            return super().make_pdf()
        pdf = self.env.ref("l10n_br_nfse.report_br_nfse_danfe").render_qweb_pdf(
            self.ids
        )[0]

        if self.document_number:
            filename = "NFS-e-" + self.document_number + ".pdf"
        else:
            filename = "RPS-" + self.rps_number + ".pdf"

        vals_dict = {
            "name": filename,
            "datas_fname": filename,
            "res_model": self._name,
            "res_id": self.id,
            "datas": base64.b64encode(pdf),<commit_msg>[FIX] l10n_br_nfse: make_pdf<commit_after># Copyright 2019 KMEE INFORMATICA LTDA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import base64
import logging

from erpbrasil.assinatura import certificado as cert
from erpbrasil.base import misc
from erpbrasil.edoc.provedores.cidades import NFSeFactory
from erpbrasil.transmissao import TransmissaoSOAP
from requests import Session

from odoo import fields, models

from odoo.addons.l10n_br_fiscal.constants.fiscal import (
    EVENT_ENV_HML,
    EVENT_ENV_PROD,
    MODELO_FISCAL_NFSE,
    PROCESSADOR_OCA,
    TAX_FRAMEWORK_SIMPLES_ALL,
)

from ..constants.nfse import (
    NFSE_ENVIRONMENTS,
    OPERATION_NATURE,
    RPS_TYPE,
    TAXATION_SPECIAL_REGIME,
)

_logger = logging.getLogger(__name__)


def filter_processador_edoc_nfse(record):
    if record.processador_edoc == PROCESSADOR_OCA and record.document_type_id.code in [
        MODELO_FISCAL_NFSE,
    ]:
        return True
    return False


class Document(models.Model):

    _inherit = "l10n_br_fiscal.document"

    edoc_error_message = fields.Text(
        readonly=True,
        copy=False,
    )

    rps_type = fields.Selection(
        string="RPS Type",
        selection=RPS_TYPE,
        default="1",
    )
    operation_nature = fields.Selection(
        string="Operation Nature",
        selection=OPERATION_NATURE,
        default="1",
    )
    taxation_special_regime = fields.Selection(
        string="Taxation Special Regime",
        selection=TAXATION_SPECIAL_REGIME,
        default="1",
    )
    verify_code = fields.Char(
        string="Verify Code",
        readonly=True,
        copy=False,
    )
    nfse_environment = fields.Selection(
        selection=NFSE_ENVIRONMENTS,
        string="NFSe Environment",
        default=lambda self: self.env.company.nfse_environment,
    )

    def _document_date(self):
        super()._document_date()
        for record in self.filtered(filter_processador_edoc_nfse):
            if not record.date_in_out:
                record.date_in_out = fields.Datetime.now()

    def make_pdf(self):
        if not self.filtered(filter_processador_edoc_nfse):
            return super().make_pdf()
        pdf = self.env.ref("l10n_br_nfse.report_br_nfse_danfe")._render_qweb_pdf(
            self.ids
        )[0]

        if self.document_number:
            filename = "NFS-e-" + self.document_number + ".pdf"
        else:
            filename = "RPS-" + self.rps_number + ".pdf"

        vals_dict = {
            "name": filename,
            "res_model": self._name,
            "res_id": self.id,
            "datas": base64.b64encode(pdf),
{'fraction_non_alphanumeric': 0.0913887318635764, 'fraction_numerical': 0.008667797248916526, 'mean_word_length': 2.6784476784476783, 'pattern_counts': {'":': 9, '<': 3, '<?xml version=': 0, '>': 3, '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': '3494197', 'n_tokens_mistral': 1818, 'n_tokens_neox': 1738, 'n_words': 315}