From da1b52f68071de33fafd55f1158d9ea68c3c4131 Mon Sep 17 00:00:00 2001 From: Danimar Ribeiro Date: Wed, 24 Jun 2015 14:51:14 -0300 Subject: [PATCH] =?UTF-8?q?Ajeitando=20m=C3=A9todos=20da=20nf-e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pytrustnfe/ChaveNFe.py | 29 ++++++++++++++++++++ pytrustnfe/servicos/Comunicacao.py | 9 +++++++ pytrustnfe/servicos/NFeAutorizacao.py | 42 +++++++++++++++++++++++------ pytrustnfe/servicos/NFeDistribuicaoDFe.py | 15 +++++------ pytrustnfe/servicos/NFeRetAutorizacao.py | 13 +++++---- pytrustnfe/servicos/NfeConsultaCadastro.py | 7 +---- pytrustnfe/servicos/NfeConsultaProtocolo.py | 15 +++++------ pytrustnfe/servicos/NfeInutilizacao.py | 13 +++++---- pytrustnfe/servicos/NfeStatusServico.py | 21 +++++++-------- pytrustnfe/servicos/RecepcaoEvento.py | 15 +++++------ pytrustnfe/servicos/Validacao.py | 5 ++++ pytrustnfe/utils.py | 26 +++++++++++++++++- 12 files changed, 146 insertions(+), 64 deletions(-) create mode 100644 pytrustnfe/ChaveNFe.py create mode 100644 pytrustnfe/servicos/Validacao.py diff --git a/pytrustnfe/ChaveNFe.py b/pytrustnfe/ChaveNFe.py new file mode 100644 index 0000000..2bdac6c --- /dev/null +++ b/pytrustnfe/ChaveNFe.py @@ -0,0 +1,29 @@ +# coding=utf-8 +''' +Created on 23/06/2015 + +@author: danimar +''' + + +class ChaveNFe(object): + + def __init__(self, valores): + self.cnpj = '' + self.estado = '' + self.emissao = '' + self.modelo = '' + self.serie = '' + self.numero = '' + self.tipo = '' + self.codigo = '' + + def validar(self): + assert self.cnpj != '', 'CNPJ necessário para criar chave NF-e' + assert self.estado != '', 'Estado necessário para criar chave NF-e' + assert self.emissao != '', 'Emissão necessário para criar chave NF-e' + assert self.modelo != '', 'Modelo necessário para criar chave NF-e' + assert self.serie != '', 'Série necessária para criar chave NF-e' + assert self.numero != '', 'Número necessário para criar chave NF-e' + assert self.tipo != '', 'Tipo necessário para criar chave NF-e' + assert self.codigo != '', 'Código necessário para criar chave NF-e' diff --git a/pytrustnfe/servicos/Comunicacao.py b/pytrustnfe/servicos/Comunicacao.py index 4db7fdb..2502bc4 100644 --- a/pytrustnfe/servicos/Comunicacao.py +++ b/pytrustnfe/servicos/Comunicacao.py @@ -69,6 +69,15 @@ class Comunicacao(object): assert self.metodo != '', "Método não configurado" assert self.tag_retorno != '', "Tag de retorno não configurado" + def _validar_xml(self, obj): + xml = None + if isinstance(obj, DynamicXml): + xml = obj.render() + if isinstance(obj, basestring): + xml = obj + assert xml is not None, "Objeto deve ser do tipo DynamicXml ou string" + return xml + def _executar_consulta(self, xmlEnviar): self._validar_dados() chave, certificado = self._preparar_temp_pem() diff --git a/pytrustnfe/servicos/NFeAutorizacao.py b/pytrustnfe/servicos/NFeAutorizacao.py index e6e2cde..25a25b4 100644 --- a/pytrustnfe/servicos/NFeAutorizacao.py +++ b/pytrustnfe/servicos/NFeAutorizacao.py @@ -6,17 +6,43 @@ Created on 21/06/2015 ''' from pytrustnfe.servicos.Comunicacao import Comunicacao from pytrustnfe.xml import DynamicXml +from pytrustnfe import utils class NfeAutorizacao(Comunicacao): - def autorizar_nfe(self, nfe, sincrono=True): - xml = None - if isinstance(nfe, DynamicXml): - xml = nfe.render() - if isinstance(nfe, basestring): - xml = nfe - assert xml is not None, "Objeto nfe deve ser do tipo DynamicXml ou string" + def __init__(self, certificado, senha): + Comunicacao.__init__(self, certificado, senha) + + def autorizar_nfe(self, nfe): + xml = self._validar_xml(nfe) + self.metodo = 'NFeAutorizacao' + self.tag_retorno = 'retEnviNFe' + self.web_service = 'ws/NfeAutorizacao/NFeAutorizacao.asmx' + self.url = 'nfe.sefazrs.rs.gov.br' + + return self._executar_consulta(xml) + + def autorizar_nfe_e_recibo(self, nfe): + xml = self._validar_xml(nfe) + + self.metodo = 'NFeAutorizacao' + self.tag_retorno = 'retEnviNFe' + self.web_service = 'ws/NfeAutorizacao/NFeAutorizacao.asmx' + self.url = 'nfe.sefazrs.rs.gov.br' + + xml_recibo, recibo = self._executar_consulta(xml) + + consulta_recibo = utils.gerar_consulta_recibo(recibo) + xml = self._validar_xml(nfe) + + self.metodo = 'NFeRetAutorizacao' + self.tag_retorno = 'retConsReciNFe' + self.web_service = 'ws/NfeRetAutorizacao/NFeRetAutorizacao.asmx' + self.url = 'nfe.sefazrs.rs.gov.br' + + return self._executar_consulta(xml) + - return self._executar_consulta(xml) \ No newline at end of file + \ No newline at end of file diff --git a/pytrustnfe/servicos/NFeDistribuicaoDFe.py b/pytrustnfe/servicos/NFeDistribuicaoDFe.py index f96dcdf..59a70cf 100644 --- a/pytrustnfe/servicos/NFeDistribuicaoDFe.py +++ b/pytrustnfe/servicos/NFeDistribuicaoDFe.py @@ -11,12 +11,11 @@ from pytrustnfe.xml import DynamicXml class NfeDistribuicaoDFe(Comunicacao): def distribuicao(self, dfe): - xml = None - if isinstance(dfe, DynamicXml): - xml = dfe.render() - if isinstance(dfe, basestring): - xml = dfe - assert xml is not None, "Objeto recibo deve ser do tipo DynamicXml ou string" - - + xml = self._validar_xml(recibo) + + self.metodo = 'NFeDistribuicaoDFe' + self.tag_retorno = 'retDistDFeInt' + self.web_service = 'NFeDistribuicaoDFe/NFeDistribuicaoDFe.asmx' + self.url = 'www1.nfe.fazenda.gov.br' + return self._executar_consulta(xml) \ No newline at end of file diff --git a/pytrustnfe/servicos/NFeRetAutorizacao.py b/pytrustnfe/servicos/NFeRetAutorizacao.py index 628a422..82cc39e 100644 --- a/pytrustnfe/servicos/NFeRetAutorizacao.py +++ b/pytrustnfe/servicos/NFeRetAutorizacao.py @@ -11,12 +11,11 @@ from pytrustnfe.xml import DynamicXml class NfeRetAutorizacao(Comunicacao): def consulta_autorizacao(self, recibo): - xml = None - if isinstance(recibo, DynamicXml): - xml = recibo.render() - if isinstance(recibo, basestring): - xml = recibo - assert xml is not None, "Objeto recibo deve ser do tipo DynamicXml ou string" - + xml = self._validar_xml(recibo) + + self.metodo = 'NFeRetAutorizacao' + self.tag_retorno = 'retConsReciNFe' + self.web_service = 'ws/NfeRetAutorizacao/NFeRetAutorizacao.asmx' + self.url = 'nfe.sefazrs.rs.gov.br' return self._executar_consulta(xml) \ No newline at end of file diff --git a/pytrustnfe/servicos/NfeConsultaCadastro.py b/pytrustnfe/servicos/NfeConsultaCadastro.py index 7b494c6..e1bd79f 100644 --- a/pytrustnfe/servicos/NfeConsultaCadastro.py +++ b/pytrustnfe/servicos/NfeConsultaCadastro.py @@ -17,12 +17,7 @@ class NfeConsultaCadastro(Comunicacao): def consultar_cadastro(self, cadastro, estado): - xml = None - if isinstance(cadastro, DynamicXml): - xml = cadastro.render() - if isinstance(cadastro, basestring): - xml = cadastro - assert xml is not None, "Objeto cadastro deve ser do tipo DynamicXml ou string" + xml = self._validar_xml(cadastro) self.web_service = '/ws/cadconsultacadastro/cadconsultacadastro2.asmx' self.url = 'cad.svrs.rs.gov.br' diff --git a/pytrustnfe/servicos/NfeConsultaProtocolo.py b/pytrustnfe/servicos/NfeConsultaProtocolo.py index 3a98c7f..7ae759c 100644 --- a/pytrustnfe/servicos/NfeConsultaProtocolo.py +++ b/pytrustnfe/servicos/NfeConsultaProtocolo.py @@ -11,12 +11,11 @@ from pytrustnfe.xml import DynamicXml class NfeConsultaProtocolo(Comunicacao): def consultar_protocolo(self, recibo): - xml = None - if isinstance(recibo, DynamicXml): - xml = recibo.render() - if isinstance(recibo, basestring): - xml = recibo - assert xml is not None, "Objeto recibo deve ser do tipo DynamicXml ou string" - - + xml = self._validar_xml(recibo) + + self.metodo = 'NfeConsulta2' + self.tag_retorno = 'retConsSitNFe' + self.web_service = 'ws/NfeConsulta/NfeConsulta2.asmx' + self.url = 'nfe.sefazrs.rs.gov.br' + return self._executar_consulta(xml) \ No newline at end of file diff --git a/pytrustnfe/servicos/NfeInutilizacao.py b/pytrustnfe/servicos/NfeInutilizacao.py index 3eab33c..558ecc8 100644 --- a/pytrustnfe/servicos/NfeInutilizacao.py +++ b/pytrustnfe/servicos/NfeInutilizacao.py @@ -11,12 +11,11 @@ from pytrustnfe.xml import DynamicXml class NfeInutilizacao(Comunicacao): def inutilizar(self, inutilizacao): - xml = None - if isinstance(inutilizacao, DynamicXml): - xml = inutilizacao.render() - if isinstance(inutilizacao, basestring): - xml = inutilizacao - assert xml is not None, "Objeto inutilização deve ser do tipo DynamicXml ou string" - + xml = self._validar_xml(recibo) + + self.metodo = 'nfeinutilizacao2' + self.tag_retorno = 'retInutNFe' + self.web_service = 'ws/nfeinutilizacao/nfeinutilizacao2.asmx' + self.url = 'nfe.sefazrs.rs.gov.br' return self._executar_consulta(xml) \ No newline at end of file diff --git a/pytrustnfe/servicos/NfeStatusServico.py b/pytrustnfe/servicos/NfeStatusServico.py index afe9d83..fd03ec4 100644 --- a/pytrustnfe/servicos/NfeStatusServico.py +++ b/pytrustnfe/servicos/NfeStatusServico.py @@ -1,4 +1,4 @@ -#coding=utf-8 +# coding=utf-8 ''' Created on 21/06/2015 @@ -9,14 +9,13 @@ from pytrustnfe.xml import DynamicXml class NfeStatusServico(Comunicacao): - + def status(self, consulta): - xml = None - if isinstance(consulta, DynamicXml): - xml = consulta.render() - if isinstance(consulta, basestring): - xml = consulta - assert xml is not None, "Objeto consulta deve ser do tipo DynamicXml ou string" - - - return self._executar_consulta(xml) \ No newline at end of file + xml = self._validar_xml(recibo) + + self.metodo = 'NfeStatusServico2' + self.tag_retorno = 'retConsStatServ' + self.web_service = 'ws/NfeStatusServico/NfeStatusServico2.asmx' + self.url = 'nfe.sefazrs.rs.gov.br' + + return self._executar_consulta(xml) diff --git a/pytrustnfe/servicos/RecepcaoEvento.py b/pytrustnfe/servicos/RecepcaoEvento.py index 870aa79..487c73c 100644 --- a/pytrustnfe/servicos/RecepcaoEvento.py +++ b/pytrustnfe/servicos/RecepcaoEvento.py @@ -11,12 +11,11 @@ from pytrustnfe.xml import DynamicXml class RecepcaoEvento(Comunicacao): def registrar_evento(self, evento): - xml = None - if isinstance(evento, DynamicXml): - xml = evento.render() - if isinstance(evento, basestring): - xml = evento - assert xml is not None, "Objeto recibo deve ser do tipo DynamicXml ou string" - - + xml = self._validar_xml(recibo) + + self.metodo = 'RecepcaoEvento' + self.tag_retorno = 'retEnvEvento' + self.web_service = 'ws/recepcaoevento/recepcaoevento.asmx' + self.url = 'nfe.sefazrs.rs.gov.br' + return self._executar_consulta(xml) \ No newline at end of file diff --git a/pytrustnfe/servicos/Validacao.py b/pytrustnfe/servicos/Validacao.py new file mode 100644 index 0000000..0f43e0d --- /dev/null +++ b/pytrustnfe/servicos/Validacao.py @@ -0,0 +1,5 @@ +''' +Created on 24/06/2015 + +@author: danimar +''' diff --git a/pytrustnfe/utils.py b/pytrustnfe/utils.py index d9f23c0..ffe9aea 100644 --- a/pytrustnfe/utils.py +++ b/pytrustnfe/utils.py @@ -5,11 +5,35 @@ Created on 22/06/2015 @author: danimar ''' from datetime import date, datetime +from pytrustnfe.ChaveNFe import ChaveNFe + def date_tostring(data): assert isinstance(data, date), "Objeto date requerido" return data.strftime("%d-%m-%y") + def datetime_tostring(data): assert isinstance(data, datetime), "Objeto datetime requerido" - return data.strftime("%d-%m-%y %H:%M:%S") \ No newline at end of file + return data.strftime("%d-%m-%y %H:%M:%S") + + +def gerar_consulta_recibo(recibo): + c = DynamicXml('consReciNFe') + c(xmlns="http://www.portalfiscal.inf.br/nfe", versao="2.00") + c.tpAmb = recibo.tpAmb + c.nRec = recibo.infRec.nRec + return c + + +def gerar_chave(obj_chave): + assert isinstance(obj_chave, ChaveNFe), "Objeto deve ser do tipo ChaveNFe" + obj_chave.validar() + + return "%s%s%s%s%s%s%s%s" % (obj_chave.estado, obj_chave.emissao, + obj_chave.cnpj, obj_chave.modelo, + obj_chave.serie, obj_chave.numero, + obj_chave.tipo, obj_chave.codigo) + +def descompacta_nfe_distribuicao(xml): + pass