Skip to content

Commit

Permalink
[WIP]Emission of cte
Browse files Browse the repository at this point in the history
  • Loading branch information
ODBreno committed Nov 10, 2023
1 parent cbfd8a1 commit 8e74610
Show file tree
Hide file tree
Showing 16 changed files with 159 additions and 100 deletions.
6 changes: 5 additions & 1 deletion l10n_br_cte/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
# "views/document_line.xml",
# 'views/document_related.xml',
# 'views/res_partner.xml',
"modal/modal_rodoviario.xml",
"modal/modal_aquaviario.xml",
"modal/modal_ferroviario.xml",
"modal/modal_aereo.xml",
"views/res_company.xml",
"views/cte_document.xml",
],
Expand All @@ -29,7 +33,7 @@
"auto_install": False,
"external_dependencies": {
"python": [
"nfelib>=2.0.0",
"nfelib",
"erpbrasil.transmissao",
"erpbrasil.edoc",
"erpbrasil.assinatura",
Expand Down
31 changes: 31 additions & 0 deletions l10n_br_cte/constants/modal.py
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
CTE_MODAL_VERSION_DEFAULT = "4.00"

TUF = [
("AC", "AC"),
("AL", "AL"),
("AM", "AM"),
("AP", "AP"),
("BA", "BA"),
("CE", "CE"),
("DF", "DF"),
("ES", "ES"),
("GO", "GO"),
("MA", "MA"),
("MG", "MG"),
("MS", "MS"),
("MT", "MT"),
("PA", "PA"),
("PB", "PB"),
("PE", "PE"),
("PI", "PI"),
("PR", "PR"),
("RJ", "RJ"),
("RN", "RN"),
("RO", "RO"),
("RR", "RR"),
("RS", "RS"),
("SC", "SC"),
("SE", "SE"),
("SP", "SP"),
("TO", "TO"),
("EX", "EX"),
]
2 changes: 1 addition & 1 deletion l10n_br_cte/modal/modal_aquaviario.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<odoo>
<record id="modal_aquaviario_balsa_form_view" model="ir.ui.view">
<field name="name">modal.aquaviario.balsa.form.view (in l10n_br_cte)</field>
<field name="model">l10n_br_cte.modal.aquaviario.balsa</field>
<field name="model">l10n_br_cte.modal.aquav.balsa</field>
<field name="arch" type="xml">
<form>
<sheet>
Expand Down
25 changes: 2 additions & 23 deletions l10n_br_cte/modal/modal_rodoviario.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,14 @@
<odoo>
<record id="modal_rodoviario_occ_form_view" model="ir.ui.view">
<field name="name">modal.rodoviario.occ.form.view (in l10n_br_cte)</field>
<field name="model">l10n_br_cte.modal.rodoviario.occ</field>
<field name="model">l10n_br_cte.modal.rodo.occ</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<field name="cte40_serie" />
<field name="cte40_nOcc" />
<field
name="cte40_emiOcc"
context="{
'form_view_ref': 'l10n_br_cte.res_partner_emiocc_form_view'
}"
domain="[
('cnpj_cpf', '!=', False),
]"
/>
</group>
</sheet>
</form>
</field>
</record>

<record id="res_partner_emiocc_form_view" model="ir.ui.view">
<field name="name">res.partner.emi.occ.form.view</field>
<field name="model">res.partner</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<field name="cte40_dEmi" />
<field name="cte40_CNPJ" />
<field name="cte40_cInt" />
<field name="cte40_IE" />
Expand Down
10 changes: 5 additions & 5 deletions l10n_br_cte/models/aquaviario.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from odoo.addons.spec_driven_model.models import spec_models


class Aquaviario(spec_models.StackedModel):
_name = "l10n_br_cte.modal.aquaviario"
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"
Expand All @@ -21,10 +21,10 @@ class Aquaviario(spec_models.StackedModel):

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

cte40_vPrest = fields.Monetary(related="document_id.cte40_vPrest")

cte40_vAFRMM = fields.Monetary(related="document_id.cte40_vAFRMM")

cte40_vPrest = fields.Monetary(related="document_id.cte40_vPrest")

cte40_xNavio = fields.Char(related="document_id.cte40_xNavio")

cte40_nViag = fields.Char(related="document_id.cte40_nViag")
Expand All @@ -43,7 +43,7 @@ def _prepare_dacte_values(self):


class Balsa(spec_models.SpecModel):
_name = "l10n_br_cte.modal.aquaviario.balsa"
_name = "l10n_br_cte.modal.aquav.balsa"
_inherit = "cte.40.balsa"
_description = "Grupo de informações das balsas"

Expand Down
89 changes: 50 additions & 39 deletions l10n_br_cte/models/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from erpbrasil.assinatura import certificado as cert
from erpbrasil.transmissao import TransmissaoSOAP
from nfelib.cte.bindings.v4_0.cte_v4_00 import Cte
from nfelib.nfe.ws.edoc_legacy import CTeAdapter as edoc_cte
from requests import Session

