Skip to content

Commit

Permalink
Merge PR #3510 into 15.0
Browse files Browse the repository at this point in the history
Signed-off-by rvalyi
  • Loading branch information
OCA-git-bot committed Jan 15, 2025
2 parents d31d7d4 + 41181b6 commit a01506b
Show file tree
Hide file tree
Showing 24 changed files with 323 additions and 280 deletions.
2 changes: 0 additions & 2 deletions l10n_br_account_nfe/tests/test_nfce_contingency.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class TestAccountNFCeContingency(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
# this hook is required to test l10n_br_account_nfe alone:
cls.env["spec.mixin.nfe"]._register_hook()
cls.document_id = cls.env.ref("l10n_br_nfe.demo_nfce_same_state")
cls.prepare_account_move_nfce()

Expand Down
3 changes: 1 addition & 2 deletions l10n_br_nfe/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

def post_init_hook(cr, registry):
env = api.Environment(cr, SUPERUSER_ID, {})
env["nfe.40.infnfe"]._register_hook()
cr.execute("select demo from ir_module_module where name='l10n_br_nfe';")
is_demo = cr.fetchone()[0]
if is_demo:
Expand All @@ -37,7 +36,7 @@ def post_init_hook(cr, registry):
nfe = (
env["nfe.40.infnfe"]
.with_context(tracking_disable=True, edoc_type="in")
.build_from_binding(binding.NFe.infNFe)
.build_from_binding("nfe", "40", binding.NFe.infNFe)
)
_logger.info(nfe.nfe40_emit.nfe40_CNPJ)
except ValidationError:
Expand Down
3 changes: 3 additions & 0 deletions l10n_br_nfe/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@
from . import invalidate_number
from . import dfe
from . import mde

spec_schema = "nfe"
spec_version = "40"
42 changes: 22 additions & 20 deletions l10n_br_nfe/models/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,19 @@ def filter_processador_edoc_nfe(record):

class NFe(spec_models.StackedModel):
_name = "l10n_br_fiscal.document"
_inherit = ["l10n_br_fiscal.document", "nfe.40.infnfe", "nfe.40.fat"]
_stacked = "nfe.40.infnfe"
_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"
_nfe_search_keys = ["nfe40_Id"]
_inherit = ["l10n_br_fiscal.document", "nfe.40.infnfe"]

_nfe40_odoo_module = "odoo.addons.l10n_br_nfe_spec.models.v4_0.leiaute_nfe_v4_00"
_nfe40_stacking_mixin = "nfe.40.infnfe"
# all m2o at this level will be stacked even if not required:
_force_stack_paths = (
_nfe40_stacking_force_paths = (
"infnfe.total",
"infnfe.infAdic",
"infnfe.exporta",
"infnfe.cobr",
"infnfe.cobr.fat",
)
_nfe_search_keys = ["nfe40_Id"]

# When dynamic stacking is applied the NFe structure is:
INFNFE_TREE = """
Expand Down Expand Up @@ -675,29 +670,31 @@ def _export_many2one(self, field_name, xsd_required, class_obj=None):
denormalized inner attribute has been set.
"""
self.ensure_one()
if field_name in self._stacking_points.keys():
if field_name in self._get_stacking_points().keys():
if field_name == "nfe40_ISSQNtot" and not any(
t == "issqn"
for t in self.nfe40_det.mapped("product_id.tax_icms_or_issqn")
):
return False

elif (not xsd_required) and field_name not in ["nfe40_enderDest"]:
comodel = self.env[self._stacking_points.get(field_name).comodel_name]
comodel = self.env[
self._get_stacking_points().get(field_name).comodel_name
]
fields = [
f
for f in comodel._fields
if f.startswith(self._field_prefix)
if f.startswith(self._spec_prefix())
and f in self._fields.keys()
and f
# don't try to nfe40_fat id when reading nfe40_cobr for instance
not in self._stacking_points.keys()
not in self._get_stacking_points().keys()
]
sub_tag_read = self.read(fields)[0]
if not any(
v
for k, v in sub_tag_read.items()
if k.startswith(self._field_prefix)
if k.startswith(self._spec_prefix())
):
return False

Expand Down Expand Up @@ -897,11 +894,11 @@ def _serialize(self, edocs):
):
record.flush()
record.invalidate_cache()
inf_nfe = record.export_ds()[0]
inf_nfe = record._build_binding("nfe", "40")

inf_nfe_supl = None
if record.nfe40_infNFeSupl:
inf_nfe_supl = record.nfe40_infNFeSupl.export_ds()[0]
inf_nfe_supl = record.nfe40_infNFeSupl._build_binding("nfe", "40")

nfe = Nfe(infNFe=inf_nfe, infNFeSupl=inf_nfe_supl, signature=None)
edocs.append(nfe)
Expand Down Expand Up @@ -1070,9 +1067,13 @@ 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):
date = fields.Datetime.context_timestamp(record, record.document_date)
if self.document_type_id.code not in [
MODELO_FISCAL_NFE,
MODELO_FISCAL_NFCE,
]:
return super()._generate_key()

