diff --git a/pytrustnfe/nfe/assinatura.py b/pytrustnfe/nfe/assinatura.py index d4ef99c..e14ec5d 100644 --- a/pytrustnfe/nfe/assinatura.py +++ b/pytrustnfe/nfe/assinatura.py @@ -30,14 +30,16 @@ class Assinatura(object): ns[None] = signer.namespaces['ds'] signer.namespaces = ns + ref_uri = ('#%s' % reference) if reference else None signed_root = signer.sign( xml_element, key=key, cert=cert, - reference_uri=('#%s' % reference)) - element_signed = signed_root.find(".//*[@Id='%s']" % reference) - signature = signed_root.find( - ".//{http://www.w3.org/2000/09/xmldsig#}Signature") - - if element_signed is not None and signature is not None: - parent = element_signed.getparent() - parent.append(signature) + reference_uri=ref_uri) + if reference: + element_signed = signed_root.find(".//*[@Id='%s']" % reference) + signature = signed_root.find( + ".//{http://www.w3.org/2000/09/xmldsig#}Signature") + + if element_signed is not None and signature is not None: + parent = element_signed.getparent() + parent.append(signature) return etree.tostring(signed_root) diff --git a/pytrustnfe/nfse/ginfes/__init__.py b/pytrustnfe/nfse/ginfes/__init__.py index 6afc878..f2655dc 100644 --- a/pytrustnfe/nfse/ginfes/__init__.py +++ b/pytrustnfe/nfse/ginfes/__init__.py @@ -4,16 +4,27 @@ import os import suds +from lxml import etree from pytrustnfe.xml import render_xml, sanitize_response from pytrustnfe.client import get_authenticated_client from pytrustnfe.certificado import extract_cert_and_key_from_pfx, save_cert_key -from pytrustnfe.nfse.assinatura import Assinatura +from pytrustnfe.nfe.assinatura import Assinatura -def _send(certificado, method, **kwargs): +def _render(certificado, method, **kwargs): path = os.path.join(os.path.dirname(__file__), 'templates') - xml_send = render_xml(path, '%s.xml' % method, False, **kwargs) + xml_send = render_xml(path, '%s.xml' % method, True, **kwargs) + + reference = '' + if method == 'RecepcionarLoteRpsV3': + reference = 'rps%s' % kwargs['nfse']['lista_rps'][0]['numero'] + + signer = Assinatura(certificado.pfx, certificado.password) + xml_send = signer.assina_xml(xml_send, reference) + return xml_send + +def _send(certificado, method, **kwargs): base_url = '' if kwargs['ambiente'] == 'producao': base_url = 'https://producao.ginfes.com.br/ServiceGinfesImpl?wsdl' @@ -23,14 +34,9 @@ def _send(certificado, method, **kwargs): cert, key = extract_cert_and_key_from_pfx( certificado.pfx, certificado.password) cert, key = save_cert_key(cert, key) - client = get_authenticated_client(base_url, cert, key) - - pfx_path = certificado.save_pfx() - signer = Assinatura(pfx_path, certificado.password) - xml_send = signer.assina_xml(xml_send, '') - try: + xml_send = kwargs['xml'] header = '3' #noqa response = getattr(client.service, method)(header, xml_send) except suds.WebFault, e: @@ -48,11 +54,23 @@ def _send(certificado, method, **kwargs): } -def envio_lote_rps(certificado, **kwargs): +def xml_recepcionar_lote_rps(certificado, **kwargs): + return _render(certificado, 'RecepcionarLoteRpsV3', **kwargs) + + +def recepcionar_lote_rps(certificado, **kwargs): + if "xml" not in kwargs: + kwargs['xml'] = xml_recepcionar_lote_rps(certificado, **kwargs) return _send(certificado, 'RecepcionarLoteRpsV3', **kwargs) +def xml_consultar_situacao_lote(certificado, **kwargs): + return _render(certificado, 'ConsultarSituacaoLoteRpsV3', **kwargs) + + def consultar_situacao_lote(certificado, **kwargs): + if "xml" not in kwargs: + kwargs['xml'] = xml_consultar_situacao_lote(certificado, **kwargs) return _send(certificado, 'ConsultarSituacaoLoteRpsV3', **kwargs) @@ -60,7 +78,13 @@ def consultar_nfse_por_rps(certificado, **kwargs): return _send(certificado, 'ConsultarNfsePorRpsV3', **kwargs) -def consultar_lote(certificado, **kwargs): +def xml_consultar_lote_rps(certificado, **kwargs): + return _render(certificado, 'ConsultarLoteRpsV3', **kwargs) + + +def consultar_lote_rps(certificado, **kwargs): + if "xml" not in kwargs: + kwargs['xml'] = xml_consultar_lote_rps(certificado, **kwargs) return _send(certificado, 'ConsultarLoteRpsV3', **kwargs) @@ -68,5 +92,11 @@ def consultar_nfse(certificado, **kwargs): return _send(certificado, 'ConsultarNfseV3', **kwargs) +def xml_cancelar_nfse(certificado, **kwargs): + return _render(certificado, 'CancelarNfseV3', **kwargs) + + def cancelar_nfse(certificado, **kwargs): + if "xml" not in kwargs: + kwargs['xml'] = xml_cancelar_nfse(certificado, **kwargs) return _send(certificado, 'CancelarNfseV3', **kwargs) diff --git a/pytrustnfe/nfse/ginfes/templates/CancelarNfseV3.xml b/pytrustnfe/nfse/ginfes/templates/CancelarNfseV3.xml index ecb5a16..d63af4b 100644 --- a/pytrustnfe/nfse/ginfes/templates/CancelarNfseV3.xml +++ b/pytrustnfe/nfse/ginfes/templates/CancelarNfseV3.xml @@ -1,15 +1,15 @@ - + - 58 + {{ cancelamento.numero_nfse }} - 45111111111100 + {{ cancelamento.cnpj_prestador }} - 123498 - 4204608 + {{ cancelamento.inscricao_municipal }} + {{ cancelamento.cidade }} - 1 + {{ cancelamento.codigo_cancelamento }} diff --git a/pytrustnfe/nfse/ginfes/templates/ConsultarLoteRpsV3.xml b/pytrustnfe/nfse/ginfes/templates/ConsultarLoteRpsV3.xml index 3861c49..e9d8669 100644 --- a/pytrustnfe/nfse/ginfes/templates/ConsultarLoteRpsV3.xml +++ b/pytrustnfe/nfse/ginfes/templates/ConsultarLoteRpsV3.xml @@ -1,8 +1,7 @@ - + - - 45111111111100 - + {{ consulta.cnpj_prestador }} + {{ consulta.inscricao_municipal }} - 141542179222170 + {{ consulta.protocolo }} diff --git a/pytrustnfe/nfse/ginfes/templates/ConsultarSituacaoLoteRpsV3.xml b/pytrustnfe/nfse/ginfes/templates/ConsultarSituacaoLoteRpsV3.xml index 8495be6..9851986 100644 --- a/pytrustnfe/nfse/ginfes/templates/ConsultarSituacaoLoteRpsV3.xml +++ b/pytrustnfe/nfse/ginfes/templates/ConsultarSituacaoLoteRpsV3.xml @@ -1,7 +1,7 @@ - 45111111111100 - 123456 + {{ consulta.cnpj_prestador }} + {{ consulta.inscricao_municipal }} - 141542179222170 + {{ consulta.protocolo }} diff --git a/pytrustnfe/nfse/ginfes/templates/RecepcionarLoteRpsV3.xml b/pytrustnfe/nfse/ginfes/templates/RecepcionarLoteRpsV3.xml index d82352e..38b6dba 100644 --- a/pytrustnfe/nfse/ginfes/templates/RecepcionarLoteRpsV3.xml +++ b/pytrustnfe/nfse/ginfes/templates/RecepcionarLoteRpsV3.xml @@ -1,11 +1,13 @@ - - - 2012024 - 45111111111100 - 123498 - 1 - - {% include 'rps.xml' %} + + + {{ nfse.numero_lote }} + {{ nfse.cnpj_prestador }} + {{ nfse.inscricao_municipal }} + {{ nfse.lista_rps|length }} + + {% for rps in nfse.lista_rps -%} + {% include 'Rps.xml' %} + {% endfor %} diff --git a/pytrustnfe/nfse/ginfes/templates/Rps.xml b/pytrustnfe/nfse/ginfes/templates/Rps.xml index 3c55b6b..6f7b12a 100644 --- a/pytrustnfe/nfse/ginfes/templates/Rps.xml +++ b/pytrustnfe/nfse/ginfes/templates/Rps.xml @@ -1,82 +1,91 @@ - + - 25 - A1 - 1 + {{ rps.numero }} + {{ rps.serie }} + {{ rps.tipo_rps }} - 2014-12-06 - 1 - 1 - 1 - 1 - 1 + {{ rps.data_emissao }} + {{ rps.natureza_operacao }} + {{ rps.regime_tributacao }} + {{ rps.optante_simples }} + {{ rps.incentivador_cultural }} + {{ rps.status }} - 1 - 1 - 1 + {{ rps.numero_substituido }} + {{ rps.serie_substituido }} + {{ rps.tipo_substituido }} - 1 - 100 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 + {{ rps.valor_servico }} + {{ rps.valor_deducao }} + {{ rps.valor_pis }} + {{ rps.valor_cofins }} + {{ rps.valor_inss }} + {{ rps.valor_ir }} + {{ rps.valor_csll }} + {{ rps.iss_retido }} + {{ rps.valor_iss }} + {{ rps.valor_iss_retido }} + {{ rps.outras_retencoes }} + {{ rps.base_calculo }} + {{ rps.aliquota }} + {{ rps.valor_liquido_nfse }} + {{ rps.desconto_incondicionado }} + {{ rps.desconto_condicionado }} - 2 - 0702 - 2525 - Prog. - 4204608 - 1 - 4204608 + {{ rps.codigo_servico }} + {{ rps.cnae_servico }} + {{ rps.codigo_tributacao_municipio }} + {{ rps.descricao }} + {{ rps.codigo_municipio }} - - 45111111111100 - - 123498 + {{ rps.prestador.cnpj }} + {{ rps.prestador.inscricao_municipal }} - 83787494000123 + {% if rps.tomador.cnpj_cpf|length == 14 %} + {{ rps.tomador.cnpj_cpf }} + {% endif %} + {% if rps.tomador.cnpj_cpf|length == 11 %} + {{ rps.tomador.cnpj_cpf }} + {% endif %} + {{ rps.tomador.inscricao_municipal }} - INSTITUICAO FINANCEIRA + {{ rps.tomador.razao_social }} - AV. 7 DE SETEMBRO - 1505 - AO LADO DO JOAO AUTOMOVEIS - CENTRO - 4201406 - SC - 88900000 + {{ rps.tomador.logradouro }} + {{ rps.tomador.numero }} + {{ rps.tomador.complemento }} + {{ rps.tomador.bairro }} + {{ rps.tomador.cidade }} + {{ rps.tomador.uf }} + {{ rps.tomador.cep }} - 4835220026 - luiz.alves@cxpostal.com + {{ rps.tomador.telefone }} + {{ rps.tomador.email }} + {% if rps.intermediario is defined -%} - - - 06410987065144 - - 22252 - - CONSTRUTORA TERRA FIRME + {{ rps.intermediario.razao_social }} + + {{ rps.intermediario.cnpj }} + + {{ rps.intermediario.inscricao_municipal }} - - 142 - 1/2014 - - + {% endif %} + {% if rps.construcao_civil is defined -%} + + {{ rps.construcao_civil.codigo_obra }} + {{ rps.construcao_civil.art }} + + {% endif %} + diff --git a/pytrustnfe/nfse/simpliss/__init__.py b/pytrustnfe/nfse/simpliss/__init__.py index 372770a..14820d7 100644 --- a/pytrustnfe/nfse/simpliss/__init__.py +++ b/pytrustnfe/nfse/simpliss/__init__.py @@ -12,22 +12,20 @@ import os from lxml import etree from pytrustnfe import HttpClient from pytrustnfe.xml import render_xml, sanitize_response -from pytrustnfe.certificado import extract_cert_and_key_from_pfx, save_cert_key -from pytrustnfe.nfse.assinatura import Assinatura def _render_xml(certificado, method, **kwargs): path = os.path.join(os.path.dirname(__file__), 'templates') - xml_send = render_xml(path, '%s.xml' % method, False, **kwargs) - - cert, key = extract_cert_and_key_from_pfx( - certificado.pfx, certificado.password) - cert, key = save_cert_key(cert, key) - - pfx_path = certificado.save_pfx() - signer = Assinatura(pfx_path, certificado.password) - xml_send = signer.assina_xml(xml_send, '') - + xml_send = render_xml(path, '%s.xml' % method, True, **kwargs) + xml_send = etree.tostring(xml_send) + + # cert, key = extract_cert_and_key_from_pfx( + # certificado.pfx, certificado.password) + # cert, key = save_cert_key(cert, key) + # + # pfx_path = certificado.save_pfx() + # signer = Assinatura(pfx_path, certificado.password) + # xml_send = signer.assina_xml(xml_send, '') return xml_send @@ -44,10 +42,11 @@ def _validate(method, xml): def _send(method, **kwargs): if kwargs['ambiente'] == 'producao': - base_url = 'http://sistemas.pmp.sp.gov.br/semfi/simpliss/ws_nfse/nfseservice.svc?WSDL' # noqa + base_url = 'http://sistemas.pmp.sp.gov.br/semfi/simpliss/ws_nfse/nfseservice.svc' # noqa else: - base_url = 'http://wshomologacao.simplissweb.com.br/nfseservice.svc?WSDL' # noqa + base_url = 'http://wshomologacao.simplissweb.com.br/nfseservice.svc' # noqa + base_url = 'http://wshomologacao.simplissweb.com.br/nfseservice.svc' xml_send = kwargs["xml"].replace('', '') path = os.path.join(os.path.dirname(__file__), 'templates') soap = render_xml(path, 'SoapRequest.xml', False, soap_body=xml_send) @@ -57,7 +56,6 @@ def _send(method, **kwargs): client = HttpClient(base_url) response = client.post_soap(soap, act) - print response response, obj = sanitize_response(response) return { 'sent_xml': xml_send, @@ -90,13 +88,29 @@ def consultar_nfse_por_rps(certificado, **kwargs): return _send('ConsultarNfsePorRps', **kwargs) +def xml_consultar_lote_rps(certificado, **kwargs): + return _render_xml(certificado, 'ConsultarLoteRps', **kwargs) + + def consultar_lote_rps(certificado, **kwargs): + if "xml" not in kwargs: + kwargs['xml'] = xml_consultar_lote_rps(certificado, **kwargs) return _send('ConsultarLoteRps', **kwargs) +def xml_consultar_nfse(certificado, **kwargs): + return _render_xml(certificado, 'ConsultarNfse', **kwargs) + + def consultar_nfse(certificado, **kwargs): return _send('ConsultarNfse', **kwargs) +def xml_cancelar_nfse(certificado, **kwargs): + return _render_xml(certificado, 'CancelarNfse', **kwargs) + + def cancelar_nfse(certificado, **kwargs): + if "xml" not in kwargs: + kwargs['xml'] = xml_cancelar_nfse(certificado, **kwargs) return _send('CancelarNfse', **kwargs) diff --git a/pytrustnfe/nfse/simpliss/templates/CancelarNfse.xml b/pytrustnfe/nfse/simpliss/templates/CancelarNfse.xml index 8b1d937..2164a6d 100644 --- a/pytrustnfe/nfse/simpliss/templates/CancelarNfse.xml +++ b/pytrustnfe/nfse/simpliss/templates/CancelarNfse.xml @@ -3,17 +3,17 @@ - [nonNegativeInteger?] - [string?] - [string?] - [int] + {{ cancelamento.numero_nfse }} + {{ cancelamento.cnpj_prestador }} + {{ cancelamento.inscricao_municipal }} + {{ cancelamento.cidade }} - [string?] + {{ cancelamento.codigo_cancelamento }} - [string?] - [string?] + {{ cancelamento.cnpj_prestador }} + {{ cancelamento.senha }} diff --git a/pytrustnfe/nfse/simpliss/templates/ConsultarLoteRps.xml b/pytrustnfe/nfse/simpliss/templates/ConsultarLoteRps.xml index f11b044..a64f733 100644 --- a/pytrustnfe/nfse/simpliss/templates/ConsultarLoteRps.xml +++ b/pytrustnfe/nfse/simpliss/templates/ConsultarLoteRps.xml @@ -1,13 +1,13 @@ - [string?] - [string?] + {{ consulta.cnpj_prestador }} + {{ consulta.inscricao_municipal }} - [string?] + {{ consulta.protocolo }} - [string?] - [string?] + {{ consulta.cnpj_prestador }} + {{ consulta.senha }} diff --git a/pytrustnfe/nfse/simpliss/templates/ConsultarSituacaoLoteRps.xml b/pytrustnfe/nfse/simpliss/templates/ConsultarSituacaoLoteRps.xml index e24cc0a..a3077ea 100644 --- a/pytrustnfe/nfse/simpliss/templates/ConsultarSituacaoLoteRps.xml +++ b/pytrustnfe/nfse/simpliss/templates/ConsultarSituacaoLoteRps.xml @@ -1,13 +1,13 @@ - [string?] - [string?] + {{ consulta.cnpj_prestador }} + {{ consulta.inscricao_municipal }} - [string?] + {{ consulta.protocolo }} - [string?] - [string?] + {{ consulta.cnpj_prestador }} + {{ consulta.senha }} diff --git a/pytrustnfe/nfse/simpliss/templates/RecepcionarLoteRps.xml b/pytrustnfe/nfse/simpliss/templates/RecepcionarLoteRps.xml index 641a711..bc2a69c 100644 --- a/pytrustnfe/nfse/simpliss/templates/RecepcionarLoteRps.xml +++ b/pytrustnfe/nfse/simpliss/templates/RecepcionarLoteRps.xml @@ -4,7 +4,7 @@ {{ nfse.numero_lote }} {{ nfse.cnpj_prestador }} {{ nfse.inscricao_municipal }} - 1 + {{ nfse.lista_rps|length }} {% for rps in nfse.lista_rps -%} {% include 'Rps.xml' %} diff --git a/pytrustnfe/nfse/simpliss/templates/Rps.xml b/pytrustnfe/nfse/simpliss/templates/Rps.xml index 0a172e5..6b1fe3e 100644 --- a/pytrustnfe/nfse/simpliss/templates/Rps.xml +++ b/pytrustnfe/nfse/simpliss/templates/Rps.xml @@ -3,7 +3,7 @@ {{ rps.numero }} {{ rps.serie }} - {{ rps.tipo }} + {{ rps.tipo_rps }} {{ rps.data_emissao }} {{ rps.natureza_operacao }} @@ -55,8 +55,12 @@ + {% if rps.tomador.cnpj_cpf|length == 14 %} {{ rps.tomador.cnpj_cpf }} + {% endif %} + {% if rps.tomador.cnpj_cpf|length == 11 %} {{ rps.tomador.cnpj_cpf }} + {% endif %} {{ rps.tomador.inscricao_municipal }} @@ -66,7 +70,7 @@ {{ rps.tomador.numero }} {{ rps.tomador.complemento }} {{ rps.tomador.bairro }} - {{ rps.tomador.codigo_municipio }} + {{ rps.tomador.cidade }} {{ rps.tomador.uf }} {{ rps.tomador.cep }} diff --git a/pytrustnfe/test/test_ginfes.py b/pytrustnfe/test/test_ginfes.py index ffb9f20..d2895f2 100644 --- a/pytrustnfe/test/test_ginfes.py +++ b/pytrustnfe/test/test_ginfes.py @@ -11,6 +11,7 @@ class test_nfse_ginfes(unittest.TestCase): caminho = os.path.dirname(__file__) + @unittest.skip def test_consulta_situacao_lote(self): pfx_source = open('/home/danimar/Downloads/machado.pfx', 'r').read() pfx = Certificado(pfx_source, '123456789') diff --git a/pytrustnfe/test/test_simpliss.py b/pytrustnfe/test/test_simpliss.py index a063007..5107b85 100644 --- a/pytrustnfe/test/test_simpliss.py +++ b/pytrustnfe/test/test_simpliss.py @@ -19,5 +19,5 @@ class test_nfse_simpliss(unittest.TestCase): 'inscricao_prestador': '123', 'protocolo': '123'} response = recepcionar_lote_rps( - pfx, consulta=dados, ambiente='homologacao') + pfx, nfse=dados, ambiente='homologacao') print response