from odoo import _, api, fields
Expand Down Expand Up @@ -42,7 +43,7 @@
try:
pass
except ImportError:
_logger.error("Biblioteca erpbrasil.base não instalada")
_logger.error("Biblioteca erpbrasil.base não in stalada")


def filter_processador_edoc_cte(record):
Expand Down Expand Up @@ -129,7 +130,7 @@ def _inverse_cte40_Id(self):
string="cte40_cUF",
)

cte40_cCT = fields.Char(related="document_key")
cte40_cCT = fields.Char(compute="_compute_cct")

cfop_id = fields.Many2one(
comodel_name="l10n_br_fiscal.cfop",
Expand Down Expand Up @@ -291,6 +292,11 @@ def _compute_cDV(self):
if rec.document_key:
rec.cte40_cDV = rec.document_key[:-1]

def _compute_cct(self):
for rec in self:
if rec.document_key:
rec.cte40_cCT = rec.document_key[35:43]

@api.depends("partner_id", "company_id")
def _compute_cte40_data(self):
for doc in self:
Expand Down Expand Up @@ -515,7 +521,7 @@ def _default_cte40_autxml(self):
cte40_infRespTec = fields.Many2one(
comodel_name="res.partner",
compute="_compute_infresptec",
string="Responsável Técnico MDFe",
string="Responsável Técnico CTe",
)

##########################
Expand All @@ -534,7 +540,7 @@ def _compute_infresptec(self):

cte40_modal = fields.Selection(related="transport_modal")

cte40_versaoModal = fields.Char(CTE_MODAL_VERSION_DEFAULT)
cte40_versaoModal = fields.Char(default=CTE_MODAL_VERSION_DEFAULT)

# Campos do Modal Aereo
modal_aereo_id = fields.Many2one(comodel_name="l10n_br_cte.modal.aereo")
Expand Down Expand Up @@ -608,10 +614,10 @@ def _compute_infresptec(self):
)

# Campos do Modal Aquaviario
modal_aquaviario_id = fields.Many2one(comodel_name="l10n_br_cte.modal.aquaviario")
modal_aquaviario_id = fields.Many2one(comodel_name="l10n_br_cte.modal.aquav")

cte40_vPrest = fields.Monetary(
compute="_compute_cte40_vPrest",
compute="_compute_cte40_vPrest", # FIX
store=True,
string="Valor da Prestação Base de Cálculo",
)
Expand All @@ -638,7 +644,7 @@ def _compute_infresptec(self):
)

cte40_irin = fields.Char(
string="Irin do navio sempre deverá",
string="Irin do navio",
help="Irin do navio sempre deverá ser informado",
)

Expand All @@ -655,13 +661,13 @@ def _compute_infresptec(self):
)

cte40_balsa = fields.One2many(
comodel_name="l10n_br_cte.modal.aquaviario.balsa",
comodel_name="l10n_br_cte.modal.aquav.balsa",
inverse_name="document_id",
string="Grupo de informações das balsas",
)

# Campos do Modal Dutoviario
modal_dutoviario_id = fields.Many2one(comodel_name="l10n_br_cte.modal.dutoviario")
modal_dutoviario_id = fields.Many2one(comodel_name="l10n_br_cte.modal.duto")

cte40_dIni = fields.Date(string="Data de Início da prestação do serviço")

Expand All @@ -670,7 +676,7 @@ def _compute_infresptec(self):
cte40_vTar = fields.Float(string="Valor da tarifa")

# Campos do Modal Ferroviario
modal_ferroviario_id = fields.Many2one(comodel_name="l10n_br_cte.modal.ferroviario")
modal_ferroviario_id = fields.Many2one(comodel_name="l10n_br_cte.modal.ferrov")

cte40_tpTraf = fields.Selection(
selection=FERROV_TPTRAF,
Expand Down Expand Up @@ -718,15 +724,15 @@ def _compute_infresptec(self):
)

# Campos do Modal rodoviario
modal_rodoviario_id = fields.Many2one(comodel_name="l10n_br_cte.modal.rodoviario")
modal_rodoviario_id = fields.Many2one(comodel_name="l10n_br_cte.modal.rodo")

cte40_RNTRC = fields.Char(
string="RNTRC",
help="Registro Nacional de Transportadores Rodoviários de Carga",
)

cte40_occ = fields.One2many(
comodel_name="l10n_br_cte.modal.rodoviario.occ",
comodel_name="l10n_br_cte.modal.rodo.occ",
inverse_name="document_id",
string="Ordens de Coleta associados",
)
Expand All @@ -744,7 +750,6 @@ def _compute_cte40_vPrest(self):

def _export_fields_cte_40_tcte_infmodal(self, xsd_fields, class_obj, export_dict):
self = self.with_context(module="l10n_br_cte")

