Commit Graph
121 Commits
Author SHA1 Message Date
Lizandro GuarnizoandClaude Sonnet 4.6 200a3f550b feat: excluir contratos del envío TNS
- Columna excluir en tabla contratos (migración automática)
- 25 contratos nuevos sembrados con excluir=1 (laboratorios externos, clínicas, médicos particulares)
- load_excluded_set() en contratos.py para consultar el set en runtime
- preview: marca items excluidos y los reporta en el conteo
- send/send-one/send-direct: bloquea envío si contrato está excluido
- automation preview+run: filtra excluidos antes del loop, reporta count
- UI /contratos: columna Envío TNS con toggle Activo/Excluido, fila en rojo si excluida

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-10 15:44:26 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 3baa6e5f75 feat(transaccion): envío RDA directo por prefijo + número de factura
Nuevo endpoint POST /transaccion/send-direct y panel "Enviar RDA directo"
en la UI: ingresa Tipo (prefijo) y Número, busca en Firebird y envía a TNS
sin necesidad de rango de fechas ni pasar por el flujo de preview.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-09 15:10:41 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 d3a6ab7ab6 fix(tipousuario): contrato 012 no debe ser tratado como particular
El check `"012".lstrip("0") == "12"` causaba false positive marcando
contratos EPS como particulares. Ahora la heurística solo aplica si el
contrato no está en la tabla contratos; si está, el tipo_usuario del mapa
tiene precedencia y también controla es_particular.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-09 15:06:38 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 0f3a760de4 feat(contratos): tabla de contratos con tipo_usuario por contrato
Agrega tabla `contratos` en SQLite sembrada con los 17 contratos del
laboratorio. `generar_rda_paciente` consulta el mapa contrato→tipoUsuario
como primera prioridad antes de derivar de TIPOUSU/TIPOUSUSISPRO.
Incluye CRUD en /contratos con UI en sidebar.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-09 15:00:21 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 7d2a9df447 feat: GET /pacientes/examenes — exams registered in last 5 min for a cedula
Queries Firebird RECEPCION+RELACION+EXAMEN for today's date,
filters by HORAINICIORECEPCION >= now-5min in Python, returns
list of {recepcion_id, hora, cod_examen, nombre, cups, precio}.
Auth via X-Lab-Key header (whatsapp_api_key config value).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-09 14:06:22 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 e2a9e7c4d3 fix(whatsapp_sync): enviar origen='lab' en lugar de 'rips'
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-09 11:35:39 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 b0175a2ca5 feat(sync-log): historial de sincronizaciones WhatsApp en sync_wa_log
- Nueva tabla sync_wa_log (total, created, skipped, updated, errores, origen, modo)
- Log guardado en /pacientes/sync-all, /terceros/send y /automation/run
- Ruta GET /pacientes/historial devuelve últimos 100 registros
- UI: tabla de historial con origen, modo, conteos y detalle de errores

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-09 11:13:28 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 1aab14f705 feat(pacientes): modo insertar en sync-all — solo crea nuevos, omite existentes
sync_todos() ahora acepta modo='insertar'|'upsert'. La ruta /pacientes/sync-all
usa insertar: skippea cédulas que ya están en lab_pacientes. Los hooks
automáticos (terceros/automation) siguen en upsert.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-09 11:06:14 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 e7c6bc794a feat(pacientes): sync de pacientes Firebird → WhatsApp Lab
- Nuevo servicio whatsapp_sync.py: mapeo de campos RIPS→lab_pacientes
  y cliente HTTP para /api/lab/ingest_paciente.php
- Nueva ruta /pacientes con sync masiva por rango de fechas o todos
- Hook en /terceros/send y /automation/run: sync silencioso a WhatsApp
  después de cada envío a TNS
