API: agrega por_prioridad con último código llamado a muestras por letra. Vista: badges con color de prioridad mostrando "A: A-023", "B: B-011", etc. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
72 lines
2.4 KiB
PHP
72 lines
2.4 KiB
PHP
<?php
|
|
/**
|
|
* GET get_resumen_muestras.php
|
|
* Resumen de estaciones de toma de muestras para recepción.
|
|
*/
|
|
require_once __DIR__ . '/_helpers.php';
|
|
requireTurnero();
|
|
|
|
$pdo = db();
|
|
$sesion = sesionHoy();
|
|
if (!$sesion) jsonOk(['estaciones' => [], 'en_espera' => 0, 'en_espera_muestra' => 0]);
|
|
$sesionId = (int)$sesion['id'];
|
|
|
|
// Estaciones con el turno en servicio actual
|
|
$stmt = $pdo->prepare(
|
|
"SELECT l.id, l.nombre,
|
|
t.id AS turno_id, t.codigo, t.paciente_nombre
|
|
FROM turnero_lugares l
|
|
LEFT JOIN turnero_turnos t
|
|
ON t.lugar_destino_id = l.id
|
|
AND t.sesion_id = ?
|
|
AND t.estado = 'en_servicio'
|
|
AND t.muestra_espera_at IS NULL
|
|
AND t.fin_lugar_at IS NULL
|
|
WHERE l.tipo = 'muestras' AND l.activo = 1
|
|
ORDER BY l.sort_order"
|
|
);
|
|
$stmt->execute([$sesionId]);
|
|
$estaciones = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Cola global de muestras (en_espera_lugar)
|
|
$s = $pdo->prepare(
|
|
"SELECT COUNT(*) FROM turnero_turnos
|
|
WHERE sesion_id = ? AND estado = 'en_espera_lugar' AND muestra_espera_at IS NULL
|
|
AND lugar_destino_id IN (SELECT id FROM turnero_lugares WHERE tipo='muestras' AND activo=1)"
|
|
);
|
|
$s->execute([$sesionId]);
|
|
$enEspera = (int)$s->fetchColumn();
|
|
|
|
// Pacientes entre tomas (espera progresiva)
|
|
$s2 = $pdo->prepare(
|
|
"SELECT COUNT(*) FROM turnero_turnos
|
|
WHERE sesion_id = ? AND muestra_espera_at IS NOT NULL AND fin_lugar_at IS NULL
|
|
AND lugar_destino_id IN (SELECT id FROM turnero_lugares WHERE tipo='muestras' AND activo=1)"
|
|
);
|
|
$s2->execute([$sesionId]);
|
|
$enEsperaMuestra = (int)$s2->fetchColumn();
|
|
|
|
// Último turno atendido en muestras por prioridad (letra)
|
|
$sp = $pdo->prepare(
|
|
"SELECT p.codigo, p.color,
|
|
(SELECT t.codigo FROM turnero_turnos t
|
|
WHERE t.sesion_id = ? AND t.prioridad_id = p.id
|
|
AND t.lugar_destino_id IN (SELECT id FROM turnero_lugares WHERE tipo='muestras' AND activo=1)
|
|
AND t.inicio_lugar_at IS NOT NULL
|
|
ORDER BY t.numero DESC LIMIT 1) AS ultimo_codigo
|
|
FROM turnero_prioridades p
|
|
ORDER BY p.orden_peso ASC"
|
|
);
|
|
$sp->execute([$sesionId]);
|
|
$porPrioridad = array_values(array_filter(
|
|
$sp->fetchAll(PDO::FETCH_ASSOC),
|
|
fn($r) => $r['ultimo_codigo'] !== null
|
|
));
|
|
|
|
jsonOk([
|
|
'estaciones' => $estaciones,
|
|
'en_espera' => $enEspera,
|
|
'en_espera_muestra' => $enEsperaMuestra,
|
|
'por_prioridad' => $porPrioridad,
|
|
]);
|