Skip to content

Commit

Permalink
[NEW][l10n_br_nfe] Product import map
Browse files Browse the repository at this point in the history
  • Loading branch information
mileo committed Jan 30, 2025
1 parent cbe772c commit b0f3f02
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
7 changes: 7 additions & 0 deletions l10n_br_nfe/models/document_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,11 @@ def _build_attr(self, node, fields, vals, path, attr):
.search([("code_unmasked", "=", value)], limit=1)
.id
)
vals["fiscal_genre_id"] = (
self.env["l10n_br_fiscal.product.genre"]
.search([("code", "=", value[:2])], limit=1)
.id
)
if key == "nfe40_CEST" and value:
vals["cest_id"] = (
self.env["l10n_br_fiscal.cest"]
Expand Down Expand Up @@ -1410,13 +1415,15 @@ def action_create_product(self):
spec_version="40",
# dry_run=True
)
# TODO: Melhorar o comportamento ao descartar a criação do produto.
values = self.read([])[0]
self._prepare_import_dict(values)
record_dict = {}
self.product_id = self.product_id.match_or_create_m2o(

Check warning on line 1422 in l10n_br_nfe/models/document_line.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_nfe/models/document_line.py#L1419-L1422

Added lines #L1419 - L1422 were not covered by tests
rec_dict=record_dict,
parent_dict=values,
)
self._onchange_product_id_fiscal()
return {

Check warning on line 1427 in l10n_br_nfe/models/document_line.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_nfe/models/document_line.py#L1426-L1427

Added lines #L1426 - L1427 were not covered by tests
"type": "ir.actions.act_window",
"res_model": "product.product",
Expand Down
45 changes: 44 additions & 1 deletion l10n_br_nfe/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
from odoo import api, models
from odoo.osv import expression

from odoo.addons.l10n_br_fiscal.constants.fiscal import (
CFOP_TYPE_MOVE_2_PRODUCT_FISCAL_TYPE,
)


class ProductProduct(models.Model):
_inherit = "product.product"
Expand Down Expand Up @@ -50,6 +54,7 @@ def match_or_create_m2o(self, rec_dict, parent_dict, model=None):
rec_id = self.new(rec_dict).id
else:
rec_id = self.with_context(parent_dict=parent_dict).create(rec_dict).id

return rec_id

@api.model
Expand Down Expand Up @@ -86,8 +91,12 @@ def default_get(self, default_fields):
ncm = self.env["l10n_br_fiscal.ncm"].search(
[("code_unmasked", "=", parent_dict["nfe40_NCM"])], limit=1
)

values["ncm_id"] = ncm.id
values["fiscal_genre_id"] = (
self.env["l10n_br_fiscal.product.genre"]
.search([("code", "=", parent_dict["nfe40_NCM"][:2])], limit=1)
.id
)

if not ncm: # FIXME should not happen with prod data
ncm = (
Expand All @@ -101,4 +110,38 @@ def default_get(self, default_fields):
)
)
values["ncm_id"] = ncm.id
values["fiscal_type"] = self._map_fiscal_type_from_context()
return values

def _map_fiscal_type_from_context(self):
"""
Automatically determines the product's fiscal type based on the
imported invoice context.
:param env: Odoo environment containing the execution context.
:return: Product fiscal type or False if it cannot be determined.
"""
fiscal_type = False
parent_dict = self.env.context.get("parent_dict")

if parent_dict:
# Mapping based on CFOP
cfop_tuple = parent_dict.get("cfop_inverse_id")
if cfop_tuple and isinstance(cfop_tuple, tuple):
cfop = cfop_tuple[0]
cfop_id = self.env["l10n_br_fiscal.cfop"].browse(cfop)
fiscal_type = CFOP_TYPE_MOVE_2_PRODUCT_FISCAL_TYPE.get(

Check warning on line 133 in l10n_br_nfe/models/product_product.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_nfe/models/product_product.py#L131-L133

Added lines #L131 - L133 were not covered by tests
cfop_id.type_move
)

# If a fiscal operation line exists, overwrite the previous value
fol_tuple = parent_dict.get("fiscal_operation_line_id")
if fol_tuple and isinstance(fol_tuple, tuple):
fol_id = fol_tuple[0]
fiscal_operation_line_id = self.env[

Check warning on line 141 in l10n_br_nfe/models/product_product.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_nfe/models/product_product.py#L140-L141

Added lines #L140 - L141 were not covered by tests
"l10n_br_fiscal.operation.line"
].browse(fol_id)
if fiscal_operation_line_id.product_type:
fiscal_type = fiscal_operation_line_id.product_type

Check warning on line 145 in l10n_br_nfe/models/product_product.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_nfe/models/product_product.py#L145

Added line #L145 was not covered by tests

return fiscal_type
13 changes: 0 additions & 13 deletions l10n_br_nfe/views/nfe_document_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,6 @@
Nf-e Environment: <strong><field name="nfe_environment" /></strong>
</div>
</xpath>

<xpath
expr="//field[@name='fiscal_line_ids']/tree/field[@name='product_id']"
position="before"
>
<button
type="object"
name="action_create_product"
icon="fa-plus-square-o"
attrs="{'invisible': [('product_id', '!=', False)]}"
/>
</xpath>

<page name="delivery" position="inside">
<group
name="nfe_transport"
Expand Down

0 comments on commit b0f3f02

Please sign in to comment.