up
This commit is contained in:
@@ -49,54 +49,49 @@ if ($sesionCerrada) {
|
|||||||
|
|
||||||
// ── Cola según área ───────────────────────────────────────────
|
// ── Cola según área ───────────────────────────────────────────
|
||||||
if ($area === 'recepcion') {
|
if ($area === 'recepcion') {
|
||||||
$estadosCola = ["'espera'", "'en_recepcion'"];
|
// Recepción: todos los turnos en espera/en_recepcion
|
||||||
|
$inEstados = "'espera', 'en_recepcion'";
|
||||||
$filtroLugar = '';
|
$filtroLugar = '';
|
||||||
$bindsCola = [$sesionId];
|
$bindsCola = [$sesionId];
|
||||||
|
|
||||||
|
$stmt = $pdo->prepare(
|
||||||
|
"SELECT t.id, t.codigo, t.numero, t.estado, t.paciente_nombre,
|
||||||
|
t.recepcion_desk_id, t.creado_at, t.llamado_recepcion_at, t.llamado_lugar_at,
|
||||||
|
p.codigo AS prioridad_codigo, p.nombre AS prioridad_nombre,
|
||||||
|
p.color AS prioridad_color, p.orden_peso
|
||||||
|
FROM turnero_turnos t
|
||||||
|
JOIN turnero_prioridades p ON p.id = t.prioridad_id
|
||||||
|
WHERE t.sesion_id = ? AND t.estado IN ($inEstados)
|
||||||
|
ORDER BY p.orden_peso ASC, t.creado_at ASC"
|
||||||
|
);
|
||||||
|
$stmt->execute($bindsCola);
|
||||||
} else {
|
} else {
|
||||||
$estadosCola = ["'en_espera_lugar'", "'en_servicio'"];
|
// Lugar: cola compartida (grupo) solo en_espera_lugar
|
||||||
// Expandir grupo de lugares
|
|
||||||
$grupoIds = lugarIdsDeGrupo($lugarId);
|
$grupoIds = lugarIdsDeGrupo($lugarId);
|
||||||
$inLugares = implode(',', array_map('intval', $grupoIds));
|
$inLugares = implode(',', array_map('intval', $grupoIds));
|
||||||
$filtroLugar = "AND t.lugar_destino_id IN ($inLugares)";
|
|
||||||
$bindsCola = [$sesionId];
|
|
||||||
}
|
|
||||||
|
|
||||||
$inEstados = implode(', ', $estadosCola);
|
$stmt = $pdo->prepare(
|
||||||
|
"SELECT t.id, t.codigo, t.numero, t.estado, t.paciente_nombre,
|
||||||
$stmt = $pdo->prepare(
|
t.recepcion_desk_id, t.creado_at, t.llamado_recepcion_at, t.llamado_lugar_at,
|
||||||
"SELECT t.id,
|
p.codigo AS prioridad_codigo, p.nombre AS prioridad_nombre,
|
||||||
t.codigo,
|
p.color AS prioridad_color, p.orden_peso
|
||||||
t.numero,
|
|
||||||
t.estado,
|
|
||||||
t.paciente_nombre,
|
|
||||||
t.recepcion_desk_id,
|
|
||||||
t.creado_at,
|
|
||||||
t.llamado_recepcion_at,
|
|
||||||
t.llamado_lugar_at,
|
|
||||||
p.codigo AS prioridad_codigo,
|
|
||||||
p.nombre AS prioridad_nombre,
|
|
||||||
p.color AS prioridad_color,
|
|
||||||
p.orden_peso
|
|
||||||
FROM turnero_turnos t
|
FROM turnero_turnos t
|
||||||
JOIN turnero_prioridades p ON p.id = t.prioridad_id
|
JOIN turnero_prioridades p ON p.id = t.prioridad_id
|
||||||
WHERE t.sesion_id = ?
|
WHERE t.sesion_id = ?
|
||||||
AND t.estado IN ({$inEstados})
|
AND t.estado = 'en_espera_lugar'
|
||||||
{$filtroLugar}
|
AND t.lugar_destino_id IN ($inLugares)
|
||||||
ORDER BY p.orden_peso ASC, t.creado_at ASC"
|
ORDER BY p.orden_peso ASC, t.creado_at ASC"
|
||||||
);
|
);
|
||||||
$stmt->execute($bindsCola);
|
$stmt->execute([$sesionId]);
|
||||||
|
}
|
||||||
$cola = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
$cola = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
// ── Turno actualmente en pantalla (último llamado) ────────────
|
// ── Turno actualmente en pantalla (último llamado) ────────────
|
||||||
$campoLlamado = $area === 'recepcion' ? 'llamado_recepcion_at' : 'llamado_lugar_at';
|
$campoLlamado = $area === 'recepcion' ? 'llamado_recepcion_at' : 'llamado_lugar_at';
|
||||||
$estadoActivo = $area === 'recepcion' ? "'en_recepcion'" : "'en_servicio'";
|
$estadoActivo = $area === 'recepcion' ? "'en_recepcion'" : "'en_servicio'";
|
||||||
if ($area === 'lugar') {
|
// Activo: para lugar filtrar SOLO por esta estación (ya fue asignada al llamar)
|
||||||
$filtroActivo = "AND t.lugar_destino_id IN ($inLugares)";
|
$filtroActivo = $area === 'lugar' ? 'AND t.lugar_destino_id = ?' : '';
|
||||||
$bindActivo = [$sesionId];
|
$bindActivo = $area === 'lugar' ? [$sesionId, $lugarId] : [$sesionId];
|
||||||
} else {
|
|
||||||
$filtroActivo = '';
|
|
||||||
$bindActivo = [$sesionId];
|
|
||||||
}
|
|
||||||
|
|
||||||
$stmt = $pdo->prepare(
|
$stmt = $pdo->prepare(
|
||||||
"SELECT t.id,
|
"SELECT t.id,
|
||||||
|
|||||||
@@ -61,12 +61,12 @@ try {
|
|||||||
} else {
|
} else {
|
||||||
// Cola de un Lugar específico: turnos 'en_espera_lugar' asignados a ese lugar
|
// Cola de un Lugar específico: turnos 'en_espera_lugar' asignados a ese lugar
|
||||||
if ($turnoId) {
|
if ($turnoId) {
|
||||||
// Llamar uno específico por id
|
// Llamar uno específico por id (puede ser en_espera_lugar o ya en_servicio para re-llamar)
|
||||||
$stmt = $pdo->prepare(
|
$stmt = $pdo->prepare(
|
||||||
'SELECT t.*, p.codigo AS prioridad_codigo, p.nombre AS prioridad_nombre, p.color AS prioridad_color
|
'SELECT t.*, p.codigo AS prioridad_codigo, p.nombre AS prioridad_nombre, p.color AS prioridad_color
|
||||||
FROM turnero_turnos t
|
FROM turnero_turnos t
|
||||||
JOIN turnero_prioridades p ON p.id = t.prioridad_id
|
JOIN turnero_prioridades p ON p.id = t.prioridad_id
|
||||||
WHERE t.id = ? AND t.estado = "en_espera_lugar"'
|
WHERE t.id = ? AND t.estado IN ("en_espera_lugar", "en_servicio")'
|
||||||
);
|
);
|
||||||
$stmt->execute([$turnoId]);
|
$stmt->execute([$turnoId]);
|
||||||
$turno = $stmt->fetch(PDO::FETCH_ASSOC);
|
$turno = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||||
@@ -82,15 +82,33 @@ try {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Re-llamar: si ya está en_servicio en ESTA estación, solo actualiza llamado_at
|
||||||
|
if ($turno['estado'] === 'en_servicio' && $turno['lugar_destino_id'] == $lugarId) {
|
||||||
|
$pdo->prepare(
|
||||||
|
'UPDATE turnero_turnos SET llamado_lugar_at = NOW() WHERE id = ?'
|
||||||
|
)->execute([$turno['id']]);
|
||||||
|
$pdo->commit();
|
||||||
|
// Refrescar y retornar
|
||||||
|
$stmt = $pdo->prepare(
|
||||||
|
'SELECT t.*, p.codigo AS prioridad_codigo, p.nombre AS prioridad_nombre, p.color AS prioridad_color
|
||||||
|
FROM turnero_turnos t JOIN turnero_prioridades p ON p.id = t.prioridad_id WHERE t.id = ?'
|
||||||
|
);
|
||||||
|
$stmt->execute([$turno['id']]);
|
||||||
|
$turnoActualizado = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||||
|
notificarSSE((int)$turnoActualizado['sesion_id']);
|
||||||
|
jsonOk(['turno' => $turnoActualizado], "Turno {$turnoActualizado['codigo']} re-llamado");
|
||||||
|
}
|
||||||
|
|
||||||
$stmt = $pdo->prepare(
|
$stmt = $pdo->prepare(
|
||||||
'UPDATE turnero_turnos
|
'UPDATE turnero_turnos
|
||||||
SET estado = "en_servicio",
|
SET estado = "en_servicio",
|
||||||
llamado_lugar_at = NOW(),
|
llamado_lugar_at = NOW(),
|
||||||
inicio_lugar_at = NOW(),
|
inicio_lugar_at = NOW(),
|
||||||
atendido_lugar_por = ?
|
atendido_lugar_por= ?,
|
||||||
|
lugar_destino_id = ?
|
||||||
WHERE id = ? AND estado = "en_espera_lugar"'
|
WHERE id = ? AND estado = "en_espera_lugar"'
|
||||||
);
|
);
|
||||||
$stmt->execute([adminId(), $turno['id']]);
|
$stmt->execute([adminId(), $lugarId, $turno['id']]);
|
||||||
|
|
||||||
if ($stmt->rowCount() === 0) {
|
if ($stmt->rowCount() === 0) {
|
||||||
$pdo->rollBack();
|
$pdo->rollBack();
|
||||||
|
|||||||
@@ -320,6 +320,9 @@ require_once __DIR__ . '/../../../shared/components/sidebar.php';
|
|||||||
<button class="btn btn-outline-warning d-none" id="btn-regresar" onclick="regresarCola()">
|
<button class="btn btn-outline-warning d-none" id="btn-regresar" onclick="regresarCola()">
|
||||||
<i class="fas fa-undo me-1"></i>Regresar a cola
|
<i class="fas fa-undo me-1"></i>Regresar a cola
|
||||||
</button>
|
</button>
|
||||||
|
<button class="btn btn-outline-info d-none" id="btn-rellamar" onclick="rellamarTurno()">
|
||||||
|
<i class="fas fa-bullhorn me-1"></i>Re-llamar
|
||||||
|
</button>
|
||||||
<button class="btn btn-secondary ms-auto" id="btn-ausente" onclick="marcarAusente()">
|
<button class="btn btn-secondary ms-auto" id="btn-ausente" onclick="marcarAusente()">
|
||||||
<i class="fas fa-user-slash me-1"></i>Ausente
|
<i class="fas fa-user-slash me-1"></i>Ausente
|
||||||
</button>
|
</button>
|
||||||
@@ -364,10 +367,22 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
function iniciarPuesto() {
|
function iniciarPuesto() {
|
||||||
|
recuperarTurnoActivo(); // ver si ya hay un turno en_servicio en esta estación
|
||||||
cargarCola();
|
cargarCola();
|
||||||
pollingColaId = setInterval(cargarCola, 7000);
|
pollingColaId = setInterval(cargarCola, 7000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Recupera el turno en_servicio de ESTA estación (si existe) al cargar la página
|
||||||
|
async function recuperarTurnoActivo() {
|
||||||
|
try {
|
||||||
|
const res = await fetch(`${API}get_cola.php?area=lugar&lugar_id=${lugarId}`);
|
||||||
|
const json = await res.json();
|
||||||
|
if (json.ok && json.activo) {
|
||||||
|
await abrirFicha(json.activo);
|
||||||
|
}
|
||||||
|
} catch (_) {}
|
||||||
|
}
|
||||||
|
|
||||||
// ── Selector de lugar ─────────────────────────────────────────
|
// ── Selector de lugar ─────────────────────────────────────────
|
||||||
function confirmarLugar() {
|
function confirmarLugar() {
|
||||||
const sel = document.getElementById('sel-lugar-init');
|
const sel = document.getElementById('sel-lugar-init');
|
||||||
@@ -774,14 +789,36 @@ function actualizarEstadoBadge(estado) {
|
|||||||
const btnIni = document.getElementById('btn-iniciar');
|
const btnIni = document.getElementById('btn-iniciar');
|
||||||
const btnFin = document.getElementById('btn-finalizar');
|
const btnFin = document.getElementById('btn-finalizar');
|
||||||
const btnReg = document.getElementById('btn-regresar');
|
const btnReg = document.getElementById('btn-regresar');
|
||||||
|
const btnRellamar = document.getElementById('btn-rellamar');
|
||||||
if (estado === 'en_espera_lugar') {
|
if (estado === 'en_espera_lugar') {
|
||||||
btnIni.classList.remove('d-none'); btnIni.disabled = hayPendientes;
|
btnIni.classList.remove('d-none'); btnIni.disabled = hayPendientes;
|
||||||
btnFin.classList.add('d-none');
|
btnFin.classList.add('d-none');
|
||||||
btnReg.classList.add('d-none');
|
btnReg.classList.add('d-none');
|
||||||
|
btnRellamar.classList.add('d-none');
|
||||||
} else if (estado === 'en_servicio') {
|
} else if (estado === 'en_servicio') {
|
||||||
btnIni.classList.add('d-none');
|
btnIni.classList.add('d-none');
|
||||||
btnFin.classList.remove('d-none');
|
btnFin.classList.remove('d-none');
|
||||||
btnReg.classList.remove('d-none');
|
btnReg.classList.remove('d-none');
|
||||||
|
btnRellamar.classList.remove('d-none');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function rellamarTurno() {
|
||||||
|
if (!turnoActivo) return;
|
||||||
|
const btn = document.getElementById('btn-rellamar');
|
||||||
|
btn.disabled = true;
|
||||||
|
try {
|
||||||
|
const res = await fetch(API + 'llamar_turno.php', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ area: 'lugar', lugar_id: lugarId, turno_id: turnoActivo.id }),
|
||||||
|
});
|
||||||
|
const json = await res.json();
|
||||||
|
if (!json.ok) alert('Error: ' + json.error);
|
||||||
|
} catch (err) {
|
||||||
|
alert('Error: ' + err.message);
|
||||||
|
} finally {
|
||||||
|
btn.disabled = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user