35330-vm/index.php
Flatlogic Bot a0c63edc92 versao 2
2025-10-29 14:04:14 +00:00

172 lines
7.4 KiB
PHP

<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
require_once __DIR__ . '/db/config.php';
$user_id = $_SESSION['user_id'];
$pdo = db();
$current_month_date = date('Y-m-01');
// --- Coleta de Dados para o Dashboard ---
// 1. Despesas do Mês Atual
$stmt_expenses = $pdo->prepare(
"SELECT category, SUM(amount) as total_spent FROM expenses
WHERE user_id = :user_id AND MONTH(expense_date) = MONTH(CURRENT_DATE()) AND YEAR(expense_date) = YEAR(CURRENT_DATE())
GROUP BY category"
);
$stmt_expenses->execute(['user_id' => $user_id]);
$monthly_expenses = $stmt_expenses->fetchAll(PDO::FETCH_KEY_PAIR);
// 2. Orçamentos do Mês Atual
$stmt_budgets = $pdo->prepare("SELECT category, amount FROM budgets WHERE user_id = :user_id AND budget_month = :budget_month");
$stmt_budgets->execute(['user_id' => $user_id, 'budget_month' => $current_month_date]);
$monthly_budgets = $stmt_budgets->fetchAll(PDO::FETCH_KEY_PAIR);
// 3. Consolidar dados de Orçamento e Despesas
$categories = ['Alimentação', 'Transporte', 'Moradia', 'Lazer', 'Saúde', 'Outros'];
$summary = [];
$total_spent_this_month = 0;
$total_budget_this_month = 0;
foreach ($categories as $category) {
$spent = $monthly_expenses[$category] ?? 0;
$budget = $monthly_budgets[$category] ?? 0;
$summary[$category] = [
'spent' => $spent,
'budget' => $budget,
'remaining' => $budget - $spent,
];
$total_spent_this_month += $spent;
$total_budget_this_month += $budget;
}
// 4. Últimas 5 despesas
$stmt_recent = $pdo->prepare("SELECT * FROM expenses WHERE user_id = :user_id ORDER BY expense_date DESC LIMIT 5");
$stmt_recent->execute(['user_id' => $user_id]);
$recent_expenses = $stmt_recent->fetchAll(PDO::FETCH_ASSOC);
// Função para determinar a classe da barra de progresso
function get_progress_bar_class($percentage) {
if ($percentage > 100) return 'bg-danger';
if ($percentage > 75) return 'bg-warning';
return 'bg-success';
}
include __DIR__ . '/includes/header.php';
?>
<main class="container mt-4">
<h1 class="mb-4">Dashboard de <?php echo date('F', strtotime('now')); ?></h1>
<!-- Resumo Geral do Mês -->
<div class="card mb-4">
<div class="card-body">
<div class="row align-items-center">
<div class="col-md-6">
<h4>Visão Geral do Mês</h4>
<p class="text-muted">Gastos vs. Orçamento Total</p>
<span class="fs-3 fw-bold">R$ <?php echo number_format($total_spent_this_month, 2, ',', '.'); ?></span>
<span class="fs-5 text-muted">/ R$ <?php echo number_format($total_budget_this_month, 2, ',', '.'); ?></span>
</div>
<div class="col-md-6">
<?php
$overall_percentage = ($total_budget_this_month > 0) ? ($total_spent_this_month / $total_budget_this_month) * 100 : 0;
$progress_class = get_progress_bar_class($overall_percentage);
?>
<div class="progress" style="height: 25px;">
<div class="progress-bar <?php echo $progress_class; ?>" role="progressbar"
style="width: <?php echo min(100, $overall_percentage); ?>%;"
aria-valuenow="<?php echo $overall_percentage; ?>" aria-valuemin="0" aria-valuemax="100">
<?php echo number_format($overall_percentage, 1); ?>%
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Resumo por Categoria -->
<div class="card mb-4">
<div class="card-header">
<h5 class="mb-0">Resumo do Orçamento por Categoria</h5>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>Categoria</th>
<th class="text-end">Gasto</th>
<th class="text-end">Orçamento</th>
<th class="text-end">Restante</th>
<th style="width: 25%;">Progresso</th>
</tr>
</thead>
<tbody>
<?php foreach ($summary as $category => $data): ?>
<tr>
<td><?php echo htmlspecialchars($category); ?></td>
<td class="text-end">R$ <?php echo number_format($data['spent'], 2, ',', '.'); ?></td>
<td class="text-end">R$ <?php echo number_format($data['budget'], 2, ',', '.'); ?></td>
<td class="text-end <?php echo ($data['remaining'] < 0) ? 'text-danger' : 'text-success'; ?>">
R$ <?php echo number_format($data['remaining'], 2, ',', '.'); ?>
</td>
<td>
<?php
$percentage = ($data['budget'] > 0) ? ($data['spent'] / $data['budget']) * 100 : 0;
$progress_class = get_progress_bar_class($percentage);
?>
<div class="progress">
<div class="progress-bar <?php echo $progress_class; ?>" role="progressbar"
style="width: <?php echo min(100, $percentage); ?>%;"
aria-valuenow="<?php echo $percentage; ?>" aria-valuemin="0" aria-valuemax="100">
</div>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<!-- Despesas Recentes -->
<div class="card">
<div class="card-header">
<h5 class="mb-0">Despesas Recentes</h5>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table">
<tbody>
<?php if (empty($recent_expenses)): ?>
<tr><td class="text-center">Nenhuma despesa registrada este mês.</td></tr>
<?php else: ?>
<?php foreach ($recent_expenses as $expense): ?>
<tr>
<td><span class="badge bg-secondary me-2"><?php echo htmlspecialchars($expense['category']); ?></span> <?php echo htmlspecialchars($expense['description']); ?></td>
<td class="text-end">R$ <?php echo number_format($expense['amount'], 2, ',', '.'); ?></td>
<td class="text-end text-muted"><?php echo date('d/m/Y', strtotime($expense['expense_date'])); ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
<div class="text-end mt-2">
<a href="expenses.php">Ver todas as despesas &rarr;</a>
</div>
</div>
</div>
</main>
<?php include __DIR__ . '/includes/footer.php'; ?>