Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][spec_driven_model][l10n_br_nfe][l10n_br_nfe_spec][l10n_br_account_nfe] Oca port spec driven model 14.0 to 15.0 51e0f3 (fwp 3427 + 3431) #3514

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 0 additions & 5 deletions l10n_br_nfe/i18n/l10n_br_nfe.pot
Original file line number Diff line number Diff line change
Expand Up @@ -3092,11 +3092,6 @@ msgstr ""
msgid "Mês e Ano de Referência, formato: MM/AAAA"
msgstr ""

#. module: l10n_br_nfe
#: model:ir.model,name:l10n_br_nfe.model_l10n_br_fiscal_nbm
msgid "NBM"
msgstr ""

#. module: l10n_br_nfe
#: model:ir.model,name:l10n_br_nfe.model_l10n_br_fiscal_ncm
msgid "NCM"
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 @@ -20,3 +20,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 @@ -78,24 +78,19 @@

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 @@ -676,29 +671,31 @@
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 @@ -899,11 +896,11 @@
):
record.flush_model()
self.env.invalidate_all()
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 @@ -1072,9 +1069,13 @@
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()

Check warning on line 1076 in l10n_br_nfe/models/document.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_nfe/models/document.py#L1076

Added line #L1076 was not covered by tests

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 @@ -1092,6 +1093,7 @@
_("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 @@ -1350,7 +1352,7 @@
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 @@ -530,7 +525,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_fiscal_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 @@ -112,16 +125,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 @@ -157,7 +173,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
Loading