- Add detalle_json column to sync_wa_log via migration - guardar_sync_log() now stores doc, nombre, action, examenes count per patient - Historial table shows "Ver N" button that expands a row with patient list, action badge (Nuevo/Actualizado/Omitido) and exam count per patient Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
185 lines
7.4 KiB
Python
185 lines
7.4 KiB
Python
import sqlite3
|
|
import os
|
|
from datetime import datetime
|
|
|
|
DB_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)), "rips_manager.db")
|
|
|
|
|
|
def get_connection():
|
|
conn = sqlite3.connect(DB_PATH)
|
|
conn.row_factory = sqlite3.Row
|
|
conn.execute("PRAGMA journal_mode=WAL")
|
|
conn.execute("PRAGMA foreign_keys=ON")
|
|
return conn
|
|
|
|
|
|
def _migrate(conn):
|
|
sync_cols = {r[1] for r in conn.execute("PRAGMA table_info(sync_wa_log)")}
|
|
if "detalle_json" not in sync_cols:
|
|
conn.execute("ALTER TABLE sync_wa_log ADD COLUMN detalle_json TEXT")
|
|
|
|
cols = {r[1] for r in conn.execute("PRAGMA table_info(envios)")}
|
|
if "idrecepcion" not in cols:
|
|
conn.execute("ALTER TABLE envios ADD COLUMN idrecepcion INTEGER")
|
|
if "contrato" not in cols:
|
|
conn.execute("ALTER TABLE envios ADD COLUMN contrato TEXT")
|
|
if "mensaje_tns" not in cols:
|
|
conn.execute("ALTER TABLE envios ADD COLUMN mensaje_tns TEXT")
|
|
|
|
tables = {r[0] for r in conn.execute("SELECT name FROM sqlite_master WHERE type='table'")}
|
|
if "activity_log" not in tables:
|
|
conn.execute("""
|
|
CREATE TABLE activity_log (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
user_id INTEGER,
|
|
username TEXT NOT NULL DEFAULT '',
|
|
accion TEXT NOT NULL,
|
|
detalle TEXT NOT NULL DEFAULT '',
|
|
ip TEXT NOT NULL DEFAULT '',
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
)
|
|
""")
|
|
if "contratos" not in tables:
|
|
conn.execute("""
|
|
CREATE TABLE contratos (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
numero_contrato TEXT UNIQUE NOT NULL,
|
|
nit_empresa TEXT NOT NULL DEFAULT '',
|
|
tipo_usuario TEXT NOT NULL,
|
|
descripcion TEXT NOT NULL DEFAULT '',
|
|
excluir INTEGER NOT NULL DEFAULT 0,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
)
|
|
""")
|
|
else:
|
|
contrato_cols = {r[1] for r in conn.execute("PRAGMA table_info(contratos)")}
|
|
if "excluir" not in contrato_cols:
|
|
conn.execute("ALTER TABLE contratos ADD COLUMN excluir INTEGER NOT NULL DEFAULT 0")
|
|
if "sin_contrato" not in contrato_cols:
|
|
conn.execute("ALTER TABLE contratos ADD COLUMN sin_contrato INTEGER NOT NULL DEFAULT 0")
|
|
if "excluir_ventas" not in contrato_cols:
|
|
conn.execute("ALTER TABLE contratos ADD COLUMN excluir_ventas INTEGER NOT NULL DEFAULT 0")
|
|
# Ampliar CHECK constraint de queries para incluir 'ventas'
|
|
q_sql = conn.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name='queries'").fetchone()
|
|
if q_sql and "'ventas'" not in q_sql[0]:
|
|
conn.execute("PRAGMA foreign_keys = OFF")
|
|
conn.execute("""CREATE TABLE queries_new (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL,
|
|
query_type TEXT NOT NULL CHECK(query_type IN ('terceros','transaccion','ventas')),
|
|
query_text TEXT NOT NULL,
|
|
description TEXT,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
)""")
|
|
conn.execute("INSERT INTO queries_new SELECT * FROM queries")
|
|
conn.execute("DROP TABLE queries")
|
|
conn.execute("ALTER TABLE queries_new RENAME TO queries")
|
|
conn.execute("PRAGMA foreign_keys = ON")
|
|
conn.commit()
|
|
|
|
# Ampliar CHECK constraint de envios para incluir 'ventas'
|
|
e_sql = conn.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name='envios'").fetchone()
|
|
if e_sql and "'ventas'" not in e_sql[0]:
|
|
conn.execute("PRAGMA foreign_keys = OFF")
|
|
conn.execute("""CREATE TABLE envios_new (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
user_id INTEGER,
|
|
tipo TEXT NOT NULL CHECK(tipo IN ('terceros','transaccion','ventas')),
|
|
factura TEXT,
|
|
fecha_inicio TEXT,
|
|
fecha_fin TEXT,
|
|
pacientes_count INTEGER DEFAULT 0,
|
|
servicios_count INTEGER DEFAULT 0,
|
|
status TEXT NOT NULL DEFAULT 'pendiente',
|
|
json_enviado TEXT,
|
|
respuesta_api TEXT,
|
|
codigo_cuv TEXT,
|
|
idrecepcion INTEGER,
|
|
contrato TEXT,
|
|
mensaje_tns TEXT,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
FOREIGN KEY (user_id) REFERENCES users(id)
|
|
)""")
|
|
conn.execute("INSERT INTO envios_new SELECT id,user_id,tipo,factura,fecha_inicio,fecha_fin,"
|
|
"pacientes_count,servicios_count,status,json_enviado,respuesta_api,codigo_cuv,"
|
|
"idrecepcion,contrato,mensaje_tns,created_at FROM envios")
|
|
conn.execute("DROP TABLE envios")
|
|
conn.execute("ALTER TABLE envios_new RENAME TO envios")
|
|
conn.execute("PRAGMA foreign_keys = ON")
|
|
conn.commit()
|
|
|
|
conn.commit()
|
|
|
|
|
|
def init_db():
|
|
conn = get_connection()
|
|
conn.executescript("""
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
username TEXT UNIQUE NOT NULL,
|
|
email TEXT UNIQUE NOT NULL,
|
|
password_hash TEXT NOT NULL,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS config (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
key TEXT UNIQUE NOT NULL,
|
|
value TEXT
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS queries (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL,
|
|
query_type TEXT NOT NULL CHECK(query_type IN ('terceros','transaccion','ventas')),
|
|
query_text TEXT NOT NULL,
|
|
description TEXT,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS rda_test_log (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
idrecepcion INTEGER NOT NULL,
|
|
factura TEXT,
|
|
contrato TEXT,
|
|
ok INTEGER NOT NULL DEFAULT 0,
|
|
mensaje TEXT,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS envios (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
user_id INTEGER,
|
|
tipo TEXT NOT NULL CHECK(tipo IN ('terceros','transaccion','ventas')),
|
|
factura TEXT,
|
|
fecha_inicio TEXT,
|
|
fecha_fin TEXT,
|
|
pacientes_count INTEGER DEFAULT 0,
|
|
servicios_count INTEGER DEFAULT 0,
|
|
status TEXT NOT NULL DEFAULT 'pendiente',
|
|
json_enviado TEXT,
|
|
respuesta_api TEXT,
|
|
codigo_cuv TEXT,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
FOREIGN KEY (user_id) REFERENCES users(id)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS sync_wa_log (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
user_id INTEGER,
|
|
origen TEXT NOT NULL DEFAULT 'manual',
|
|
modo TEXT NOT NULL DEFAULT 'insertar',
|
|
total INTEGER NOT NULL DEFAULT 0,
|
|
created INTEGER NOT NULL DEFAULT 0,
|
|
skipped INTEGER NOT NULL DEFAULT 0,
|
|
updated INTEGER NOT NULL DEFAULT 0,
|
|
errores INTEGER NOT NULL DEFAULT 0,
|
|
errores_det TEXT,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
FOREIGN KEY (user_id) REFERENCES users(id)
|
|
);
|
|
""")
|
|
conn.commit()
|
|
_migrate(conn)
|
|
conn.close()
|