diff --git a/api/lab/eps.php b/api/lab/eps.php new file mode 100644 index 0000000..c651e23 --- /dev/null +++ b/api/lab/eps.php @@ -0,0 +1,64 @@ +query($sql)->fetchAll(PDO::FETCH_ASSOC); + jsonOk(['eps' => $rows]); +} + +requireMethod('POST'); +requireAdmin(); + +$body = inputJson(); +$action = trim($body['action'] ?? ''); + +if ($action === 'save') { + $nombre = trim($body['nombre'] ?? ''); + $id = (int)($body['id'] ?? 0); + if (!$nombre) jsonError('nombre requerido.'); + if (strlen($nombre) > 120) jsonError('nombre demasiado largo (máx 120).'); + + if ($id) { + $pdo->prepare('UPDATE lab_eps SET nombre = ? WHERE id = ?')->execute([$nombre, $id]); + jsonOk(['id' => $id], 'EPS actualizada.'); + } else { + $st = $pdo->prepare('INSERT INTO lab_eps (nombre) VALUES (?)'); + try { + $st->execute([$nombre]); + } catch (\PDOException $e) { + if ($e->getCode() == 23000) jsonError('Ya existe una EPS con ese nombre.'); + throw $e; + } + jsonOk(['id' => (int)$pdo->lastInsertId()], 'EPS creada.'); + } +} + +if ($action === 'toggle') { + $id = (int)($body['id'] ?? 0); + if (!$id) jsonError('id requerido.'); + $pdo->prepare('UPDATE lab_eps SET activa = NOT activa WHERE id = ?')->execute([$id]); + jsonOk([], 'Estado actualizado.'); +} + +if ($action === 'delete') { + $id = (int)($body['id'] ?? 0); + if (!$id) jsonError('id requerido.'); + $uso = $pdo->prepare('SELECT COUNT(*) FROM lab_pacientes WHERE eps = (SELECT nombre FROM lab_eps WHERE id = ?)'); + $uso->execute([$id]); + if ((int)$uso->fetchColumn() > 0) jsonError('No se puede eliminar: hay pacientes con esta EPS. Desactívela en su lugar.'); + $pdo->prepare('DELETE FROM lab_eps WHERE id = ?')->execute([$id]); + jsonOk([], 'EPS eliminada.'); +} + +jsonError('action inválida.'); diff --git a/modules/lab_eps/module.php b/modules/lab_eps/module.php new file mode 100644 index 0000000..dfb80e6 --- /dev/null +++ b/modules/lab_eps/module.php @@ -0,0 +1,12 @@ + 'lab_eps', + 'name' => 'EPS', + 'icon' => 'fas fa-hospital', + 'category' => 'lab', + 'route' => '/lab_eps.php', + 'is_active' => true, + 'sort_order' => 22, + 'oleada' => 0, + 'description' => 'Gestión de EPS y aseguradoras', + 'links' => [['name' => 'EPS', 'icon' => 'fas fa-hospital', 'route' => '/lab_eps.php']], +]; diff --git a/modules/lab_eps/views/index.php b/modules/lab_eps/views/index.php new file mode 100644 index 0000000..032d610 --- /dev/null +++ b/modules/lab_eps/views/index.php @@ -0,0 +1,178 @@ + + + +
+ +| Nombre | +Estado | +Acciones | +
|---|---|---|
| Cargando… | ||