34628-vm/financial.php
2025-10-03 14:17:07 +00:00

139 lines
4.3 KiB
PHP

<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
require_once 'db/config.php';
include 'includes/header.php';
include 'includes/sidebar.php';
// Fetch transactions from the database
try {
$pdo = db();
$stmt = $pdo->query('SELECT ft.*, fc.name as category_name FROM financial_transactions ft JOIN financial_categories fc ON ft.category_id = fc.id ORDER BY ft.date DESC');
$transactions = $stmt->fetchAll();
// Calculate summary
$total_income = 0;
$total_expenses = 0;
foreach ($transactions as $t) {
if ($t['type'] == 'receita') {
$total_income += $t['amount'];
} else {
$total_expenses += $t['amount'];
}
}
$net_result = $total_income - $total_expenses;
} catch (PDOException $e) {
$transactions = [];
$error = "Erro ao buscar transações: " . $e->getMessage();
}
?>
<header class="top-bar">
<h1>Controle Financeiro</h1>
</header>
<main class="content">
<div class="card">
<div class="card-header">
Resumo Financeiro
</div>
<div class="financial-summary">
<div class="summary-item income">
<h4>Receitas</h4>
<p>R$ <?php echo number_format($total_income, 2, ',', '.'); ?></p>
</div>
<div class="summary-item expense">
<h4>Despesas</h4>
<p>R$ <?php echo number_format($total_expenses, 2, ',', '.'); ?></p>
</div>
<div class="summary-item net-result">
<h4>Resultado Líquido</h4>
<p>R$ <?php echo number_format($net_result, 2, ',', '.'); ?></p>
</div>
</div>
</div>
<div class="card">
<div class="card-header">
<span>Histórico de Transações</span>
<a href="add_transaction.php" class="btn" style="float: right; background-color: #3498db; color: white; text-decoration: none; padding: 5px 10px; border-radius: 4px;">Adicionar Transação</a>
</div>
<?php if (isset($error)): ?>
<p class="error"><?php echo $error; ?></p>
<?php endif; ?>
<table>
<thead>
<tr>
<th>Data</th>
<th>Descrição</th>
<th>Tipo</th>
<th>Categoria</th>
<th>Valor</th>
</tr>
</thead>
<tbody>
<?php if (empty($transactions)): ?>
<tr>
<td colspan="5">Nenhuma transação encontrada.</td>
</tr>
<?php else: ?>
<?php foreach ($transactions as $transaction): ?>
<tr>
<td><?php echo htmlspecialchars(date('d/m/Y', strtotime($transaction['date']))); ?></td>
<td><?php echo htmlspecialchars($transaction['description']); ?></td>
<td><?php echo htmlspecialchars(ucfirst($transaction['type'])); ?></td>
<td><?php echo htmlspecialchars($transaction['category_name']); ?></td>
<td style="color: <?php echo $transaction['type'] == 'receita' ? 'green' : 'red'; ?>;">
R$ <?php echo number_format($transaction['amount'], 2, ',', '.'); ?>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</main>
<style>
.financial-summary {
display: flex;
justify-content: space-around;
text-align: center;
margin-bottom: 20px;
}
.summary-item h4 {
margin-bottom: 5px;
}
.summary-item p {
font-size: 1.5rem;
font-weight: bold;
margin: 0;
}
.income p { color: green; }
.expense p { color: red; }
.net-result p { color: <?php echo $net_result >= 0 ? 'blue' : 'red'; ?>; }
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
</style>
<?php include 'includes/footer.php'; ?>