2026-03-17 11:37:57 +00:00
from app . config import load_config , logger
from . import sql_sentences as SQL
def sync_invoices_deleted_factuges ( conn_factuges , conn_mysql , last_execution_date ) :
config = load_config ( )
# LIMPIAMOS LAS FACTURAS DE FACTUGES QUE HAYAN SIDO ELIMINADAS DEL PROGRAMA NUEVO DE FACTURACION, PARA QUE PUEDAN SER MODIFICADAS
# Crear un cursor para ejecutar consultas SQL
cursor_mysql = None
try :
cursor_mysql = conn_mysql . cursor ( )
2026-07-15 17:21:14 +00:00
cursor_mysql . execute ( SQL . SELECT_PROFORMAS_DELETED )
2026-03-17 11:37:57 +00:00
filas = cursor_mysql . fetchall ( )
cursor_mysql . close ( )
# Crear un conjunto con los IDs [0] de los customer_inovices que debo liberar en FactuGES, porque han sido eliminadas en programa de facturación nuevo
ids_verifactu_deleted = { str ( fila [ 0 ] ) for fila in filas }
# logger.info(f"Customer invoices rows to be deleted: {len(ids_verifactu_deleted)}")
# Verificar si hay filas en el resultado
if ids_verifactu_deleted :
sync_delete_invoices ( conn_factuges , ids_verifactu_deleted , config )
else :
logger . info ( " There are NOT customer invoices deleted since the last run " )
except Exception as e :
if cursor_mysql is not None :
cursor_mysql . close ( )
logger . error (
f " (ERROR) Failed to fetch from database: { config [ ' FWEB_MYSQL_DATABASE ' ] } - using user: { config [ ' FWEB_MYSQL_USER ' ] } "
)
logger . error ( e )
raise e
def sync_delete_invoices ( conn_factuges , ids_verifactu_deleted , config ) :
# Eliminamos todos los IDs asociados en FactuGES que han sido eliminados así liberaremos la factura borrador y podermos modificarla de nuevo, para volverla a subir una vez hechos los cambios.
# VERIFACTU = 0 and ID_VERIFACTU = NULL
cursor_FactuGES = None
try :
cursor_FactuGES = conn_factuges . cursor ( )
if ids_verifactu_deleted :
logger . info ( f " Liberate factuGES: { ids_verifactu_deleted } " )
cursor_FactuGES . executemany (
SQL . LIMPIAR_FACTUGES_LINK ,
[ ( id_verifactu , ) for id_verifactu in ids_verifactu_deleted ] ,
)
else :
logger . info ( " No articles to delete. " )
except Exception as e :
# Escribir el error en el archivo de errores
logger . error ( str ( e ) )
raise e # Re-lanzar la excepción para detener el procesamiento
finally :
# Cerrar la conexión
if cursor_FactuGES is not None :
cursor_FactuGES . close ( )