Skip to content

Commit

Permalink
[RFC] l10n_br_nfe: refactor from l10n_br_cte wip
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelsavegnago committed Sep 14, 2024
1 parent 61d1826 commit 665a135
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 13 deletions.
51 changes: 40 additions & 11 deletions l10n_br_nfe/models/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class NFe(spec_models.StackedModel):
_spec_module = "odoo.addons.l10n_br_nfe_spec.models.v4_0.leiaute_nfe_v4_00"
_spec_tab_name = "NFe"
_nfe_search_keys = ["nfe40_Id"]
_binding_module = "nfelib.nfe.bindings.v4_0.leiaute_nfe_v4_00"

# all m2o at this level will be stacked even if not required:
_force_stack_paths = (
Expand Down Expand Up @@ -760,16 +761,20 @@ def _build_many2one(self, comodel, vals, new_value, key, value, path):
company_cnpj = self.env.company.cnpj_cpf.translate(
str.maketrans("", "", string.punctuation)
)
emit_cnpj = new_value.get("nfe40_CNPJ").translate(
str.maketrans("", "", string.punctuation)
)
if company_cnpj != emit_cnpj:
vals["issuer"] = "partner"
new_value["is_company"] = True
new_value["cnpj_cpf"] = emit_cnpj
super()._build_many2one(
self.env["res.partner"], vals, new_value, "partner_id", value, path
)
if new_value.get("nfe40_CNPJ"):
emit_cnpj = new_value.get("nfe40_CNPJ").translate(
str.maketrans("", "", string.punctuation)
)
if company_cnpj != emit_cnpj:
vals["issuer"] = "partner"
new_value["is_company"] = True
new_value["cnpj_cpf"] = emit_cnpj
super()._build_many2one(
self.env["res.partner"], vals, new_value, "partner_id", value, path
)
else:
new_value["name"] = value.xFant
super()._build_many2one(comodel, vals, new_value, key, value, path)
elif key == "nfe40_dest" and self.env.context.get("edoc_type") == "out":
enderDest_value = self.env["res.partner"].build_attrs(
value.enderDest, path=path
Expand Down Expand Up @@ -897,6 +902,9 @@ def _serialize(self, edocs):
return edocs

def _processador(self):
if self.document_type not in (MODELO_FISCAL_NFE, MODELO_FISCAL_NFCE):
return super()._processador()

self._check_nfe_environment()
certificado = self.company_id._get_br_ecertificate()
session = Session()
Expand Down Expand Up @@ -1037,6 +1045,10 @@ def _nfe_save_protocol(self, inf_prot, nfe_proc_xml=None):

def _valida_xml(self, xml_file):
self.ensure_one()

if self.document_type not in (MODELO_FISCAL_NFE, MODELO_FISCAL_NFCE):
return super()._valida_xml(xml_file)

erros = Nfe.schema_validation(xml_file)
erros = "\n".join(erros)
self.write({"xml_error_message": erros or False})
Expand All @@ -1059,7 +1071,11 @@ def _exec_after_SITUACAO_EDOC_AUTORIZADA(self, old_state, new_state):
return super()._exec_after_SITUACAO_EDOC_AUTORIZADA(old_state, new_state)

def _generate_key(self):
for record in self.filtered(filter_processador_edoc_nfe):
records = self.filtered(filter_processador_edoc_nfe)
if not records:
return super()._generate_key()

for record in records:
date = fields.Datetime.context_timestamp(record, record.document_date)

required_fields_gen_edoc = []
Expand Down Expand Up @@ -1103,6 +1119,19 @@ def _nfe_consult_receipt(self):
self._nfe_response_add_proc(receipt_process)
self._nfe_process_authorization(receipt_process)

def _document_qrcode(self):
super()._document_qrcode()

for record in self.filtered(filter_processador_edoc_nfe):
record.nfe40_infNFeSupl = self.env[
"l10n_br_fiscal.document.supplement"
].create(
{
"qrcode": record.get_nfce_qrcode(),
"url_key": record.get_nfce_qrcode_url(),
}
)

def _nfe_response_add_proc(self, ws_response_process):
"""
Inject the final NF-e, tag `nfeProc`, into the response.
Expand Down
1 change: 1 addition & 0 deletions l10n_br_nfe/models/document_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class NFeLine(spec_models.StackedModel):
# all m2o below this level will be stacked even if not required:
_force_stack_paths = ("det.imposto.",)
_stack_skip = ("nfe40_det_infNFe_id",)
_binding_module = "nfelib.nfe.bindings.v4_0.leiaute_nfe_v4_00"

# When dynamic stacking is applied, the NFe line has the following structure:
DET_TREE = """
Expand Down
1 change: 1 addition & 0 deletions l10n_br_nfe/models/document_related.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class NFeRelated(spec_models.StackedModel):
_stack_skip = ("nfe40_NFref_ide_id",)
# all m2o below this level will be stacked even if not required:
_rec_name = "nfe40_refNFe"
_binding_module = "nfelib.nfe.bindings.v4_0.leiaute_nfe_v4_00"

# When dynamic stacking is applied, this class has the following structure:
NFREF_TREE = """
Expand Down
9 changes: 7 additions & 2 deletions l10n_br_nfe/models/document_supplement.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
# Copyright 2023 KMEE (Felipe Zago Rodrigues <[email protected]>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

from odoo import fields

from odoo.addons.spec_driven_model.models import spec_models


class NFeSupplement(spec_models.StackedModel):
_name = "l10n_br_fiscal.document.supplement"
_description = "NFe Supplement Document"
_inherit = "nfe.40.infnfesupl"
_inherit = ["l10n_br_fiscal.document.supplement", "nfe.40.infnfesupl"]
_stacked = "nfe.40.infnfesupl"
_field_prefix = "nfe40_"
_schema_name = "nfe"
_schema_version = "4.0.0"
_odoo_module = "l10n_br_nfe"
_spec_module = "odoo.addons.l10n_br_nfe_spec.models.v4_0.leiaute_nfe_v4_00"
_spec_tab_name = "NFe"
_binding_module = "nfelib.nfe.bindings.v4_0.leiaute_nfe_v4_00"

nfe40_qrCode = fields.Char(related="qrcode")

nfe40_urlChave = fields.Char(related="url_key")
2 changes: 2 additions & 0 deletions l10n_br_nfe/models/res_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class ResCompany(spec_models.SpecModel):
_name = "res.company"
_inherit = ["res.company", "nfe.40.emit"]
_nfe_search_keys = ["nfe40_CNPJ", "nfe40_xNome", "nfe40_xFant"]
_binding_module = "nfelib.nfe.bindings.v4_0.leiaute_nfe_v4_00"
_field_prefix = "nfe40_"

nfe40_CNPJ = fields.Char(related="partner_id.nfe40_CNPJ")
nfe40_CPF = fields.Char(related="partner_id.nfe40_CPF")
Expand Down
2 changes: 2 additions & 0 deletions l10n_br_nfe/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class ResPartner(spec_models.SpecModel):
"nfe.40.autxml",
]
_nfe_search_keys = ["nfe40_CNPJ", "nfe40_CPF", "nfe40_xNome"]
_binding_module = "nfelib.nfe.bindings.v4_0.leiaute_nfe_v4_00"
_field_prefix = "nfe40_"

@api.model
def _prepare_import_dict(
Expand Down

0 comments on commit 665a135

Please sign in to comment.