Browse Source

Implementado GerarNfse com wsdl

pull/7/head
Junior Tada 10 years ago
parent
commit
a2ddeaef97
  1. 19
      pynfe/processamento/assinatura.py
  2. 47
      pynfe/processamento/comunicacao.py
  3. 229
      pynfe/processamento/serializacao.py

19
pynfe/processamento/assinatura.py

@ -64,7 +64,7 @@ class AssinaturaA1(Assinatura):
except Exception as e:
raise e
def assinarNfse(self, xml, xpath='/GerarNfseEnvio/ns1:Rps/ns1:InfDeclaracaoPrestacaoServico', retorna_string=False):
def assinarNfse(self, xml, xpath='/GerarNfseEnvio/ns1:Rps', retorna_string=False):
try:
xml = etree.fromstring(xml)
# No raiz do XML de saida
@ -76,7 +76,7 @@ class AssinaturaA1(Assinatura):
# Tenta achar a tag infNFe
# TODO a proxima linha nao eh encontrada pq precisa colocar o namespace, GerarNfseEnvio.
ref = etree.SubElement(siginfo, 'Reference', URI='#' +
xml.xpath(xpath, namespaces={'ns1': 'http://www.betha.com.br/e-nota-contribuinte-ws'})[0].attrib['Id'])
xml.xpath(xpath + '/ns1:InfDeclaracaoPrestacaoServico', namespaces={'ns1': 'http://www.betha.com.br/e-nota-contribuinte-ws'})[0].attrib['Id'])
trans = etree.SubElement(ref, 'Transforms')
etree.SubElement(trans, 'Transform', Algorithm='http://www.w3.org/2000/09/xmldsig#enveloped-signature')
@ -87,7 +87,7 @@ class AssinaturaA1(Assinatura):
keyinfo = etree.SubElement(raiz, 'KeyInfo')
etree.SubElement(keyinfo, 'X509Data')
rps = xml.xpath('ns1:Rps', namespaces={'ns1': 'http://www.betha.com.br/e-nota-contribuinte-ws'})[0]
rps = xml.xpath(xpath, namespaces={'ns1': 'http://www.betha.com.br/e-nota-contribuinte-ws'})[0]
rps.append(raiz)
# Escreve no arquivo depois de remover caracteres especiais e parse string
@ -107,7 +107,7 @@ class AssinaturaA1(Assinatura):
def assinarLoteNfse(self, lote, retorna_string=False):
try:
# Assina a nota
lote = self.assinarNfse(lote, xpath='', retorna_string=True)
lote = self.assinarNfse(xml=lote, xpath='/EnviarLoteRpsSincronoEnvio/ns1:LoteRps/ns1:ListaRps/ns1:Rps', retorna_string=True)
# Assina o lote
lote = etree.fromstring(lote)
# No raiz do XML de saida
@ -117,10 +117,7 @@ class AssinaturaA1(Assinatura):
etree.SubElement(siginfo, 'CanonicalizationMethod', Algorithm='http://www.w3.org/TR/2001/REC-xml-c14n-20010315')
etree.SubElement(siginfo, 'SignatureMethod', Algorithm='http://www.w3.org/2000/09/xmldsig#rsa-sha1')
# Tenta achar a tag LoteRps
ref = etree.SubElement(siginfo, 'Reference', URI='#' +
lote.xpath('/EnviarLoteRpsSincronoEnvio/ns1:LoteRps',
namespaces={'ns1': 'http://www.betha.com.br/e-nota-contribuinte-ws'})[0].attrib['Id'])
ref = etree.SubElement(siginfo, 'Reference', URI='#' + lote[0].attrib['Id'])
trans = etree.SubElement(ref, 'Transforms')
etree.SubElement(trans, 'Transform', Algorithm='http://www.w3.org/2000/09/xmldsig#enveloped-signature')
etree.SubElement(trans, 'Transform', Algorithm='http://www.w3.org/TR/2001/REC-xml-c14n-20010315')
@ -136,11 +133,11 @@ class AssinaturaA1(Assinatura):
with open('nfse.xml', 'w') as arquivo:
arquivo.write(remover_acentos(etree.tostring(lote, encoding="unicode", pretty_print=False).replace('ns1:', '').replace(':ns1', '')))
subprocess.call(['xmlsec1', '--sign', '--pkcs12', self.certificado, '--pwd', self.senha, '--crypto', 'openssl', '--output', 'funfa.xml', '--id-attr:Id', tag, 'nfse.xml'])
xml = etree.parse('funfa.xml').getroot()
subprocess.call(['xmlsec1', '--sign', '--pkcs12', self.certificado, '--pwd', self.senha, '--crypto', 'openssl', '--output', 'lote.xml', '--id-attr:Id', tag, 'nfse.xml'])
xml = etree.parse('lote.xml').getroot()
if retorna_string:
return etree.tostring(lote, encoding="unicode", pretty_print=False)
return etree.tostring(xml, encoding="unicode", pretty_print=False)
else:
return xml
except Exception as e:

