diff --git a/delete_macro_area.php b/delete_macro_area.php new file mode 100644 index 0000000..243a9b2 --- /dev/null +++ b/delete_macro_area.php @@ -0,0 +1,41 @@ +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; diff --git a/macro_area_form.php b/macro_area_form.php index 5c0399b..740e625 100644 --- a/macro_area_form.php +++ b/macro_area_form.php @@ -85,7 +85,7 @@ include_once 'includes/header.php';
-
Detalhes da Macro Área
+
Detalhes da Macro Área
diff --git a/macro_areas.php b/macro_areas.php index 08e7e9b..3f5461f 100644 --- a/macro_areas.php +++ b/macro_areas.php @@ -5,16 +5,6 @@ include_once 'includes/header.php'; $pdo = db(); -// Handle deletion -if (isset($_GET['delete'])) { - $id = $_GET['delete']; - // TODO: Add check to see if there are expenses associated before deleting - $stmt = $pdo->prepare('DELETE FROM macro_areas WHERE id = ?'); - $stmt->execute([$id]); - header("Location: macro_areas.php"); - exit; -} - $stmt = $pdo->query('SELECT * FROM macro_areas ORDER BY nome ASC'); $macro_areas = $stmt->fetchAll(PDO::FETCH_ASSOC); @@ -24,7 +14,7 @@ $macro_areas = $stmt->fetchAll(PDO::FETCH_ASSOC);

Macro Áreas

- @@ -35,10 +25,26 @@ $macro_areas = $stmt->fetchAll(PDO::FETCH_ASSOC);
+ + + + + + + + + +
-
- Lista de Macro Áreas +
+ Lista de Macro Áreas
@@ -70,16 +76,16 @@ $macro_areas = $stmt->fetchAll(PDO::FETCH_ASSOC); - + Nenhuma macro área encontrada. @@ -150,6 +156,14 @@ document.addEventListener('DOMContentLoaded', function () { +