103 lines
3.0 KiB
PHP
103 lines
3.0 KiB
PHP
<?php
|
|
/**
|
|
* modules/registro_exams/api/get_ordenes.php
|
|
* GET ?fecha=YYYY-MM-DD &estado=pendiente &busqueda=texto &page=1 &limit=25
|
|
* Devuelve lista de órdenes con datos del paciente e ítems (count).
|
|
*/
|
|
|
|
require_once __DIR__ . '/_helpers.php';
|
|
requireMethod('GET');
|
|
requireExams();
|
|
|
|
$pdo = db();
|
|
|
|
$fecha = trim($_GET['fecha'] ?? date('Y-m-d'));
|
|
$estado = trim($_GET['estado'] ?? '');
|
|
$busqueda = trim($_GET['busqueda'] ?? '');
|
|
$page = max(1, (int) ($_GET['page'] ?? 1));
|
|
$limit = min(100, max(1, (int) ($_GET['limit'] ?? 25)));
|
|
$offset = ($page - 1) * $limit;
|
|
|
|
// Validar fecha
|
|
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $fecha)) {
|
|
$fecha = date('Y-m-d');
|
|
}
|
|
|
|
$where = ['DATE(o.creado_at) = ?'];
|
|
$params = [$fecha];
|
|
|
|
if ($estado !== '') {
|
|
$allowed = ['pendiente', 'en_proceso', 'completa', 'entregada', 'cancelada'];
|
|
if (in_array($estado, $allowed, true)) {
|
|
$where[] = 'o.estado = ?';
|
|
$params[] = $estado;
|
|
}
|
|
}
|
|
|
|
if ($busqueda !== '') {
|
|
$like = '%' . $busqueda . '%';
|
|
$where[] = '(p.nombre_completo LIKE ? OR p.numero_documento LIKE ? OR o.codigo LIKE ?)';
|
|
$params[] = $like;
|
|
$params[] = $like;
|
|
$params[] = $like;
|
|
}
|
|
|
|
$whereSQL = 'WHERE ' . implode(' AND ', $where);
|
|
|
|
// Total para paginación
|
|
$countStmt = $pdo->prepare(
|
|
"SELECT COUNT(*) FROM exam_ordenes o
|
|
JOIN lab_pacientes p ON p.id = o.paciente_id
|
|
{$whereSQL}"
|
|
);
|
|
$countStmt->execute($params);
|
|
$total = (int) $countStmt->fetchColumn();
|
|
|
|
// Datos
|
|
$sql = "
|
|
SELECT
|
|
o.id, o.codigo, o.prioridad, o.estado, o.requiere_ayuno, o.horas_ayuno,
|
|
o.medico_nombre, o.diagnostico, o.notas,
|
|
o.creado_at, o.updated_at,
|
|
p.id AS paciente_id,
|
|
p.nombre_completo,
|
|
p.numero_documento,
|
|
p.tipo_documento,
|
|
p.telefono,
|
|
p.fecha_nacimiento,
|
|
(SELECT COUNT(*) FROM exam_items ei WHERE ei.orden_id = o.id) AS total_items,
|
|
(SELECT COUNT(*) FROM exam_items ei WHERE ei.orden_id = o.id AND ei.estado = 'resultado_listo') AS items_listos,
|
|
(SELECT COUNT(*) FROM exam_muestras em WHERE em.orden_id = o.id AND em.estado = 'tomada') AS muestras_tomadas
|
|
FROM exam_ordenes o
|
|
JOIN lab_pacientes p ON p.id = o.paciente_id
|
|
{$whereSQL}
|
|
ORDER BY
|
|
FIELD(o.prioridad, 'stat', 'urgente', 'normal'),
|
|
o.creado_at ASC
|
|
LIMIT {$limit} OFFSET {$offset}
|
|
";
|
|
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute($params);
|
|
$ordenes = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Calcular edad en años para cada paciente
|
|
foreach ($ordenes as &$row) {
|
|
if ($row['fecha_nacimiento']) {
|
|
$diff = (new DateTime($row['fecha_nacimiento']))->diff(new DateTime());
|
|
$row['edad'] = $diff->y;
|
|
} else {
|
|
$row['edad'] = null;
|
|
}
|
|
}
|
|
unset($row);
|
|
|
|
jsonOk([
|
|
'data' => $ordenes,
|
|
'total' => $total,
|
|
'page' => $page,
|
|
'limit' => $limit,
|
|
'pages' => (int) ceil($total / $limit),
|
|
'fecha' => $fecha,
|
|
]);
|