diff --git a/pynfe/processamento/comunicacao.py b/pynfe/processamento/comunicacao.py index 43a5e70..cb40536 100644 --- a/pynfe/processamento/comunicacao.py +++ b/pynfe/processamento/comunicacao.py @@ -17,6 +17,7 @@ from pynfe.utils.flags import ( from pynfe.utils.webservices import NFE, NFCE, NFSE from pynfe.entidades.certificado import CertificadoA1 from .assinatura import AssinaturaA1 +from pynfe.utils.descompactar import DescompactaGzip class Comunicacao(object): @@ -167,7 +168,7 @@ class ComunicacaoSefaz(Comunicacao): # url url = self._get_url_an(consulta='DISTRIBUICAO') # Monta XML para envio da requisição - raiz = etree.Element('distDFeInt', versao='1.00', xmlns=NAMESPACE_NFE) + raiz = etree.Element('distDFeInt', versao='1.01', xmlns=NAMESPACE_NFE) etree.SubElement(raiz, 'tpAmb').text = str(self._ambiente) if self.uf: etree.SubElement(raiz, 'cUFAutor').text = CODIGOS_ESTADOS[self.uf.upper()] @@ -175,15 +176,16 @@ class ComunicacaoSefaz(Comunicacao): etree.SubElement(raiz, 'CNPJ').text = cnpj else: etree.SubElement(raiz, 'CPF').text = cpf - distNSU = etree.SubElement(raiz, 'distNSU') - etree.SubElement(distNSU, 'ultNSU').text = str(nsu).zfill(15) - # if chave: - # consChNFe = etree.SubElement(raiz, 'consChNFe') - # etree.SubElement(consChNFe, 'chNFe').text = chave - # Monta XML para envio da requisição + if not chave: + distNSU = etree.SubElement(raiz, 'distNSU') + etree.SubElement(distNSU, 'ultNSU').text = str(nsu).zfill(15) + if chave: + consChNFe = etree.SubElement(raiz, 'consChNFe') + etree.SubElement(consChNFe, 'chNFe').text = chave + #Monta XML para envio da requisição xml = self._construir_xml_soap('NFeDistribuicaoDFe', raiz) - # print(url) - # print(etree.tostring(xml)) + + return self._post(url, xml) def consulta_cadastro(self, modelo, cnpj): diff --git a/pynfe/utils/descompactar.py b/pynfe/utils/descompactar.py new file mode 100644 index 0000000..ab4e1ea --- /dev/null +++ b/pynfe/utils/descompactar.py @@ -0,0 +1,30 @@ + +""" + @author: Lucas Resende + + classe que descompacta o gzip recebido pela consulta distribuicao + +""" + +from io import BytesIO +import base64 +import gzip +from lxml import etree + +class DescompactaGzip(object): + @staticmethod + def descompacta(stringZipada): + """ + :paramn stringZipada: String + + :return : Etree + """ + arq = BytesIO() + arq.write(base64.b64decode(stringZipada)) + arq.seek(0) + zip = gzip.GzipFile(fileobj=arq) + texto = zip.read() + arq.close() + zip.close() + descompactado = texto.decode('utf-8') + return etree.fromstring(descompactado)