35330-vm/delete_macro_area.php
Flatlogic Bot e9a7ca1d46 versao 16
2025-10-29 17:01:16 +00:00

44 lines
1.4 KiB
PHP

<?php
require_once 'includes/session.php';
require_once 'db/config.php';
// Check if ID is provided
if (isset($_GET['id'])) {
$id = $_GET['id'];
$pdo = db();
// First, find the macro area to get its slug
$stmt = $pdo->prepare("SELECT slug FROM macro_areas WHERE id = ?");
$stmt->execute([$id]);
$macro_area = $stmt->fetch();
// If the macro area exists, proceed with checks and deletion
if ($macro_area) {
$slug = $macro_area['slug'];
// Check for dependent expenses
$stmt = $pdo->prepare("SELECT COUNT(*) FROM expenses WHERE category = ?");
$stmt->execute([$slug]);
$expense_count = $stmt->fetchColumn();
if ($expense_count > 0) {
// Dependencies found, set error message and redirect
$_SESSION['error_message'] = "Não é possível excluir a macro área. Existem {$expense_count} despesas associadas.";
} else {
// No dependencies, proceed with deletion
$stmt = $pdo->prepare("DELETE FROM macro_areas WHERE id = ?");
$stmt->execute([$id]);
$_SESSION['success_message'] = "Macro área excluída com sucesso.";
}
}
// If the macro area was not found, we just redirect without any message.
} else {
// ID not specified in URL
$_SESSION['error_message'] = "ID da macro área não especificado.";
}
// Redirect back to the list in all cases
header('Location: macro_areas.php');
exit;
?>