diff --git a/source/app/controllers/util.py b/source/app/controllers/util.py index eb6b9d0..8b40f46 100644 --- a/source/app/controllers/util.py +++ b/source/app/controllers/util.py @@ -11,6 +11,7 @@ import sqlite3 import socket import subprocess import tempfile +import threading import time import unicodedata import uuid @@ -1163,6 +1164,29 @@ def cancel_cfdi(uuid, pk12, rfc, auth): return data, result +#~ def run_in_thread(fn, *args, **kwargs): + #~ t = threading.Thread(target=fn, args=args, kwargs=kwargs) + #~ t.daemon = True + #~ t.start() + #~ return + + +def run_in_thread(fn): + def run(*k, **kw): + t = threading.Thread(target=fn, args=k, kwargs=kw) + t.start() + return t + return run + + +def get_bool(value): + if not value: + return False + if value == '1': + return True + return False + + class ImportFacturaLibre(object): def __init__(self, path): diff --git a/source/app/models/main.py b/source/app/models/main.py index 4e7f67d..5ebb61f 100644 --- a/source/app/models/main.py +++ b/source/app/models/main.py @@ -76,6 +76,15 @@ class Configuracion(BaseModel): @classmethod def get_(cls, keys): + if isinstance(keys, str): + data = (Configuracion + .select(Configuracion.valor) + .where(Configuracion.clave == keys) + ) + if data: + return data[0].valor + return '' + if keys['fields'] == 'correo': fields = ('correo_servidor', 'correo_puerto', 'correo_ssl', 'correo_usuario', 'correo_contra', 'correo_copia', @@ -268,7 +277,12 @@ class Emisor(BaseModel): def get_auth(cls): try: obj = Emisor.select()[0] - return {'USER': obj.correo_timbrado, 'PASS': obj.token_timbrado} + data = { + 'RFC': obj.rfc, + 'USER': obj.correo_timbrado, + 'PASS': obj.token_timbrado, + } + return data except: return {} @@ -1248,6 +1262,10 @@ class Facturas(BaseModel): return file_zip, name_zip + @util.run_in_thread + def _send(self, id, rfc): + return Facturas.send(id, rfc) + @classmethod def send(cls, id, rfc): values = Configuracion.get_({'fields': 'correo'}) @@ -1600,9 +1618,9 @@ class Facturas(BaseModel): obj.estatus = 'Generada' obj.save() + enviar_correo = util.get_bool(Configuracion.get_('correo_directo')) auth = Emisor.get_auth() - #~ error = False msg = 'Factura timbrada correctamente' result = util.timbra_xml(obj.xml, auth) if result['ok']: @@ -1611,15 +1629,17 @@ class Facturas(BaseModel): obj.fecha_timbrado = result['fecha'] obj.estatus = 'Timbrada' obj.error = '' - #~ obj.save() + obj.save() row = {'uuid': obj.uuid, 'estatus': 'Timbrada'} + if enviar_correo: + cls._send(cls, id, auth['RFC']) else: - #~ error = True msg = result['error'] obj.estatus = 'Error' obj.error = msg + obj.save() row = {'estatus': 'Error'} - obj.save() + return {'ok': result['ok'], 'msg': msg, 'row': row}