Skip to content

Commit

Permalink
Merge PR #2552 into 14.0
Browse files Browse the repository at this point in the history
Signed-off-by mileo
  • Loading branch information
OCA-git-bot committed Oct 24, 2023
2 parents e155ebd + 4881717 commit 11e9c36
Show file tree
Hide file tree
Showing 29 changed files with 1,181 additions and 60 deletions.
1 change: 1 addition & 0 deletions l10n_br_fiscal/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"wizards/document_correction_wizard.xml",
"wizards/document_status_wizard.xml",
"wizards/invalidate_number_wizard.xml",
"wizards/document_import_wizard_mixin.xml",
# Actions
"views/l10n_br_fiscal_action.xml",
# Menus
Expand Down
2 changes: 2 additions & 0 deletions l10n_br_fiscal/models/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ class Document(models.Model):
store=True,
)

imported_document = fields.Boolean(string="Imported", default=False)

xml_error_message = fields.Text(
readonly=True,
string="XML validation errors",
Expand Down
1 change: 1 addition & 0 deletions l10n_br_fiscal/security/ir.model.access.csv
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,4 @@
"l10n_br_fiscal_document_correction_wizard_user",l10n_br_fiscal_document_correction_wizard,model_l10n_br_fiscal_document_correction_wizard,base.group_user,1,1,1,1
"l10n_br_fiscal_document_status_wizard_user",l10n_br_fiscal_document_status_wizard,model_l10n_br_fiscal_document_status_wizard,base.group_user,1,1,1,1
"l10n_br_fiscal_invalidate_number_wizard_user",l10n_br_fiscal_invalidate_number_wizard,model_l10n_br_fiscal_invalidate_number_wizard,base.group_user,1,1,1,1
"l10n_br_fiscal_document_import_wizard_mixin_user",l10n_br_fiscal_document_import_wizard_mixin_user,model_l10n_br_fiscal_document_import_wizard_mixin,base.group_user,1,1,1,1
1 change: 1 addition & 0 deletions l10n_br_fiscal/wizards/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
from . import document_correction_wizard
from . import document_status_wizard
from . import invalidate_number_wizard
from . import document_import_wizard_mixin
5 changes: 2 additions & 3 deletions l10n_br_fiscal/wizards/base_wizard_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,10 @@ def _document_fields(self):
@api.model
def default_get(self, fields_list):
default_values = super().default_get(fields_list)
active_model = self.env.context["active_model"]
active_model = self._context.get("active_model")

if self._prepare_key_fields().get(active_model):

active_id = self.env.context["active_id"]
active_id = self._context["active_id"]
active_vals = (
self.env[active_model]
.browse(active_id)
Expand Down
31 changes: 31 additions & 0 deletions l10n_br_fiscal/wizards/document_import_wizard_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright (C) 2023 Felipe Zago Rodrigues - Kmee
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from odoo import fields, models

from odoo.addons.l10n_br_fiscal.constants.fiscal import FISCAL_IN_OUT_ALL


class DocumentImportWizardMixin(models.TransientModel):

_name = "l10n_br_fiscal.document.import.wizard.mixin"
_inherit = "l10n_br_fiscal.base.wizard.mixin"

company_id = fields.Many2one(
comodel_name="res.company",
string="Company",
default=lambda self: self.env.company.id,
)

importing_type = fields.Selection(
selection=[("xml_file", "XML File")],
string="Importing Type",
required=True,
default="xml_file",
)

xml = fields.Binary(string="XML to Import")

fiscal_operation_type = fields.Selection(
string="Fiscal Operation Type", selection=FISCAL_IN_OUT_ALL
)
41 changes: 41 additions & 0 deletions l10n_br_fiscal/wizards/document_import_wizard_mixin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2023 KMEE
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>

<record id="l10n_br_fiscal_document_import_wizard_mixin_form" model="ir.ui.view">
<field name="name">l10n_br_fiscal.document.import.wizard.mixin.form</field>
<field name="model">l10n_br_fiscal.document.import.wizard.mixin</field>
<field name="arch" type="xml">
<form string="Import Document">
<group>
<field name="importing_type" />
<field
name="xml"
attrs="{
'invisible': [('importing_type', '!=', 'xml_file')],
'required': [('importing_type', '=', 'xml_file')]
}"
/>
</group>
<separator
string="Preview Data"
attrs="{'invisible': [('xml', '=', False)]}"
/>
<group id="document_info" attrs="{'invisible': [('xml', '=', False)]}">
<group>
<field name="document_key" readonly="1" />
<field name="document_number" readonly="1" />
<field name="document_serie" readonly="1" />
</group>

<group>
<field name="partner_id" readonly="1" />
</group>
</group>
<footer />
</form>
</field>
</record>

</odoo>
2 changes: 2 additions & 0 deletions l10n_br_nfe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

from .hooks import post_init_hook
from . import models
from . import tests
from . import wizards
9 changes: 9 additions & 0 deletions l10n_br_nfe/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (C) 2019 Renato Lima - Akretion
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html


{
"name": "NF-e",
"summary": "Brazilian Eletronic Invoice NF-e",
Expand All @@ -24,15 +25,23 @@
# Security
"security/ir_model_access.xml",
"security/nfe_security.xml",
"security/ir.model.access.csv",
# Views
"views/res_company_view.xml",
"views/nfe_document_view.xml",
"views/res_config_settings_view.xml",
"views/mde/mde_views.xml",
"views/dfe/dfe_views.xml",
"views/supplier_info_view.xml",
# Report
"report/reports.xml",
"report/danfe_nfce.xml",
# Wizards
"wizards/import_document.xml",
# Actions,
"views/nfe_action.xml",
# Menus
"views/nfe_menu.xml",
],
"demo": [
"demo/res_users_demo.xml",
Expand Down
2 changes: 1 addition & 1 deletion l10n_br_nfe/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def post_init_hook(cr, registry):
existing_nfes.unlink()
nfe = (
env["nfe.40.infnfe"]
.with_context(tracking_disable=True, edoc_type="in", lang="pt_BR")
.with_context(tracking_disable=True, edoc_type="in")
.build_from_binding(binding.NFe.infNFe)
)
_logger.info(nfe.nfe40_emit.nfe40_CNPJ)
Expand Down
1 change: 1 addition & 0 deletions l10n_br_nfe/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from . import res_partner
from . import res_company
from . import product_product
from . import product_supplierinfo
from . import ncm
from . import nbm
from . import cest
Expand Down
6 changes: 1 addition & 5 deletions l10n_br_nfe/models/dfe.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,4 @@ def import_documents(self):
@api.model
def parse_procNFe(self, xml):
binding = TnfeProc.from_xml(xml.read().decode())
return (
self.env["nfe.40.infnfe"]
.with_context(tracking_disable=True, edoc_type="in", dry_run=False)
.build_from_binding(binding.NFe.infNFe)
)
return self.env["l10n_br_fiscal.document"].import_nfe_xml(binding)
25 changes: 25 additions & 0 deletions l10n_br_nfe/models/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from unicodedata import normalize

from erpbrasil.assinatura import certificado as cert
from erpbrasil.base.fiscal import cnpj_cpf
from erpbrasil.base.fiscal.edoc import ChaveEdoc
from erpbrasil.edoc.pdf import base
from erpbrasil.transmissao import TransmissaoSOAP
Expand Down Expand Up @@ -683,6 +684,15 @@ def _export_one2many(self, field_name, class_obj=None):
field_data.nItem = i
return res

@api.model
def _prepare_import_dict(
self, values, model=None, parent_dict=None, defaults_model=None
):
return {
**super()._prepare_import_dict(values, model, parent_dict, defaults_model),
"imported_document": True,
}

def _build_attr(self, node, fields, vals, path, attr):
key = "nfe40_%s" % (attr[0],) # TODO schema wise
value = getattr(node, attr[0])
Expand Down Expand Up @@ -1066,6 +1076,21 @@ def make_pdf(self):
}
)

