From 2bbd78c295fcfc9dbe673788b4c293367420e6a8 Mon Sep 17 00:00:00 2001 From: Danimar Ribeiro Date: Tue, 27 Mar 2018 18:47:12 -0300 Subject: [PATCH] =?UTF-8?q?Implementa=C3=A7=C3=A3o=20da=20nota=20carioca?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pytrustnfe/nfse/carioca/__init__.py | 71 +++++++++++++++++ .../nfse/carioca/templates/CancelarNfseEnvio.xml | 13 ++++ .../nfse/carioca/templates/ConsultarNfseEnvio.xml | 8 ++ pytrustnfe/nfse/carioca/templates/GerarNfse.xml | 3 + pytrustnfe/nfse/carioca/templates/Rps.xml | 91 ++++++++++++++++++++++ setup.py | 1 + 6 files changed, 187 insertions(+) create mode 100644 pytrustnfe/nfse/carioca/__init__.py create mode 100644 pytrustnfe/nfse/carioca/templates/CancelarNfseEnvio.xml create mode 100644 pytrustnfe/nfse/carioca/templates/ConsultarNfseEnvio.xml create mode 100644 pytrustnfe/nfse/carioca/templates/GerarNfse.xml create mode 100644 pytrustnfe/nfse/carioca/templates/Rps.xml diff --git a/pytrustnfe/nfse/carioca/__init__.py b/pytrustnfe/nfse/carioca/__init__.py new file mode 100644 index 0000000..939c7ab --- /dev/null +++ b/pytrustnfe/nfse/carioca/__init__.py @@ -0,0 +1,71 @@ +# © 2018 Danimar Ribeiro, Trustcode +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +import os +from pytrustnfe.client import get_authenticated_client +from pytrustnfe.certificado import extract_cert_and_key_from_pfx, save_cert_key +from pytrustnfe.xml import render_xml, sanitize_response +from pytrustnfe.nfe.assinatura import Assinatura + + +def _render(certificado, method, **kwargs): + path = os.path.join(os.path.dirname(__file__), 'templates') + xml_send = render_xml(path, '%s.xml' % method, True, **kwargs) + + reference = '' + if method == 'GerarNfse': + reference = 'r%s' % kwargs['rps']['numero'] + + signer = Assinatura(certificado.pfx, certificado.password) + xml_send = signer.assina_xml(xml_send, reference) + return xml_send.encode('utf-8') + + +def _send(certificado, method, **kwargs): + base_url = '' + if kwargs['ambiente'] == 'producao': + base_url = 'https://notacarioca.rio.gov.br/WSNacional/nfse.asmx?wsdl' + else: + base_url = 'https://homologacao.notacarioca.rio.gov.br/WSNacional/nfse.asmx?wsdl' # noqa + + xml_send = kwargs["xml"].decode('utf-8') + 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) + + try: + response = client.service.GerarNfse(xml_send) + except suds.WebFault as e: + return { + 'sent_xml': str(xml_send), + 'received_xml': str(e.fault.faultstring), + 'object': None + } + + response, obj = sanitize_response(response) + return { + 'sent_xml': str(xml_send), + 'received_xml': str(response), + 'object': obj + } + + +def xml_gerar_nfse(certificado, **kwargs): + return _render(certificado, 'GerarNfse', **kwargs) + + +def gerar_nfse(certificado, **kwargs): + if "xml" not in kwargs: + kwargs['xml'] = xml_gerar_nfse(certificado, **kwargs) + return _send(certificado, 'GerarNfse', **kwargs) + + +def xml_cancelar_nfse(certificado, **kwargs): + return _render(certificado, 'CancelarNfseEnvio', **kwargs) + + +def cancelar_nfse(certificado, **kwargs): + if "xml" not in kwargs: + kwargs['xml'] = xml_cancelar_nfse(certificado, **kwargs) + return _send(certificado, 'CancelarNfseEnvio', **kwargs) diff --git a/pytrustnfe/nfse/carioca/templates/CancelarNfseEnvio.xml b/pytrustnfe/nfse/carioca/templates/CancelarNfseEnvio.xml new file mode 100644 index 0000000..5a1154e --- /dev/null +++ b/pytrustnfe/nfse/carioca/templates/CancelarNfseEnvio.xml @@ -0,0 +1,13 @@ + + + + + {{ rps.nfse_number }} + {{ rps.emitter.cnpj }} + {{ rps.emitter.city_inscription }} + {{ rps.service.city_code }} + + 1 + + + diff --git a/pytrustnfe/nfse/carioca/templates/ConsultarNfseEnvio.xml b/pytrustnfe/nfse/carioca/templates/ConsultarNfseEnvio.xml new file mode 100644 index 0000000..1443a0b --- /dev/null +++ b/pytrustnfe/nfse/carioca/templates/ConsultarNfseEnvio.xml @@ -0,0 +1,8 @@ + + + + {{ rps.emitter.cnpj }} + {{ rps.emitter.city_inscription }} + + {{ rps.nfse_number }} + diff --git a/pytrustnfe/nfse/carioca/templates/GerarNfse.xml b/pytrustnfe/nfse/carioca/templates/GerarNfse.xml new file mode 100644 index 0000000..04829af --- /dev/null +++ b/pytrustnfe/nfse/carioca/templates/GerarNfse.xml @@ -0,0 +1,3 @@ + + {% include 'Rps.xml' %} + diff --git a/pytrustnfe/nfse/carioca/templates/Rps.xml b/pytrustnfe/nfse/carioca/templates/Rps.xml new file mode 100644 index 0000000..7b883af --- /dev/null +++ b/pytrustnfe/nfse/carioca/templates/Rps.xml @@ -0,0 +1,91 @@ + + + + {{ rps.numero }} + {{ rps.serie }} + {{ rps.tipo_rps }} + + {{ rps.data_emissao }} + {{ rps.natureza_operacao }} + {{ rps.regime_tributacao }} + {{ rps.optante_simples }} + {{ rps.incentivador_cultural }} + {{ rps.status }} + + {{ rps.numero_substituido }} + {{ rps.serie_substituido }} + {{ rps.tipo_substituido }} + + + + {{ 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_issqn }} + {{ rps.valor_liquido_nfse }} + {{ rps.desconto_incondicionado }} + {{ rps.desconto_condicionado }} + + {{ rps.codigo_servico }} + {{ rps.cnae_servico }} + {{ rps.codigo_tributacao_municipio }} + {{ rps.descricao }} + {{ rps.codigo_municipio }} + + + {{ rps.prestador.cnpj }} + {{ rps.prestador.inscricao_municipal }} + + + + + {% 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 }} + + {{ rps.tomador.razao_social }} + + {{ rps.tomador.logradouro }} + {{ rps.tomador.numero }} + {{ rps.tomador.complemento }} + {{ rps.tomador.bairro }} + {{ rps.tomador.cidade }} + {{ rps.tomador.uf }} + {{ rps.tomador.cep }} + + + {{ rps.tomador.telefone }} + {{ rps.tomador.email }} + + + {% if rps.intermediario is defined -%} + + {{ rps.intermediario.razao_social }} + + {{ rps.intermediario.cnpj }} + + {{ rps.intermediario.inscricao_municipal }} + + {% endif %} + {% if rps.construcao_civil is defined -%} + + {{ rps.construcao_civil.codigo_obra }} + {{ rps.construcao_civil.art }} + + {% endif %} + + diff --git a/setup.py b/setup.py index d954ca7..c7e626b 100644 --- a/setup.py +++ b/setup.py @@ -36,6 +36,7 @@ later (LGPLv2+)', 'nfse/susesu/templates/*xml', 'nfse/imperial/templates/*xml', 'nfse/floripa/templates/*xml', + 'nfse/carioca/templates/*xml', 'xml/schemas/*xsd', ]}, url='https://github.com/danimaribeiro/PyTrustNFe',