diff --git a/pytrustnfe/nfse/ginfes/__init__.py b/pytrustnfe/nfse/ginfes/__init__.py index 449d2f8..6afc878 100644 --- a/pytrustnfe/nfse/ginfes/__init__.py +++ b/pytrustnfe/nfse/ginfes/__init__.py @@ -1,3 +1,72 @@ # -*- coding: utf-8 -*- # © 2016 Danimar Ribeiro, Trustcode # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +import os +import suds +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 + + +def _send(certificado, method, **kwargs): + path = os.path.join(os.path.dirname(__file__), 'templates') + xml_send = render_xml(path, '%s.xml' % method, False, **kwargs) + + base_url = '' + if kwargs['ambiente'] == 'producao': + base_url = 'https://producao.ginfes.com.br/ServiceGinfesImpl?wsdl' + else: + base_url = 'https://homologacao.ginfes.com.br/ServiceGinfesImpl?wsdl' + + 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: + header = '3' #noqa + response = getattr(client.service, method)(header, xml_send) + except suds.WebFault, e: + return { + 'sent_xml': xml_send, + 'received_xml': e.fault.faultstring, + 'object': None + } + + response, obj = sanitize_response(response) + return { + 'sent_xml': xml_send, + 'received_xml': response, + 'object': obj + } + + +def envio_lote_rps(certificado, **kwargs): + return _send(certificado, 'RecepcionarLoteRpsV3', **kwargs) + + +def consultar_situacao_lote(certificado, **kwargs): + return _send(certificado, 'ConsultarSituacaoLoteRpsV3', **kwargs) + + +def consultar_nfse_por_rps(certificado, **kwargs): + return _send(certificado, 'ConsultarNfsePorRpsV3', **kwargs) + + +def consultar_lote(certificado, **kwargs): + return _send(certificado, 'ConsultarLoteRpsV3', **kwargs) + + +def consultar_nfse(certificado, **kwargs): + return _send(certificado, 'ConsultarNfseV3', **kwargs) + + +def 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 new file mode 100644 index 0000000..ecb5a16 --- /dev/null +++ b/pytrustnfe/nfse/ginfes/templates/CancelarNfseV3.xml @@ -0,0 +1,15 @@ + + + + + 58 + + 45111111111100 + + 123498 + 4204608 + + 1 + + + diff --git a/pytrustnfe/nfse/ginfes/templates/ConsultarLoteRpsV3.xml b/pytrustnfe/nfse/ginfes/templates/ConsultarLoteRpsV3.xml new file mode 100644 index 0000000..3861c49 --- /dev/null +++ b/pytrustnfe/nfse/ginfes/templates/ConsultarLoteRpsV3.xml @@ -0,0 +1,8 @@ + + + + 45111111111100 + + + 141542179222170 + diff --git a/pytrustnfe/nfse/ginfes/templates/ConsultarNfsePorRpsV3.xml b/pytrustnfe/nfse/ginfes/templates/ConsultarNfsePorRpsV3.xml new file mode 100644 index 0000000..e86e16b --- /dev/null +++ b/pytrustnfe/nfse/ginfes/templates/ConsultarNfsePorRpsV3.xml @@ -0,0 +1,13 @@ + + + 24 + A1 + 1 + + + + 45111111111100 + + 123498 + + diff --git a/pytrustnfe/nfse/ginfes/templates/ConsultarNfseV3.xml b/pytrustnfe/nfse/ginfes/templates/ConsultarNfseV3.xml new file mode 100644 index 0000000..3861c49 --- /dev/null +++ b/pytrustnfe/nfse/ginfes/templates/ConsultarNfseV3.xml @@ -0,0 +1,8 @@ + + + + 45111111111100 + + + 141542179222170 + diff --git a/pytrustnfe/nfse/ginfes/templates/ConsultarSituacaoLoteRpsV3.xml b/pytrustnfe/nfse/ginfes/templates/ConsultarSituacaoLoteRpsV3.xml new file mode 100644 index 0000000..8495be6 --- /dev/null +++ b/pytrustnfe/nfse/ginfes/templates/ConsultarSituacaoLoteRpsV3.xml @@ -0,0 +1,7 @@ + + + 45111111111100 + 123456 + + 141542179222170 + diff --git a/pytrustnfe/nfse/ginfes/templates/RecepcionarLoteRpsV3.xml b/pytrustnfe/nfse/ginfes/templates/RecepcionarLoteRpsV3.xml new file mode 100644 index 0000000..d82352e --- /dev/null +++ b/pytrustnfe/nfse/ginfes/templates/RecepcionarLoteRpsV3.xml @@ -0,0 +1,11 @@ + + + 2012024 + 45111111111100 + 123498 + 1 + + {% include 'rps.xml' %} + + + diff --git a/pytrustnfe/nfse/ginfes/templates/Rps.xml b/pytrustnfe/nfse/ginfes/templates/Rps.xml new file mode 100644 index 0000000..3c55b6b --- /dev/null +++ b/pytrustnfe/nfse/ginfes/templates/Rps.xml @@ -0,0 +1,82 @@ + + + + 25 + A1 + 1 + + 2014-12-06 + 1 + 1 + 1 + 1 + 1 + + 1 + 1 + 1 + + 1 + + + 100 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + + 2 + 0702 + 2525 + Prog. + 4204608 + 1 + 4204608 + + + + 45111111111100 + + 123498 + + + + + 83787494000123 + + + INSTITUICAO FINANCEIRA + + AV. 7 DE SETEMBRO + 1505 + AO LADO DO JOAO AUTOMOVEIS + CENTRO + 4201406 + SC + 88900000 + + + 4835220026 + luiz.alves@cxpostal.com + + + + + + 06410987065144 + + 22252 + + CONSTRUTORA TERRA FIRME + + + 142 + 1/2014 + + + diff --git a/pytrustnfe/nfse/simpliss/__init__.py b/pytrustnfe/nfse/simpliss/__init__.py new file mode 100644 index 0000000..fb5becb --- /dev/null +++ b/pytrustnfe/nfse/simpliss/__init__.py @@ -0,0 +1,68 @@ +# -*- coding: utf-8 -*- +# © 2016 Danimar Ribeiro, Trustcode +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +import os +import suds +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 + + +def _send(certificado, method, **kwargs): + # A little hack to test + path = os.path.join(os.path.dirname(__file__), 'templates') + + xml_send = render_xml(path, '%s.xml' % method, False, **kwargs) + + base_url = 'Achar URL' + + 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: + response = getattr(client.service, method)(1, xml_send) + except suds.WebFault, e: + return { + 'sent_xml': xml_send, + 'received_xml': e.fault.faultstring, + 'object': None + } + + response, obj = sanitize_response(response) + return { + 'sent_xml': xml_send, + 'received_xml': response, + 'object': obj + } + + +def envio_lote_rps(certificado, **kwargs): + return _send(certificado, 'EnvioLoteRps', **kwargs) + + +def consultar_situacao_lote(certificado, **kwargs): + return _send(certificado, 'ConsultarSituacaoLote', **kwargs) + + +def consultar_nfse_por_rps(certificado, **kwargs): + return _send(certificado, 'ConsultarNFSePorRps', **kwargs) + + +def consultar_lote(certificado, **kwargs): + return _send(certificado, 'ConsultarLote', **kwargs) + + +def consultar_nfse(certificado, **kwargs): + return _send(certificado, 'ConsultarNFSe', **kwargs) + + +def cancelar_nfse(certificado, **kwargs): + return _send(certificado, 'CancelarNFSe', **kwargs) diff --git a/pytrustnfe/nfse/simpliss/templates/CancelarNFSe.xml b/pytrustnfe/nfse/simpliss/templates/CancelarNFSe.xml new file mode 100644 index 0000000..ecb5a16 --- /dev/null +++ b/pytrustnfe/nfse/simpliss/templates/CancelarNFSe.xml @@ -0,0 +1,15 @@ + + + + + 58 + + 45111111111100 + + 123498 + 4204608 + + 1 + + + diff --git a/pytrustnfe/nfse/simpliss/templates/ConsultarLote.xml b/pytrustnfe/nfse/simpliss/templates/ConsultarLote.xml new file mode 100644 index 0000000..3861c49 --- /dev/null +++ b/pytrustnfe/nfse/simpliss/templates/ConsultarLote.xml @@ -0,0 +1,8 @@ + + + + 45111111111100 + + + 141542179222170 + diff --git a/pytrustnfe/nfse/simpliss/templates/ConsultarNFSe.xml b/pytrustnfe/nfse/simpliss/templates/ConsultarNFSe.xml new file mode 100644 index 0000000..3861c49 --- /dev/null +++ b/pytrustnfe/nfse/simpliss/templates/ConsultarNFSe.xml @@ -0,0 +1,8 @@ + + + + 45111111111100 + + + 141542179222170 + diff --git a/pytrustnfe/nfse/simpliss/templates/ConsultarNFSePorRps.xml b/pytrustnfe/nfse/simpliss/templates/ConsultarNFSePorRps.xml new file mode 100644 index 0000000..e86e16b --- /dev/null +++ b/pytrustnfe/nfse/simpliss/templates/ConsultarNFSePorRps.xml @@ -0,0 +1,13 @@ + + + 24 + A1 + 1 + + + + 45111111111100 + + 123498 + + diff --git a/pytrustnfe/nfse/simpliss/templates/ConsultarSituacaoLote.xml b/pytrustnfe/nfse/simpliss/templates/ConsultarSituacaoLote.xml new file mode 100644 index 0000000..3861c49 --- /dev/null +++ b/pytrustnfe/nfse/simpliss/templates/ConsultarSituacaoLote.xml @@ -0,0 +1,8 @@ + + + + 45111111111100 + + + 141542179222170 + diff --git a/pytrustnfe/nfse/simpliss/templates/EnvioLoteRps.xml b/pytrustnfe/nfse/simpliss/templates/EnvioLoteRps.xml new file mode 100644 index 0000000..d82352e --- /dev/null +++ b/pytrustnfe/nfse/simpliss/templates/EnvioLoteRps.xml @@ -0,0 +1,11 @@ + + + 2012024 + 45111111111100 + 123498 + 1 + + {% include 'rps.xml' %} + + + diff --git a/pytrustnfe/nfse/simpliss/templates/Rps.xml b/pytrustnfe/nfse/simpliss/templates/Rps.xml new file mode 100644 index 0000000..3c55b6b --- /dev/null +++ b/pytrustnfe/nfse/simpliss/templates/Rps.xml @@ -0,0 +1,82 @@ + + + + 25 + A1 + 1 + + 2014-12-06 + 1 + 1 + 1 + 1 + 1 + + 1 + 1 + 1 + + 1 + + + 100 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + + 2 + 0702 + 2525 + Prog. + 4204608 + 1 + 4204608 + + + + 45111111111100 + + 123498 + + + + + 83787494000123 + + + INSTITUICAO FINANCEIRA + + AV. 7 DE SETEMBRO + 1505 + AO LADO DO JOAO AUTOMOVEIS + CENTRO + 4201406 + SC + 88900000 + + + 4835220026 + luiz.alves@cxpostal.com + + + + + + 06410987065144 + + 22252 + + CONSTRUTORA TERRA FIRME + + + 142 + 1/2014 + + + diff --git a/pytrustnfe/test/test_ginfes.py b/pytrustnfe/test/test_ginfes.py new file mode 100644 index 0000000..b58dd1c --- /dev/null +++ b/pytrustnfe/test/test_ginfes.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# © 2016 Danimar Ribeiro, Trustcode +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +import os.path +import unittest +from pytrustnfe.certificado import Certificado +from pytrustnfe.nfse.ginfes import consultar_situacao_lote +from pytrustnfe.nfse.ginfes import consultar_nfse + + +class test_nfse_ginfes(unittest.TestCase): + + caminho = os.path.dirname(__file__) + + def test_consulta_situacao_lote(self): + pfx_source = open('/home/danimar/Downloads/machado.pfx', 'r').read() + pfx = Certificado(pfx_source, '123456789') + + dados = {'ambiente': 'homologacao'} + retorno = consultar_situacao_lote( + pfx, consulta=dados, ambiente='homologacao') + + print retorno + self.assertNotEqual(retorno['received_xml'], '') + self.assertEqual(retorno['object'].Cabecalho.Sucesso, True) diff --git a/pytrustnfe/test/test_simpliss.py b/pytrustnfe/test/test_simpliss.py new file mode 100644 index 0000000..e69de29 diff --git a/setup.py b/setup.py index 284efff..b623ba1 100644 --- a/setup.py +++ b/setup.py @@ -23,6 +23,9 @@ later (LGPLv2+)', package_data={'pytrustnfe': [ 'nfe/templates/*xml', 'nfse/paulistana/templates/*xml', + 'nfse/ginfes/templates/*xml', + 'nfse/simpliss/templates/*xml', + 'nfse/betha/templates/*xml', 'xml/schemas/*xsd', ]}, url='https://github.com/danimaribeiro/PyTrustNFe',