- Config: campos whatsapp_url y whatsapp_api_key + botón probar conexión

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-09 10:49:00 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 b4288ac899 fix: remove undefined facturas_usadas_sesion causing double RDA submission on test-rda
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-07 12:00:33 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 a94e2cce9f feat: omit fechaAutorizacion and vigenciaAutorizacion when numeroAutorizacion is null
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-07 10:23:42 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 b88d727623 feat: add 5 minutes to fechaHoraEgreso in RDA
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-06 19:44:06 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 235e20e38d feat: remove unused fields from RDA JSON output
Remove codFormaPago, codBanco, descuento from header.
Remove tipoUnidad, descuento, porcentajeIva, impConsumo from detallePedido.
Remove detalleFormaPago entirely.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-02 21:59:02 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 3aa0e09aa6 chore: bump version marker in json_generator
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-02 09:31:24 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 b3b283a7d0 fix: syntaxHighlight regex adding spaces inside time values
The number regex /:(\s*)(\d+)/ was matching colons inside time strings
like "14:51:26" turning them into "14: 51: 26". Changed to require the
space that JSON.stringify always adds after key-value colons (": \d+")
so colons inside string values are not affected.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 17:41:37 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 07038ac431 fix: clean time spaces in logs view without modifying stored data
Applies _clean_times when serving json_enviado in /logs/detalle
so old records with "12: 47: 37" display correctly as "12:47:37".

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 17:36:45 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 72973973d4 feat: calculate real discount % from TARIFA vs RELACION.PRECIO
Joins TARIFA using EMPRESA.TARIFA to get the base price per exam,
then calculates descuento = round((sum(TARIFA) - sum(PRECIO)) / sum(TARIFA) * 100)
and sends it as integer percentage in the RDA header encabezado.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 17:26:49 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 15f6ca65db feat: add descuento from RECEPCION.VALORDESC to RDA header
Pulls r.VALORDESC in all SQL queries (automation, test_rda, queries
defaults) and maps it to the top-level descuento field in the
RdaPaciente JSON encabezado.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 17:13:46 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 f35a39bc9b fix: add EXAMEN join to QUERY_DEFAULTS so CUPs codes survive restarts
ensure_defaults() runs on every startup and was overwriting the db
queries with the old versions from QUERY_DEFAULTS — wiping any manual
fixes. Now the source-of-truth in code has the EXAMEN join and CUPS
column, so transaccion page always shows CUPs after any restart.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 14:38:30 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 e32214fef7 tmp: add fix_queries.py to update transaccion SQL with EXAMEN join
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 13:11:25 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 eba512720f fix: remove duplicate EXAMEN JOIN in saved transaccion queries
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 13:03:48 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 b12e8fcf88 feat: use EXAMEN.NUM_ISS as codigoMaterial (CUPs) via LEFT JOIN EXAMEN
CH4=902210, PO=907106, GRAM=901107, etc. Falls back to COD_EXAMEN if empty.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 12:53:16 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 b22b47a1e1 debug: search 903967 across all candidate tables
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 12:34:50 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 fb81ddf263 debug: search PROTOCOLO table and all CUP fields system-wide
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 12:32:27 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 942d68b75d debug: catch all exceptions and return traceback as JSON
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 12:31:24 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 bc5fc36a5d debug: explore RELACION_HIMS table for CUPS_REF field
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 12:30:12 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 68aa5e0355 feat: join ITEM on COD_PROTOCOLO to get CUPS_DETALLE as codigoMaterial
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 12:29:33 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 d95e2b67ff debug: probe possible ITEM join fields, show first row without join
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 12:27:44 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 41ee88c1b4 fix: remove rel.DESCUENTO (column does not exist in RELACION table)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 12:26:09 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 b4748e34d8 debug: minimal endpoint - only ITEM columns and sample row
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 12:25:20 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 d1c1d3205f revert: remove broken ITEM join until correct field name is known
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 12:24:01 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 e6d0b74992 debug: show ITEM columns and first row to find join key
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 12:22:35 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 ff08c1b3a8 feat: use ITEM.CUPS_DETALLE as codigoMaterial in RDA
LEFT JOIN ITEM on COD_EXAMEN to get CUPS_DETALLE. Falls back to
COD_EXAMEN if CUPS is empty. Applied to automation, test_rda, and
saved queries in SQLite.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 12:20:22 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 ebd45446a4 tmp: add check_cups.py script to query Firebird schema
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 12:16:15 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 9aa04307c6 debug: query ITEM table columns and CUPS_DETALLE samples
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 12:15:33 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 44af9680f2 fix: add LIKE %CUP% search across all tables in debug endpoint
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 12:14:21 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 0896e14e62 fix: simplify debug cups endpoint to avoid Firebird CONTAINING error
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 12:13:30 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 8b2f8eb3be fix: add rel.DESCUENTO to all SQL queries; update debug endpoint with discount check
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 12:11:00 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 8732db9c1f fix: use Firebird PREFIJO as codigoPrefijo; add debug endpoint to find CUPs field
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 12:07:43 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 ed9fead353 fix: remove valor field from detallePedido; filter by PREFIJO in transaccion
- json_generator: remove valor entirely from detalle items (not even 0)
- transaccion: after fetching rows, filter by prefix if user typed full
  factura like "LHXC03726" and query returns PREFIJO column

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 12:00:42 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 1548aadf3c fix: set valor=0 in detallePedido; parse full factura (prefix+number) in search
- json_generator: send valor:0 per item instead of PRECIO from Firebird
- test_rda + transaccion: accept "LHXC03726" format — extracts numeric part
  for NUM_FACTURA and letter prefix for PREFIJO filter when prefix present

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-01 11:54:08 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 4b77f0a0ac fix: quitar rel.DESCUENTO — columna no existe en tabla RELACION de Firebird
json_generator ya usa row.get('DESCUENTO') or 0 como fallback.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-30 21:15:10 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 02c2f7a17b fix: combinar fecha+hora separados de Firebird en fechaHoraIngreso
HORAINICIORECEPCION es campo TIME en Firebird — fdb lo devuelve como
datetime.time sin fecha. _fmt_datetime recibía '06:08:29' y lo pasaba
tal cual al JSON. TNS rechazaba por formato inválido.

