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

[14.0][spec_driven_model][l10n_br_nfe] multi schemas support - round 2 #3431

Merged
merged 11 commits into from
Oct 16, 2024
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 @@ -7,8 +7,6 @@
class TestAccountNFCeContingency(TransactionCase):
def setUp(self):
super().setUp()
# this hook is required to test l10n_br_account_nfe alone:
self.env["spec.mixin.nfe"]._register_hook()
self.document_id = self.env.ref("l10n_br_nfe.demo_nfce_same_state")
self.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"
32 changes: 17 additions & 15 deletions l10n_br_nfe/models/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,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"
_spec_module = "odoo.addons.l10n_br_nfe_spec.models.v4_0.leiaute_nfe_v4_00"
_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 @@ -671,29 +671,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 @@ -894,11 +896,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 @@ -1067,9 +1069,8 @@ 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):
super()._generate_key()
for record in self.filtered(filter_processador_edoc_nfe):
date = fields.Datetime.context_timestamp(record, record.document_date)

required_fields_gen_edoc = []
if not record.company_cnpj_cpf:
required_fields_gen_edoc.append("CNPJ/CPF")
Expand All @@ -1087,6 +1088,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 @@ -1343,7 +1345,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
12 changes: 6 additions & 6 deletions l10n_br_nfe/models/document_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +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"
_spec_module = "odoo.addons.l10n_br_nfe_spec.models.v4_0.leiaute_nfe_v4_00"
_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 @@ -514,7 +514,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
8 changes: 4 additions & 4 deletions l10n_br_nfe/models/document_related.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +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"
_stacking_points = {}
_spec_module = "odoo.addons.l10n_br_nfe_spec.models.v4_0.leiaute_nfe_v4_00"
_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
6 changes: 3 additions & 3 deletions l10n_br_nfe/models/document_supplement.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +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"
_stacking_points = {}
_spec_module = "odoo.addons.l10n_br_nfe_spec.models.v4_0.leiaute_nfe_v4_00"

_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(SavepointCase):
@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
1 change: 0 additions & 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 Down
33 changes: 28 additions & 5 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(SavepointCase):
@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 @@ -118,7 +131,13 @@ def test_doc_stacking_points(self):
"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 @@ -154,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 <raphael.valyi@akretion.com>
# Copyright 2019-TODAY Akretion - Raphaël Valyi <raphael.valyi@akretion.com>
# 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
25 changes: 16 additions & 9 deletions spec_driven_model/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Spec Driven Model
Intro
~~~~~

This module is a databinding framework for Odoo and XML data: it allows to go from XML to Odoo objects back and forth. This module started with the `GenerateDS <https://www.davekuhlman.org/generateDS.html>`_ pure Python databinding framework and is now being migrated to xsdata. So a good starting point is to read `the xsdata documentation here <https://xsdata.readthedocs.io/>`_
This module is a databinding framework for Odoo and XML data: it allows to go from XML to Odoo objects back and forth. While having no hard dependency with it, it has been designed to be used with xsdata. So a good starting point is to read `the xsdata documentation here <https://xsdata.readthedocs.io/>`_

But what if instead of only generating Python structures from XML files you could actually generate full blown Odoo objects or serialize Odoo objects back to XML? This is what this module is for!

Expand All @@ -56,7 +56,7 @@ Now that you have generated these Odoo abstract bindings you should tell Odoo ho

Notice you should inherit from `spec_models.SpecModel` and not the usual `models.Model`.

**Field mapping**: You can then define two ways mapping between fields by overriding fields from Odoo or from the binding and using `_compute=` , `_inverse=` or simply `related=`.
**Field mapping**: You can then define two ways mapping between fields by overriding fields from Odoo or from the binding using `_compute=` , `_inverse=` or simply `related=`.

**Relational fields**: simple fields are easily mapped this way. However what about relational fields? In your XSD schema, your electronic invoice is related to the `partner.binding.mixin` not to an Odoo `res.partner`. Don't worry, when `SpecModel` classes are instanciated for all relational fields, we look if their comodel have been injected into some existing Odoo model and if so we remap them to the proper Odoo model.