for record in self.filtered(filter_processador_edoc_nfe):
required_fields_gen_edoc = []
if not record.company_cnpj_cpf:
required_fields_gen_edoc.append("CNPJ/CPF")
Expand All @@ -1090,6 +1091,7 @@ def _generate_key(self):
_("To Generate EDoc Key, you need to fill the %s field.") % field
)

date = fields.Datetime.context_timestamp(record, record.document_date)
chave_edoc = ChaveEdoc(
ano_mes=date.strftime("%y%m").zfill(4),
cnpj_cpf_emitente=record.company_cnpj_cpf,
Expand Down Expand Up @@ -1346,7 +1348,7 @@ def import_binding_nfe(self, binding, edoc_type="out"):
document = (
self.env["nfe.40.infnfe"]
.with_context(tracking_disable=True, edoc_type=edoc_type, dry_run=False)
.build_from_binding(binding.NFe.infNFe)
.build_from_binding("nfe", "40", binding.NFe.infNFe)
)

if edoc_type == "in" and document.company_id.cnpj_cpf != cnpj_cpf.formata(
Expand Down
17 changes: 6 additions & 11 deletions l10n_br_nfe/models/document_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,12 @@ class NFeLine(spec_models.StackedModel):

_name = "l10n_br_fiscal.document.line"
_inherit = ["l10n_br_fiscal.document.line", "nfe.40.det"]
_stacked = "nfe.40.det"
_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"
_stacking_points = {}

_nfe40_odoo_module = "odoo.addons.l10n_br_nfe_spec.models.v4_0.leiaute_nfe_v4_00"
_nfe40_stacking_mixin = "nfe.40.det"
# all m2o below this level will be stacked even if not required:
_force_stack_paths = ("det.imposto.",)
_stack_skip = ("nfe40_det_infNFe_id",)
_nfe40_stacking_force_paths = ("det.imposto.",)
_nfe40_stacking_skip_paths = ("nfe40_det_infNFe_id",)

# When dynamic stacking is applied, the NFe line has the following structure:
DET_TREE = """
Expand Down Expand Up @@ -526,7 +521,7 @@ def _export_fields_nfe_40_icms(self, xsd_fields, class_obj, export_dict):
.replace("ICMS", "Icms")
.replace("IcmsSN", "Icmssn")
)
binding_module = sys.modules[self._binding_module]
binding_module = sys.modules[self._get_spec_property("binding_module")]
# Tnfe.InfNfe.Det.Imposto.Icms.Icms00
# see https://stackoverflow.com/questions/31174295/
# getattr-and-setattr-on-nested-subobjects-chained-properties
Expand Down
12 changes: 4 additions & 8 deletions l10n_br_nfe/models/document_related.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,11 @@
class NFeRelated(spec_models.StackedModel):
_name = "l10n_br_fiscal.document.related"
_inherit = ["l10n_br_fiscal.document.related", "nfe.40.nfref"]
_stacked = "nfe.40.nfref"
_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"
_stack_skip = ("nfe40_NFref_ide_id",)

_nfe40_odoo_module = "odoo.addons.l10n_br_nfe_spec.models.v4_0.leiaute_nfe_v4_00"
_nfe40_stacking_mixin = "nfe.40.nfref"
# all m2o below this level will be stacked even if not required:
_nfe40_stacking_skip_paths = ("nfe40_NFref_ide_id",)
_rec_name = "nfe40_refNFe"

# When dynamic stacking is applied, this class has the following structure:
Expand Down
10 changes: 3 additions & 7 deletions l10n_br_nfe/models/document_supplement.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ class NFeSupplement(spec_models.StackedModel):
_name = "l10n_br_fiscal.document.supplement"
_description = "NFe Supplement Document"
_inherit = "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"

_nfe40_odoo_module = "odoo.addons.l10n_br_nfe_spec.models.v4_0.leiaute_nfe_v4_00"
_nfe40_stacking_mixin = "nfe.40.infnfesupl"
9 changes: 2 additions & 7 deletions l10n_br_nfe/tests/test_nfe_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@


class NFeImportTest(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.env["spec.mixin.nfe"]._register_hook()

def test_import_in_nfe_dry_run(self):
res_items = (
"nfe",
Expand All @@ -32,7 +27,7 @@ def test_import_in_nfe_dry_run(self):
nfe = (
self.env["nfe.40.infnfe"]
.with_context(tracking_disable=True, edoc_type="in")
.build_from_binding(binding.NFe.infNFe, dry_run=True)
.build_from_binding("nfe", "40", binding.NFe.infNFe, dry_run=True)
)
assert isinstance(nfe.id, NewId)
self._check_nfe(nfe)
Expand All @@ -51,7 +46,7 @@ def test_import_in_nfe(self):
nfe = (
self.env["nfe.40.infnfe"]
.with_context(tracking_disable=True, edoc_type="in")
.build_from_binding(binding.NFe.infNFe, dry_run=False)
.build_from_binding("nfe", "40", binding.NFe.infNFe, dry_run=False)
)

assert isinstance(nfe.id, int)
Expand Down
2 changes: 1 addition & 1 deletion l10n_br_nfe/tests/test_nfe_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
class TestNFeExport(TransactionCase):
def setUp(self, nfe_list):
super().setUp()
self.env["spec.mixin.nfe"]._register_hook()
self.nfe_list = nfe_list
for nfe_data in self.nfe_list:
nfe = self.env.ref(nfe_data["record_ref"])
Expand All @@ -39,6 +38,7 @@ def prepare_test_nfe(self, nfe):
line._onchange_fiscal_operation_line_id()

nfe._compute_amount()
nfe._register_hook() # required in v16 for next statement
nfe.nfe40_detPag = [
(5, 0, 0),
(
Expand Down
36 changes: 28 additions & 8 deletions l10n_br_nfe/tests/test_nfe_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class NFeStructure(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.env["spec.mixin.nfe"]._register_hook()

@classmethod
def get_stacked_tree(cls, klass):
Expand All @@ -26,11 +25,25 @@ def get_stacked_tree(cls, klass):
# ≡ means o2m. Eventually followd by the mapped Odoo model
"""
spec_module = "odoo.addons.l10n_br_nfe_spec.models.v4_0.leiaute_nfe_v4_00"
node = SpecModel._odoo_name_to_class(klass._stacked, spec_module)
spec_prefix = "nfe40"
stacking_settings = {
"odoo_module": getattr(klass, f"_{spec_prefix}_odoo_module"),
"stacking_mixin": getattr(klass, f"_{spec_prefix}_stacking_mixin"),
"stacking_points": getattr(klass, f"_{spec_prefix}_stacking_points"),
"stacking_skip_paths": getattr(
klass, f"_{spec_prefix}_stacking_skip_paths", []
),
"stacking_force_paths": getattr(
klass, f"_{spec_prefix}_stacking_force_paths", []
),
}
node = SpecModel._odoo_name_to_class(
stacking_settings["stacking_mixin"], spec_module
)
tree = StringIO()
visited = set()
for kind, n, path, field_path, child_concrete in klass._visit_stack(
cls.env, node
cls.env, node, stacking_settings
):
visited.add(n)
path_items = path.split(".")
Expand Down Expand Up @@ -110,16 +123,19 @@ def test_doc_stacking_points(self):
"nfe40_ide",
"nfe40_infAdic",
"nfe40_pag",
"nfe40_refECF",
"nfe40_refNF",
"nfe40_refNFP",
"nfe40_retTrib",
"nfe40_total",
"nfe40_transp",
"nfe40_cobr",
"nfe40_fat",
]
keys = [k for k in self.env["l10n_br_fiscal.document"]._stacking_points.keys()]
keys = [
k
for k in self.env["l10n_br_fiscal.document"]
.with_context(spec_schema="nfe", spec_version="40")
._get_stacking_points()
.keys()
]
self.assertEqual(sorted(keys), sorted(doc_keys))

def test_doc_tree(self):
Expand Down Expand Up @@ -155,7 +171,11 @@ def test_doc_line_stacking_points(self):
"nfe40_prod",
]
keys = [
k for k in self.env["l10n_br_fiscal.document.line"]._stacking_points.keys()
k
for k in self.env["l10n_br_fiscal.document.line"]
.with_context(spec_schema="nfe", spec_version="40")
._get_stacking_points()
.keys()
]
self.assertEqual(sorted(keys), line_keys)

Expand Down
2 changes: 1 addition & 1 deletion l10n_br_nfe_spec/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from . import spec_models
from . import spec_mixin
from . import v4_0
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2019-2020 Akretion - Raphael Valyi <[email protected]>
# Copyright 2019-TODAY Akretion - Raphaël Valyi <[email protected]>
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0.en.html).

from odoo import fields, models
Expand All @@ -7,13 +7,8 @@
class NfeSpecMixin(models.AbstractModel):
_description = "Abstract Model"
_name = "spec.mixin.nfe"
_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"
_binding_module = "nfelib.nfe.bindings.v4_0.leiaute_nfe_v4_00"
_spec_tab_name = "NFe"
_nfe40_odoo_module = "odoo.addons.l10n_br_nfe_spec.models.v4_0.leiaute_nfe_v4_00"
_nfe40_binding_module = "nfelib.nfe.bindings.v4_0.leiaute_nfe_v4_00"

brl_currency_id = fields.Many2one(
comodel_name="res.currency",
Expand Down
Loading

0 comments on commit a01506b

Please sign in to comment.