versao 16
This commit is contained in:
parent
6e8e69c52f
commit
e9a7ca1d46
@ -2,40 +2,43 @@
|
|||||||
require_once 'includes/session.php';
|
require_once 'includes/session.php';
|
||||||
require_once 'db/config.php';
|
require_once 'db/config.php';
|
||||||
|
|
||||||
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
|
// Check if ID is provided
|
||||||
header('Location: macro_areas.php');
|
if (isset($_GET['id'])) {
|
||||||
exit;
|
$id = $_GET['id'];
|
||||||
}
|
$pdo = db();
|
||||||
|
|
||||||
$id = $_GET['id'];
|
// First, find the macro area to get its slug
|
||||||
$pdo = db();
|
$stmt = $pdo->prepare("SELECT slug FROM macro_areas WHERE id = ?");
|
||||||
|
|
||||||
// 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]);
|
$stmt->execute([$id]);
|
||||||
$_SESSION['success_message'] = 'Macro área excluída com sucesso.';
|
$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');
|
header('Location: macro_areas.php');
|
||||||
exit;
|
exit;
|
||||||
|
?>
|
||||||
@ -85,7 +85,7 @@ $macro_areas = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<?php endforeach; ?>
|
<?php endforeach; ?>
|
||||||
<?php if (empty($macro_areas)):
|
<?php if (empty($macro_areas)): ?>
|
||||||
<tr>
|
<tr>
|
||||||
<td colspan="4" class="text-center">Nenhuma macro área encontrada.</td>
|
<td colspan="4" class="text-center">Nenhuma macro área encontrada.</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user