51 lines
1.4 KiB
PHP
51 lines
1.4 KiB
PHP
<?php
|
|
session_start();
|
|
if (!isset($_SESSION["user_id"])) {
|
|
header("Location: auth/login.php");
|
|
exit();
|
|
}
|
|
|
|
require_once 'db/config.php';
|
|
|
|
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
|
|
|
|
if ($id === 0) {
|
|
$_SESSION['error_message'] = "ID de ciudad no válido.";
|
|
header("Location: ciudades.php");
|
|
exit();
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
// Check if the city is being used in stock_por_ciudad
|
|
$check_sql = "SELECT COUNT(*) FROM stock_por_ciudad WHERE ciudad_id = :id";
|
|
$check_stmt = $pdo->prepare($check_sql);
|
|
$check_stmt->bindParam(':id', $id, PDO::PARAM_INT);
|
|
$check_stmt->execute();
|
|
$usage_count = $check_stmt->fetchColumn();
|
|
|
|
if ($usage_count > 0) {
|
|
$_SESSION['error_message'] = "No se puede eliminar la ciudad porque tiene stock asociado. Por favor, reasigne o elimine el stock primero.";
|
|
header("Location: ciudades.php");
|
|
exit();
|
|
}
|
|
|
|
// If not in use, proceed with deletion
|
|
$sql = "DELETE FROM ciudades WHERE id = :id";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
|
|
|
|
if ($stmt->execute()) {
|
|
$_SESSION['success_message'] = "Ciudad eliminada exitosamente.";
|
|
} else {
|
|
$_SESSION['error_message'] = "Error al eliminar la ciudad.";
|
|
}
|
|
} catch (PDOException $e) {
|
|
$_SESSION['error_message'] = "Error de base de datos: " . $e->getMessage();
|
|
}
|
|
|
|
header("Location: ciudades.php");
|
|
exit();
|
|
?>
|