- _fmt_datetime ahora maneja entrada solo-fecha → agrega 00:00:00
- generar_rda_paciente combina HORAINICIORECEPCION + FECHA_RECEPCION
  antes de llamar _fmt_datetime cuando detecta hora sin fecha

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-30 20:50:01 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 9d80279114 fix: strip spaces around colons in entire RDA JSON before returning
Adds _clean_times() that recursively walks every string in the result
dict and applies re.sub(r'\s*:\s*', ':'), guaranteeing no "12: 47: 37"
spaces survive to the TNS API regardless of Firebird driver behaviour.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-30 20:16:23 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 9d87c3fa1c fix: usar regex en _fmt_datetime para eliminar espacios en horas de Firebird
re.sub('\s*:\s*', ':') captura cualquier tipo de espacio alrededor
de los dos puntos, incluyendo tipos no-ASCII que .replace() no atrapa.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-30 20:01:28 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 4aa30c74c1 fix: restaurar codFormaPago en header RDA (campo requerido segun docs)
La documentacion marca CodFormaPago como Requerido=Si. Su ausencia
causaba 'Datos incorrectos' en TNS.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-30 19:23:08 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 c7428a196b fix: quitar codFormaPago header, fechaAutorizacion=hoy, descuento de Firebird
- Elimina codFormaPago del header RDA (no va segun JSON de referencia)
- fechaAutorizacion usa fecha de hoy, no la de recepcion
- descuento toma rel.DESCUENTO de Firebird en vez de hardcodeado 0
- Agrega rel.DESCUENTO al SELECT en test_rda y automation

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-30 16:12:48 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 543af23779 fix: restaurar codFormaPago en header RDA (requerido por TNS)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-30 15:26:35 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 20a22e8e5b fix: limpiar espacios en horas desde la fuente en _safe de Firebird
Usa strftime en vez de isoformat y aplica re.sub para eliminar
espacios alrededor de los dos puntos en strings de tiempo de fdb.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-30 15:20:57 -05:00
Lizandro GuarnizoandClaude Sonnet 4.6 c5d6b9857e fix: eliminar espacios en horas de Firebird en fechaHoraIngreso/Egreso/Realizacion
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-30 15:16:58 -05:00