Skip to content

Commit

Permalink
[RFC] l10n_br_cte: multi-schema refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelsavegnago committed Oct 25, 2024
1 parent 9a14b8f commit 8a756f3
Show file tree
Hide file tree
Showing 16 changed files with 152 additions and 161 deletions.
41 changes: 35 additions & 6 deletions l10n_br_cte/hooks.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
# Copyright (C) 2019-2020 - Raphael Valyi Akretion
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
import logging

import nfelib
import pkg_resources
from nfelib.cte.bindings.v4_0.cte_tipos_basico_v4_00 import Tcte

from odoo import SUPERUSER_ID, api
from odoo.exceptions import ValidationError

_logger = logging.getLogger(__name__)


def post_init_hook(cr, registry):
env = api.Environment(cr, SUPERUSER_ID, {})
env["cte.40.tcte_infcte"]._register_hook()
# hooks.register_hook(
# env,
# "l10n_br_cte",
# "odoo.addons.l10n_br_cte_spec.models.v4_0.cte_tipos_basico_v4_00",
# )
cr.execute("select demo from ir_module_module where name='l10n_br_cte';")
is_demo = cr.fetchone()[0]
if is_demo:
res_items = (
"cte",
"samples",
"v3_0",
"43120178408960000182570010000000041000000047-cte.xml",
)
resource_path = "/".join(res_items)
doc_stream = pkg_resources.resource_stream(nfelib.__name__, resource_path)
binding = Tcte.from_xml(doc_stream.read().decode())
document_number = binding.infCte.ide.nCTE
existing_docs = env["l10n_br_fiscal.document"].search(
[("document_number", "=", document_number)]
)
try:
existing_docs.unlink()
doc = (
env["cte.40.tcte_infcte"]
.with_context(tracking_disable=True, edoc_type="in")
.build_from_binding("cte", "40", binding.infMDFe)
)
_logger.info(doc.cte40_emit.cte40_CNPJ)
except ValidationError:
_logger.info(f"CTE-e already {document_number} imported by hooks")
5 changes: 4 additions & 1 deletion l10n_br_cte/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@
from . import document_supplement
from . import document_transported_vehicles
from . import normal_cte_infos
from . import comment
from . import document_comment

spec_schema = "cte"
spec_version = "40"
26 changes: 10 additions & 16 deletions l10n_br_cte/models/aereo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@
class Aereo(spec_models.StackedModel):
_name = "l10n_br_cte.modal.aereo"
_inherit = "cte.40.aereo"
_stacked = "cte.40.aereo"
_binding_module = "nfelib.cte.bindings.v4_0.cte_modal_aereo_v4_00"
_field_prefix = "cte40_"
_schema_name = "cte"
_schema_version = "4.0.0"
_odoo_module = "l10n_br_cte"
_spec_module = "odoo.addons.l10n_br_cte_spec.models.v4_0.cte_modal_aereo_v4_00"
_spec_tab_name = "CTe"
_description = "Modal Aereo CTe"

_cte40_stacking_mixin = "cte.40.aereo"
_cte40_odoo_module = (
"odoo.addons.l10n_br_cte_spec.models.v4_0.cte_modal_aereo_v4_00"
)

document_id = fields.Many2one(comodel_name="l10n_br_fiscal.document")

cte40_nMinu = fields.Char(related="document_id.cte40_nMinu")
Expand All @@ -46,18 +43,15 @@ def _prepare_dacte_values(self):
class Peri(spec_models.StackedModel):
_name = "l10n_br_cte.modal.aereo.peri"
_inherit = "cte.40.peri"
_stacked = "cte.40.peri"
_binding_module = "nfelib.cte.bindings.v4_0.cte_modal_aereo_v4_00"
_field_prefix = "cte40_"
_schema_name = "cte"
_schema_version = "4.0.0"
_odoo_module = "l10n_br_cte"
_spec_module = "odoo.addons.l10n_br_cte_spec.models.v4_0.cte_modal_aereo_v4_00"
_spec_tab_name = "CTe"
_description = """Preenchido quando for transporte de produtos classificados pela
ONU como perigosos. O preenchimento desses campos não desobriga a empresa aérea de
emitir os demais documentos que constam na legislação vigente."""

_cte40_stacking_mixin = "cte.40.peri"
_cte40_odoo_module = (
"odoo.addons.l10n_br_cte_spec.models.v4_0.cte_modal_aereo_v4_00"
)

document_id = fields.Many2one(comodel_name="l10n_br_fiscal.document")

cte40_nONU = fields.Char(required=True)
Expand Down
13 changes: 5 additions & 8 deletions l10n_br_cte/models/aquaviario.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@
class Aquav(spec_models.StackedModel):
_name = "l10n_br_cte.modal.aquav"
_inherit = "cte.40.aquav"
_stacked = "cte.40.aquav"
_binding_module = "nfelib.cte.bindings.v4_0.cte_modal_aquaviario_v4_00"
_field_prefix = "cte40_"
_schema_name = "cte"
_schema_version = "4.0.0"
_odoo_module = "l10n_br_cte"
_spec_module = "odoo.addons.l10n_br_cte_spec.models.v4_0.cte_modal_aquaviario_v4_00"
_spec_tab_name = "CTe"
_description = "Modal Aquaviário CTe"

_cte40_stacking_mixin = "cte.40.aquav"
_cte40_odoo_module = (
"odoo.addons.l10n_br_cte_spec.models.v4_0.cte_modal_aquaviario_v4_00"
)

document_id = fields.Many2one(comodel_name="l10n_br_fiscal.document")

cte40_vAFRMM = fields.Monetary(related="document_id.cte40_vAFRMM")
Expand Down
Loading

0 comments on commit 8a756f3

Please sign in to comment.