47
pynfe/processamento/comunicacao.py

@ -369,11 +369,10 @@ class ComunicacaoNfse(Comunicacao):
self._versao = '2.02'
# url do serviço
url = self._get_url(autorizador)
# dados
raiz = etree.Element('nfseDadosMsg')
raiz.append(nota)
return self._post2(url, raiz)
# xml
xml = etree.tostring(nota, encoding='unicode', pretty_print=False)
# comunica via wsdl
return self._post2(url, xml, 'gerar')
def consulta_nota(self, autorizador, nota):
if autorizador.upper() == 'BETHA':
@ -396,14 +395,19 @@ class ComunicacaoNfse(Comunicacao):
def cancelar(self, autorizador):
pass
def _cabecalho(self, retorna_string=False):
u"""Monta o XML do cabeçalho da requisição SOAP"""
def _cabecalho(self, retorna_string=True):
u"""Monta o XML do cabeçalho da requisição wsdl"""
xml_declaration='<?xml version="1.0" encoding="UTF-8"?>'
# cabecalho
raiz = etree.Element('cabecalho', xmlns='http://www.betha.com.br/e-nota-contribuinte-ws', versao='2.02')
etree.SubElement(raiz, 'versaoDados').text = '2.02'
raiz = etree.Element('nfseCabecMsg')
cabecalho = etree.SubElement(raiz, 'cabecalho', xmlns=self._namespace, versao=self._versao)
etree.SubElement(cabecalho, 'versaoDados').text = self._versao
if retorna_string:
return etree.tostring(raiz, encoding="unicode", pretty_print=False)
cabecalho = etree.tostring(raiz, encoding='unicode', pretty_print=False).replace('\n','')
cabecalho = xml_declaration + cabecalho
return cabecalho
else:
return raiz
@ -461,16 +465,19 @@ class ComunicacaoNfse(Comunicacao):
finally:
certificadoA1.excluir()
def _post2(self, url, xml):
# declaraçao xml
xml_declaration='<?xml version="1.0" encoding="utf-8"?>'
def _post2(self, url, xml, metodo):
# cabecalho
cabecalho = self._cabecalho(retorna_string=True)
cabecalho = self._cabecalho()
# comunicacao wsdl
try:
from suds.client import Client
cliente = Client(url)
import ipdb
ipdb.set_trace()
# gerar nfse
if metodo == 'gerar':
return cliente.service.GerarNfse(cabecalho, xml)
elif metodo == 'cancelar':
pass
else:
pass
except Exception as e:
raise e

229
pynfe/processamento/serializacao.py

