2025-08-28 08:51:05 +00:00
|
|
|
import sys
|
|
|
|
|
import logging
|
|
|
|
|
|
2025-11-30 09:43:57 +00:00
|
|
|
from app.__version_sync_factuges__ import __version__
|
2025-08-28 08:51:05 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
from dateutil import tz
|
|
|
|
|
from config import setup_logging, load_config
|
2025-11-27 19:08:06 +00:00
|
|
|
from db import get_mysql_connection, get_factuges_connection, sync_invoices_verifactu
|
|
|
|
|
from utils import obtener_fecha_ultima_ejecucion, actualizar_fecha_ultima_ejecucion, log_system_metrics, send_orders_mail
|
2025-08-28 08:51:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
|
|
# Cargar la configuración
|
|
|
|
|
config = load_config()
|
|
|
|
|
|
|
|
|
|
local_tz = tz.gettz(config['LOCAL_TZ'])
|
|
|
|
|
|
|
|
|
|
# Logging
|
|
|
|
|
setup_logging()
|
|
|
|
|
|
2025-11-27 19:08:06 +00:00
|
|
|
logging.info("== START SYNC VERIFACTU ==")
|
|
|
|
|
logging.info(f"Version: {__version__}")
|
2025-08-28 08:51:05 +00:00
|
|
|
logging.info(f"Environment: {config['ENVIRONMENT']}")
|
|
|
|
|
log_system_metrics()
|
|
|
|
|
|
|
|
|
|
conn_factuges = None
|
|
|
|
|
conn_mysql = None
|
|
|
|
|
try:
|
|
|
|
|
# Obtener la fecha de la última ejecución del programa
|
|
|
|
|
last_execution_date_utc = obtener_fecha_ultima_ejecucion()
|
|
|
|
|
last_execution_date_local_tz = last_execution_date_utc.astimezone(
|
|
|
|
|
tz=local_tz).strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
|
|
|
|
|
|
logging.info("Last execution (UTC): %s",
|
|
|
|
|
last_execution_date_utc.strftime("%Y-%m-%d %H:%M:%S %Z"))
|
|
|
|
|
logging.info("Last execution (Local time): %s",
|
|
|
|
|
last_execution_date_local_tz)
|
|
|
|
|
|
2025-11-27 19:08:06 +00:00
|
|
|
# Abrimos conexión con una única transacción para que todo esté controlado
|
2025-10-29 16:08:14 +00:00
|
|
|
conn_mysql = get_mysql_connection(config)
|
|
|
|
|
|
2025-10-03 18:22:15 +00:00
|
|
|
# Sync Verifactu
|
2025-11-06 19:18:37 +00:00
|
|
|
logging.info(
|
2025-11-27 19:08:06 +00:00
|
|
|
f">>>>>>>>>> INI Sync facturas emitidas to Verifactu")
|
2025-11-20 18:51:03 +00:00
|
|
|
sync_invoices_verifactu(conn_mysql, last_execution_date_local_tz)
|
2025-10-29 16:08:14 +00:00
|
|
|
conn_mysql.commit()
|
|
|
|
|
conn_mysql.close()
|
2025-11-27 19:08:06 +00:00
|
|
|
logging.info(f">>>>>>>>>> FIN Sync facturas emitidas to Verifactu")
|
2025-10-03 18:22:15 +00:00
|
|
|
|
2025-11-27 19:08:06 +00:00
|
|
|
actualizar_fecha_ultima_ejecucion()
|
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
|
|
|
|
|
|
|
|
logging.info("== END (0) ==")
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logging.error("Se ha producido un error en la última ejecución.")
|
|
|
|
|
logging.error(e)
|
2025-09-04 16:54:32 +00:00
|
|
|
logging.error("Traceback:", exc_info=True)
|
2025-08-28 08:51:05 +00:00
|
|
|
logging.info("== END (1) ==")
|
|
|
|
|
|
|
|
|
|
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()
|