From 0cd99ceb08b691f346782e01e6e38efd17c14078 Mon Sep 17 00:00:00 2001 From: Pablo Date: Tue, 1 Aug 2023 11:44:40 -0300 Subject: [PATCH 01/16] [NEW] cte --- src/erpbrasil/edoc/cte.py | 286 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 286 insertions(+) create mode 100644 src/erpbrasil/edoc/cte.py diff --git a/src/erpbrasil/edoc/cte.py b/src/erpbrasil/edoc/cte.py new file mode 100644 index 0000000..6b6d9e2 --- /dev/null +++ b/src/erpbrasil/edoc/cte.py @@ -0,0 +1,286 @@ +from lxml import etree +from erpbrasil.transmissao import TransmissaoSOAP +from erpbrasil.edoc.edoc import DocumentoEletronico +from erpbrasil.assinatura.assinatura import Assinatura +from erpbrasil.base.fiscal.edoc import ChaveEdoc + +try: + from nfelib.cte.bindings.v4_0 import TconsStatServ + from nfelib.cte.bindings.v4_0 import RetConsStatServCte + from nfelib.cte.bindings.v4_0 import TconsSitCte + from nfelib.cte.bindings.v4_0 import RetConsSitCte + from nfelib.cte.bindings.v4_0 import Tcte + from nfelib.cte.bindings.v4_0 import RetCte + from nfelib.cte.bindings.v4_0 import EvCancCte + from nfelib.cte.bindings.v4_0 import RetEventoCte +except: + pass + +SIGLA_ESTADO = {'AC': 12, + 'AL': 27, + 'AM': 13, + 'AP': 16, + 'BA': 29, + 'CE': 23, + 'DF': 53, + 'ES': 32, + 'GO': 52, + 'MA': 21, + 'MG': 31, + 'MS': 50, + 'MT': 51, + 'PA': 15, + 'PB': 25, + 'PE': 26, + 'PI': 22, + 'PR': 41, + 'RJ': 33, + 'RN': 24, + 'RO': 11, + 'RR': 14, + 'RS': 43, + 'SC': 42, + 'SE': 28, + 'SP': 35, + 'TO': 17, + 'AN': 91, } + +SVSP = ["AP", "PE", "RR", "SP"] +SVRS = ["AC", "AL", "AM", "BA", "CE", "DF", "ES", "GO", "MA", "PA", "PB", "PI", "RJ", "RN", "RO", "SC", "SE", "TO"] + +SVSP_PRODUCAO = { + "CTeConsultaV4": "https://nfe.fazenda.sp.gov.br/CTeWS/WS/CTeConsultaV4.asmx?wsdl", + "CTeRecepcaoEventoV4": "https://nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoEventoV4.asmx?wsdl", + "CTeRecepcaoGTVeV4": "https://nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoGTVeV4.asmx?wsdl", + "CTeRecepcaoOSV4": "https://nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoOSV4.asmx?wsdl", + "CTeRecepcaoSincV4": "https://nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoSincV4.asmx?wsdl", + "CTeStatusServicoV4": "https://nfe.fazenda.sp.gov.br/CTeWS/WS/CTeStatusServicoV4.asmx?wsdl", + } + +SVSP_HOMOLOGACAO = { + "CTeConsultaV4": "https://homologacao.nfe.fazenda.sp.gov.br/CTeWS/WS/CTeConsultaV4.asmx?wsdl", + "CTeRecepcaoEventoV4": "https://homologacao.nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoEventoV4.asmx?wsdl", + "CTeRecepcaoGTVeV4": "https://homologacao.nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoGTVeV4.asmx?wsdl", + "CTeRecepcaoOSV4": "https://homologacao.nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoOSV4.asmx?wsdl", + "CTeRecepcaoSincV4": "https://homologacao.nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoSincV4.asmx?wsdl", + "CTeStatusServicoV4": "https://homologacao.nfe.fazenda.sp.gov.br/CTeWS/WS/CTeStatusServicoV4.asmx?wsdl", + } + +SVRS_PRODUCAO = { + "CTeStatusServicoV4": "https://cte.svrs.rs.gov.br/ws/CTeStatusServicoV4/CTeStatusServicoV4.asmx?wsdl", + "CTeConsultaV4": "https://cte.svrs.rs.gov.br/ws/CTeConsultaV4/CTeConsultaV4.asmx?wsdl", + "CTeRecepcaoSincV4": "https://cte.svrs.rs.gov.br/ws/CTeRecepcaoSincV4/CTeRecepcaoSincV4.asmx?wsdl", + "CTeRecepcaoOSV4": "https://cte.svrs.rs.gov.br/ws/CTeRecepcaoOSV4/CTeRecepcaoOSV4.asmx?wsdl", + "CTeRecepcaoGTVeV4": "https://cte.svrs.rs.gov.br/ws/CTeRecepcaoGTVeV4/CTeRecepcaoGTVeV4.asmx?wsdl", + "CTeRecepcaoEventoV4": "https://cte.svrs.rs.gov.br/ws/CTeRecepcaoEventoV4/CTeRecepcaoEventoV4.asmx?wsdl", + } + +SVRS_HOMOLOGACAO = { + "CTeStatusServicoV4": "https://cte-homologacao.svrs.rs.gov.br/ws/CTeStatusServicoV4/CTeStatusServicoV4.asmx?wsdl", + "CTeConsultaV4": "https://cte-homologacao.svrs.rs.gov.br/ws/CTeConsultaV4/CTeConsultaV4.asmx?wsdl", + "CTeRecepcaoSincV4": "https://cte-homologacao.svrs.rs.gov.br/ws/CTeRecepcaoSincV4/CTeRecepcaoSincV4.asmx?wsdl", + "CTeRecepcaoOSV4": "https://cte-homologacao.svrs.rs.gov.br/ws/CTeRecepcaoOSV4/CTeRecepcaoOSV4.asmx?wsdl", + "CTeRecepcaoGTVeV4": "https://cte-homologacao.svrs.rs.gov.br/ws/CTeRecepcaoGTVeV4/CTeRecepcaoGTVeV4.asmx?wsdl", + "CTeRecepcaoEventoV4": "https://cte-homologacao.svrs.rs.gov.br/ws/CTeRecepcaoEventoV4/CTeRecepcaoEventoV4.asmx?wsdl", + } + +MT_PRODUCAO = { + "CTeConsultaV4": "https://cte.sefaz.mt.gov.br/ctews2/services/CTeConsultaV4?wsdl", + "CTeRecepcaoEventoV4": "https://cte.sefaz.mt.gov.br/ctews2/services/CTeRecepcaoEventoV4?wsdl", + "CTeStatusServicoV4": "https://cte.sefaz.mt.gov.br/ctews2/services/CTeStatusServicoV4?wsdl", + "CTeRecepcaoSincV4": "https://cte.sefaz.mt.gov.br/ctews2/services/CTeRecepcaoSincV4?wsdl", + "CTeRecepcaoGTVeV4": "https://cte.sefaz.mt.gov.br/ctews2/services/CTeRecepcaoGTVeV4?wsdl", + "CTeRecepcaoOSV4": "https://cte.sefaz.mt.gov.br/ctews/services/CTeRecepcaoOSV4?wsdl", + } + +MT_HOMOLOGACAO = { + "CTeStatusServicoV4": "https://homologacao.sefaz.mt.gov.br/ctews2/services/CTeStatusServicoV4?wsdl", + "CTeConsultaV4": "https://homologacao.sefaz.mt.gov.br/ctews2/services/CTeConsultaV4?wsdl", + "CTeRecepcaoSincV4": "https://homologacao.sefaz.mt.gov.br/ctews2/services/CTeRecepcaoSincV4?wsdl", + "CTeRecepcaoGTVeV4": "https://homologacao.sefaz.mt.gov.br/ctews2/services/CTeRecepcaoGTVeV4?wsdl", + "CTeRecepcaoEventoV4": "https://homologacao.sefaz.mt.gov.br/ctews2/services/CTeRecepcaoEventoV4?wsdl", + "CTeRecepcaoOSV4": "https://homologacao.sefaz.mt.gov.br/ctews/services/CTeRecepcaoOSV4?wsdl", + } + +MS_PRODUCAO = { + "CTeRecepcaoSincV4": "https://producao.cte.ms.gov.br/ws/CTeRecepcaoSincV4?wsdl", + "CTeStatusServicoV4": "https://producao.cte.ms.gov.br/ws/CTeStatusServicoV4?wsdl", + "CTeConsultaV4": "https://producao.cte.ms.gov.br/ws/CTeConsultaV4?wsdl", + "CTeRecepcaoEventoV4": "https://producao.cte.ms.gov.br/ws/CTeRecepcaoEventoV4?wsdl", + "CTeRecepcaoOSV4": "https://producao.cte.ms.gov.br/ws/CTeRecepcaoOSV4?wsdl", + "CTeRecepcaoGTVeV4": "https://producao.cte.ms.gov.br/ws/CTeRecepcaoGTVeV4?wsdl", + } + +MS_HOMOLOGACAO = { + "CTeStatusServicoV4": "https://homologacao.cte.ms.gov.br/ws/CTeStatusServicoV4?wsdl", + "CTeRecepcaoEventoV4": "https://homologacao.cte.ms.gov.br/ws/CTeRecepcaoEventoV4?wsdl", + "CTeConsultaV4": "https://homologacao.cte.ms.gov.br/ws/CTeConsultaV4?wsdl", + "CTeRecepcaoSincV4": "https://homologacao.cte.ms.gov.br/ws/CTeRecepcaoSincV4?wsdl", + } + +MG_PRODUCAO = { + "CTeRecepcaoSincV4": "https://cte.fazenda.mg.gov.br/cte/services/CTeRecepcaoSincV4?wsdl", + "CTeRecepcaoGTVeV4": "https://cte.fazenda.mg.gov.br/cte/services/CTeRecepcaoGTVeV4?wsdl", + "CTeRecepcaoOSV4": "https://cte.fazenda.mg.gov.br/cte/services/CTeRecepcaoOSV4?wsdl", + "CTeConsultaV4": "https://cte.fazenda.mg.gov.br/cte/services/CTeConsultaV4?wsdl", + "CTeStatusServicoV4": "https://cte.fazenda.mg.gov.br/cte/services/CTeStatusServicoV4?wsdl", + "CTeRecepcaoEventoV4": "https://cte.fazenda.mg.gov.br/cte/services/CTeRecepcaoEventoV4?wsdl", + } + +MG_HOMOLOGACAO = { + "CTeRecepcaoSincV4": "https://hcte.fazenda.mg.gov.br/cte/services/CTeRecepcaoSincV4?wsdl", + "CTeRecepcaoOSV4": "https://hcte.fazenda.mg.gov.br/cte/services/CTeRecepcaoOSV4?wsdl", + "CTeRecepcaoGTVeV4": "https://hcte.fazenda.mg.gov.br/cte/services/CTeRecepcaoGTVeV4?wsdl", + "CTeConsultaV4": "https://hcte.fazenda.mg.gov.br/cte/services/CTeConsultaV4?wsdl", + "CTeStatusServicoV4": "https://hcte.fazenda.mg.gov.br/cte/services/CTeStatusServicoV4?wsdl", + "CTeRecepcaoEventoV4": "https://hcte.fazenda.mg.gov.br/cte/services/CTeRecepcaoEventoV4?wsdl", + } + +PR_PRODUCAO = { + "CTeRecepcaoSincV4": "https://cte.fazenda.pr.gov.br/cte4/CTeRecepcaoSincV4?wsdl", + "CTeRecepcaoGTVeV4": "https://cte.fazenda.pr.gov.br/cte4/CTeRecepcaoGTVeV4?wsdl", + "CTeRecepcaoOSV4": "https://cte.fazenda.pr.gov.br/cte4/CTeRecepcaoOSV4?wsdl", + "CTeConsultaV4": "https://cte.fazenda.pr.gov.br/cte4/CTeConsultaV4?wsdl", + "CTeStatusServicoV4": "https://cte.fazenda.pr.gov.br/cte4/CTeStatusServicoV4?wsdl", + "CTeRecepcaoEventoV4": "https://cte.fazenda.pr.gov.br/cte4/CTeRecepcaoEventoV4?wsdl", + } + +PR_HOMOLOGACAO = { + "CTeConsultaV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeConsultaV4?wsdl", + "CTeStatusServicoV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeStatusServicoV4?wsdl", + "CTeRecepcaoSincV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeRecepcaoSincV4?wsdl", + "CTeRecepcaoEventoV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeRecepcaoEventoV4?wsdl", + "CTeRecepcaoGTVeV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeRecepcaoGTVeV4?wsdl", + "CTeRecepcaoOSV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeRecepcaoOSV4?wsdl", + } + + +class CTe(DocumentoEletronico): + _namespace = 'http://www.portalfiscal.inf.br/cte' + _edoc_situacao_arquivo_recebido_com_sucesso = '103' + _edoc_situacao_servico_em_operacao = '107' + _consulta_servico_ao_enviar = True + _consulta_documento_antes_de_enviar = True + _maximo_tentativas_consulta_recibo = 5 + + def __init__(self, config, xml): + self.config = config + self._xml = xml + self._ambiente = 2 + self._uf = "SP" + self._versao = 4.00 + self._chaveCte = 123456789 + self.assinatura = Assinatura(self.config) # self.config.certificado + self.transmissao = TransmissaoSOAP(self.config) # self.config.certificado + super(CTe, self).__init__(self.transmissao) + + def status_servico(self): + raiz = TconsStatServ( + tpAmb=self._ambiente, + cUF=SIGLA_ESTADO[self._uf], + versao=self._versao + ) + return self._post( + raiz=raiz, + url=self._search_url("CTeStatusServicoV4"), + operacao="cteStatusServicoCT", + classe=RetConsStatServCte + ) + + def consulta_documento(self, chave): + raiz = TconsSitCte( + tpAmb=self._ambiente, + chCTe=chave, + versao=self._versao + ) + return self._post( + raiz=raiz, + url=self._search_url("CTeConsultaV4"), + operacao="cteConsultaCT", + classe=RetConsSitCte + ) + + def envia_documento(self, edoc): + xml_assinado = self.assina_raiz(edoc, edoc.infCTe.Id) + raiz = Tcte( + infCte=edoc, + signature=self.assinatura + ) + xml_envio_string, xml_envio_etree = self._generateds_to_string_etree( + raiz + ) + xml_envio_etree.append(etree.fromstring(xml_assinado)) + return self._post( + raiz=xml_envio_etree, + url=self._search_url("CTeRecepcaoSincV4"), + operacao="cteRecepcao", + classe=RetCte + ) + + def cancela_documento(self, doc_numero, justificativa): + raiz = EvCancCte( + descEvento='Cancelamento', + nProt=doc_numero, + xJust=justificativa + ) + return self._post( + raiz=raiz, + url=self._search_url("CTeRecepcaoEventoV4"), + operacao="cteRecepcaoEvento", + classe=RetEventoCte + ) + + def _gerar_chave(self): + """ + 0 2 6 20 22 25 34 35 43 --> índice + | | | | | | | | | + | | | | | | | | | + CTe 32 1712 32438772000104 57 001 000199075 1 39868226 3 + """ + chave = ChaveEdoc() + chave.codigo_uf = "Código da UF do emitente do Documento Fiscal" + chave.cUF = "Código da UF do emitente do Documento Fiscal" + chave.AAMM = "Ano e Mês de emissão do CTe" + chave.CNPJ = "CNPJ do emitente" + chave.mod = "Modelo do Documento Fiscal" + chave.serie = "Série do Documento Fiscal" + chave.nCT = "Número do Documento Fiscal" + chave.tpEmis = "forma de emissão do CTe" + chave.cCT = "Código Numérico que compõe a Chave de Acesso" + chave.cDV = "Dígito Verificador da Chave de Acesso" + return chave + + def _search_url(self, service): + if self._uf == "MG": + if self._ambiente == 1: + return MG_PRODUCAO[service] + else: + return MG_HOMOLOGACAO[service] + elif self._uf == "MS": + if self._ambiente == 1: + return MS_PRODUCAO[service] + else: + return MS_HOMOLOGACAO[service] + elif self._uf == "MT": + if self._ambiente == 1: + return MT_PRODUCAO[service] + else: + return MT_HOMOLOGACAO[service] + elif self._uf == "PR": + if self._ambiente == 1: + return PR_PRODUCAO[service] + else: + return PR_HOMOLOGACAO[service] + elif self._uf == "SVSP": + if self._ambiente == 1: + return SVSP_PRODUCAO[service] + else: + return SVSP_HOMOLOGACAO[service] + elif self._uf == "SVRS": + if self._ambiente == 1: + return SVRS_PRODUCAO[service] + else: + return SVRS_HOMOLOGACAO[service] + + def consulta_recibo(self): + pass From a59735129b38cdfa295cc2e5233aab89ad2bc1fe Mon Sep 17 00:00:00 2001 From: Pablo Date: Fri, 4 Aug 2023 09:14:23 -0300 Subject: [PATCH 02/16] [FIX] substitucao do generateds --- src/erpbrasil/edoc/cte.py | 41 ++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/erpbrasil/edoc/cte.py b/src/erpbrasil/edoc/cte.py index 6b6d9e2..5d76c5f 100644 --- a/src/erpbrasil/edoc/cte.py +++ b/src/erpbrasil/edoc/cte.py @@ -3,6 +3,7 @@ from erpbrasil.edoc.edoc import DocumentoEletronico from erpbrasil.assinatura.assinatura import Assinatura from erpbrasil.base.fiscal.edoc import ChaveEdoc +from .resposta import analisar_retorno_raw try: from nfelib.cte.bindings.v4_0 import TconsStatServ @@ -13,6 +14,7 @@ from nfelib.cte.bindings.v4_0 import RetCte from nfelib.cte.bindings.v4_0 import EvCancCte from nfelib.cte.bindings.v4_0 import RetEventoCte + from nfelib.cte.bindings.v4_0 import Cte except: pass @@ -206,9 +208,8 @@ def envia_documento(self, edoc): infCte=edoc, signature=self.assinatura ) - xml_envio_string, xml_envio_etree = self._generateds_to_string_etree( - raiz - ) + xml_envio_string = raiz.to_xml() + xml_envio_etree = xml_envio_string.from_xml() xml_envio_etree.append(etree.fromstring(xml_assinado)) return self._post( raiz=xml_envio_etree, @@ -230,24 +231,25 @@ def cancela_documento(self, doc_numero, justificativa): classe=RetEventoCte ) - def _gerar_chave(self): + def _gerar_chave(self, edoc): """ 0 2 6 20 22 25 34 35 43 --> índice | | | | | | | | | | | | | | | | | | CTe 32 1712 32438772000104 57 001 000199075 1 39868226 3 """ + cte = Cte(edoc) chave = ChaveEdoc() - chave.codigo_uf = "Código da UF do emitente do Documento Fiscal" - chave.cUF = "Código da UF do emitente do Documento Fiscal" - chave.AAMM = "Ano e Mês de emissão do CTe" - chave.CNPJ = "CNPJ do emitente" - chave.mod = "Modelo do Documento Fiscal" - chave.serie = "Série do Documento Fiscal" - chave.nCT = "Número do Documento Fiscal" - chave.tpEmis = "forma de emissão do CTe" - chave.cCT = "Código Numérico que compõe a Chave de Acesso" - chave.cDV = "Dígito Verificador da Chave de Acesso" + chave.cUF = cte.InfCte.Ide.cUF + dhEmi = cte.InfCte.Ide.dhEmi + chave.AAMM = dhEmi[2:4] + dhEmi[5:7] + chave.CNPJ = cte.InfCte.Emit.CNPJ + chave.mod = cte.InfCte.Ide.mod + chave.serie = cte.InfCte.Ide.serie + chave.nCT = cte.InfCte.Ide.nCT + chave.tpEmis = cte.InfCte.Ide.tpEmis + chave.cCT = cte.InfCte.Ide.cCT + chave.cDV = cte.InfCte.Ide.cDV return chave def _search_url(self, service): @@ -284,3 +286,14 @@ def _search_url(self, service): def consulta_recibo(self): pass + + def _post(self, raiz, url, operacao, classe): + xml_string = raiz.to_xml() + xml_etree = xml_string.from_xml() + with self._transmissao.cliente(url): + retorno = self._transmissao.enviar( + operacao, xml_etree + ) + return analisar_retorno_raw( + operacao, raiz, xml_string, retorno, classe + ) From ca342274e91c2a24298b77446650013ef931a391 Mon Sep 17 00:00:00 2001 From: Pablo Date: Fri, 4 Aug 2023 12:04:25 -0300 Subject: [PATCH 03/16] [FIX] substituicao de constantes por variaveis no metodo init --- src/erpbrasil/edoc/cte.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/erpbrasil/edoc/cte.py b/src/erpbrasil/edoc/cte.py index 5d76c5f..3f036f9 100644 --- a/src/erpbrasil/edoc/cte.py +++ b/src/erpbrasil/edoc/cte.py @@ -168,10 +168,10 @@ class CTe(DocumentoEletronico): def __init__(self, config, xml): self.config = config self._xml = xml - self._ambiente = 2 - self._uf = "SP" + self._ambiente = self.config #tpAmb + self._uf = self.config #uf self._versao = 4.00 - self._chaveCte = 123456789 + self._chaveCte = self._gerar_chave(self.config) self.assinatura = Assinatura(self.config) # self.config.certificado self.transmissao = TransmissaoSOAP(self.config) # self.config.certificado super(CTe, self).__init__(self.transmissao) From 319921c51864816d030579ccde5763257aee3dfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Felipe=20Mil=C3=A9o?= Date: Thu, 27 Jun 2024 11:21:49 -0300 Subject: [PATCH 04/16] [FIX] pre-commit --- src/erpbrasil/edoc/cte.py | 337 ++++++++++++++++++++------------------ 1 file changed, 174 insertions(+), 163 deletions(-) diff --git a/src/erpbrasil/edoc/cte.py b/src/erpbrasil/edoc/cte.py index 3f036f9..5ff4319 100644 --- a/src/erpbrasil/edoc/cte.py +++ b/src/erpbrasil/edoc/cte.py @@ -1,166 +1,192 @@ +from contextlib import suppress + from lxml import etree -from erpbrasil.transmissao import TransmissaoSOAP -from erpbrasil.edoc.edoc import DocumentoEletronico + from erpbrasil.assinatura.assinatura import Assinatura from erpbrasil.base.fiscal.edoc import ChaveEdoc +from erpbrasil.edoc.edoc import DocumentoEletronico +from erpbrasil.transmissao import TransmissaoSOAP + from .resposta import analisar_retorno_raw -try: - from nfelib.cte.bindings.v4_0 import TconsStatServ - from nfelib.cte.bindings.v4_0 import RetConsStatServCte - from nfelib.cte.bindings.v4_0 import TconsSitCte - from nfelib.cte.bindings.v4_0 import RetConsSitCte - from nfelib.cte.bindings.v4_0 import Tcte - from nfelib.cte.bindings.v4_0 import RetCte - from nfelib.cte.bindings.v4_0 import EvCancCte - from nfelib.cte.bindings.v4_0 import RetEventoCte - from nfelib.cte.bindings.v4_0 import Cte -except: - pass +with suppress(ImportError): + from nfelib.cte.bindings.v4_0 import ( + Cte, + EvCancCte, + RetConsSitCte, + RetConsStatServCte, + RetCte, + RetEventoCte, + TconsSitCte, + TconsStatServ, + Tcte, + ) + -SIGLA_ESTADO = {'AC': 12, - 'AL': 27, - 'AM': 13, - 'AP': 16, - 'BA': 29, - 'CE': 23, - 'DF': 53, - 'ES': 32, - 'GO': 52, - 'MA': 21, - 'MG': 31, - 'MS': 50, - 'MT': 51, - 'PA': 15, - 'PB': 25, - 'PE': 26, - 'PI': 22, - 'PR': 41, - 'RJ': 33, - 'RN': 24, - 'RO': 11, - 'RR': 14, - 'RS': 43, - 'SC': 42, - 'SE': 28, - 'SP': 35, - 'TO': 17, - 'AN': 91, } +SIGLA_ESTADO = { + "AC": 12, + "AL": 27, + "AM": 13, + "AP": 16, + "BA": 29, + "CE": 23, + "DF": 53, + "ES": 32, + "GO": 52, + "MA": 21, + "MG": 31, + "MS": 50, + "MT": 51, + "PA": 15, + "PB": 25, + "PE": 26, + "PI": 22, + "PR": 41, + "RJ": 33, + "RN": 24, + "RO": 11, + "RR": 14, + "RS": 43, + "SC": 42, + "SE": 28, + "SP": 35, + "TO": 17, + "AN": 91, +} SVSP = ["AP", "PE", "RR", "SP"] -SVRS = ["AC", "AL", "AM", "BA", "CE", "DF", "ES", "GO", "MA", "PA", "PB", "PI", "RJ", "RN", "RO", "SC", "SE", "TO"] +SVRS = [ + "AC", + "AL", + "AM", + "BA", + "CE", + "DF", + "ES", + "GO", + "MA", + "PA", + "PB", + "PI", + "RJ", + "RN", + "RO", + "SC", + "SE", + "TO", +] SVSP_PRODUCAO = { - "CTeConsultaV4": "https://nfe.fazenda.sp.gov.br/CTeWS/WS/CTeConsultaV4.asmx?wsdl", - "CTeRecepcaoEventoV4": "https://nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoEventoV4.asmx?wsdl", - "CTeRecepcaoGTVeV4": "https://nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoGTVeV4.asmx?wsdl", - "CTeRecepcaoOSV4": "https://nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoOSV4.asmx?wsdl", - "CTeRecepcaoSincV4": "https://nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoSincV4.asmx?wsdl", - "CTeStatusServicoV4": "https://nfe.fazenda.sp.gov.br/CTeWS/WS/CTeStatusServicoV4.asmx?wsdl", - } + "CTeConsultaV4": "https://nfe.fazenda.sp.gov.br/CTeWS/WS/CTeConsultaV4.asmx?wsdl", + "CTeRecepcaoEventoV4": "https://nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoEventoV4.asmx?wsdl", + "CTeRecepcaoGTVeV4": "https://nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoGTVeV4.asmx?wsdl", + "CTeRecepcaoOSV4": "https://nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoOSV4.asmx?wsdl", + "CTeRecepcaoSincV4": "https://nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoSincV4.asmx?wsdl", + "CTeStatusServicoV4": "https://nfe.fazenda.sp.gov.br/CTeWS/WS/CTeStatusServicoV4.asmx?wsdl", +} SVSP_HOMOLOGACAO = { - "CTeConsultaV4": "https://homologacao.nfe.fazenda.sp.gov.br/CTeWS/WS/CTeConsultaV4.asmx?wsdl", - "CTeRecepcaoEventoV4": "https://homologacao.nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoEventoV4.asmx?wsdl", - "CTeRecepcaoGTVeV4": "https://homologacao.nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoGTVeV4.asmx?wsdl", - "CTeRecepcaoOSV4": "https://homologacao.nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoOSV4.asmx?wsdl", - "CTeRecepcaoSincV4": "https://homologacao.nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoSincV4.asmx?wsdl", - "CTeStatusServicoV4": "https://homologacao.nfe.fazenda.sp.gov.br/CTeWS/WS/CTeStatusServicoV4.asmx?wsdl", - } + "CTeConsultaV4": "https://homologacao.nfe.fazenda.sp.gov.br/CTeWS/WS/CTeConsultaV4.asmx?wsdl", + "CTeRecepcaoEventoV4": "https://homologacao.nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoEventoV4.asmx?wsdl", + "CTeRecepcaoGTVeV4": "https://homologacao.nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoGTVeV4.asmx?wsdl", + "CTeRecepcaoOSV4": "https://homologacao.nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoOSV4.asmx?wsdl", + "CTeRecepcaoSincV4": "https://homologacao.nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoSincV4.asmx?wsdl", + "CTeStatusServicoV4": "https://homologacao.nfe.fazenda.sp.gov.br/CTeWS/WS/CTeStatusServicoV4.asmx?wsdl", +} SVRS_PRODUCAO = { - "CTeStatusServicoV4": "https://cte.svrs.rs.gov.br/ws/CTeStatusServicoV4/CTeStatusServicoV4.asmx?wsdl", - "CTeConsultaV4": "https://cte.svrs.rs.gov.br/ws/CTeConsultaV4/CTeConsultaV4.asmx?wsdl", - "CTeRecepcaoSincV4": "https://cte.svrs.rs.gov.br/ws/CTeRecepcaoSincV4/CTeRecepcaoSincV4.asmx?wsdl", - "CTeRecepcaoOSV4": "https://cte.svrs.rs.gov.br/ws/CTeRecepcaoOSV4/CTeRecepcaoOSV4.asmx?wsdl", - "CTeRecepcaoGTVeV4": "https://cte.svrs.rs.gov.br/ws/CTeRecepcaoGTVeV4/CTeRecepcaoGTVeV4.asmx?wsdl", - "CTeRecepcaoEventoV4": "https://cte.svrs.rs.gov.br/ws/CTeRecepcaoEventoV4/CTeRecepcaoEventoV4.asmx?wsdl", - } + "CTeStatusServicoV4": "https://cte.svrs.rs.gov.br/ws/CTeStatusServicoV4/CTeStatusServicoV4.asmx?wsdl", + "CTeConsultaV4": "https://cte.svrs.rs.gov.br/ws/CTeConsultaV4/CTeConsultaV4.asmx?wsdl", + "CTeRecepcaoSincV4": "https://cte.svrs.rs.gov.br/ws/CTeRecepcaoSincV4/CTeRecepcaoSincV4.asmx?wsdl", + "CTeRecepcaoOSV4": "https://cte.svrs.rs.gov.br/ws/CTeRecepcaoOSV4/CTeRecepcaoOSV4.asmx?wsdl", + "CTeRecepcaoGTVeV4": "https://cte.svrs.rs.gov.br/ws/CTeRecepcaoGTVeV4/CTeRecepcaoGTVeV4.asmx?wsdl", + "CTeRecepcaoEventoV4": "https://cte.svrs.rs.gov.br/ws/CTeRecepcaoEventoV4/CTeRecepcaoEventoV4.asmx?wsdl", +} SVRS_HOMOLOGACAO = { - "CTeStatusServicoV4": "https://cte-homologacao.svrs.rs.gov.br/ws/CTeStatusServicoV4/CTeStatusServicoV4.asmx?wsdl", - "CTeConsultaV4": "https://cte-homologacao.svrs.rs.gov.br/ws/CTeConsultaV4/CTeConsultaV4.asmx?wsdl", - "CTeRecepcaoSincV4": "https://cte-homologacao.svrs.rs.gov.br/ws/CTeRecepcaoSincV4/CTeRecepcaoSincV4.asmx?wsdl", - "CTeRecepcaoOSV4": "https://cte-homologacao.svrs.rs.gov.br/ws/CTeRecepcaoOSV4/CTeRecepcaoOSV4.asmx?wsdl", - "CTeRecepcaoGTVeV4": "https://cte-homologacao.svrs.rs.gov.br/ws/CTeRecepcaoGTVeV4/CTeRecepcaoGTVeV4.asmx?wsdl", - "CTeRecepcaoEventoV4": "https://cte-homologacao.svrs.rs.gov.br/ws/CTeRecepcaoEventoV4/CTeRecepcaoEventoV4.asmx?wsdl", - } + "CTeStatusServicoV4": "https://cte-homologacao.svrs.rs.gov.br/ws/CTeStatusServicoV4/CTeStatusServicoV4.asmx?wsdl", + "CTeConsultaV4": "https://cte-homologacao.svrs.rs.gov.br/ws/CTeConsultaV4/CTeConsultaV4.asmx?wsdl", + "CTeRecepcaoSincV4": "https://cte-homologacao.svrs.rs.gov.br/ws/CTeRecepcaoSincV4/CTeRecepcaoSincV4.asmx?wsdl", + "CTeRecepcaoOSV4": "https://cte-homologacao.svrs.rs.gov.br/ws/CTeRecepcaoOSV4/CTeRecepcaoOSV4.asmx?wsdl", + "CTeRecepcaoGTVeV4": "https://cte-homologacao.svrs.rs.gov.br/ws/CTeRecepcaoGTVeV4/CTeRecepcaoGTVeV4.asmx?wsdl", + "CTeRecepcaoEventoV4": "https://cte-homologacao.svrs.rs.gov.br/ws/CTeRecepcaoEventoV4/CTeRecepcaoEventoV4.asmx?wsdl", +} MT_PRODUCAO = { - "CTeConsultaV4": "https://cte.sefaz.mt.gov.br/ctews2/services/CTeConsultaV4?wsdl", - "CTeRecepcaoEventoV4": "https://cte.sefaz.mt.gov.br/ctews2/services/CTeRecepcaoEventoV4?wsdl", - "CTeStatusServicoV4": "https://cte.sefaz.mt.gov.br/ctews2/services/CTeStatusServicoV4?wsdl", - "CTeRecepcaoSincV4": "https://cte.sefaz.mt.gov.br/ctews2/services/CTeRecepcaoSincV4?wsdl", - "CTeRecepcaoGTVeV4": "https://cte.sefaz.mt.gov.br/ctews2/services/CTeRecepcaoGTVeV4?wsdl", - "CTeRecepcaoOSV4": "https://cte.sefaz.mt.gov.br/ctews/services/CTeRecepcaoOSV4?wsdl", - } + "CTeConsultaV4": "https://cte.sefaz.mt.gov.br/ctews2/services/CTeConsultaV4?wsdl", + "CTeRecepcaoEventoV4": "https://cte.sefaz.mt.gov.br/ctews2/services/CTeRecepcaoEventoV4?wsdl", + "CTeStatusServicoV4": "https://cte.sefaz.mt.gov.br/ctews2/services/CTeStatusServicoV4?wsdl", + "CTeRecepcaoSincV4": "https://cte.sefaz.mt.gov.br/ctews2/services/CTeRecepcaoSincV4?wsdl", + "CTeRecepcaoGTVeV4": "https://cte.sefaz.mt.gov.br/ctews2/services/CTeRecepcaoGTVeV4?wsdl", + "CTeRecepcaoOSV4": "https://cte.sefaz.mt.gov.br/ctews/services/CTeRecepcaoOSV4?wsdl", +} MT_HOMOLOGACAO = { - "CTeStatusServicoV4": "https://homologacao.sefaz.mt.gov.br/ctews2/services/CTeStatusServicoV4?wsdl", - "CTeConsultaV4": "https://homologacao.sefaz.mt.gov.br/ctews2/services/CTeConsultaV4?wsdl", - "CTeRecepcaoSincV4": "https://homologacao.sefaz.mt.gov.br/ctews2/services/CTeRecepcaoSincV4?wsdl", - "CTeRecepcaoGTVeV4": "https://homologacao.sefaz.mt.gov.br/ctews2/services/CTeRecepcaoGTVeV4?wsdl", - "CTeRecepcaoEventoV4": "https://homologacao.sefaz.mt.gov.br/ctews2/services/CTeRecepcaoEventoV4?wsdl", - "CTeRecepcaoOSV4": "https://homologacao.sefaz.mt.gov.br/ctews/services/CTeRecepcaoOSV4?wsdl", - } + "CTeStatusServicoV4": "https://homologacao.sefaz.mt.gov.br/ctews2/services/CTeStatusServicoV4?wsdl", + "CTeConsultaV4": "https://homologacao.sefaz.mt.gov.br/ctews2/services/CTeConsultaV4?wsdl", + "CTeRecepcaoSincV4": "https://homologacao.sefaz.mt.gov.br/ctews2/services/CTeRecepcaoSincV4?wsdl", + "CTeRecepcaoGTVeV4": "https://homologacao.sefaz.mt.gov.br/ctews2/services/CTeRecepcaoGTVeV4?wsdl", + "CTeRecepcaoEventoV4": "https://homologacao.sefaz.mt.gov.br/ctews2/services/CTeRecepcaoEventoV4?wsdl", + "CTeRecepcaoOSV4": "https://homologacao.sefaz.mt.gov.br/ctews/services/CTeRecepcaoOSV4?wsdl", +} MS_PRODUCAO = { - "CTeRecepcaoSincV4": "https://producao.cte.ms.gov.br/ws/CTeRecepcaoSincV4?wsdl", - "CTeStatusServicoV4": "https://producao.cte.ms.gov.br/ws/CTeStatusServicoV4?wsdl", - "CTeConsultaV4": "https://producao.cte.ms.gov.br/ws/CTeConsultaV4?wsdl", - "CTeRecepcaoEventoV4": "https://producao.cte.ms.gov.br/ws/CTeRecepcaoEventoV4?wsdl", - "CTeRecepcaoOSV4": "https://producao.cte.ms.gov.br/ws/CTeRecepcaoOSV4?wsdl", - "CTeRecepcaoGTVeV4": "https://producao.cte.ms.gov.br/ws/CTeRecepcaoGTVeV4?wsdl", - } + "CTeRecepcaoSincV4": "https://producao.cte.ms.gov.br/ws/CTeRecepcaoSincV4?wsdl", + "CTeStatusServicoV4": "https://producao.cte.ms.gov.br/ws/CTeStatusServicoV4?wsdl", + "CTeConsultaV4": "https://producao.cte.ms.gov.br/ws/CTeConsultaV4?wsdl", + "CTeRecepcaoEventoV4": "https://producao.cte.ms.gov.br/ws/CTeRecepcaoEventoV4?wsdl", + "CTeRecepcaoOSV4": "https://producao.cte.ms.gov.br/ws/CTeRecepcaoOSV4?wsdl", + "CTeRecepcaoGTVeV4": "https://producao.cte.ms.gov.br/ws/CTeRecepcaoGTVeV4?wsdl", +} MS_HOMOLOGACAO = { - "CTeStatusServicoV4": "https://homologacao.cte.ms.gov.br/ws/CTeStatusServicoV4?wsdl", - "CTeRecepcaoEventoV4": "https://homologacao.cte.ms.gov.br/ws/CTeRecepcaoEventoV4?wsdl", - "CTeConsultaV4": "https://homologacao.cte.ms.gov.br/ws/CTeConsultaV4?wsdl", - "CTeRecepcaoSincV4": "https://homologacao.cte.ms.gov.br/ws/CTeRecepcaoSincV4?wsdl", - } + "CTeStatusServicoV4": "https://homologacao.cte.ms.gov.br/ws/CTeStatusServicoV4?wsdl", + "CTeRecepcaoEventoV4": "https://homologacao.cte.ms.gov.br/ws/CTeRecepcaoEventoV4?wsdl", + "CTeConsultaV4": "https://homologacao.cte.ms.gov.br/ws/CTeConsultaV4?wsdl", + "CTeRecepcaoSincV4": "https://homologacao.cte.ms.gov.br/ws/CTeRecepcaoSincV4?wsdl", +} MG_PRODUCAO = { - "CTeRecepcaoSincV4": "https://cte.fazenda.mg.gov.br/cte/services/CTeRecepcaoSincV4?wsdl", - "CTeRecepcaoGTVeV4": "https://cte.fazenda.mg.gov.br/cte/services/CTeRecepcaoGTVeV4?wsdl", - "CTeRecepcaoOSV4": "https://cte.fazenda.mg.gov.br/cte/services/CTeRecepcaoOSV4?wsdl", - "CTeConsultaV4": "https://cte.fazenda.mg.gov.br/cte/services/CTeConsultaV4?wsdl", - "CTeStatusServicoV4": "https://cte.fazenda.mg.gov.br/cte/services/CTeStatusServicoV4?wsdl", - "CTeRecepcaoEventoV4": "https://cte.fazenda.mg.gov.br/cte/services/CTeRecepcaoEventoV4?wsdl", - } + "CTeRecepcaoSincV4": "https://cte.fazenda.mg.gov.br/cte/services/CTeRecepcaoSincV4?wsdl", + "CTeRecepcaoGTVeV4": "https://cte.fazenda.mg.gov.br/cte/services/CTeRecepcaoGTVeV4?wsdl", + "CTeRecepcaoOSV4": "https://cte.fazenda.mg.gov.br/cte/services/CTeRecepcaoOSV4?wsdl", + "CTeConsultaV4": "https://cte.fazenda.mg.gov.br/cte/services/CTeConsultaV4?wsdl", + "CTeStatusServicoV4": "https://cte.fazenda.mg.gov.br/cte/services/CTeStatusServicoV4?wsdl", + "CTeRecepcaoEventoV4": "https://cte.fazenda.mg.gov.br/cte/services/CTeRecepcaoEventoV4?wsdl", +} MG_HOMOLOGACAO = { - "CTeRecepcaoSincV4": "https://hcte.fazenda.mg.gov.br/cte/services/CTeRecepcaoSincV4?wsdl", - "CTeRecepcaoOSV4": "https://hcte.fazenda.mg.gov.br/cte/services/CTeRecepcaoOSV4?wsdl", - "CTeRecepcaoGTVeV4": "https://hcte.fazenda.mg.gov.br/cte/services/CTeRecepcaoGTVeV4?wsdl", - "CTeConsultaV4": "https://hcte.fazenda.mg.gov.br/cte/services/CTeConsultaV4?wsdl", - "CTeStatusServicoV4": "https://hcte.fazenda.mg.gov.br/cte/services/CTeStatusServicoV4?wsdl", - "CTeRecepcaoEventoV4": "https://hcte.fazenda.mg.gov.br/cte/services/CTeRecepcaoEventoV4?wsdl", - } + "CTeRecepcaoSincV4": "https://hcte.fazenda.mg.gov.br/cte/services/CTeRecepcaoSincV4?wsdl", + "CTeRecepcaoOSV4": "https://hcte.fazenda.mg.gov.br/cte/services/CTeRecepcaoOSV4?wsdl", + "CTeRecepcaoGTVeV4": "https://hcte.fazenda.mg.gov.br/cte/services/CTeRecepcaoGTVeV4?wsdl", + "CTeConsultaV4": "https://hcte.fazenda.mg.gov.br/cte/services/CTeConsultaV4?wsdl", + "CTeStatusServicoV4": "https://hcte.fazenda.mg.gov.br/cte/services/CTeStatusServicoV4?wsdl", + "CTeRecepcaoEventoV4": "https://hcte.fazenda.mg.gov.br/cte/services/CTeRecepcaoEventoV4?wsdl", +} PR_PRODUCAO = { - "CTeRecepcaoSincV4": "https://cte.fazenda.pr.gov.br/cte4/CTeRecepcaoSincV4?wsdl", - "CTeRecepcaoGTVeV4": "https://cte.fazenda.pr.gov.br/cte4/CTeRecepcaoGTVeV4?wsdl", - "CTeRecepcaoOSV4": "https://cte.fazenda.pr.gov.br/cte4/CTeRecepcaoOSV4?wsdl", - "CTeConsultaV4": "https://cte.fazenda.pr.gov.br/cte4/CTeConsultaV4?wsdl", - "CTeStatusServicoV4": "https://cte.fazenda.pr.gov.br/cte4/CTeStatusServicoV4?wsdl", - "CTeRecepcaoEventoV4": "https://cte.fazenda.pr.gov.br/cte4/CTeRecepcaoEventoV4?wsdl", - } + "CTeRecepcaoSincV4": "https://cte.fazenda.pr.gov.br/cte4/CTeRecepcaoSincV4?wsdl", + "CTeRecepcaoGTVeV4": "https://cte.fazenda.pr.gov.br/cte4/CTeRecepcaoGTVeV4?wsdl", + "CTeRecepcaoOSV4": "https://cte.fazenda.pr.gov.br/cte4/CTeRecepcaoOSV4?wsdl", + "CTeConsultaV4": "https://cte.fazenda.pr.gov.br/cte4/CTeConsultaV4?wsdl", + "CTeStatusServicoV4": "https://cte.fazenda.pr.gov.br/cte4/CTeStatusServicoV4?wsdl", + "CTeRecepcaoEventoV4": "https://cte.fazenda.pr.gov.br/cte4/CTeRecepcaoEventoV4?wsdl", +} PR_HOMOLOGACAO = { - "CTeConsultaV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeConsultaV4?wsdl", - "CTeStatusServicoV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeStatusServicoV4?wsdl", - "CTeRecepcaoSincV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeRecepcaoSincV4?wsdl", - "CTeRecepcaoEventoV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeRecepcaoEventoV4?wsdl", - "CTeRecepcaoGTVeV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeRecepcaoGTVeV4?wsdl", - "CTeRecepcaoOSV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeRecepcaoOSV4?wsdl", - } + "CTeConsultaV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeConsultaV4?wsdl", + "CTeStatusServicoV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeStatusServicoV4?wsdl", + "CTeRecepcaoSincV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeRecepcaoSincV4?wsdl", + "CTeRecepcaoEventoV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeRecepcaoEventoV4?wsdl", + "CTeRecepcaoGTVeV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeRecepcaoGTVeV4?wsdl", + "CTeRecepcaoOSV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeRecepcaoOSV4?wsdl", +} class CTe(DocumentoEletronico): - _namespace = 'http://www.portalfiscal.inf.br/cte' - _edoc_situacao_arquivo_recebido_com_sucesso = '103' - _edoc_situacao_servico_em_operacao = '107' + _namespace = "http://www.portalfiscal.inf.br/cte" + _edoc_situacao_arquivo_recebido_com_sucesso = "103" + _edoc_situacao_servico_em_operacao = "107" _consulta_servico_ao_enviar = True _consulta_documento_antes_de_enviar = True _maximo_tentativas_consulta_recibo = 5 @@ -168,46 +194,37 @@ class CTe(DocumentoEletronico): def __init__(self, config, xml): self.config = config self._xml = xml - self._ambiente = self.config #tpAmb - self._uf = self.config #uf + self._ambiente = self.config # tpAmb + self._uf = self.config # uf self._versao = 4.00 self._chaveCte = self._gerar_chave(self.config) self.assinatura = Assinatura(self.config) # self.config.certificado self.transmissao = TransmissaoSOAP(self.config) # self.config.certificado - super(CTe, self).__init__(self.transmissao) + super().__init__(self.transmissao) def status_servico(self): raiz = TconsStatServ( - tpAmb=self._ambiente, - cUF=SIGLA_ESTADO[self._uf], - versao=self._versao + tpAmb=self._ambiente, cUF=SIGLA_ESTADO[self._uf], versao=self._versao ) return self._post( raiz=raiz, url=self._search_url("CTeStatusServicoV4"), operacao="cteStatusServicoCT", - classe=RetConsStatServCte + classe=RetConsStatServCte, ) def consulta_documento(self, chave): - raiz = TconsSitCte( - tpAmb=self._ambiente, - chCTe=chave, - versao=self._versao - ) + raiz = TconsSitCte(tpAmb=self._ambiente, chCTe=chave, versao=self._versao) return self._post( raiz=raiz, url=self._search_url("CTeConsultaV4"), operacao="cteConsultaCT", - classe=RetConsSitCte - ) + classe=RetConsSitCte, + ) def envia_documento(self, edoc): xml_assinado = self.assina_raiz(edoc, edoc.infCTe.Id) - raiz = Tcte( - infCte=edoc, - signature=self.assinatura - ) + raiz = Tcte(infCte=edoc, signature=self.assinatura) xml_envio_string = raiz.to_xml() xml_envio_etree = xml_envio_string.from_xml() xml_envio_etree.append(etree.fromstring(xml_assinado)) @@ -215,28 +232,26 @@ def envia_documento(self, edoc): raiz=xml_envio_etree, url=self._search_url("CTeRecepcaoSincV4"), operacao="cteRecepcao", - classe=RetCte + classe=RetCte, ) def cancela_documento(self, doc_numero, justificativa): raiz = EvCancCte( - descEvento='Cancelamento', - nProt=doc_numero, - xJust=justificativa + descEvento="Cancelamento", nProt=doc_numero, xJust=justificativa ) return self._post( raiz=raiz, url=self._search_url("CTeRecepcaoEventoV4"), operacao="cteRecepcaoEvento", - classe=RetEventoCte + classe=RetEventoCte, ) - + def _gerar_chave(self, edoc): """ - 0 2 6 20 22 25 34 35 43 --> índice - | | | | | | | | | - | | | | | | | | | - CTe 32 1712 32438772000104 57 001 000199075 1 39868226 3 + 0 2 6 20 22 25 34 35 43 --> índice + | | | | | | | | | + | | | | | | | | | + CTe 32 1712 32438772000104 57 001 000199075 1 39868226 3 """ cte = Cte(edoc) chave = ChaveEdoc() @@ -244,7 +259,7 @@ def _gerar_chave(self, edoc): dhEmi = cte.InfCte.Ide.dhEmi chave.AAMM = dhEmi[2:4] + dhEmi[5:7] chave.CNPJ = cte.InfCte.Emit.CNPJ - chave.mod = cte.InfCte.Ide.mod + chave.mod = cte.InfCte.Ide.mod chave.serie = cte.InfCte.Ide.serie chave.nCT = cte.InfCte.Ide.nCT chave.tpEmis = cte.InfCte.Ide.tpEmis @@ -291,9 +306,5 @@ def _post(self, raiz, url, operacao, classe): xml_string = raiz.to_xml() xml_etree = xml_string.from_xml() with self._transmissao.cliente(url): - retorno = self._transmissao.enviar( - operacao, xml_etree - ) - return analisar_retorno_raw( - operacao, raiz, xml_string, retorno, classe - ) + retorno = self._transmissao.enviar(operacao, xml_etree) + return analisar_retorno_raw(operacao, raiz, xml_string, retorno, classe) From de3c03f986bbc3743881c2def3dff5c37a4f231a Mon Sep 17 00:00:00 2001 From: Marcel Savegnago Date: Tue, 2 Jul 2024 16:22:19 -0300 Subject: [PATCH 05/16] [RFC] erpbrasil.edoc: cte refactor --- src/erpbrasil/edoc/cte.py | 89 +++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 51 deletions(-) diff --git a/src/erpbrasil/edoc/cte.py b/src/erpbrasil/edoc/cte.py index 5ff4319..a1dcb2a 100644 --- a/src/erpbrasil/edoc/cte.py +++ b/src/erpbrasil/edoc/cte.py @@ -2,10 +2,7 @@ from lxml import etree -from erpbrasil.assinatura.assinatura import Assinatura -from erpbrasil.base.fiscal.edoc import ChaveEdoc from erpbrasil.edoc.edoc import DocumentoEletronico -from erpbrasil.transmissao import TransmissaoSOAP from .resposta import analisar_retorno_raw @@ -191,20 +188,22 @@ class CTe(DocumentoEletronico): _consulta_documento_antes_de_enviar = True _maximo_tentativas_consulta_recibo = 5 - def __init__(self, config, xml): - self.config = config - self._xml = xml - self._ambiente = self.config # tpAmb - self._uf = self.config # uf - self._versao = 4.00 - self._chaveCte = self._gerar_chave(self.config) - self.assinatura = Assinatura(self.config) # self.config.certificado - self.transmissao = TransmissaoSOAP(self.config) # self.config.certificado - super().__init__(self.transmissao) + def __init__(self, transmissao, uf, versao="4.00", ambiente="1", mod="57"): + super().__init__(transmissao) + self.versao = str(versao) + self.ambiente = str(ambiente) + self.uf = int(uf) + self.mod = str(mod) + + def _verifica_resposta_envio_sucesso(self, proc_envio): + return ( + proc_envio.resposta.cStat + == self._edoc_situacao_arquivo_recebido_com_sucesso + ) def status_servico(self): raiz = TconsStatServ( - tpAmb=self._ambiente, cUF=SIGLA_ESTADO[self._uf], versao=self._versao + tpAmb=self.ambiente, cUF=self.uf, versao=self.versao ) return self._post( raiz=raiz, @@ -214,7 +213,7 @@ def status_servico(self): ) def consulta_documento(self, chave): - raiz = TconsSitCte(tpAmb=self._ambiente, chCTe=chave, versao=self._versao) + raiz = TconsSitCte(tpAmb=self.ambiente, chCTe=chave, versao=self.versao) return self._post( raiz=raiz, url=self._search_url("CTeConsultaV4"), @@ -223,10 +222,11 @@ def consulta_documento(self, chave): ) def envia_documento(self, edoc): - xml_assinado = self.assina_raiz(edoc, edoc.infCTe.Id) - raiz = Tcte(infCte=edoc, signature=self.assinatura) + xml_assinado = self.assina_raiz(edoc, edoc.infCte.Id) + raiz = Tcte(infCte=edoc, signature=None) xml_envio_string = raiz.to_xml() - xml_envio_etree = xml_envio_string.from_xml() + xml_envio_bytes = xml_envio_string.encode('utf-8') + xml_envio_etree = etree.fromstring(xml_envio_bytes) xml_envio_etree.append(etree.fromstring(xml_assinado)) return self._post( raiz=xml_envio_etree, @@ -246,55 +246,39 @@ def cancela_documento(self, doc_numero, justificativa): classe=RetEventoCte, ) - def _gerar_chave(self, edoc): - """ - 0 2 6 20 22 25 34 35 43 --> índice - | | | | | | | | | - | | | | | | | | | - CTe 32 1712 32438772000104 57 001 000199075 1 39868226 3 - """ - cte = Cte(edoc) - chave = ChaveEdoc() - chave.cUF = cte.InfCte.Ide.cUF - dhEmi = cte.InfCte.Ide.dhEmi - chave.AAMM = dhEmi[2:4] + dhEmi[5:7] - chave.CNPJ = cte.InfCte.Emit.CNPJ - chave.mod = cte.InfCte.Ide.mod - chave.serie = cte.InfCte.Ide.serie - chave.nCT = cte.InfCte.Ide.nCT - chave.tpEmis = cte.InfCte.Ide.tpEmis - chave.cCT = cte.InfCte.Ide.cCT - chave.cDV = cte.InfCte.Ide.cDV - return chave - def _search_url(self, service): - if self._uf == "MG": - if self._ambiente == 1: + sigla = "" + for uf_code, ibge_code in SIGLA_ESTADO.items(): + if ibge_code == self.uf: + sigla = uf_code + + if sigla == "MG": + if self.ambiente == 1: return MG_PRODUCAO[service] else: return MG_HOMOLOGACAO[service] - elif self._uf == "MS": - if self._ambiente == 1: + elif sigla == "MS": + if self.ambiente == 1: return MS_PRODUCAO[service] else: return MS_HOMOLOGACAO[service] - elif self._uf == "MT": - if self._ambiente == 1: + elif sigla == "MT": + if self.ambiente == 1: return MT_PRODUCAO[service] else: return MT_HOMOLOGACAO[service] - elif self._uf == "PR": - if self._ambiente == 1: + elif sigla == "PR": + if self.ambiente == 1: return PR_PRODUCAO[service] else: return PR_HOMOLOGACAO[service] - elif self._uf == "SVSP": - if self._ambiente == 1: + elif sigla in SVSP: + if self.ambiente == 1: return SVSP_PRODUCAO[service] else: return SVSP_HOMOLOGACAO[service] - elif self._uf == "SVRS": - if self._ambiente == 1: + elif sigla in SVRS: + if self.ambiente == 1: return SVRS_PRODUCAO[service] else: return SVRS_HOMOLOGACAO[service] @@ -308,3 +292,6 @@ def _post(self, raiz, url, operacao, classe): with self._transmissao.cliente(url): retorno = self._transmissao.enviar(operacao, xml_etree) return analisar_retorno_raw(operacao, raiz, xml_string, retorno, classe) + + def get_documento_id(self, edoc): + return edoc.infCte.Id[:3], edoc.infCte.Id[3:] From 9f390b5206017c96b6dc98b3f85d4596515e708e Mon Sep 17 00:00:00 2001 From: Marcel Savegnago Date: Wed, 3 Jul 2024 11:42:15 -0300 Subject: [PATCH 06/16] [FIX] erpbrasil.edoc: fix consulta status servico --- src/erpbrasil/edoc/cte.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/erpbrasil/edoc/cte.py b/src/erpbrasil/edoc/cte.py index a1dcb2a..505edc4 100644 --- a/src/erpbrasil/edoc/cte.py +++ b/src/erpbrasil/edoc/cte.py @@ -15,8 +15,8 @@ RetCte, RetEventoCte, TconsSitCte, - TconsStatServ, Tcte, + ConsStatServCte ) @@ -202,7 +202,7 @@ def _verifica_resposta_envio_sucesso(self, proc_envio): ) def status_servico(self): - raiz = TconsStatServ( + raiz = ConsStatServCte( tpAmb=self.ambiente, cUF=self.uf, versao=self.versao ) return self._post( From b3833c843361acd5c30544633d0a435023e9af37 Mon Sep 17 00:00:00 2001 From: Marcel Savegnago Date: Wed, 3 Jul 2024 12:34:00 -0300 Subject: [PATCH 07/16] [FIX] erpbrasil.edoc: fix consulta status cte --- src/erpbrasil/edoc/cte.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/erpbrasil/edoc/cte.py b/src/erpbrasil/edoc/cte.py index 505edc4..1f11648 100644 --- a/src/erpbrasil/edoc/cte.py +++ b/src/erpbrasil/edoc/cte.py @@ -14,7 +14,7 @@ RetConsStatServCte, RetCte, RetEventoCte, - TconsSitCte, + ConsSitCte, Tcte, ConsStatServCte ) @@ -213,7 +213,7 @@ def status_servico(self): ) def consulta_documento(self, chave): - raiz = TconsSitCte(tpAmb=self.ambiente, chCTe=chave, versao=self.versao) + raiz = ConsSitCte(tpAmb=self.ambiente, chCTe=chave, versao=self.versao) return self._post( raiz=raiz, url=self._search_url("CTeConsultaV4"), From 0613085d2c9e328ce29c45012ff53bf0ef14c7d7 Mon Sep 17 00:00:00 2001 From: Marcel Savegnago Date: Thu, 4 Jul 2024 14:13:36 -0300 Subject: [PATCH 08/16] [FIX] erpbrasil.edoc: fix enviar documento cte --- src/erpbrasil/edoc/cte.py | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/erpbrasil/edoc/cte.py b/src/erpbrasil/edoc/cte.py index 1f11648..b942dc7 100644 --- a/src/erpbrasil/edoc/cte.py +++ b/src/erpbrasil/edoc/cte.py @@ -1,8 +1,11 @@ from contextlib import suppress from lxml import etree +import gzip +import base64 from erpbrasil.edoc.edoc import DocumentoEletronico +from erpbrasil.transmissao import TransmissaoSOAP from .resposta import analisar_retorno_raw @@ -223,13 +226,15 @@ def consulta_documento(self, chave): def envia_documento(self, edoc): xml_assinado = self.assina_raiz(edoc, edoc.infCte.Id) - raiz = Tcte(infCte=edoc, signature=None) - xml_envio_string = raiz.to_xml() - xml_envio_bytes = xml_envio_string.encode('utf-8') - xml_envio_etree = etree.fromstring(xml_envio_bytes) - xml_envio_etree.append(etree.fromstring(xml_assinado)) + + # Compactar o XML assinado com GZip + gzipped_xml = gzip.compress(xml_assinado.encode('utf-8')) + + # Codificar o XML compactado em Base64 + base64_gzipped_xml = base64.b64encode(gzipped_xml).decode('utf-8') + return self._post( - raiz=xml_envio_etree, + raiz=base64_gzipped_xml, url=self._search_url("CTeRecepcaoSincV4"), operacao="cteRecepcao", classe=RetCte, @@ -286,12 +291,19 @@ def _search_url(self, service): def consulta_recibo(self): pass - def _post(self, raiz, url, operacao, classe): - xml_string = raiz.to_xml() - xml_etree = xml_string.from_xml() - with self._transmissao.cliente(url): - retorno = self._transmissao.enviar(operacao, xml_etree) - return analisar_retorno_raw(operacao, raiz, xml_string, retorno, classe) - def get_documento_id(self, edoc): return edoc.infCte.Id[:3], edoc.infCte.Id[3:] + + +class TransmissaoCTE(TransmissaoSOAP): + + def interpretar_mensagem(self, mensagem, **kwargs): + if isinstance(mensagem, str): + try: + return etree.fromstring(mensagem, parser=etree.XMLParser( + remove_blank_text=True + )) + except (etree.XMLSyntaxError, ValueError): + # Retorna a string original se houver um erro na conversão + return mensagem + return mensagem From 80ca34f4e5ca224a3fa53c611c04c91bea334c0e Mon Sep 17 00:00:00 2001 From: Marcel Savegnago Date: Sun, 7 Jul 2024 02:34:31 -0300 Subject: [PATCH 09/16] [IMP] imp monta_qrcode --- src/erpbrasil/edoc/cte.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/erpbrasil/edoc/cte.py b/src/erpbrasil/edoc/cte.py index b942dc7..e90e7c5 100644 --- a/src/erpbrasil/edoc/cte.py +++ b/src/erpbrasil/edoc/cte.py @@ -182,6 +182,8 @@ "CTeRecepcaoOSV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeRecepcaoOSV4?wsdl", } +QR_CODE_URL = "https://dfe-portal.svrs.rs.gov.br/cte/qrCode" + class CTe(DocumentoEletronico): _namespace = "http://www.portalfiscal.inf.br/cte" @@ -294,6 +296,9 @@ def consulta_recibo(self): def get_documento_id(self, edoc): return edoc.infCte.Id[:3], edoc.infCte.Id[3:] + def monta_qrcode(self, chave): + return f"{QR_CODE_URL}?chCTe={chave}&tpAmb={self.ambiente}" + class TransmissaoCTE(TransmissaoSOAP): From 5d9e58748cdc690f85aa81352c389fb6cd4d139d Mon Sep 17 00:00:00 2001 From: Marcel Savegnago Date: Sun, 7 Jul 2024 03:33:15 -0300 Subject: [PATCH 10/16] [RFC] refactor url search --- src/erpbrasil/edoc/cte.py | 305 +++++++++++++++++++++----------------- 1 file changed, 170 insertions(+), 135 deletions(-) diff --git a/src/erpbrasil/edoc/cte.py b/src/erpbrasil/edoc/cte.py index e90e7c5..099e1b7 100644 --- a/src/erpbrasil/edoc/cte.py +++ b/src/erpbrasil/edoc/cte.py @@ -22,6 +22,16 @@ ConsStatServCte ) +AMBIENTE_PRODUCAO = "producao" +AMBIENTE_HOMOLOGACAO = "homologacao" + +WS_CTE_CONSULTA = "CTeConsultaV4" +WS_CTE_RECEPCAO_EVENTO = "CTeRecepcaoEventoV4" +WS_CTE_RECEPCAO_GT = "CTeRecepcaoGTVeV4" +WS_CTE_RECEPCAO_OS = "CTeRecepcaoOSV4" +WS_CTE_RECEPCAO_SINC = "CTeRecepcaoSincV4" +WS_CTE_STATUS_SERVICO = "CTeStatusServicoV4" +QR_CODE_URL = "QRCode" SIGLA_ESTADO = { "AC": 12, @@ -54,8 +64,8 @@ "AN": 91, } -SVSP = ["AP", "PE", "RR", "SP"] -SVRS = [ +SVSP_STATES = ["AP", "PE", "RR", "SP"] +SVRS_STATES = [ "AC", "AL", "AM", @@ -76,113 +86,163 @@ "TO", ] -SVSP_PRODUCAO = { - "CTeConsultaV4": "https://nfe.fazenda.sp.gov.br/CTeWS/WS/CTeConsultaV4.asmx?wsdl", - "CTeRecepcaoEventoV4": "https://nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoEventoV4.asmx?wsdl", - "CTeRecepcaoGTVeV4": "https://nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoGTVeV4.asmx?wsdl", - "CTeRecepcaoOSV4": "https://nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoOSV4.asmx?wsdl", - "CTeRecepcaoSincV4": "https://nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoSincV4.asmx?wsdl", - "CTeStatusServicoV4": "https://nfe.fazenda.sp.gov.br/CTeWS/WS/CTeStatusServicoV4.asmx?wsdl", -} - -SVSP_HOMOLOGACAO = { - "CTeConsultaV4": "https://homologacao.nfe.fazenda.sp.gov.br/CTeWS/WS/CTeConsultaV4.asmx?wsdl", - "CTeRecepcaoEventoV4": "https://homologacao.nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoEventoV4.asmx?wsdl", - "CTeRecepcaoGTVeV4": "https://homologacao.nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoGTVeV4.asmx?wsdl", - "CTeRecepcaoOSV4": "https://homologacao.nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoOSV4.asmx?wsdl", - "CTeRecepcaoSincV4": "https://homologacao.nfe.fazenda.sp.gov.br/CTeWS/WS/CTeRecepcaoSincV4.asmx?wsdl", - "CTeStatusServicoV4": "https://homologacao.nfe.fazenda.sp.gov.br/CTeWS/WS/CTeStatusServicoV4.asmx?wsdl", -} - -SVRS_PRODUCAO = { - "CTeStatusServicoV4": "https://cte.svrs.rs.gov.br/ws/CTeStatusServicoV4/CTeStatusServicoV4.asmx?wsdl", - "CTeConsultaV4": "https://cte.svrs.rs.gov.br/ws/CTeConsultaV4/CTeConsultaV4.asmx?wsdl", - "CTeRecepcaoSincV4": "https://cte.svrs.rs.gov.br/ws/CTeRecepcaoSincV4/CTeRecepcaoSincV4.asmx?wsdl", - "CTeRecepcaoOSV4": "https://cte.svrs.rs.gov.br/ws/CTeRecepcaoOSV4/CTeRecepcaoOSV4.asmx?wsdl", - "CTeRecepcaoGTVeV4": "https://cte.svrs.rs.gov.br/ws/CTeRecepcaoGTVeV4/CTeRecepcaoGTVeV4.asmx?wsdl", - "CTeRecepcaoEventoV4": "https://cte.svrs.rs.gov.br/ws/CTeRecepcaoEventoV4/CTeRecepcaoEventoV4.asmx?wsdl", +SVSP = { + AMBIENTE_PRODUCAO: { + "servidor": "nfe.fazenda.sp.gov.br", + WS_CTE_CONSULTA: "CTeWS/WS/CTeConsultaV4.asmx?wsdl", + WS_CTE_RECEPCAO_EVENTO: "CTeWS/WS/CTeRecepcaoEventoV4.asmx?wsdl", + WS_CTE_RECEPCAO_GT: "CTeWS/WS/CTeRecepcaoGTVeV4.asmx?wsdl", + WS_CTE_RECEPCAO_OS: "CTeWS/WS/CTeRecepcaoOSV4.asmx?wsdl", + WS_CTE_RECEPCAO_SINC: "CTeWS/WS/CTeRecepcaoSincV4.asmx?wsdl", + WS_CTE_STATUS_SERVICO: "CTeWS/WS/CTeStatusServicoV4.asmx?wsdl", + QR_CODE_URL: "https://nfe.fazenda.sp.gov.br/CTeConsulta/qrCode", + }, + AMBIENTE_HOMOLOGACAO: { + "servidor": "homologacao.nfe.fazenda.sp.gov.br", + WS_CTE_CONSULTA: "CTeWS/WS/CTeConsultaV4.asmx?wsdl", + WS_CTE_RECEPCAO_EVENTO: "CTeWS/WS/CTeRecepcaoEventoV4.asmx?wsdl", + WS_CTE_RECEPCAO_GT: "CTeWS/WS/CTeRecepcaoGTVeV4.asmx?wsdl", + WS_CTE_RECEPCAO_OS: "CTeWS/WS/CTeRecepcaoOSV4.asmx?wsdl", + WS_CTE_RECEPCAO_SINC: "CTeWS/WS/CTeRecepcaoSincV4.asmx?wsdl", + WS_CTE_STATUS_SERVICO: "CTeWS/WS/CTeStatusServicoV4.asmx?wsdl", + QR_CODE_URL: "https://homologacao.nfe.fazenda.sp.gov.br/CTeConsulta/qrCode", + } } -SVRS_HOMOLOGACAO = { - "CTeStatusServicoV4": "https://cte-homologacao.svrs.rs.gov.br/ws/CTeStatusServicoV4/CTeStatusServicoV4.asmx?wsdl", - "CTeConsultaV4": "https://cte-homologacao.svrs.rs.gov.br/ws/CTeConsultaV4/CTeConsultaV4.asmx?wsdl", - "CTeRecepcaoSincV4": "https://cte-homologacao.svrs.rs.gov.br/ws/CTeRecepcaoSincV4/CTeRecepcaoSincV4.asmx?wsdl", - "CTeRecepcaoOSV4": "https://cte-homologacao.svrs.rs.gov.br/ws/CTeRecepcaoOSV4/CTeRecepcaoOSV4.asmx?wsdl", - "CTeRecepcaoGTVeV4": "https://cte-homologacao.svrs.rs.gov.br/ws/CTeRecepcaoGTVeV4/CTeRecepcaoGTVeV4.asmx?wsdl", - "CTeRecepcaoEventoV4": "https://cte-homologacao.svrs.rs.gov.br/ws/CTeRecepcaoEventoV4/CTeRecepcaoEventoV4.asmx?wsdl", +SVRS = { + AMBIENTE_PRODUCAO: { + "servidor": "cte.svrs.rs.gov.br", + WS_CTE_CONSULTA: "ws/CTeConsultaV4/CTeConsultaV4.asmx?wsdl", + WS_CTE_RECEPCAO_EVENTO: "ws/CTeRecepcaoEventoV4/CTeRecepcaoEventoV4.asmx?wsdl", + WS_CTE_RECEPCAO_GT: "ws/CTeRecepcaoGTVeV4/CTeRecepcaoGTVeV4.asmx?wsdl", + WS_CTE_RECEPCAO_OS: "ws/CTeRecepcaoOSV4/CTeRecepcaoOSV4.asmx?wsdl", + WS_CTE_RECEPCAO_SINC: "ws/CTeRecepcaoSincV4/CTeRecepcaoSincV4.asmx?wsdl", + WS_CTE_STATUS_SERVICO: "ws/CTeStatusServicoV4/CTeStatusServicoV4.asmx?wsdl", + QR_CODE_URL: "https://dfe-portal.svrs.rs.gov.br/cte/qrCode", + }, + AMBIENTE_HOMOLOGACAO: { + "servidor": "cte-homologacao.svrs.rs.gov.br", + WS_CTE_CONSULTA: "ws/CTeConsultaV4/CTeConsultaV4.asmx?wsdl", + WS_CTE_RECEPCAO_EVENTO: "ws/CTeRecepcaoEventoV4/CTeRecepcaoEventoV4.asmx?wsdl", + WS_CTE_RECEPCAO_GT: "ws/CTeRecepcaoGTVeV4/CTeRecepcaoGTVeV4.asmx?wsdl", + WS_CTE_RECEPCAO_OS: "ws/CTeRecepcaoOSV4/CTeRecepcaoOSV4.asmx?wsdl", + WS_CTE_RECEPCAO_SINC: "ws/CTeRecepcaoSincV4/CTeRecepcaoSincV4.asmx?wsdl", + WS_CTE_STATUS_SERVICO: "ws/CTeStatusServicoV4/CTeStatusServicoV4.asmx?wsdl", + QR_CODE_URL: "https://dfe-portal.svrs.rs.gov.br/cte/qrCode", + } } -MT_PRODUCAO = { - "CTeConsultaV4": "https://cte.sefaz.mt.gov.br/ctews2/services/CTeConsultaV4?wsdl", - "CTeRecepcaoEventoV4": "https://cte.sefaz.mt.gov.br/ctews2/services/CTeRecepcaoEventoV4?wsdl", - "CTeStatusServicoV4": "https://cte.sefaz.mt.gov.br/ctews2/services/CTeStatusServicoV4?wsdl", - "CTeRecepcaoSincV4": "https://cte.sefaz.mt.gov.br/ctews2/services/CTeRecepcaoSincV4?wsdl", - "CTeRecepcaoGTVeV4": "https://cte.sefaz.mt.gov.br/ctews2/services/CTeRecepcaoGTVeV4?wsdl", - "CTeRecepcaoOSV4": "https://cte.sefaz.mt.gov.br/ctews/services/CTeRecepcaoOSV4?wsdl", +MT = { + AMBIENTE_PRODUCAO: { + "servidor": "cte.sefaz.mt.gov.br", + WS_CTE_CONSULTA: "ctews2/services/CTeConsultaV4?wsdl", + WS_CTE_RECEPCAO_EVENTO: "ctews2/services/CTeRecepcaoEventoV4?wsdl", + WS_CTE_RECEPCAO_GT: "ctews2/services/CTeRecepcaoGTVeV4?wsdl", + WS_CTE_RECEPCAO_OS: "ctews/services/CTeRecepcaoOSV4?wsdl", + WS_CTE_RECEPCAO_SINC: "ctews2/services/CTeRecepcaoSincV4?wsdl", + WS_CTE_STATUS_SERVICO: "ctews2/services/CTeStatusServicoV4?wsdl", + QR_CODE_URL: "https://www.sefaz.mt.gov.br/cte/qrcode", + }, + AMBIENTE_HOMOLOGACAO: { + "servidor": "homologacao.sefaz.mt.gov.br", + WS_CTE_CONSULTA: "ctews2/services/CTeConsultaV4?wsdl", + WS_CTE_RECEPCAO_EVENTO: "ctews2/services/CTeRecepcaoEventoV4?wsdl", + WS_CTE_RECEPCAO_GT: "ctews2/services/CTeRecepcaoGTVeV4?wsdl", + WS_CTE_RECEPCAO_OS: "ctews/services/CTeRecepcaoOSV4?wsdl", + WS_CTE_RECEPCAO_SINC: "ctews2/services/CTeRecepcaoSincV4?wsdl", + WS_CTE_STATUS_SERVICO: "ctews2/services/CTeStatusServicoV4?wsdl", + QR_CODE_URL: "https://homologacao.sefaz.mt.gov.br/cte/qrcode", + } } -MT_HOMOLOGACAO = { - "CTeStatusServicoV4": "https://homologacao.sefaz.mt.gov.br/ctews2/services/CTeStatusServicoV4?wsdl", - "CTeConsultaV4": "https://homologacao.sefaz.mt.gov.br/ctews2/services/CTeConsultaV4?wsdl", - "CTeRecepcaoSincV4": "https://homologacao.sefaz.mt.gov.br/ctews2/services/CTeRecepcaoSincV4?wsdl", - "CTeRecepcaoGTVeV4": "https://homologacao.sefaz.mt.gov.br/ctews2/services/CTeRecepcaoGTVeV4?wsdl", - "CTeRecepcaoEventoV4": "https://homologacao.sefaz.mt.gov.br/ctews2/services/CTeRecepcaoEventoV4?wsdl", - "CTeRecepcaoOSV4": "https://homologacao.sefaz.mt.gov.br/ctews/services/CTeRecepcaoOSV4?wsdl", +MS = { + AMBIENTE_PRODUCAO: { + "servidor": "producao.cte.ms.gov.br", + WS_CTE_CONSULTA: "ws/CTeConsultaV4?wsdl", + WS_CTE_RECEPCAO_EVENTO: "ws/CTeRecepcaoEventoV4?wsdl", + WS_CTE_RECEPCAO_GT: "ws/CTeRecepcaoGTVeV4?wsdl", + WS_CTE_RECEPCAO_OS: "ws/CTeRecepcaoOSV4?wsdl", + WS_CTE_RECEPCAO_SINC: "ws/CTeRecepcaoSincV4?wsdl", + WS_CTE_STATUS_SERVICO: "ws/CTeStatusServicoV4?wsdl", + QR_CODE_URL: "http://www.dfe.ms.gov.br/cte/qrcode", + }, + AMBIENTE_HOMOLOGACAO: { + "servidor": "homologacao.cte.ms.gov.br", + WS_CTE_CONSULTA: "ws/CTeConsultaV4?wsdl", + WS_CTE_RECEPCAO_EVENTO: "ws/CTeRecepcaoEventoV4?wsdl", + WS_CTE_RECEPCAO_GT: "ws/CTeRecepcaoGTVeV4?wsdl", + WS_CTE_RECEPCAO_OS: "ws/CTeRecepcaoOSV4?wsdl", + WS_CTE_RECEPCAO_SINC: "ws/CTeRecepcaoSincV4?wsdl", + WS_CTE_STATUS_SERVICO: "ws/CTeStatusServicoV4?wsdl", + QR_CODE_URL: "http://www.dfe.ms.gov.br/cte/qrcode", + } } -MS_PRODUCAO = { - "CTeRecepcaoSincV4": "https://producao.cte.ms.gov.br/ws/CTeRecepcaoSincV4?wsdl", - "CTeStatusServicoV4": "https://producao.cte.ms.gov.br/ws/CTeStatusServicoV4?wsdl", - "CTeConsultaV4": "https://producao.cte.ms.gov.br/ws/CTeConsultaV4?wsdl", - "CTeRecepcaoEventoV4": "https://producao.cte.ms.gov.br/ws/CTeRecepcaoEventoV4?wsdl", - "CTeRecepcaoOSV4": "https://producao.cte.ms.gov.br/ws/CTeRecepcaoOSV4?wsdl", - "CTeRecepcaoGTVeV4": "https://producao.cte.ms.gov.br/ws/CTeRecepcaoGTVeV4?wsdl", +MG = { + AMBIENTE_PRODUCAO: { + "servidor": "cte.fazenda.mg.gov.br", + WS_CTE_CONSULTA: "cte/services/CTeConsultaV4?wsdl", + WS_CTE_RECEPCAO_EVENTO: "cte/services/CTeRecepcaoEventoV4?wsdl", + WS_CTE_RECEPCAO_GT: "cte/services/CTeRecepcaoGTVeV4?wsdl", + WS_CTE_RECEPCAO_OS: "cte/services/CTeRecepcaoOSV4?wsdl", + WS_CTE_RECEPCAO_SINC: "cte/services/CTeRecepcaoSincV4?wsdl", + WS_CTE_STATUS_SERVICO: "cte/services/CTeStatusServicoV4?wsdl", + QR_CODE_URL: "https://cte.fazenda.mg.gov.br/portalcte/sistema/qrcode.xhtml", + }, + AMBIENTE_HOMOLOGACAO: { + "servidor": "hcte.fazenda.mg.gov.br", + WS_CTE_CONSULTA: "cte/services/CTeConsultaV4?wsdl", + WS_CTE_RECEPCAO_EVENTO: "cte/services/CTeRecepcaoEventoV4?wsdl", + WS_CTE_RECEPCAO_GT: "cte/services/CTeRecepcaoGTVeV4?wsdl", + WS_CTE_RECEPCAO_OS: "cte/services/CTeRecepcaoOSV4?wsdl", + WS_CTE_RECEPCAO_SINC: "cte/services/CTeRecepcaoSincV4?wsdl", + WS_CTE_STATUS_SERVICO: "cte/services/CTeStatusServicoV4?wsdl", + QR_CODE_URL: "https://cte.fazenda.mg.gov.br/portalcte/sistema/qrcode.xhtml", + } } -MS_HOMOLOGACAO = { - "CTeStatusServicoV4": "https://homologacao.cte.ms.gov.br/ws/CTeStatusServicoV4?wsdl", - "CTeRecepcaoEventoV4": "https://homologacao.cte.ms.gov.br/ws/CTeRecepcaoEventoV4?wsdl", - "CTeConsultaV4": "https://homologacao.cte.ms.gov.br/ws/CTeConsultaV4?wsdl", - "CTeRecepcaoSincV4": "https://homologacao.cte.ms.gov.br/ws/CTeRecepcaoSincV4?wsdl", +PR = { + AMBIENTE_PRODUCAO: { + "servidor": "cte.fazenda.pr.gov.br", + WS_CTE_CONSULTA: "cte4/CTeConsultaV4?wsdl", + WS_CTE_RECEPCAO_EVENTO: "cte4/CTeRecepcaoEventoV4?wsdl", + WS_CTE_RECEPCAO_GT: "cte4/CTeRecepcaoGTVeV4?wsdl", + WS_CTE_RECEPCAO_OS: "cte4/CTeRecepcaoOSV4?wsdl", + WS_CTE_RECEPCAO_SINC: "cte4/CTeRecepcaoSincV4?wsdl", + WS_CTE_STATUS_SERVICO: "cte4/CTeStatusServicoV4?wsdl", + QR_CODE_URL: "http://www.fazenda.pr.gov.br/cte/qrcode", + }, + AMBIENTE_HOMOLOGACAO: { + "servidor": "homologacao.cte.fazenda.pr.gov.br", + WS_CTE_CONSULTA: "cte4/CTeConsultaV4?wsdl", + WS_CTE_RECEPCAO_EVENTO: "cte4/CTeRecepcaoEventoV4?wsdl", + WS_CTE_RECEPCAO_GT: "cte4/CTeRecepcaoGTVeV4?wsdl", + WS_CTE_RECEPCAO_OS: "cte4/CTeRecepcaoOSV4?wsdl", + WS_CTE_RECEPCAO_SINC: "cte4/CTeRecepcaoSincV4?wsdl", + WS_CTE_STATUS_SERVICO: "cte4/CTeStatusServicoV4?wsdl", + QR_CODE_URL: "http://www.fazenda.pr.gov.br/cte/qrcode", + } } -MG_PRODUCAO = { - "CTeRecepcaoSincV4": "https://cte.fazenda.mg.gov.br/cte/services/CTeRecepcaoSincV4?wsdl", - "CTeRecepcaoGTVeV4": "https://cte.fazenda.mg.gov.br/cte/services/CTeRecepcaoGTVeV4?wsdl", - "CTeRecepcaoOSV4": "https://cte.fazenda.mg.gov.br/cte/services/CTeRecepcaoOSV4?wsdl", - "CTeConsultaV4": "https://cte.fazenda.mg.gov.br/cte/services/CTeConsultaV4?wsdl", - "CTeStatusServicoV4": "https://cte.fazenda.mg.gov.br/cte/services/CTeStatusServicoV4?wsdl", - "CTeRecepcaoEventoV4": "https://cte.fazenda.mg.gov.br/cte/services/CTeRecepcaoEventoV4?wsdl", -} -MG_HOMOLOGACAO = { - "CTeRecepcaoSincV4": "https://hcte.fazenda.mg.gov.br/cte/services/CTeRecepcaoSincV4?wsdl", - "CTeRecepcaoOSV4": "https://hcte.fazenda.mg.gov.br/cte/services/CTeRecepcaoOSV4?wsdl", - "CTeRecepcaoGTVeV4": "https://hcte.fazenda.mg.gov.br/cte/services/CTeRecepcaoGTVeV4?wsdl", - "CTeConsultaV4": "https://hcte.fazenda.mg.gov.br/cte/services/CTeConsultaV4?wsdl", - "CTeStatusServicoV4": "https://hcte.fazenda.mg.gov.br/cte/services/CTeStatusServicoV4?wsdl", - "CTeRecepcaoEventoV4": "https://hcte.fazenda.mg.gov.br/cte/services/CTeRecepcaoEventoV4?wsdl", -} +def get_service_url(sigla_estado, service, ambiente): + if sigla_estado in SVSP_STATES: + state_config = SVSP + elif sigla_estado in SVRS_STATES: + state_config = SVRS + else: + state_config = sigla_estado -PR_PRODUCAO = { - "CTeRecepcaoSincV4": "https://cte.fazenda.pr.gov.br/cte4/CTeRecepcaoSincV4?wsdl", - "CTeRecepcaoGTVeV4": "https://cte.fazenda.pr.gov.br/cte4/CTeRecepcaoGTVeV4?wsdl", - "CTeRecepcaoOSV4": "https://cte.fazenda.pr.gov.br/cte4/CTeRecepcaoOSV4?wsdl", - "CTeConsultaV4": "https://cte.fazenda.pr.gov.br/cte4/CTeConsultaV4?wsdl", - "CTeStatusServicoV4": "https://cte.fazenda.pr.gov.br/cte4/CTeStatusServicoV4?wsdl", - "CTeRecepcaoEventoV4": "https://cte.fazenda.pr.gov.br/cte4/CTeRecepcaoEventoV4?wsdl", -} + if not state_config: + raise ValueError(f"Estado {sigla_estado} não suportado ou configuração ausente.") -PR_HOMOLOGACAO = { - "CTeConsultaV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeConsultaV4?wsdl", - "CTeStatusServicoV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeStatusServicoV4?wsdl", - "CTeRecepcaoSincV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeRecepcaoSincV4?wsdl", - "CTeRecepcaoEventoV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeRecepcaoEventoV4?wsdl", - "CTeRecepcaoGTVeV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeRecepcaoGTVeV4?wsdl", - "CTeRecepcaoOSV4": "https://homologacao.cte.fazenda.pr.gov.br/cte4/CTeRecepcaoOSV4?wsdl", -} + environment = AMBIENTE_PRODUCAO if ambiente == 1 else AMBIENTE_HOMOLOGACAO + if service == "QRCode": + return state_config[environment][QR_CODE_URL] -QR_CODE_URL = "https://dfe-portal.svrs.rs.gov.br/cte/qrCode" + server = state_config[environment]["servidor"] + service_path = state_config[environment][service] + return f"https://{server}/{service_path}" class CTe(DocumentoEletronico): @@ -200,6 +260,18 @@ def __init__(self, transmissao, uf, versao="4.00", ambiente="1", mod="57"): self.uf = int(uf) self.mod = str(mod) + def _get_ws_endpoint(self, service): + sigla = None + for uf_code, ibge_code in SIGLA_ESTADO.items(): + if ibge_code == self.uf: + sigla = uf_code + break + + if not sigla: + raise ValueError(f"UF {self.uf} não suportado ou configuração ausente.") + + return get_service_url(sigla, service, self.ambiente) + def _verifica_resposta_envio_sucesso(self, proc_envio): return ( proc_envio.resposta.cStat @@ -212,7 +284,7 @@ def status_servico(self): ) return self._post( raiz=raiz, - url=self._search_url("CTeStatusServicoV4"), + url=self._get_ws_endpoint(WS_CTE_STATUS_SERVICO), operacao="cteStatusServicoCT", classe=RetConsStatServCte, ) @@ -221,7 +293,7 @@ def consulta_documento(self, chave): raiz = ConsSitCte(tpAmb=self.ambiente, chCTe=chave, versao=self.versao) return self._post( raiz=raiz, - url=self._search_url("CTeConsultaV4"), + url=self._get_ws_endpoint(WS_CTE_CONSULTA), operacao="cteConsultaCT", classe=RetConsSitCte, ) @@ -237,7 +309,7 @@ def envia_documento(self, edoc): return self._post( raiz=base64_gzipped_xml, - url=self._search_url("CTeRecepcaoSincV4"), + url=self._get_ws_endpoint(WS_CTE_RECEPCAO_SINC), operacao="cteRecepcao", classe=RetCte, ) @@ -248,48 +320,11 @@ def cancela_documento(self, doc_numero, justificativa): ) return self._post( raiz=raiz, - url=self._search_url("CTeRecepcaoEventoV4"), + url=self._get_ws_endpoint(WS_CTE_RECEPCAO_EVENTO), operacao="cteRecepcaoEvento", classe=RetEventoCte, ) - def _search_url(self, service): - sigla = "" - for uf_code, ibge_code in SIGLA_ESTADO.items(): - if ibge_code == self.uf: - sigla = uf_code - - if sigla == "MG": - if self.ambiente == 1: - return MG_PRODUCAO[service] - else: - return MG_HOMOLOGACAO[service] - elif sigla == "MS": - if self.ambiente == 1: - return MS_PRODUCAO[service] - else: - return MS_HOMOLOGACAO[service] - elif sigla == "MT": - if self.ambiente == 1: - return MT_PRODUCAO[service] - else: - return MT_HOMOLOGACAO[service] - elif sigla == "PR": - if self.ambiente == 1: - return PR_PRODUCAO[service] - else: - return PR_HOMOLOGACAO[service] - elif sigla in SVSP: - if self.ambiente == 1: - return SVSP_PRODUCAO[service] - else: - return SVSP_HOMOLOGACAO[service] - elif sigla in SVRS: - if self.ambiente == 1: - return SVRS_PRODUCAO[service] - else: - return SVRS_HOMOLOGACAO[service] - def consulta_recibo(self): pass @@ -297,7 +332,7 @@ def get_documento_id(self, edoc): return edoc.infCte.Id[:3], edoc.infCte.Id[3:] def monta_qrcode(self, chave): - return f"{QR_CODE_URL}?chCTe={chave}&tpAmb={self.ambiente}" + return f"{self._get_ws_endpoint(QR_CODE_URL)}?chCTe={chave}&tpAmb={self.ambiente}" class TransmissaoCTE(TransmissaoSOAP): From 7cf9314fa622585ef82e2f1c3348e6658398d4c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Felipe=20Mil=C3=A9o?= Date: Sun, 7 Jul 2024 09:07:53 -0300 Subject: [PATCH 11/16] [REF] pre-commit --- src/erpbrasil/edoc/cte.py | 47 ++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/src/erpbrasil/edoc/cte.py b/src/erpbrasil/edoc/cte.py index 099e1b7..8bcf6be 100644 --- a/src/erpbrasil/edoc/cte.py +++ b/src/erpbrasil/edoc/cte.py @@ -1,25 +1,21 @@ +import base64 +import gzip from contextlib import suppress from lxml import etree -import gzip -import base64 from erpbrasil.edoc.edoc import DocumentoEletronico from erpbrasil.transmissao import TransmissaoSOAP -from .resposta import analisar_retorno_raw - with suppress(ImportError): from nfelib.cte.bindings.v4_0 import ( - Cte, + ConsSitCte, + ConsStatServCte, EvCancCte, RetConsSitCte, RetConsStatServCte, RetCte, RetEventoCte, - ConsSitCte, - Tcte, - ConsStatServCte ) AMBIENTE_PRODUCAO = "producao" @@ -106,7 +102,7 @@ WS_CTE_RECEPCAO_SINC: "CTeWS/WS/CTeRecepcaoSincV4.asmx?wsdl", WS_CTE_STATUS_SERVICO: "CTeWS/WS/CTeStatusServicoV4.asmx?wsdl", QR_CODE_URL: "https://homologacao.nfe.fazenda.sp.gov.br/CTeConsulta/qrCode", - } + }, } SVRS = { @@ -129,7 +125,7 @@ WS_CTE_RECEPCAO_SINC: "ws/CTeRecepcaoSincV4/CTeRecepcaoSincV4.asmx?wsdl", WS_CTE_STATUS_SERVICO: "ws/CTeStatusServicoV4/CTeStatusServicoV4.asmx?wsdl", QR_CODE_URL: "https://dfe-portal.svrs.rs.gov.br/cte/qrCode", - } + }, } MT = { @@ -152,7 +148,7 @@ WS_CTE_RECEPCAO_SINC: "ctews2/services/CTeRecepcaoSincV4?wsdl", WS_CTE_STATUS_SERVICO: "ctews2/services/CTeStatusServicoV4?wsdl", QR_CODE_URL: "https://homologacao.sefaz.mt.gov.br/cte/qrcode", - } + }, } MS = { @@ -175,7 +171,7 @@ WS_CTE_RECEPCAO_SINC: "ws/CTeRecepcaoSincV4?wsdl", WS_CTE_STATUS_SERVICO: "ws/CTeStatusServicoV4?wsdl", QR_CODE_URL: "http://www.dfe.ms.gov.br/cte/qrcode", - } + }, } MG = { @@ -198,7 +194,7 @@ WS_CTE_RECEPCAO_SINC: "cte/services/CTeRecepcaoSincV4?wsdl", WS_CTE_STATUS_SERVICO: "cte/services/CTeStatusServicoV4?wsdl", QR_CODE_URL: "https://cte.fazenda.mg.gov.br/portalcte/sistema/qrcode.xhtml", - } + }, } PR = { @@ -221,7 +217,7 @@ WS_CTE_RECEPCAO_SINC: "cte4/CTeRecepcaoSincV4?wsdl", WS_CTE_STATUS_SERVICO: "cte4/CTeStatusServicoV4?wsdl", QR_CODE_URL: "http://www.fazenda.pr.gov.br/cte/qrcode", - } + }, } @@ -234,7 +230,9 @@ def get_service_url(sigla_estado, service, ambiente): state_config = sigla_estado if not state_config: - raise ValueError(f"Estado {sigla_estado} não suportado ou configuração ausente.") + raise ValueError( + f"Estado {sigla_estado} não suportado ou configuração ausente." + ) environment = AMBIENTE_PRODUCAO if ambiente == 1 else AMBIENTE_HOMOLOGACAO if service == "QRCode": @@ -279,9 +277,7 @@ def _verifica_resposta_envio_sucesso(self, proc_envio): ) def status_servico(self): - raiz = ConsStatServCte( - tpAmb=self.ambiente, cUF=self.uf, versao=self.versao - ) + raiz = ConsStatServCte(tpAmb=self.ambiente, cUF=self.uf, versao=self.versao) return self._post( raiz=raiz, url=self._get_ws_endpoint(WS_CTE_STATUS_SERVICO), @@ -302,10 +298,10 @@ def envia_documento(self, edoc): xml_assinado = self.assina_raiz(edoc, edoc.infCte.Id) # Compactar o XML assinado com GZip - gzipped_xml = gzip.compress(xml_assinado.encode('utf-8')) + gzipped_xml = gzip.compress(xml_assinado.encode("utf-8")) # Codificar o XML compactado em Base64 - base64_gzipped_xml = base64.b64encode(gzipped_xml).decode('utf-8') + base64_gzipped_xml = base64.b64encode(gzipped_xml).decode("utf-8") return self._post( raiz=base64_gzipped_xml, @@ -332,17 +328,18 @@ def get_documento_id(self, edoc): return edoc.infCte.Id[:3], edoc.infCte.Id[3:] def monta_qrcode(self, chave): - return f"{self._get_ws_endpoint(QR_CODE_URL)}?chCTe={chave}&tpAmb={self.ambiente}" + return ( + f"{self._get_ws_endpoint(QR_CODE_URL)}?chCTe={chave}&tpAmb={self.ambiente}" + ) class TransmissaoCTE(TransmissaoSOAP): - def interpretar_mensagem(self, mensagem, **kwargs): if isinstance(mensagem, str): try: - return etree.fromstring(mensagem, parser=etree.XMLParser( - remove_blank_text=True - )) + return etree.fromstring( + mensagem, parser=etree.XMLParser(remove_blank_text=True) + ) except (etree.XMLSyntaxError, ValueError): # Retorna a string original se houver um erro na conversão return mensagem From 8066cf777e7240d19f2d0e3bef2c8a4c470a5666 Mon Sep 17 00:00:00 2001 From: Marcel Savegnago Date: Wed, 10 Jul 2024 18:06:34 -0300 Subject: [PATCH 12/16] [IMP] erpbrasil.edoc: imp cte cancel --- src/erpbrasil/edoc/cte.py | 46 ++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/src/erpbrasil/edoc/cte.py b/src/erpbrasil/edoc/cte.py index 8bcf6be..446806c 100644 --- a/src/erpbrasil/edoc/cte.py +++ b/src/erpbrasil/edoc/cte.py @@ -12,6 +12,7 @@ ConsSitCte, ConsStatServCte, EvCancCte, + EventoCte, RetConsSitCte, RetConsStatServCte, RetCte, @@ -310,16 +311,45 @@ def envia_documento(self, edoc): classe=RetCte, ) - def cancela_documento(self, doc_numero, justificativa): - raiz = EvCancCte( - descEvento="Cancelamento", nProt=doc_numero, xJust=justificativa - ) + def enviar_lote_evento(self, lista_eventos, numero_lote=False): + for raiz_evento in lista_eventos: + evento = EventoCte( + versao="4.00", + infEvento=raiz_evento, + ) + xml_assinado = self.assina_raiz(evento, evento.infEvento.Id) + return self._post( - raiz=raiz, - url=self._get_ws_endpoint(WS_CTE_RECEPCAO_EVENTO), - operacao="cteRecepcaoEvento", - classe=RetEventoCte, + xml_assinado, + self._get_ws_endpoint(WS_CTE_RECEPCAO_EVENTO), + "cteRecepcaoEvento", + RetEventoCte, + ) + + def cancela_documento( + self, chave, protocolo_autorizacao, justificativa, data_hora_evento=False + ): + tipo_evento = "110111" + sequencia = "1" + raiz = EventoCte.InfEvento( + Id="ID" + tipo_evento + chave + sequencia.zfill(3), + cOrgao=self.uf, + tpAmb=self.ambiente, + CNPJ=chave[6:20], + chCTe=chave, + dhEvento=data_hora_evento or self._hora_agora(), + tpEvento=tipo_evento, + nSeqEvento=sequencia, + detEvento=EventoCte.InfEvento.DetEvento( + EvCancCte( + descEvento="Cancelamento", + nProt=protocolo_autorizacao, + xJust=justificativa, + ), + versaoEvento="4.00", + ), ) + return raiz def consulta_recibo(self): pass From 70ceb7038bdb24a66e2b5d5c312f9aeed793ade1 Mon Sep 17 00:00:00 2001 From: Marcel Savegnago Date: Tue, 16 Jul 2024 12:16:30 -0300 Subject: [PATCH 13/16] =?UTF-8?q?[IMP]=20implementa=20a=20carta=20de=20cor?= =?UTF-8?q?re=C3=A7=C3=A3o=20do=20CTE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/erpbrasil/edoc/cte.py | 78 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/src/erpbrasil/edoc/cte.py b/src/erpbrasil/edoc/cte.py index 446806c..a7556c6 100644 --- a/src/erpbrasil/edoc/cte.py +++ b/src/erpbrasil/edoc/cte.py @@ -12,6 +12,7 @@ ConsSitCte, ConsStatServCte, EvCancCte, + EvCceCte, EventoCte, RetConsSitCte, RetConsStatServCte, @@ -19,6 +20,17 @@ RetEventoCte, ) +TEXTO_CARTA_CORRECAO = """A Carta de Correcao e disciplinada pelo Art. 58-B do \ +CONVENIO/SINIEF 06/89: Fica permitida a utilizacao de carta de \ +correcao, para regularizacao de erro ocorrido na emissao de \ +documentos fiscais relativos a prestacao de servico de \ +transporte, desde que o erro nao esteja relacionado com: \ +I - as variaveis que determinam o valor do imposto tais como: \ +base de calculo, aliquota, diferenca de preco, quantidade, \ +valor da prestacao; II - a correcao de dados cadastrais que \ +implique mudanca do emitente, tomador, remetente ou do \ +destinatario; III - a data de emissao ou de saida.""" + AMBIENTE_PRODUCAO = "producao" AMBIENTE_HOMOLOGACAO = "homologacao" @@ -351,6 +363,72 @@ def cancela_documento( ) return raiz + def carta_correcao( + self, + chave, + protocolo_autorizacao, + justificativa, + sequencia, + data_hora_evento=False, + ): + tipo_evento = "110110" + + temp_string = justificativa.replace("\\n", "TEMP_NEW_LINE") + + # Separar a string pelo separador + lista = temp_string.split(";") + + # Dividir a lista em sublistas usando o separador temporário + result = [] + sublist = [] + + for item in lista: + if "TEMP_NEW_LINE" in item: + parts = item.split("TEMP_NEW_LINE") + sublist.append(parts[0]) + result.append(sublist) + sublist = [parts[1]] + else: + sublist.append(item) + + # Adicionar a última sublista + result.append(sublist) + correcoes = [] + nro_item = 1 # Inicia a sequência de itens alterados + + for linha in result: + grupo, campo, valor = linha + correcao = EvCceCte.InfCorrecao( + grupoAlterado=grupo, + campoAlterado=campo, + valorAlterado=valor, + nroItemAlterado=str( + nro_item + ), # Adiciona a sequência de itens alterados + ) + correcoes.append(correcao) + nro_item += 1 # Incrementa a sequência + + raiz = EventoCte.InfEvento( + Id="ID" + tipo_evento + chave + sequencia.zfill(3), + cOrgao=self.uf, + tpAmb=self.ambiente, + CNPJ=chave[6:20], + chCTe=chave, + dhEvento=data_hora_evento or self._hora_agora(), + tpEvento=tipo_evento, + nSeqEvento=sequencia, + detEvento=EventoCte.InfEvento.DetEvento( + EvCceCte( + descEvento="Carta de Correcao", + infCorrecao=correcoes, # Lista de correções + xCondUso=TEXTO_CARTA_CORRECAO, + ), + versaoEvento="4.00", + ), + ) + return raiz + def consulta_recibo(self): pass From dde727b297478efe1093d86fdbe876347f8064ae Mon Sep 17 00:00:00 2001 From: Marcel Savegnago Date: Fri, 19 Jul 2024 12:41:20 -0300 Subject: [PATCH 14/16] [IMP] cte: generate cteProc xml file --- src/erpbrasil/edoc/cte.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/erpbrasil/edoc/cte.py b/src/erpbrasil/edoc/cte.py index a7556c6..1fbd4f1 100644 --- a/src/erpbrasil/edoc/cte.py +++ b/src/erpbrasil/edoc/cte.py @@ -440,6 +440,20 @@ def monta_qrcode(self, chave): f"{self._get_ws_endpoint(QR_CODE_URL)}?chCTe={chave}&tpAmb={self.ambiente}" ) + def monta_cte_proc(self, doc, prot): + """ + Constrói e retorna o XML do processo da CT-e, + incorporando a CT-e com o seu protocolo de autorização. + """ + proc = etree.Element( + f"{{{self._namespace}}}cteProc", + versao=self.versao, + nsmap={None: self._namespace}, + ) + proc.append(doc) + proc.append(prot) + return etree.tostring(proc) + class TransmissaoCTE(TransmissaoSOAP): def interpretar_mensagem(self, mensagem, **kwargs): From 8b37406e559c2f090e520eeba712ff0403c86477 Mon Sep 17 00:00:00 2001 From: Marcel Savegnago Date: Fri, 19 Jul 2024 18:47:46 -0300 Subject: [PATCH 15/16] [FIX] cte: fix TEXTO_CARTA_CORRECAO --- src/erpbrasil/edoc/cte.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/erpbrasil/edoc/cte.py b/src/erpbrasil/edoc/cte.py index 1fbd4f1..08f898a 100644 --- a/src/erpbrasil/edoc/cte.py +++ b/src/erpbrasil/edoc/cte.py @@ -20,16 +20,15 @@ RetEventoCte, ) -TEXTO_CARTA_CORRECAO = """A Carta de Correcao e disciplinada pelo Art. 58-B do \ -CONVENIO/SINIEF 06/89: Fica permitida a utilizacao de carta de \ -correcao, para regularizacao de erro ocorrido na emissao de \ -documentos fiscais relativos a prestacao de servico de \ -transporte, desde que o erro nao esteja relacionado com: \ -I - as variaveis que determinam o valor do imposto tais como: \ -base de calculo, aliquota, diferenca de preco, quantidade, \ -valor da prestacao; II - a correcao de dados cadastrais que \ -implique mudanca do emitente, tomador, remetente ou do \ -destinatario; III - a data de emissao ou de saida.""" +TEXTO_CARTA_CORRECAO = """A Carta de Correção é disciplinada pelo Art. 58-B \ +do CONVÊNIO/SINIEF 06/89: Fica permitida a utilização de carta de correção, \ +para regularização de erro ocorrido na emissão de documentos fiscais \ +relativos à prestação de serviço de transporte, desde que o erro não esteja \ +relacionado com: I - as variáveis que determinam o valor do imposto tais \ +como: base de cálculo, alíquota, diferença de preço, quantidade, valor da \ +prestação;II - a correção de dados cadastrais que implique mudança do \ +emitente, tomador, remetente ou do destinatário;III - a data de emissão ou \ +de saída.""" AMBIENTE_PRODUCAO = "producao" AMBIENTE_HOMOLOGACAO = "homologacao" From a72da137f3d66d7ceb727ed411188d7e6b5a0578 Mon Sep 17 00:00:00 2001 From: Marcel Savegnago Date: Wed, 4 Sep 2024 15:03:48 -0300 Subject: [PATCH 16/16] AVALIAR --- src/erpbrasil/edoc/edoc.py | 3 +++ src/erpbrasil/edoc/nfe.py | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/erpbrasil/edoc/edoc.py b/src/erpbrasil/edoc/edoc.py index b3080a8..3bcd17d 100644 --- a/src/erpbrasil/edoc/edoc.py +++ b/src/erpbrasil/edoc/edoc.py @@ -228,3 +228,6 @@ def _verifica_documento_ja_enviado(self, proc_consulta): def monta_processo(self, edoc, proc_envio, proc_recibo): return True + + def monta_qrcode(self, chave): + return diff --git a/src/erpbrasil/edoc/nfe.py b/src/erpbrasil/edoc/nfe.py index 56a6aa3..733077e 100644 --- a/src/erpbrasil/edoc/nfe.py +++ b/src/erpbrasil/edoc/nfe.py @@ -1091,3 +1091,6 @@ def consultar_cadastro(self, uf, cnpj=None, cpf=None, ie=None): "consultaCadastro", retConsCad, ) + + def monta_qrcode(self, chave): + return