@ -606,7 +606,7 @@ class SerializacaoNfse(Serializacao):
def importar(self):
pass
def _serializarGerar(self, nfse):
def gerar(self, nfse):
"""Retorna string de um XML gerado a partir do
XML Schema (XSD). Binding gerado pelo modulo PyXB."""
@ -675,6 +675,13 @@ class SerializacaoNfse(Serializacao):
return gnfse.toxml(element_name='GerarNfseEnvio')
def consultar(self, numero):
pass
def cancelar(self, dados):
pass
def _serializar_lote_sincrono(self, nfse):
"""Retorna string de um XML gerado a partir do
XML Schema (XSD). Binding gerado pelo modulo PyXB."""
@ -754,115 +761,117 @@ class SerializacaoNfse(Serializacao):
return gnfse.toxml(element_name='EnviarLoteRpsSincronoEnvio')
def _serializar_emitente(self, emitente, tag_raiz='Prestador', retorna_string=False):
raiz = etree.Element(tag_raiz)
documento = etree.SubElement(raiz, 'CpfCnpj')
etree.SubElement(documento, 'Cnpj').text = emitente.cnpj
etree.SubElement(raiz, 'InscricaoMunicipal').text = emitente.inscricao_municipal
if retorna_string:
return etree.tostring(raiz, encoding="unicode", pretty_print=True)
else:
return raiz
def _serializar_cliente(self, cliente, tag_raiz='Tomador', retorna_string=False):
raiz = etree.Element(tag_raiz)
identificacao = etree.SubElement(raiz, 'IdentificacaoTomador')
documento = etree.SubElement(identificacao, 'CpfCnpj')
etree.SubElement(documento, cliente.tipo_documento).text = cliente.numero_documento # Apenas Cnpj ??
etree.SubElement(identificacao, 'InscricaoMunicipal').text = cliente.inscricao_municipal # obrigatório??
etree.SubElement(raiz, 'RazaoSocial').text = cliente.razao_social
endereco = etree.SubElement(raiz, 'Endereco')
etree.SubElement(endereco, 'Endereco').text = cliente.endereco_logradouro
etree.SubElement(endereco, 'Numero').text = cliente.endereco_numero
if cliente.endereco_complemento:
etree.SubElement(endereco, 'Complemento').text = cliente.endereco_complemento
etree.SubElement(endereco, 'Bairro').text = cliente.endereco_bairro
etree.SubElement(endereco, 'CodigoMunicipio').text = obter_codigo_por_municipio(
cliente.endereco_municipio, cliente.endereco_uf)
etree.SubElement(endereco, 'Uf').text = cliente.endereco_uf
etree.SubElement(endereco, 'CodigoPais').text = cliente.endereco_pais
etree.SubElement(endereco, 'Cep').text = so_numeros(cliente.endereco_cep)
contato = etree.SubElement(raiz, 'Contato')
etree.SubElement(contato, 'Telefone').text = cliente.endereco_telefone
etree.SubElement(contato, 'Email').text = cliente.email
if retorna_string:
return etree.tostring(raiz, encoding="unicode", pretty_print=True)
else:
return raiz
def _serializar_servico(self, servico, tag_raiz='Servico', retorna_string=False):
raiz = etree.Element(tag_raiz)
valores = etree.SubElement(raiz, 'Valores')
etree.SubElement(valores, 'ValorServicos').text = str('{:.2f}').format(servico.valor_servico)
etree.SubElement(raiz, 'IssRetido').text = str(servico.iss_retido)
#etree.SubElement(raiz, 'ResponsavelRetencao').text = ''
etree.SubElement(raiz, 'ItemListaServico').text = servico.item_lista
#etree.SubElement(raiz, 'CodigoCnae').text = ''
#etree.SubElement(raiz, 'CodigoTributacaoMunicipio').text = ''
etree.SubElement(raiz, 'Discriminacao').text = servico.discriminacao
etree.SubElement(raiz, 'CodigoMunicipio').text = servico.codigo_municipio
#etree.SubElement(raiz, 'CodigoPais').text = ''
"""
1 Exigível;
2 Não incidência;
3 Isenção;
4 Exportação;
5 Imunidade;
6 Exigibilidade Suspensa por Decisão Judicial;
7 Exigibilidade Suspensa por ProcessoAdministrativo
"""
etree.SubElement(raiz, 'ExigibilidadeISS').text = str(servico.exigibilidade)
etree.SubElement(raiz, 'MunicipioIncidencia').text = servico.codigo_municipio
#etree.SubElement(raiz, 'NumeroProcesso').text = ''
if retorna_string:
return etree.tostring(raiz, encoding="unicode", pretty_print=True)
else:
return raiz
def _serializar_gerar(self, nfse, tag_raiz='GerarNfseEnvio', retorna_string=False):
if nfse.autorizador.upper() == 'BETHA':
raiz = etree.Element(tag_raiz, xmlns=NAMESPACE_BETHA)
# TODO - implementar outros sistemas autorizadores
else:
raiz = etree.Element(tag_raiz)
rps = etree.SubElement(raiz, 'Rps')
info = etree.SubElement(rps, 'InfDeclaracaoPrestacaoServico', Id=nfse.identificador)
etree.SubElement(info, 'Competencia').text = nfse.data_emissao.strftime('%Y-%m-%d')
# Servico
info.append(self._serializar_servico(nfse.servico))
# Emitente/Prestador
info.append(self._serializar_emitente(nfse.emitente))
# Cliente/Tomador
info.append(self._serializar_cliente(nfse.cliente))
etree.SubElement(info, 'OptanteSimplesNacional').text = str(nfse.simples) # 1-Sim; 2-Não
etree.SubElement(info, 'IncentivoFiscal').text = str(nfse.incentivo) # 1-Sim; 2-Não
if retorna_string:
return etree.tostring(raiz, encoding="unicode", pretty_print=True)
else:
return raiz
def _serializar_consulta(self, nfse, tag_raiz='ConsultarNfseRpsEnvio', retorna_string=False):
if nfse.autorizador.upper() == 'BETHA':
namespace = NAMESPACE_BETHA
#versao = '2.02'
raiz = etree.Element(tag_raiz, xmlns=namespace)
identificacao = etree.SubElement(raiz, 'IdentificacaoRps')
etree.SubElement(identificacao, 'Numero').text = str(nfse.identificador)
etree.SubElement(identificacao, 'Serie').text = nfse.serie
etree.SubElement(identificacao, 'Tipo').text = nfse.tipo
raiz.append(self._serializar_emitente(nfse.emitente))
if retorna_string:
return etree.tostring(raiz, encoding="unicode", pretty_print=True)
else:
return raiz
#### Forma antiga ######
# def _serializar_emitente(self, emitente, tag_raiz='Prestador', retorna_string=False):
# raiz = etree.Element(tag_raiz)
# documento = etree.SubElement(raiz, 'CpfCnpj')
# etree.SubElement(documento, 'Cnpj').text = emitente.cnpj
# etree.SubElement(raiz, 'InscricaoMunicipal').text = emitente.inscricao_municipal
# if retorna_string:
# return etree.tostring(raiz, encoding="unicode", pretty_print=True)
# else:
# return raiz
# def _serializar_cliente(self, cliente, tag_raiz='Tomador', retorna_string=False):
# raiz = etree.Element(tag_raiz)
# identificacao = etree.SubElement(raiz, 'IdentificacaoTomador')
# documento = etree.SubElement(identificacao, 'CpfCnpj')
# etree.SubElement(documento, cliente.tipo_documento).text = cliente.numero_documento # Apenas Cnpj ??
# etree.SubElement(identificacao, 'InscricaoMunicipal').text = cliente.inscricao_municipal # obrigatório??
# etree.SubElement(raiz, 'RazaoSocial').text = cliente.razao_social
# endereco = etree.SubElement(raiz, 'Endereco')
# etree.SubElement(endereco, 'Endereco').text = cliente.endereco_logradouro
# etree.SubElement(endereco, 'Numero').text = cliente.endereco_numero
# if cliente.endereco_complemento:
# etree.SubElement(endereco, 'Complemento').text = cliente.endereco_complemento
# etree.SubElement(endereco, 'Bairro').text = cliente.endereco_bairro
# etree.SubElement(endereco, 'CodigoMunicipio').text = obter_codigo_por_municipio(
# cliente.endereco_municipio, cliente.endereco_uf)
# etree.SubElement(endereco, 'Uf').text = cliente.endereco_uf
# etree.SubElement(endereco, 'CodigoPais').text = cliente.endereco_pais
# etree.SubElement(endereco, 'Cep').text = so_numeros(cliente.endereco_cep)
# contato = etree.SubElement(raiz, 'Contato')
# etree.SubElement(contato, 'Telefone').text = cliente.endereco_telefone
# etree.SubElement(contato, 'Email').text = cliente.email
# if retorna_string:
# return etree.tostring(raiz, encoding="unicode", pretty_print=True)
# else:
# return raiz
# def _serializar_servico(self, servico, tag_raiz='Servico', retorna_string=False):
# raiz = etree.Element(tag_raiz)
# valores = etree.SubElement(raiz, 'Valores')
# etree.SubElement(valores, 'ValorServicos').text = str('{:.2f}').format(servico.valor_servico)
# etree.SubElement(raiz, 'IssRetido').text = str(servico.iss_retido)
# #etree.SubElement(raiz, 'ResponsavelRetencao').text = ''
# etree.SubElement(raiz, 'ItemListaServico').text = servico.item_lista
# #etree.SubElement(raiz, 'CodigoCnae').text = ''
# #etree.SubElement(raiz, 'CodigoTributacaoMunicipio').text = ''
# etree.SubElement(raiz, 'Discriminacao').text = servico.discriminacao
# etree.SubElement(raiz, 'CodigoMunicipio').text = servico.codigo_municipio
# #etree.SubElement(raiz, 'CodigoPais').text = ''
# """
# 1 – Exigível;
# 2 – Não incidência;
# 3 – Isenção;
# 4 – Exportação;
# 5 – Imunidade;
# 6 – Exigibilidade Suspensa por Decisão Judicial;
# 7 – Exigibilidade Suspensa por ProcessoAdministrativo
# """
# etree.SubElement(raiz, 'ExigibilidadeISS').text = str(servico.exigibilidade)
# etree.SubElement(raiz, 'MunicipioIncidencia').text = servico.codigo_municipio
# #etree.SubElement(raiz, 'NumeroProcesso').text = ''
# if retorna_string:
# return etree.tostring(raiz, encoding="unicode", pretty_print=True)
# else:
# return raiz
# def _serializar_gerar(self, nfse, tag_raiz='GerarNfseEnvio', retorna_string=False):
# if nfse.autorizador.upper() == 'BETHA':
# raiz = etree.Element(tag_raiz, xmlns=NAMESPACE_BETHA)
# # TODO - implementar outros sistemas autorizadores
# else:
# raiz = etree.Element(tag_raiz)
# rps = etree.SubElement(raiz, 'Rps')
# info = etree.SubElement(rps, 'InfDeclaracaoPrestacaoServico', Id=nfse.identificador)
# etree.SubElement(info, 'Competencia').text = nfse.data_emissao.strftime('%Y-%m-%d')
# # Servico
# info.append(self._serializar_servico(nfse.servico))
# # Emitente/Prestador
# info.append(self._serializar_emitente(nfse.emitente))
# # Cliente/Tomador
# info.append(self._serializar_cliente(nfse.cliente))
# etree.SubElement(info, 'OptanteSimplesNacional').text = str(nfse.simples) # 1-Sim; 2-Não
# etree.SubElement(info, 'IncentivoFiscal').text = str(nfse.incentivo) # 1-Sim; 2-Não
# if retorna_string:
# return etree.tostring(raiz, encoding="unicode", pretty_print=True)
# else:
# return raiz
# def _serializar_consulta(self, nfse, tag_raiz='ConsultarNfseRpsEnvio', retorna_string=False):
# if nfse.autorizador.upper() == 'BETHA':
# namespace = NAMESPACE_BETHA
# #versao = '2.02'
# raiz = etree.Element(tag_raiz, xmlns=namespace)
# identificacao = etree.SubElement(raiz, 'IdentificacaoRps')
# etree.SubElement(identificacao, 'Numero').text = str(nfse.identificador)
# etree.SubElement(identificacao, 'Serie').text = nfse.serie
# etree.SubElement(identificacao, 'Tipo').text = nfse.tipo
# raiz.append(self._serializar_emitente(nfse.emitente))
# if retorna_string:
# return etree.tostring(raiz, encoding="unicode", pretty_print=True)
# else:
# return raiz
class SerializacaoPipes(Serializacao):

Loading…
Cancel
Save