Uecko_ERP_FactuGES_sync/app/sync_factuges_main.py

93 lines
3.1 KiB
Python
Raw Normal View History

2025-08-28 08:51:05 +00:00
import sys
2025-11-30 18:28:44 +00:00
from app.config import get_package_version
2025-08-28 08:51:05 +00:00
from datetime import datetime
from dateutil import tz
2025-11-30 18:28:44 +00:00
from app.config import create_logger, load_config, log_config
2025-11-30 09:43:57 +00:00
from app.db import get_mysql_connection, get_factuges_connection, sync_invoices_factuges
2025-11-30 18:28:44 +00:00
from app.utils import obtener_fecha_ultima_ejecucion, actualizar_fecha_ultima_ejecucion
2025-08-28 08:51:05 +00:00
def main():
# Cargar la configuración
config = load_config()
2025-11-30 18:28:44 +00:00
version = get_package_version()
2025-08-28 08:51:05 +00:00
local_tz = tz.gettz(config['LOCAL_TZ'])
# Logging
2025-11-30 18:28:44 +00:00
logger = create_logger(
name="factuges-sync",
log_path="/app/logs/sync_factuges.log", # Solo lo genera en producción
)
logger.info("============================================================")
logger.info(" FACTUGES Sync FactuGES - START ")
logger.info(" Version: %s", version)
logger.info(" UTC Now: %s", datetime.utcnow().isoformat())
logger.info(" Environment: {config['ENV']}")
2025-08-28 08:51:05 +00:00
2025-11-30 18:28:44 +00:00
log_config(config)
2025-08-28 08:51:05 +00:00
conn_factuges = None
conn_mysql = None
try:
# Obtener la fecha de la última ejecución del programa
2025-11-27 19:08:06 +00:00
last_execution_date_utc = obtener_fecha_ultima_ejecucion(config['LAST_RUN_PATH'])
2025-08-28 08:51:05 +00:00
last_execution_date_local_tz = last_execution_date_utc.astimezone(
tz=local_tz).strftime("%Y-%m-%d %H:%M:%S")
2025-11-30 18:28:44 +00:00
logger.info("Last execution (UTC): %s",
last_execution_date_utc.strftime("%Y-%m-%d %H:%M:%S %Z"))
logger.info("Last execution (Local time): %s",
last_execution_date_local_tz)
2025-08-28 08:51:05 +00:00
2025-11-05 17:43:40 +00:00
# Abrimos conexiones con una única transacción para que todo esté controlado
2025-08-28 08:51:05 +00:00
conn_factuges = get_factuges_connection(config)
conn_mysql = get_mysql_connection(config)
2025-11-27 19:08:06 +00:00
# Sincronizamos
2025-11-30 18:28:44 +00:00
logger.info(
2025-11-27 19:08:06 +00:00
f">>>>>>>>>>> INI Sync invoices FactuGES escritorio to FactuGES web")
sync_invoices_factuges(conn_factuges, conn_mysql, last_execution_date_local_tz)
2025-08-28 08:51:05 +00:00
2025-10-29 16:08:14 +00:00
# Confirmar los cambios
conn_mysql.commit()
conn_factuges.commit()
conn_factuges.close()
conn_mysql.close()
2025-11-30 18:28:44 +00:00
logger.info(f">>>>>>>>>>> FIN Sync invoices FactuGES escritorio to FactuGES web")
2025-10-29 16:08:14 +00:00
2025-11-27 19:08:06 +00:00
actualizar_fecha_ultima_ejecucion(config['LAST_RUN_PATH'])
2025-08-28 08:51:05 +00:00
# Enviar email
2025-09-04 16:54:32 +00:00
# send_orders_mail(inserted_orders)
2025-08-28 08:51:05 +00:00
2025-11-30 18:28:44 +00:00
logger.info("== END (0) ==")
logger.info("============================================================")
2025-08-28 08:51:05 +00:00
sys.exit(0)
except Exception as e:
2025-11-30 18:28:44 +00:00
logger.error("Se ha producido un error en la última ejecución.")
logger.error(e)
logger.error("Traceback:", exc_info=True)
logger.info("== END (1) ==")
logger.info("============================================================")
2025-08-28 08:51:05 +00:00
if conn_mysql is not None:
conn_mysql.rollback()
if conn_factuges is not None:
conn_factuges.rollback()
sys.exit(1)
finally:
if conn_factuges:
conn_factuges.close()
if conn_mysql:
conn_mysql.close()
if __name__ == "__main__":
main()