Expand All @@ -66,7 +66,7 @@ Notice you should inherit from `spec_models.SpecModel` and not the usual `models
StackedModel
~~~~~~~~~~~~

Sadly real life XML is a bit more complex than that. Often XML structures are deeply nested just because it makes it easier for XSD schemas to validate them! for instance an electronic invoice line can be a nested structure with lots of tax details and product details. In a relational model like Odoo however you often want flatter data structures. This is where `StackedModel` comes to the rescue! It inherits from `SpecModel` and when you inherit from `StackedModel` you can inherit from all the generated mixins corresponding to the nested XML tags below some tag (here `invoice.line.binding.mixin`). All the fields corresponding to these XML tag attributes will be collected in your model and the XML parsing and serialization will happen as expected::
Sadly real life XML is a bit more complex than that. Often XML structures are deeply nested just because it makes it easier for XSD schemas to validate them! for instance an electronic invoice line can be a nested structure with lots of tax details and product details. In a relational model like Odoo however you often want flatter data structures. This is where `StackedModel` comes to the rescue! It inherits from `SpecModel` and when you inherit from `StackedModel` you can inherit from all the generated mixins corresponding to the nested XML tags below some tag (here `invoice.line.binding.mixin`). All the fields corresponding to these XML tag attributes will be collected in your model and the XML parsing and serialization will happen as expected. Here is an example inspired from the Brazilian Electronic Invoice where the schema is called `nfe` and where we use the 2 digits `40` for its short version::


from odoo.addons.spec_driven_model.models import spec_models
Expand All @@ -75,17 +75,24 @@ Sadly real life XML is a bit more complex than that. Often XML structures are de
class InvoiceLine(spec_models.StackedModel):
_inherit = [
'account.move.line',
'invoice.line.binding.mixin',
'nfe.40.det',
]
_stacked = 'invoice.line.binding.mixin'
_nfe40_spec_settings = {
"module": "odoo.addons.l10n_br_nfe_spec.models.v4_0.leiaute_nfe_v4_00",
"stacking_mixin": "nfe.40.det",
"stacking_points": {},
# all m2o below this level will be stacked even if not required:
"stacking_force_paths": ("det.imposto.",),
"stacking_skip_paths": ("nfe40_det_infNFe_id",),
}

All many2one fields that are required in the XSD (xsd_required=True) will get their model stacked automatically and recursively. You can force non required many2one fields to be stacked using the `_force_stack_paths` attribute. On the contrary, you can avoid some required many2one fields to be stacked using the `stack_skip` attribute.
All many2one fields that are required in the XSD (xsd_required=True) will get their model stacked automatically and recursively. You can force non required many2one fields to be stacked using the `stacking_force_paths` attribute. On the contrary, you can avoid some required many2one fields to be stacked using the `stacking_skip_paths` attribute.


Hooks
~~~~~
Initialization hook
~~~~~~~~~~~~~~~~~~~

Because XSD schemas can define lot's of different models, spec_driven_model comes with handy hooks that will automatically make all XSD mixins turn into concrete Odoo model (eg with a table) if you didn't inject them into existing Odoo models.
Because XSD schemas can define lot's of different models, spec_driven_model comes with a handy _register_hook that will automatically make all XSD mixins turn into concrete Odoo model (eg with a table) if you didn't inject them into existing Odoo models.

**Table of contents**

Expand Down
5 changes: 2 additions & 3 deletions spec_driven_model/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@

{
"name": "Spec Driven Model",
"summary": """
Tools for specifications driven mixins (from xsd for instance)""",
"summary": """XML binding for Odoo: XML to Odoo models and models to XML.""",
"version": "14.0.5.5.3",
"maintainers": ["rvalyi"],
"license": "LGPL-3",
"author": "Akretion,Odoo Community Association (OCA)",
"author": "Akretion, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/l10n-brazil",
"depends": [],
"data": [],
Expand Down
Loading
Loading