From 0b47eba7b5f39c7ed61a1f87d22309b4aa9dd624 Mon Sep 17 00:00:00 2001 From: Danimar Ribeiro Date: Thu, 5 Oct 2017 21:46:01 -0300 Subject: [PATCH] =?UTF-8?q?Refactor=20-=20Permitir=20criar=20o=20xml=20de?= =?UTF-8?q?=20envio=20separadamente=20Ajuste=20na=20fun=C3=A7=C3=A3o=20de?= =?UTF-8?q?=20valida=C3=A7=C3=A3o=20do=20xml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pytrustnfe/nfe/__init__.py | 106 ++++++++++++++++++--- pytrustnfe/xml/schemas/enviNFe_v3.10.xsd | 0 pytrustnfe/xml/schemas/leiauteNFe_v3.10.xsd | 0 pytrustnfe/xml/schemas/nfe_v3.10.xsd | 0 pytrustnfe/xml/schemas/tiposBasico_v3.10.xsd | 2 +- .../xml/schemas/xmldsig-core-schema_v1.01.xsd | 0 pytrustnfe/xml/validate.py | 31 +----- 7 files changed, 97 insertions(+), 42 deletions(-) mode change 100644 => 100755 pytrustnfe/xml/schemas/enviNFe_v3.10.xsd mode change 100644 => 100755 pytrustnfe/xml/schemas/leiauteNFe_v3.10.xsd mode change 100644 => 100755 pytrustnfe/xml/schemas/nfe_v3.10.xsd mode change 100644 => 100755 pytrustnfe/xml/schemas/tiposBasico_v3.10.xsd mode change 100644 => 100755 pytrustnfe/xml/schemas/xmldsig-core-schema_v1.01.xsd diff --git a/pytrustnfe/nfe/__init__.py b/pytrustnfe/nfe/__init__.py index 9854047..f2ac156 100644 --- a/pytrustnfe/nfe/__init__.py +++ b/pytrustnfe/nfe/__init__.py @@ -124,9 +124,10 @@ def _add_qrCode(xml, **kwargs): return etree.tostring(xml, encoding=str) -def _send(certificado, method, sign, **kwargs): +def _render(certificado, method, sign, **kwargs): path = os.path.join(os.path.dirname(__file__), 'templates') xmlElem_send = render_xml(path, '%s.xml' % method, True, **kwargs) + modelo = xmlElem_send.find(".//{http://www.portalfiscal.inf.br/nfe}mod") modelo = modelo.text if modelo is not None else '55' if modelo == '65': @@ -176,8 +177,12 @@ def _send(certificado, method, sign, **kwargs): else: xml_send = etree.tostring(xmlElem_send, encoding=str) + return xml_send + - url = localizar_url(method, kwargs['estado'], modelo, +def _send(certificado, method, **kwargs): + xml_send = kwargs["xml"] + url = localizar_url(method, kwargs['estado'], '55', kwargs['ambiente']) cabecalho = _build_header(method, **kwargs) @@ -194,50 +199,123 @@ def _send(certificado, method, sign, **kwargs): } -def autorizar_nfe(certificado, **kwargs): # Assinar +def xml_autorizar_nfe(certificado, **kwargs): _generate_nfe_id(**kwargs) - return _send(certificado, 'NfeAutorizacao', True, **kwargs) + return _render(certificado, 'NfeAutorizacao', True, **kwargs) + + +def autorizar_nfe(certificado, **kwargs): # Assinar + if "xml" not in kwargs: + kwargs['xml'] = xml_autorizar_nfe(certificado, **kwargs) + return _send(certificado, 'NfeAutorizacao', **kwargs) + + +def xml_retorno_autorizar_nfe(certificado, **kwargs): + return _render(certificado, 'NfeRetAutorizacao', False, **kwargs) def retorno_autorizar_nfe(certificado, **kwargs): - return _send(certificado, 'NfeRetAutorizacao', False, **kwargs) + if "xml" not in kwargs: + kwargs['xml'] = xml_retorno_autorizar_nfe(certificado, **kwargs) + return _send(certificado, 'NfeRetAutorizacao', **kwargs) + + +def xml_recepcao_evento_cancelamento(certificado, **kwargs): # Assinar + return _render(certificado, 'RecepcaoEventoCancelamento', True, **kwargs) def recepcao_evento_cancelamento(certificado, **kwargs): # Assinar - return _send(certificado, 'RecepcaoEventoCancelamento', True, **kwargs) + if "xml" not in kwargs: + kwargs['xml'] = xml_recepcao_evento_cancelamento(certificado, **kwargs) + return _send(certificado, 'RecepcaoEventoCancelamento', **kwargs) + + +def xml_inutilizar_nfe(certificado, **kwargs): + return _render(certificado, 'NfeInutilizacao', True, **kwargs) + + +def inutilizar_nfe(certificado, **kwargs): + if "xml" not in kwargs: + kwargs['xml'] = xml_inutilizar_nfe(certificado, **kwargs) + return _send(certificado, 'NfeInutilizacao', **kwargs) -def inutilizar_nfe(certificado, **kwargs): # Assinar - return _send(certificado, 'NfeInutilizacao', True, **kwargs) +def xml_consultar_protocolo_nfe(certificado, **kwargs): + return _render(certificado, 'NfeConsultaProtocolo', True, **kwargs) def consultar_protocolo_nfe(certificado, **kwargs): - return _send(certificado, 'NfeConsultaProtocolo', True, **kwargs) + if "xml" not in kwargs: + kwargs['xml'] = xml_consultar_protocolo_nfe(certificado, **kwargs) + return _send(certificado, 'NfeConsultaProtocolo', **kwargs) + + +def xml_nfe_status_servico(certificado, **kwargs): + return _render(certificado, 'NfeStatusServico', False, **kwargs) def nfe_status_servico(certificado, **kwargs): - return _send(certificado, 'NfeStatusServico', False, **kwargs) + if "xml" not in kwargs: + kwargs['xml'] = xml_nfe_status_servico(certificado, **kwargs) + return _send(certificado, 'NfeStatusServico', **kwargs) + + +def xml_consulta_cadastro(certificado, **kwargs): + return _render(certificado, 'NfeConsultaCadastro', False, **kwargs) def consulta_cadastro(certificado, **kwargs): - return _send(certificado, 'NfeConsultaCadastro', False, **kwargs) + if "xml" not in kwargs: + kwargs['xml'] = xml_consulta_cadastro(certificado, **kwargs) + return _send(certificado, 'NfeConsultaCadastro', **kwargs) + + +def xml_recepcao_evento_carta_correcao(certificado, **kwargs): # Assinar + return _render(certificado, 'RecepcaoEventoCarta', True, **kwargs) def recepcao_evento_carta_correcao(certificado, **kwargs): # Assinar + if "xml" not in kwargs: + kwargs['xml'] = xml_recepcao_evento_carta_correcao( + certificado, **kwargs) return _send(certificado, 'RecepcaoEventoCarta', True, **kwargs) +def xml_recepcao_evento_manifesto(certificado, **kwargs): # Assinar + return _render(certificado, 'RecepcaoEventoManifesto', True, **kwargs) + + def recepcao_evento_manifesto(certificado, **kwargs): # Assinar + if "xml" not in kwargs: + kwargs['xml'] = xml_recepcao_evento_manifesto(certificado, **kwargs) return _send(certificado, 'RecepcaoEventoManifesto', True, **kwargs) +def xml_recepcao_evento_epec(certificado, **kwargs): # Assinar + return _render(certificado, 'RecepcaoEventoEPEC', True, **kwargs) + + def recepcao_evento_epec(certificado, **kwargs): # Assinar - return _send(certificado, 'RecepcaoEventoEPEC', True, **kwargs) + if "xml" not in kwargs: + kwargs['xml'] = xml_recepcao_evento_epec(certificado, **kwargs) + return _send(certificado, 'RecepcaoEventoEPEC', **kwargs) + + +def xml_consulta_distribuicao_nfe(certificado, **kwargs): # Assinar + return _render(certificado, 'NFeDistribuicaoDFe', False, **kwargs) def consulta_distribuicao_nfe(certificado, **kwargs): - return _send(certificado, 'NFeDistribuicaoDFe', False, **kwargs) + if "xml" not in kwargs: + kwargs['xml'] = xml_consulta_distribuicao_nfe(certificado, **kwargs) + return _send(certificado, 'NFeDistribuicaoDFe', **kwargs) + + +def xml_download_nfe(certificado, **kwargs): # Assinar + return _render(certificado, 'NFeDistribuicaoDFe', False, **kwargs) def download_nfe(certificado, **kwargs): - return _send(certificado, 'NFeDistribuicaoDFe', False, **kwargs) + if "xml" not in kwargs: + kwargs['xml'] = xml_download_nfe(certificado, **kwargs) + return _send(certificado, 'NFeDistribuicaoDFe', **kwargs) diff --git a/pytrustnfe/xml/schemas/enviNFe_v3.10.xsd b/pytrustnfe/xml/schemas/enviNFe_v3.10.xsd old mode 100644 new mode 100755 diff --git a/pytrustnfe/xml/schemas/leiauteNFe_v3.10.xsd b/pytrustnfe/xml/schemas/leiauteNFe_v3.10.xsd old mode 100644 new mode 100755 diff --git a/pytrustnfe/xml/schemas/nfe_v3.10.xsd b/pytrustnfe/xml/schemas/nfe_v3.10.xsd old mode 100644 new mode 100755 diff --git a/pytrustnfe/xml/schemas/tiposBasico_v3.10.xsd b/pytrustnfe/xml/schemas/tiposBasico_v3.10.xsd old mode 100644 new mode 100755 index 70c9a4b..1dfe7d0 --- a/pytrustnfe/xml/schemas/tiposBasico_v3.10.xsd +++ b/pytrustnfe/xml/schemas/tiposBasico_v3.10.xsd @@ -494,7 +494,7 @@ - + diff --git a/pytrustnfe/xml/schemas/xmldsig-core-schema_v1.01.xsd b/pytrustnfe/xml/schemas/xmldsig-core-schema_v1.01.xsd old mode 100644 new mode 100755 diff --git a/pytrustnfe/xml/validate.py b/pytrustnfe/xml/validate.py index 4314d2c..3306b57 100644 --- a/pytrustnfe/xml/validate.py +++ b/pytrustnfe/xml/validate.py @@ -3,39 +3,16 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). import os -import re from lxml import etree PATH = os.path.dirname(os.path.abspath(__file__)) -SCHEMA = os.path.join(PATH, 'schemas/nfe_v3.10.xsd') +SCHEMA = os.path.join(PATH, 'schemas/enviNFe_v3.10.xsd') -def pop_encoding(xml): - xml = xml.split('\n') - if re.match(r'<\?xml version=', xml[0]): - xml.pop(0) - return '\n'.join(xml) - - -def valida_nfe(nfe): - xml = pop_encoding(nfe).encode('utf-8') - nfe = etree.fromstring(xml) +def valida_nfe(xml_nfe): + nfe = etree.fromstring(xml_nfe) esquema = etree.XMLSchema(etree.parse(SCHEMA)) esquema.validate(nfe) erros = [x.message for x in esquema.error_log] - error_msg = '{field} inválido: {valor}.' - unexpected = '{unexpected} não é esperado. O valor esperado é {expected}' - namespace = '{http://www.portalfiscal.inf.br/nfe}' - mensagens = [] - for erro in erros: - campo = re.findall(r"'([^']*)'", erro)[0] - nome = campo[campo.find('}') + 1: ] - valor = nfe.find('.//' + campo).text - if 'Expected is' in erro: - expected_name = re.findall('\(.*?\)', erro) - valor = unexpected.format(unexpected=nome, expected=expected_name) - mensagem = error_msg.format(field=campo.replace(namespace, ''), - valor=valor) - mensagens.append(mensagem) - return "\n".join(mensagens) + return "\n".join(erros)