diff --git a/CHANGELOG.md b/CHANGELOG.md index de3c704..2ec4ab6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +v 1.40.1 [10-Feb-2021] +---------------------- + - Fix #422 + + v 1.40.0 [05-ene-2021] ---------------------- - Error: Al parsear XML en Python 3.9+ diff --git a/VERSION b/VERSION index 32b7211..53a714f 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.40.0 +1.40.1 diff --git a/source/app/controllers/util.py b/source/app/controllers/util.py index fef112c..f25df79 100644 --- a/source/app/controllers/util.py +++ b/source/app/controllers/util.py @@ -430,9 +430,9 @@ def _get_uuid_fecha(xml): return node.attrib['UUID'], node.attrib['FechaTimbrado'] -def get_sat(xml): - from .pac import get_status_sat - return get_status_sat(xml) +# ~ def get_sat(xml): + # ~ from .pac import get_status_sat + # ~ return get_status_sat(xml) class LIBO(object): diff --git a/source/app/controllers/utils.py b/source/app/controllers/utils.py index 24291fe..d1c0ee2 100644 --- a/source/app/controllers/utils.py +++ b/source/app/controllers/utils.py @@ -32,6 +32,7 @@ import subprocess import threading import zipfile from pathlib import Path +from urllib import request from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase @@ -698,3 +699,63 @@ def cancel_xml_sign(invoice, auth, certificado): data = {'ok': True, 'msg': msg, 'row': {'estatus': 'Cancelada'}, 'date': result['date'], 'acuse': result['acuse']} return data + + +def _get_data_sat(xml): + BF = 'string(//*[local-name()="{}"]/@{})' + NS_CFDI = {'cfdi': 'http://www.sat.gob.mx/cfd/3'} + + try: + tree = etree.fromstring(xml.encode()) + emisor = escape( + tree.xpath('string(//cfdi:Emisor/@rfc)', namespaces=NS_CFDI) or + tree.xpath('string(//cfdi:Emisor/@Rfc)', namespaces=NS_CFDI) + ) + receptor = escape( + tree.xpath('string(//cfdi:Receptor/@rfc)', namespaces=NS_CFDI) or + tree.xpath('string(//cfdi:Receptor/@Rfc)', namespaces=NS_CFDI) + ) + total = tree.get('total') or tree.get('Total') + uuid = tree.xpath(BF.format('TimbreFiscalDigital', 'UUID')) + except Exception as e: + return '' + + data = f'?re={emisor}&rr={receptor}&tt={total}&id={uuid}' + return data + + +def get_status_sat(xml): + data = _get_data_sat(xml) + if not data: + return 'XML inválido' + + data = """ + + + + + + {} + + + + """.format(data) + headers = { + 'SOAPAction': '"http://tempuri.org/IConsultaCFDIService/Consulta"', + 'Content-type': 'text/xml; charset="UTF-8"' + } + URL = 'https://consultaqr.facturaelectronica.sat.gob.mx/consultacfdiservice.svc' + + try: + result = requests.post(URL, data=data, headers=headers) + tree = etree.fromstring(result.text) + node = tree.xpath("//*[local-name() = 'Estado']")[0] + except Exception as e: + return 'Error: {}'.format(str(e)) + + return node.text + + diff --git a/source/app/models/main.py b/source/app/models/main.py index e5c7f81..908b16d 100644 --- a/source/app/models/main.py +++ b/source/app/models/main.py @@ -5155,7 +5155,7 @@ class Facturas(BaseModel): @classmethod def get_status_sat(cls, id): obj = Facturas.get(Facturas.id == id) - estatus_sat = util.get_sat(obj.xml) + estatus_sat = utils.get_status_sat(obj.xml) if obj.estatus_sat != estatus_sat: obj.estatus_sat = estatus_sat obj.save() diff --git a/source/app/settings.py b/source/app/settings.py index 9b7af37..a23ca65 100644 --- a/source/app/settings.py +++ b/source/app/settings.py @@ -42,7 +42,7 @@ except ImportError: DEBUG = DEBUG -VERSION = '1.40.0' +VERSION = '1.40.1' EMAIL_SUPPORT = ('soporte@empresalibre.mx',) TITLE_APP = '{} v{}'.format(TITLE_APP, VERSION)