42 lines
1.1 KiB
PHP
42 lines
1.1 KiB
PHP
<?php
|
|
require_once 'includes/session.php';
|
|
require_once 'db/config.php';
|
|
|
|
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
|
|
header('Location: macro_areas.php');
|
|
exit;
|
|
}
|
|
|
|
$id = $_GET['id'];
|
|
$pdo = db();
|
|
|
|
// Get the slug of the macro area
|
|
$stmt = $pdo->prepare('SELECT slug FROM macro_areas WHERE id = ?');
|
|
$stmt->execute([$id]);
|
|
$macro_area = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$macro_area) {
|
|
$_SESSION['error_message'] = 'Macro área não encontrada.';
|
|
header('Location: macro_areas.php');
|
|
exit;
|
|
}
|
|
|
|
$slug = $macro_area['slug'];
|
|
|
|
// Check for associated expenses (categories)
|
|
$stmt = $pdo->prepare('SELECT COUNT(*) FROM expenses WHERE category = ?');
|
|
$stmt->execute([$slug]);
|
|
$count = $stmt->fetchColumn();
|
|
|
|
if ($count > 0) {
|
|
$_SESSION['error_message'] = 'Não é possível excluir a macro área, pois existem despesas associadas a ela.';
|
|
} else {
|
|
// Delete the macro area
|
|
$stmt = $pdo->prepare('DELETE FROM macro_areas WHERE id = ?');
|
|
$stmt->execute([$id]);
|
|
$_SESSION['success_message'] = 'Macro área excluída com sucesso.';
|
|
}
|
|
|
|
header('Location: macro_areas.php');
|
|
exit;
|