71 lines
2.1 KiB
PHP
71 lines
2.1 KiB
PHP
<?php
|
|
session_start();
|
|
if (!isset($_SESSION["user_id"])) {
|
|
header("Location: auth/login.php");
|
|
exit();
|
|
}
|
|
|
|
if (!isset($_SESSION['user_rol']) || $_SESSION['user_rol'] !== 'Administrador General') {
|
|
$_SESSION['error_message'] = 'No tienes permiso para realizar esta acción.';
|
|
header('Location: colaboradores.php');
|
|
exit();
|
|
}
|
|
|
|
require_once 'db/config.php';
|
|
|
|
// Check for user ID
|
|
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
|
|
$_SESSION['error_message'] = "ID de colaborador no válido.";
|
|
header("Location: colaboradores.php");
|
|
exit();
|
|
}
|
|
|
|
$id_to_delete = (int)$_GET['id'];
|
|
$current_user_id = (int)$_SESSION['user_id'];
|
|
|
|
// Security checks
|
|
if ($id_to_delete === $current_user_id) {
|
|
$_SESSION['error_message'] = "No puedes eliminar tu propia cuenta.";
|
|
header("Location: colaboradores.php");
|
|
exit();
|
|
}
|
|
|
|
if ($id_to_delete === 1) {
|
|
$_SESSION['error_message'] = "No se puede eliminar al administrador principal del sistema.";
|
|
header("Location: colaboradores.php");
|
|
exit();
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
// Check if the user exists before trying to delete
|
|
$stmt = $pdo->prepare("SELECT id FROM usuarios WHERE id = :id");
|
|
$stmt->execute([':id' => $id_to_delete]);
|
|
if (!$stmt->fetch()) {
|
|
$_SESSION['error_message'] = "El colaborador que intentas eliminar no existe.";
|
|
header("Location: colaboradores.php");
|
|
exit();
|
|
}
|
|
|
|
// Proceed with deletion
|
|
$stmt = $pdo->prepare("DELETE FROM usuarios WHERE id = :id");
|
|
$stmt->execute([':id' => $id_to_delete]);
|
|
|
|
$_SESSION['success_message'] = "Colaborador eliminado exitosamente.";
|
|
header("Location: colaboradores.php");
|
|
exit();
|
|
|
|
} catch (PDOException $e) {
|
|
error_log("Error al eliminar colaborador: " . $e->getMessage());
|
|
// Foreign key constraint check
|
|
if ($e->getCode() == '23000') {
|
|
$_SESSION['error_message'] = "No se puede eliminar este colaborador porque tiene registros asociados (movimientos, ventas, etc.).";
|
|
} else {
|
|
$_SESSION['error_message'] = "Error al conectar con la base de datos.";
|
|
}
|
|
header("Location: colaboradores.php");
|
|
exit();
|
|
}
|
|
?>
|