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'; ?>

Dashboard de

Visão Geral do Mês

Gastos vs. Orçamento Total

R$ / R$
0) ? ($total_spent_this_month / $total_budget_this_month) * 100 : 0; $progress_class = get_progress_bar_class($overall_percentage); ?>
%
Resumo do Orçamento por Categoria
$data): ?>
Categoria Gasto Orçamento Restante Progresso
R$ R$ R$ 0) ? ($data['spent'] / $data['budget']) * 100 : 0; $progress_class = get_progress_bar_class($percentage); ?>
Despesas Recentes
Nenhuma despesa registrada este mês.
R$