if self.cte40_modal == "1":
export_dict["any_element"] = self._export_modal_rodoviario()
elif self.cte40_modal == "2":
Expand Down Expand Up @@ -819,16 +824,18 @@ def _processador(self):
session = Session()
session.verify = False
transmissao = TransmissaoSOAP(certificado, session)
return Cte(
return edoc_cte(
transmissao,
self.company_id.state_id.ibge_code,
self.company_id.state_id.id,
self.cte40_versao,
self.cte40_tpAmb,
)

def _document_export(self, pretty_print=True):
result = super()._document_export()
for record in self.filtered(filter_processador_edoc_cte):
edoc = record.serialize()[0]
record._processador()
processador = record._processador()
xml_file = edoc.to_xml()
event_id = self.event_ids.create_event_save_xml(
company_id=self.company_id,
Expand All @@ -840,15 +847,15 @@ def _document_export(self, pretty_print=True):
document_id=self,
)
record.authorization_event_id = event_id
# xml_assinado = processador.assinar_edoc(edoc, edoc.infCte.Id)
# self._valida_xml(xml_assinado)
xml_assinado = processador.assina_raiz(edoc, edoc.infCte.Id)
self._valida_xml(xml_assinado)
return result

# def _valida_xml(self, xml_file):
# self.ensure_one()
# erros = Cte.schema_validation(xml_file)
# erros = "\n".join(erros)
# self.write({"xml_error_message": erros or False})
def _valida_xml(self, xml_file):
self.ensure_one()
erros = Cte.schema_validation(xml_file)
erros = "\n".join(erros)
self.write({"xml_error_message": erros or False})

def atualiza_status_cte(self, infProt, xml_file):
self.ensure_one()
Expand Down Expand Up @@ -884,34 +891,38 @@ def atualiza_status_cte(self, infProt, xml_file):
def _eletronic_document_send(self):
super(CTe, self)._eletronic_document_send()
for record in self.filtered(filter_processador_edoc_cte):
if self.xml_error_message:
if record.xml_error_message:
return
record._processador()
for _edoc in record.serialize():
processador = record._processador()
for edoc in record.serialize():
processo = None
# for p in processador.processar_documento(edoc):
# processo = p
if processo.webservice == "cteAutorizacaoLote":
record.authorization_event_id._save_event_file(
processo.envio_xml.decode("utf-8"), "xml"
)
for p in processador.processar_documento(edoc):
processo = p
if processo.webservice == "cteRecepcaoLote":
record.authorization_event_id._save_event_file(
processo.envio_xml, "xml"
)

if processo.resposta.cStat in LOTE_PROCESSADO + ["100"]:
record.atualiza_status_cte(
processo.protocolo.infProt, processo.processo_xml.decode("utf-8")
)
elif processo.resposta.cStat == "225":
state = SITUACAO_EDOC_REJEITADA
record.atualiza_status_cte(processo)

record._change_state(state)
elif processo.resposta.cStat in DENEGADO:
record._change_state(SITUACAO_EDOC_DENEGADA)
record.write(
{
"status_code": processo.resposta.cStat,
"status_name": processo.resposta.xMotivo,
}
)

else:
record._change_state(SITUACAO_EDOC_REJEITADA)
record.write(
{
"status_code": processo.resposta.cStat,
"status_name": processo.resposta.xMotivo,
}
)
return

def _document_cancel(self, justificative):
result = super(CTe, self)._document_cancel(justificative)
Expand Down
6 changes: 3 additions & 3 deletions l10n_br_cte/models/document_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,17 @@ def _compute_choice_icms(self):
record.cte40_CST = record.icms_cst_id.code
elif record.icms_cst_id.code in ICMS_SN_CST:
record.cte40_choice_icms = "cte40_ICMSSN"
record.cte40_CST = "01"
record.cte40_CST = "90"

def _export_fields_icms(self):
icms = {
"CST": self.icms_cst_id.code,
"CST": self.cte40_CST,
"vBC": str("%.02f" % self.icms_base),
"pRedBC": str("%.04f" % self.icms_reduction),
"pICMS": str("%.04f" % self.icms_percent),
"vICMS": str("%.02f" % self.icms_value),
"vICMSSubstituto": str("%.02f" % self.icms_substitute),
"indSN": self.cte40_indSN,
"indSN": int(self.cte40_indSN),
"vBCSTRet": str("%.02f" % self.icmsst_wh_base),
"vICMSSTRet": str("%.02f" % self.icmsst_wh_value),
"pICMSSTRet": str("%.02f" % self.icmsst_wh_percent),
Expand Down
7 changes: 2 additions & 5 deletions l10n_br_cte/models/document_transported_vehicles.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ class CTeCargoQuantityInfos(spec_models.SpecModel):
related="document_id.company_id.currency_id",
)

cte40_chassi = fields.Char(string="Chassi do veículo", required=True)
cte40_chassi = fields.Char(string="Chassi do veículo", required=True, size=17)

cte40_cCor = fields.Char(
string="Cor do veículo",
required=True,
)
cte40_cCor = fields.Char(string="Cor do veículo", required=True, size=4)

cte40_xCor = fields.Char(string="Descrição da cor", required=True)

Expand Down
Loading

0 comments on commit 8e74610

Please sign in to comment.