def import_nfe_xml(self, xml, edoc_type="out"):
document = (
self.env["nfe.40.infnfe"]
.with_context(tracking_disable=True, edoc_type=edoc_type, dry_run=False)
.build_from_binding(xml.NFe.infNFe)
)

if edoc_type == "in" and document.company_id.cnpj_cpf != cnpj_cpf.formata(
xml.NFe.infNFe.emit.CNPJ
):
document.fiscal_operation_type = "in"
document.issuer = "partner"

return document

def temp_xml_autorizacao(self, xml_string):
"""TODO: Migrate-me to erpbrasil.edoc.pdf ASAP"""
root = etree.fromstring(xml_string)
Expand Down
47 changes: 45 additions & 2 deletions l10n_br_nfe/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,52 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, models
from odoo.osv import expression


class ProductProduct(models.Model):
_inherit = "product.product"
_nfe_search_keys = ["default_code", "barcode"]

def match_or_create_m2o(self, rec_dict, parent_dict, model=None):
domain_name, domain_barcode, domain_default_code = [], [], []

if parent_dict.get("nfe40_xProd") and parent_dict.get("nfe40_cProd"):
supplier_id = self.env["product.supplierinfo"].search(
[
("product_code", "=", parent_dict["nfe40_cProd"]),
("product_name", "=", parent_dict["nfe40_xProd"]),
],
limit=1,
)
if supplier_id and supplier_id.product_id:
return supplier_id.product_id.id
if parent_dict.get("nfe40_xProd"):
rec_dict["name"] = parent_dict["nfe40_xProd"]
domain_name = [("name", "=", rec_dict.get("name"))]

