<commit_before># Copyright (C) 2022-Today - Engenere (<https://engenere.one>).
# @author <NAME> <<EMAIL>>
# @author <NAME> <<EMAIL>>
# Copyright (C) 2022-Today - Akretion (<https://akretion.com/pt-BR>).
# @author <NAME> <<EMAIL>>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, models
from odoo.exceptions import UserError

from odoo.addons.l10n_br_fiscal.constants.fiscal import (
    DOCUMENT_ISSUER_COMPANY,
    EDOC_PURPOSE_DEVOLUCAO,
    MODELO_FISCAL_NFCE,
    MODELO_FISCAL_NFE,
    PROCESSADOR_OCA,
)


def filter_nfe(record):
    if (
        record.processador_edoc == PROCESSADOR_OCA
        and record.document_type_id.code
        in [
            MODELO_FISCAL_NFE,
            MODELO_FISCAL_NFCE,
        ]
        and record.issuer == DOCUMENT_ISSUER_COMPANY
    ):
        return True
    return False


class DocumentWorkflow(models.AbstractModel):
    _inherit = "l10n_br_fiscal.document.workflow"

    def action_document_confirm(self):
        for record in self.filtered(filter_nfe):
            record.nfe40_dup = [(5,)]
            record.nfe40_detPag = [(5,)]
            if (
                record.amount_financial_total
                and record.edoc_purpose != EDOC_PURPOSE_DEVOLUCAO
            ):
                # TAG - Cobrança
                duplicatas = record.env["nfe.40.dup"]
                count = 1
                for mov in record.move_ids.financial_move_line_ids:
                    duplicatas += duplicatas.create(
                        {
                            "nfe40_nDup": str(count).zfill(3),
                            "nfe40_dVenc": mov.date_maturity,
                            "nfe40_vDup": mov.debit,
                        }
                    )
                    count += 1
                record.nfe40_dup = [(6, 0, duplicatas.ids)]

                # TAG - Pagamento
                if not record.move_ids.payment_mode_id.fiscal_payment_mode:
                    raise UserError(
                        _(
                            "Payment Mode {} should has Fiscal Payment Mode"
                            " filled to be used in Fiscal Document!".format(
                                record.move_ids.payment_mode_id.name
                            )
                        )
                    )

                moves_terms = record.move_ids.financial_move_line_ids.filtered(
                    lambda move_line: move_line.date_maturity > move_line.date
                )
                ind_pag = "1" if len(moves_terms) > 0 else "0"
                fiscal_payment_mode = (
                    record.move_ids.payment_mode_id.fiscal_payment_mode
                )
                v_pag = record.amount_financial_total
            else:
                ind_pag = "0"
                fiscal_payment_mode = "90"
                v_pag = 0.00

            pagamentos = record.env["nfe.40.detpag"].create(
                {
                    "nfe40_indPag": ind_pag,
                    "nfe40_tPag": fiscal_payment_mode,
                    "nfe40_vPag": v_pag,
                }
            )

            record.nfe40_detPag = [(6, 0, pagamentos.ids)]

        super().action_document_confirm()
<commit_msg>[FIX] If you create NF-e without payment_mode_id<commit_after># Copyright (C) 2022-Today - Engenere (<https://engenere.one>).
# @author <NAME> <<EMAIL>>
# @author <NAME> <<EMAIL>>
# Copyright (C) 2022-Today - Akretion (<https://akretion.com/pt-BR>).
# @author <NAME> <<EMAIL>>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, models
from odoo.exceptions import UserError

from odoo.addons.l10n_br_fiscal.constants.fiscal import (
    DOCUMENT_ISSUER_COMPANY,
    EDOC_PURPOSE_DEVOLUCAO,
    MODELO_FISCAL_NFCE,
    MODELO_FISCAL_NFE,
    PROCESSADOR_OCA,
)


def filter_nfe(record):
    if (
        record.processador_edoc == PROCESSADOR_OCA
        and record.document_type_id.code
        in [
            MODELO_FISCAL_NFE,
            MODELO_FISCAL_NFCE,
        ]
        and record.issuer == DOCUMENT_ISSUER_COMPANY
    ):
        return True
    return False


class DocumentWorkflow(models.AbstractModel):
    _inherit = "l10n_br_fiscal.document.workflow"

    def action_document_confirm(self):
        for record in self.filtered(filter_nfe):
            record.nfe40_dup = [(5,)]
            record.nfe40_detPag = [(5,)]
            ind_pag = "0"
            fiscal_payment_mode = "90"
            v_pag = 0.00
            if (
                record.amount_financial_total
                and record.edoc_purpose != EDOC_PURPOSE_DEVOLUCAO
            ):
                # TAG - Cobrança
                duplicatas = record.env["nfe.40.dup"]
                count = 1
                for mov in record.move_ids.financial_move_line_ids:
                    duplicatas += duplicatas.create(
                        {
                            "nfe40_nDup": str(count).zfill(3),
                            "nfe40_dVenc": mov.date_maturity,
                            "nfe40_vDup": mov.debit,
                        }
                    )
                    count += 1
                record.nfe40_dup = [(6, 0, duplicatas.ids)]

                # TAG - Pagamento
                if (
                    record.invoice_ids.payment_mode_id
                    and not record.invoice_ids.payment_mode_id.fiscal_payment_mode
                ):
                    raise UserError(
                        _(
                            "Payment Mode {} should has Fiscal Payment Mode"
                            " filled to be used in Fiscal Document!".format(
                                record.move_ids.payment_mode_id.name
                            )
                        )
                    )

                moves_terms = record.move_ids.financial_move_line_ids.filtered(
                    lambda move_line: move_line.date_maturity > move_line.date
                )
                ind_pag = "1" if len(moves_terms) > 0 else "0"
                fiscal_payment_mode = (
                    record.move_ids.payment_mode_id.fiscal_payment_mode
                )
                v_pag = record.amount_financial_total

            pagamentos = record.env["nfe.40.detpag"].create(
                {
                    "nfe40_indPag": ind_pag,
                    "nfe40_tPag": fiscal_payment_mode,
                    "nfe40_vPag": v_pag,
                }
            )

            record.nfe40_detPag = [(6, 0, pagamentos.ids)]

        super().action_document_confirm()

{'fraction_non_alphanumeric': 0.08796225840815705, 'fraction_numerical': 0.017044589864556384, 'mean_word_length': 1.401169163317501, 'pattern_counts': {'":': 12, '<': 25, '<?xml version=': 0, '>': 29, 'https://': 4, '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': '4377270', 'n_tokens_mistral': 2163, 'n_tokens_neox': 1966, 'n_words': 327}