-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP - Implementação da nota de BH, tentativa de usar o zeep
- Loading branch information
1 parent
eb1db21
commit 2de4d3e
Showing
4 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# © 2018 Danimar Ribeiro, Trustcode | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
|
||
import os | ||
|
||
from requests import Session | ||
from zeep import Client | ||
from zeep.transports import Transport | ||
|
||
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'] | ||
elif method == 'CancelarNfse': | ||
reference = 'Cancelamento_NF%s' % kwargs['cancelamento']['numero_nfse'] | ||
|
||
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://bhissdigital.pbh.gov.br/bhiss-ws/nfse?wsdl' | ||
else: | ||
base_url = 'https://bhisshomologa.pbh.gov.br/bhiss-ws/nfse?wsdl' | ||
|
||
xml_send = kwargs["xml"].decode('utf-8') | ||
xml_cabecalho = '' | ||
|
||
cert, key = extract_cert_and_key_from_pfx( | ||
certificado.pfx, certificado.password) | ||
cert, key = save_cert_key(cert, key) | ||
|
||
session = Session() | ||
session.cert = (cert, key) | ||
session.verify = False | ||
transport = Transport(session=session) | ||
|
||
client = Client(base_url, transport=transport) | ||
|
||
response = client.service[method](xml_cabecalho, xml_send) | ||
|
||
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, 'CancelarNfse', **kwargs) | ||
|
||
|
||
def cancelar_nfse(certificado, **kwargs): | ||
if "xml" not in kwargs: | ||
kwargs['xml'] = xml_cancelar_nfse(certificado, **kwargs) | ||
return _send(certificado, 'CancelarNfse', **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<CancelarNfseEnvio xmlns="http://www.abrasf.org.br/ABRASF/arquivos/nfse.xsd"> | ||
<Pedido xmlns="http://www.abrasf.org.br/nfse.xsd"> | ||
<InfPedidoCancelamento Id="pedidoCancelamento_{{ cancelamento.numero_nfse }}"> | ||
<IdentificacaoNfse> | ||
<Numero>{{ cancelamento.numero_nfse }}</Numero> | ||
<Cnpj>{{ cancelamento.cnpj_prestador }}</Cnpj> | ||
<InscricaoMunicipal>{{ cancelamento.inscricao_municipal }}</InscricaoMunicipal> | ||
<CodigoMunicipio>{{ cancelamento.cidade }}</CodigoMunicipio> | ||
</IdentificacaoNfse> | ||
<CodigoCancelamento>1</CodigoCancelamento> | ||
</InfPedidoCancelamento> | ||
</Pedido> | ||
</CancelarNfseEnvio> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<GerarNfseEnvio xmlns="http://www.abrasf.org.br/nfse.xsd"> | ||
<LoteRps Id="lote{{ nfse.numero_lote }}"> | ||
<NumeroLote>{{ nfse.numero_lote }}</NumeroLote> | ||
<Cnpj>{{ nfse.cnpj_prestador }}</Cnpj> | ||
<InscricaoMunicipal>{{ nfse.inscricao_municipal }}</InscricaoMunicipal> | ||
<QuantidadeRps>1</QuantidadeRps> | ||
<ListaRps xmlns="http://www.abrasf.org.br/nfse.xsd"> | ||
{% include 'Rps.xml' %} | ||
</ListaRps> | ||
</LoteRps> | ||
</GerarNfseEnvio> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<Rps> | ||
<InfRps xmlns="http://www.abrasf.org.br/ABRASF/arquivos/nfse.xsd" Id="r{{ rps.numero }}"> | ||
<IdentificacaoRps> | ||
<Numero>{{ rps.numero }}</Numero> | ||
<Serie>{{ rps.serie }}</Serie> | ||
<Tipo>{{ rps.tipo_rps }}</Tipo> | ||
</IdentificacaoRps> | ||
<DataEmissao>{{ rps.data_emissao }}</DataEmissao> | ||
<NaturezaOperacao>{{ rps.natureza_operacao }}</NaturezaOperacao> | ||
<RegimeEspecialTributacao>{{ rps.regime_tributacao }}</RegimeEspecialTributacao> | ||
<OptanteSimplesNacional>{{ rps.optante_simples }}</OptanteSimplesNacional> | ||
<IncentivadorCultural>{{ rps.incentivador_cultural }}</IncentivadorCultural> | ||
<Status>{{ rps.status }}</Status> | ||
<RpsSubstituido> | ||
<Numero>{{ rps.numero_substituido }}</Numero> | ||
<Serie>{{ rps.serie_substituido }}</Serie> | ||
<Tipo>{{ rps.tipo_substituido }}</Tipo> | ||
</RpsSubstituido> | ||
<Servico> | ||
<Valores> | ||
<ValorServicos>{{ rps.valor_servico }}</ValorServicos> | ||
<ValorDeducoes>{{ rps.valor_deducao }}</ValorDeducoes> | ||
<ValorPis>{{ rps.valor_pis }}</ValorPis> | ||
<ValorCofins>{{ rps.valor_cofins }}</ValorCofins> | ||
<ValorInss>{{ rps.valor_inss }}</ValorInss> | ||
<ValorIr>{{ rps.valor_ir }}</ValorIr> | ||
<ValorCsll>{{ rps.valor_csll }}</ValorCsll> | ||
<IssRetido>{{ rps.iss_retido }}</IssRetido> | ||
<ValorIss>{{ rps.valor_iss }}</ValorIss> | ||
<ValorIssRetido>{{ rps.valor_iss_retido }}</ValorIssRetido> | ||
<OutrasRetencoes>{{ rps.outras_retencoes }}</OutrasRetencoes> | ||
<BaseCalculo>{{ rps.base_calculo }}</BaseCalculo> | ||
<Aliquota>{{ rps.aliquota_issqn }}</Aliquota> | ||
<ValorLiquidoNfse>{{ rps.valor_liquido_nfse }}</ValorLiquidoNfse> | ||
<DescontoIncondicionado>{{ rps.desconto_incondicionado }}</DescontoIncondicionado> | ||
<DescontoCondicionado>{{ rps.desconto_condicionado }}</DescontoCondicionado> | ||
</Valores> | ||
<ItemListaServico>{{ rps.codigo_servico }}</ItemListaServico> | ||
<CodigoCnae>{{ rps.cnae_servico }}</CodigoCnae> | ||
<CodigoTributacaoMunicipio>{{ rps.codigo_tributacao_municipio }}</CodigoTributacaoMunicipio> | ||
<Discriminacao>{{ rps.descricao }}</Discriminacao> | ||
<CodigoMunicipio>{{ rps.codigo_municipio }}</CodigoMunicipio> | ||
</Servico> | ||
<Prestador> | ||
<Cnpj>{{ rps.prestador.cnpj }}</Cnpj> | ||
<InscricaoMunicipal>{{ rps.prestador.inscricao_municipal }}</InscricaoMunicipal> | ||
</Prestador> | ||
<Tomador> | ||
<IdentificacaoTomador> | ||
<CpfCnpj> | ||
{% if rps.tomador.cnpj_cpf|length == 14 %} | ||
<Cnpj>{{ rps.tomador.cnpj_cpf }}</Cnpj> | ||
{% endif %} | ||
{% if rps.tomador.cnpj_cpf|length == 11 %} | ||
<Cpf>{{ rps.tomador.cnpj_cpf }}</Cpf> | ||
{% endif %} | ||
</CpfCnpj> | ||
<InscricaoMunicipal>{{ rps.tomador.inscricao_municipal }}</InscricaoMunicipal> | ||
</IdentificacaoTomador> | ||
<RazaoSocial>{{ rps.tomador.razao_social }}</RazaoSocial> | ||
<Endereco> | ||
<Endereco>{{ rps.tomador.logradouro }}</Endereco> | ||
<Numero>{{ rps.tomador.numero }}</Numero> | ||
<Complemento>{{ rps.tomador.complemento }}</Complemento> | ||
<Bairro>{{ rps.tomador.bairro }}</Bairro> | ||
<CodigoMunicipio>{{ rps.tomador.cidade }}</CodigoMunicipio> | ||
<Uf>{{ rps.tomador.uf }}</Uf> | ||
<Cep>{{ rps.tomador.cep }}</Cep> | ||
</Endereco> | ||
<Contato> | ||
<Telefone>{{ rps.tomador.telefone }}</Telefone> | ||
<Email>{{ rps.tomador.email }}</Email> | ||
</Contato> | ||
</Tomador> | ||
{% if rps.intermediario is defined -%} | ||
<IntermediarioServico> | ||
<RazaoSocial>{{ rps.intermediario.razao_social }}</RazaoSocial> | ||
<CpfCnpj> | ||
<Cnpj>{{ rps.intermediario.cnpj }}</Cnpj> | ||
</CpfCnpj> | ||
<InscricaoMunicipal>{{ rps.intermediario.inscricao_municipal }}</InscricaoMunicipal> | ||
</IntermediarioServico> | ||
{% endif %} | ||
{% if rps.construcao_civil is defined -%} | ||
<ContrucaoCivil> | ||
<CodigoObra>{{ rps.construcao_civil.codigo_obra }}</CodigoObra> | ||
<Art>{{ rps.construcao_civil.art }}</Art> | ||
</ContrucaoCivil> | ||
{% endif %} | ||
</InfRps> | ||
</Rps> |