|
|
|
@ -5,16 +5,27 @@ except: |
|
|
|
pass # modulo necessario apenas para NFS-e. |
|
|
|
|
|
|
|
|
|
|
|
class SerializacaoAutorizador(): |
|
|
|
pass |
|
|
|
class InterfaceAutorizador(): |
|
|
|
#TODO Colocar raise Exception Not Implemented nos metodos |
|
|
|
def consultar(self): |
|
|
|
pass |
|
|
|
|
|
|
|
def consultar_faixa(self): |
|
|
|
pass |
|
|
|
|
|
|
|
class SerializacaoBetha(SerializacaoAutorizador): |
|
|
|
def cancelar(self): |
|
|
|
pass |
|
|
|
|
|
|
|
def serializar_lote_sincrono(self): |
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
class SerializacaoBetha(InterfaceAutorizador): |
|
|
|
def __init__(self): |
|
|
|
if 'nfse_schema' not in globals(): |
|
|
|
raise ImportError('No module named nfse_v202 or PyXB') |
|
|
|
|
|
|
|
def serializar_gerar(self, nfse): |
|
|
|
def gerar(self, nfse): |
|
|
|
"""Retorna string de um XML gerado a partir do |
|
|
|
XML Schema (XSD). Binding gerado pelo modulo PyXB.""" |
|
|
|
|
|
|
|
@ -83,7 +94,78 @@ class SerializacaoBetha(SerializacaoAutorizador): |
|
|
|
|
|
|
|
return gnfse.toxml(element_name='GerarNfseEnvio') |
|
|
|
|
|
|
|
def _serializar_lote_sincrono(self, nfse): |
|
|
|
def consultar(self, nfse): |
|
|
|
"""Retorna string de um XML gerado a partir do |
|
|
|
XML Schema (XSD). Binding gerado pelo modulo PyXB.""" |
|
|
|
|
|
|
|
# Rps |
|
|
|
id_rps = nfse_schema.tcIdentificacaoRps() |
|
|
|
id_rps.Numero = nfse.identificador |
|
|
|
id_rps.Serie = nfse.serie |
|
|
|
id_rps.Tipo = nfse.tipo |
|
|
|
|
|
|
|
# Prestador |
|
|
|
id_prestador = nfse_schema.tcIdentificacaoPrestador() |
|
|
|
id_prestador.CpfCnpj = nfse.emitente.cnpj |
|
|
|
id_prestador.InscricaoMunicipal = nfse.emitente.inscricao_municipal |
|
|
|
|
|
|
|
consulta = nfse_schema.ConsultarNfseRpsEnvio() |
|
|
|
consulta.IdentificacaoRps = id_rps |
|
|
|
consulta.Prestador = id_prestador |
|
|
|
|
|
|
|
consulta = consulta.toxml(element_name='ConsultarNfseRpsEnvio').replace('ns1:','').replace(':ns1','').replace('<?xml version="1.0" ?>','') |
|
|
|
|
|
|
|
return consulta |
|
|
|
|
|
|
|
def consultar_faixa(self, emitente, inicio, fim, pagina): |
|
|
|
"""Retorna string de um XML gerado a partir do |
|
|
|
XML Schema (XSD). Binding gerado pelo modulo PyXB.""" |
|
|
|
|
|
|
|
# Prestador |
|
|
|
id_prestador = nfse_schema.tcIdentificacaoPrestador() |
|
|
|
id_prestador.CpfCnpj = emitente.cnpj |
|
|
|
id_prestador.InscricaoMunicipal = emitente.inscricao_municipal |
|
|
|
|
|
|
|
consulta = nfse_schema.ConsultarNfseFaixaEnvio() |
|
|
|
consulta.Prestador = id_prestador |
|
|
|
consulta.Pagina = pagina |
|
|
|
# É necessário BIND antes de atribuir numero final e numero inicial |
|
|
|
consulta.Faixa = BIND() |
|
|
|
consulta.Faixa.NumeroNfseInicial = inicio |
|
|
|
consulta.Faixa.NumeroNfseFinal = fim |
|
|
|
|
|
|
|
consulta = consulta.toxml(element_name='ConsultarNfseFaixaEnvio').replace('ns1:','').replace(':ns1','').replace('<?xml version="1.0" ?>','') |
|
|
|
|
|
|
|
return consulta |
|
|
|
|
|
|
|
def cancelar(self, nfse): |
|
|
|
"""Retorna string de um XML gerado a partir do |
|
|
|
XML Schema (XSD). Binding gerado pelo modulo PyXB.""" |
|
|
|
|
|
|
|
# id nfse |
|
|
|
id_nfse = nfse_schema.tcIdentificacaoNfse() |
|
|
|
id_nfse.Numero = nfse.identificador |
|
|
|
id_nfse.CpfCnpj = nfse.emitente.cnpj |
|
|
|
id_nfse.InscricaoMunicipal = nfse.emitente.inscricao_municipal |
|
|
|
id_nfse.CodigoMunicipio = nfse.emitente.endereco_cod_municipio |
|
|
|
|
|
|
|
# Info Pedido de cancelamento |
|
|
|
info_pedido = nfse_schema.tcInfPedidoCancelamento() |
|
|
|
info_pedido.Id = '1' |
|
|
|
info_pedido.IdentificacaoNfse = id_nfse |
|
|
|
#pedido.CodigoCancelamento = |
|
|
|
|
|
|
|
# Pedido |
|
|
|
pedido = nfse_schema.tcPedidoCancelamento() |
|
|
|
pedido.InfPedidoCancelamento = info_pedido |
|
|
|
|
|
|
|
# Cancelamento |
|
|
|
cancelar = nfse_schema.CancelarNfseEnvio() |
|
|
|
cancelar.Pedido = pedido |
|
|
|
|
|
|
|
return cancelar.toxml(element_name='CancelarNfseEnvio') |
|
|
|
|
|
|
|
def serializar_lote_sincrono(self, nfse): |
|
|
|
"""Retorna string de um XML gerado a partir do |
|
|
|
XML Schema (XSD). Binding gerado pelo modulo PyXB.""" |
|
|
|
|
|
|
|
@ -161,113 +243,3 @@ class SerializacaoBetha(SerializacaoAutorizador): |
|
|
|
gnfse.LoteRps = lote |
|
|
|
|
|
|
|
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 |