<commit_before># -*- coding: utf-8 -*-
#
# Copyright 2020 KMEE INFORMATICA LTDA
# License AGPL-3 or later (http://www.gnu.org/licenses/agpl)
#

from __future__ import division, print_function, unicode_literals

import logging

from odoo import _, api, fields, models
import base64
from lxml import objectify

_logger = logging.getLogger(__name__)

from ...constants.mdfe import (
    SIT_MANIF_PENDENTE,
    SIT_MANIF_CIENTE,
    SIT_MANIF_CONFIRMADO,
    SIT_MANIF_DESCONHECIDO,
    SIT_MANIF_NAO_REALIZADO,
    SIT_NFE_AUTORIZADA,
    SIT_NFE_CANCELADA,
    SIT_NFE_DENEGADA,

    OPERATION_TYPE,
    SITUACAO_MANIFESTACAO,
    SITUACAO_NFE
)


class MDFe(models.Model):
    _name = "l10n_br_fiscal.mdfe"
    _description = 'Recipient Manifestation'

    @api.multi
    def name_get(self):
        return [(rec.id,
                 u"NFº: {0} ({1}): {2}".format(
                     rec.number, rec.cnpj_cpf, rec.company_id.legal_name)
                 ) for rec in self]

    company_id = fields.Many2one(
        comodel_name="res.company",
        string="Company",
    )
    key = fields.Char(
        string="Access Key",
        size=44,
    )
    serie = fields.Char(
        string="Serie",
        size=3,
        index=True,
    )
    number = fields.Float(
        string="Document Number",
        index=True,
        digits=(18, 0),
    )
    document_id = fields.Many2one(
        comodel_name="l10n_br_fiscal.document",
        string="Fiscal Document",
    )
    emitter = fields.Char(
        string="Emitter",
        size=60,
    )
    cnpj_cpf = fields.Char(
        string="CNPJ/CPF",
        size=18,
    )

    nsu = fields.Char(
        string="NSU",
        size=25,
        select=True,
    )

    operation_type = fields.Selection(
        selection=OPERATION_TYPE,
        string="Operation Type",
    )

    document_value = fields.Float(
        string="Document Total Value",
        readonly=True,
        digits=(18, 2),
    )

    ie = fields.Char(
        string="Inscrição estadual",
        size=18,
    )
    # TODO: Verificar qual comodel_name utilizar
    # partner_id = fields.Many2one(
    #     comodel_name="l10n_br_fiscal.partner.profile",
    #     string="Supplier",
    # )
    partner_id = fields.Many2one(
        comodel_name="res.partner",
        string="Supplier (partner)",
    )

    supplier = fields.Char(
        string="Supplier",
        size=60,
        index=True,
    )

    emission_datetime = fields.Datetime(
        string="Emission Date",
        index=True,
        default=fields.Datetime.now,
    )
    inclusion_datetime = fields.Datetime(
        string="Inclusion Date",
        index=True,
        default=fields.Datetime.now,
    )
    authorization_datetime = fields.Datetime(
        string="Authorization Date",
        index=True,
    )
    cancellation_datetime = fields.Datetime(
        string="Cancellation Date",
        index=True,
    )
    digest_value = fields.Char(
        string="Digest Value",
        size=28,
    )
    inclusion_mode = fields.Char(
        string="Inclusion Mode",
        size=255,
    )
    authorization_protocol = fields.Char(
        string="Authorization protocol",
        size=60,
    )
    cancellation_protocol = fields.Char(
        string="Cancellation protocol",
        size=60,
    )

    document_state = fields.Selection(
        string="Document State",
        selection=SITUACAO_NFE,
        select=True,
    )

    state = fields.Selection(
        string="Manifestation State",
        selection=SITUACAO_MANIFESTACAO,
        select=True,
    )
    dfe_id = fields.Many2one(
        string="DF-e",
        comodel_name="l10n_br_fiscal.dfe",
    )

    @api.multi
    def cria_wizard_gerenciamento(self, state=""):

        dados = {
            "manifestacao_ids": [(6, 0, self.ids)],
            "state": state,
        }

        return self.env["wizard.confirma.acao"].create(dados)

    @api.multi
    def action_import_document(self):
        self.ensure_one()
        document_id = self.dfe_id.download_documents(manifests=self)

        # TODO: Verificar necessidade
        # if len(document_id) > 1:
        #     view_id = self.env.ref(
        #         "sped_nfe.sped_documento_ajuste_recebimento_tree").id
        #     view_type = "tree"
        #
        # else:
        #     view_id = self.env.ref(
        #         "sped_nfe.sped_documento_ajuste_recebimento_form").id
        #     view_type = "form"

        return {
            "name": "Download Documents",
            "view_mode": "form",
            "view_type": 'tree',
            # "view_id": view_id,
            "res_id": document_id.ids,
            "res_model": "l10n_br_fiscal.document",
            "type": "ir.actions.act_window",
            "target": "current",
        }

    @api.multi
    def action_salva_xml(self):

        attachment_id = self.action_import_document()
        return self.download_attachment(attachment_id)

    @api.multi
    def download_attachment(self, attachment_id=None):

        action = {
            'name': _('Download Attachment'),
            'view_mode': 'form',<commit_msg>[FIX][REF] action_import_document refatorado para novos modelos<commit_after># -*- coding: utf-8 -*-
#
# Copyright 2020 KMEE INFORMATICA LTDA
# License AGPL-3 or later (http://www.gnu.org/licenses/agpl)
#

from __future__ import division, print_function, unicode_literals

import logging

from odoo import _, api, fields, models
import base64
from lxml import objectify

_logger = logging.getLogger(__name__)

from ...constants.mdfe import (
    SIT_MANIF_PENDENTE,
    SIT_MANIF_CIENTE,
    SIT_MANIF_CONFIRMADO,
    SIT_MANIF_DESCONHECIDO,
    SIT_MANIF_NAO_REALIZADO,
    SIT_NFE_AUTORIZADA,
    SIT_NFE_CANCELADA,
    SIT_NFE_DENEGADA,

    OPERATION_TYPE,
    SITUACAO_MANIFESTACAO,
    SITUACAO_NFE
)


class MDFe(models.Model):
    _name = "l10n_br_fiscal.mdfe"
    _description = 'Recipient Manifestation'

    @api.multi
    def name_get(self):
        return [(rec.id,
                 u"NFº: {0} ({1}): {2}".format(
                     rec.number, rec.cnpj_cpf, rec.company_id.legal_name)
                 ) for rec in self]

    company_id = fields.Many2one(
        comodel_name="res.company",
        string="Company",
    )
    key = fields.Char(
        string="Access Key",
        size=44,
    )
    serie = fields.Char(
        string="Serie",
        size=3,
        index=True,
    )
    number = fields.Float(
        string="Document Number",
        index=True,
        digits=(18, 0),
    )
    document_id = fields.Many2one(
        comodel_name="l10n_br_fiscal.document",
        string="Fiscal Document",
    )
    emitter = fields.Char(
        string="Emitter",
        size=60,
    )
    cnpj_cpf = fields.Char(
        string="CNPJ/CPF",
        size=18,
    )

    nsu = fields.Char(
        string="NSU",
        size=25,
        select=True,
    )

    operation_type = fields.Selection(
        selection=OPERATION_TYPE,
        string="Operation Type",
    )

    document_value = fields.Float(
        string="Document Total Value",
        readonly=True,
        digits=(18, 2),
    )

    ie = fields.Char(
        string="Inscrição estadual",
        size=18,
    )
    # TODO: Verificar qual comodel_name utilizar
    # partner_id = fields.Many2one(
    #     comodel_name="l10n_br_fiscal.partner.profile",
    #     string="Supplier",
    # )
    partner_id = fields.Many2one(
        comodel_name="res.partner",
        string="Supplier (partner)",
    )

    supplier = fields.Char(
        string="Supplier",
        size=60,
        index=True,
    )

    emission_datetime = fields.Datetime(
        string="Emission Date",
        index=True,
        default=fields.Datetime.now,
    )
    inclusion_datetime = fields.Datetime(
        string="Inclusion Date",
        index=True,
        default=fields.Datetime.now,
    )
    authorization_datetime = fields.Datetime(
        string="Authorization Date",
        index=True,
    )
    cancellation_datetime = fields.Datetime(
        string="Cancellation Date",
        index=True,
    )
    digest_value = fields.Char(
        string="Digest Value",
        size=28,
    )
    inclusion_mode = fields.Char(
        string="Inclusion Mode",
        size=255,
    )
    authorization_protocol = fields.Char(
        string="Authorization protocol",
        size=60,
    )
    cancellation_protocol = fields.Char(
        string="Cancellation protocol",
        size=60,
    )

    document_state = fields.Selection(
        string="Document State",
        selection=SITUACAO_NFE,
        select=True,
    )

    state = fields.Selection(
        string="Manifestation State",
        selection=SITUACAO_MANIFESTACAO,
        select=True,
    )
    dfe_id = fields.Many2one(
        string="DF-e",
        comodel_name="l10n_br_fiscal.dfe",
    )

    @api.multi
    def cria_wizard_gerenciamento(self, state=""):

        dados = {
            "manifestacao_ids": [(6, 0, self.ids)],
            "state": state,
        }

        return self.env["wizard.confirma.acao"].create(dados)

    @api.multi
    def action_import_document(self):
        self.ensure_one()
        document_id = self.dfe_id.download_documents(mdfe_ids=self)

        return {
            "name": "Download Documents",
            "view_mode": "form",
            "view_type": 'tree',
            # "view_id": view_id,
            "res_id": document_id.id,
            "res_model": "l10n_br_fiscal.document",
            "type": "ir.actions.act_window",
            "target": "current",
        }

    @api.multi
    def action_salva_xml(self):

        attachment_id = self.action_import_document()
        return self.download_attachment(attachment_id)

    @api.multi
    def download_attachment(self, attachment_id=None):

        action = {
            'name': _('Download Attachment'),
            'view_mode': 'form',
{'fraction_non_alphanumeric': 0.11209498893137453, 'fraction_numerical': 0.011370497081907828, 'mean_word_length': 2.1462488129154798, 'pattern_counts': {'":': 20, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7300703', 'n_tokens_mistral': 3404, 'n_tokens_neox': 3279, 'n_words': 541}