if (
parent_dict.get("nfe40_cEANTrib")
and parent_dict["nfe40_cEANTrib"] != "SEM GTIN"
):
rec_dict["barcode"] = parent_dict["nfe40_cEANTrib"]
domain_barcode = [("barcode", "=", rec_dict.get("barcode"))]

if parent_dict.get("nfe40_cProd"):
rec_dict["default_code"] = parent_dict["nfe40_cProd"]
domain_default_code = [("default_code", "=", rec_dict.get("default_code"))]

domain = expression.OR([domain_name, domain_barcode, domain_default_code])
match = self.search(domain, limit=1)
if match:
return match.id

if self._context.get("dry_run"):
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
def default_get(self, default_fields):
"""
Expand All @@ -31,8 +71,11 @@ def default_get(self, default_fields):
values["list_price"] = parent_dict.get("nfe40_vUnCom")

# Barcode
if parent_dict.get("nfe40_cEAN") and parent_dict["nfe40_cEAN"] != "SEM GTIN":
values["barcode"] = parent_dict["nfe40_cEAN"]
if (
parent_dict.get("nfe40_cEANTrib")
and parent_dict["nfe40_cEANTrib"] != "SEM GTIN"
):
values["barcode"] = parent_dict["nfe40_cEANTrib"]

# NCM
if parent_dict.get("nfe40_NCM"):
Expand Down
15 changes: 15 additions & 0 deletions l10n_br_nfe/models/product_supplierinfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (C) 2022 Renan Hiroki Bastos - Kmee
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from odoo import fields, models


class SupplierInfo(models.Model):
_inherit = "product.supplierinfo"

partner_uom_id = fields.Many2one(
comodel_name="uom.uom",
string="Partner Unit of Measure",
)

partner_uom_factor = fields.Float(string="Partner UOM Factor", default=1)
1 change: 1 addition & 0 deletions l10n_br_nfe/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from . import test_nfce
from . import test_nfe_structure
from . import test_nfe_import
from . import test_nfe_import_wizard
from . import test_nfe_serialize
from . import test_nfe_serialize_lc
from . import test_nfe_serialize_sn
Expand Down
Loading

0 comments on commit 11e9c36

Please sign in to comment.