rips: prevent reuse of already-consumed exam records

- Add turno_id column to rips_examenes_pendientes (already migrated)
- Filter turno_id IS NULL in get_examenes_rips.php query
- New marcar_rips_usado.php marks record as consumed when loaded
- cargarExamenesRips() calls marker after loading exams into the turn

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-21 16:39:22 -05:00
co-authored by Claude Sonnet 4.6
parent d46c983776
commit 26bb9e1844
3 changed files with 37 additions and 0 deletions
+1
View File
@@ -20,6 +20,7 @@ $cacheRow = db()->prepare(
WHERE numero_documento = ?
AND DATE(created_at) = CURDATE()
AND created_at >= NOW() - INTERVAL 30 MINUTE
AND turno_id IS NULL
ORDER BY created_at DESC
LIMIT 1"
);
+26
View File
@@ -0,0 +1,26 @@
<?php
/**
* POST /api/lab/marcar_rips_usado.php
* Marca el registro RIPS de una cédula como consumido por un turno.
* Body: { cedula, turno_id }
*/
require_once __DIR__ . '/_helpers.php';
requireMethod('POST');
$body = json_decode(file_get_contents('php://input'), true) ?? [];
$cedula = trim($body['cedula'] ?? '');
$turnoId = (int)($body['turno_id'] ?? 0);
if (!$cedula || !$turnoId) jsonError('cedula y turno_id requeridos', 400);
db()->prepare(
"UPDATE rips_examenes_pendientes
SET turno_id = ?
WHERE numero_documento = ?
AND DATE(created_at) = CURDATE()
AND turno_id IS NULL
ORDER BY created_at DESC
LIMIT 1"
)->execute([$turnoId, $cedula]);
jsonOk(['marcado' => true]);
+10
View File
@@ -1707,6 +1707,16 @@ async function cargarExamenesRips() {
if (_ripsData.valor_total > 0) {
document.getElementById('inp-total').value = Math.round(_ripsData.valor_total);
}
// Marcar registro RIPS como consumido para no reutilizarlo en otro turno
const cedula = pacienteActivo?.numero_documento || pacienteActivo?.documento
|| turnoActivo?.paciente_nombre || '';
if (cedula && turnoActivo?.id) {
fetch(`${BASE_WA}api/lab/marcar_rips_usado.php`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ cedula, turno_id: turnoActivo.id }),
}).catch(() => {});
}
descartarRips();
}