251 lines
12 KiB
PHP
251 lines
12 KiB
PHP
<?php
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
|
|
// Proteger a página
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
$client_id = $_SESSION['client_id'];
|
|
$user_id = $_SESSION['user_id'];
|
|
$pdo = db();
|
|
$error_message = '';
|
|
$success_message = '';
|
|
|
|
// Lógica para ADICIONAR nova despesa (POST)
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$description = $_POST['description'] ?? '';
|
|
$amount = $_POST['amount'] ?? '';
|
|
$category = $_POST['category'] ?? '';
|
|
$expense_date = $_POST['expense_date'] ?? '';
|
|
|
|
if (empty($description) || empty($amount) || empty($category) || empty($expense_date)) {
|
|
$error_message = 'Todos os campos são obrigatórios para adicionar uma despesa.';
|
|
} else {
|
|
try {
|
|
$sql = "INSERT INTO expenses (client_id, user_id, description, amount, category, expense_date) VALUES (:client_id, :user_id, :description, :amount, :category, :expense_date)";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute([
|
|
'client_id' => $client_id,
|
|
'user_id' => $user_id,
|
|
'description' => $description,
|
|
'amount' => $amount,
|
|
'category' => $category,
|
|
'expense_date' => $expense_date
|
|
]);
|
|
$_SESSION['success_message'] = 'Despesa registrada com sucesso!';
|
|
header('Location: expenses.php'); // Redirecionar para limpar o POST
|
|
exit;
|
|
} catch (PDOException $e) {
|
|
$error_message = 'Erro ao registrar a despesa: ' . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
|
|
// Lógica para FILTRAR e BUSCAR despesas (GET)
|
|
$filter_start_date = $_GET['start_date'] ?? '';
|
|
$filter_end_date = $_GET['end_date'] ?? '';
|
|
$filter_category = $_GET['category'] ?? '';
|
|
|
|
$sql = "SELECT * FROM expenses WHERE client_id = :client_id";
|
|
$params = ['client_id' => $client_id];
|
|
|
|
if ($filter_start_date) {
|
|
$sql .= " AND expense_date >= :start_date";
|
|
$params['start_date'] = $filter_start_date;
|
|
}
|
|
if ($filter_end_date) {
|
|
$sql .= " AND expense_date <= :end_date";
|
|
$params['end_date'] = $filter_end_date;
|
|
}
|
|
if ($filter_category) {
|
|
$sql .= " AND category = :category";
|
|
$params['category'] = $filter_category;
|
|
}
|
|
|
|
$sql .= " ORDER BY expense_date DESC";
|
|
|
|
$expenses = [];
|
|
$total_filtered_amount = 0;
|
|
try {
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute($params);
|
|
$expenses = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Calcular total dos itens filtrados
|
|
foreach ($expenses as $expense) {
|
|
$total_filtered_amount += $expense['amount'];
|
|
}
|
|
} catch (PDOException $e) {
|
|
$error_message = 'Erro ao buscar despesas: ' . $e->getMessage();
|
|
}
|
|
|
|
// Obter todas as categorias (macro áreas ativas) para os dropdowns
|
|
$stmt_categories = $pdo->prepare("SELECT nome, slug FROM macro_areas WHERE client_id = :client_id AND ativo = 1 ORDER BY nome ASC");
|
|
$stmt_categories->execute(['client_id' => $client_id]);
|
|
$categories = $stmt_categories->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
include __DIR__ . '/includes/header.php';
|
|
?>
|
|
|
|
<div class="container mt-4">
|
|
<div class="row">
|
|
<!-- Coluna para Adicionar Despesa -->
|
|
<div class="col-md-4">
|
|
<div class="card mb-4">
|
|
<div class="card-body">
|
|
<h4 class="card-title">Registrar Nova Despesa</h4>
|
|
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST' && $error_message): ?>
|
|
<div class="alert alert-danger"><?php echo htmlspecialchars($error_message); ?></div>
|
|
<?php endif; ?>
|
|
<form method="POST" action="expenses.php">
|
|
<!-- Campos do formulário de adição -->
|
|
<div class="mb-3">
|
|
<label for="description" class="form-label">Descrição</label>
|
|
<input type="text" class="form-control" id="description" name="description" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="amount" class="form-label">Valor (R$)</label>
|
|
<input type="number" step="0.01" class="form-control" id="amount" name="amount" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="category" class="form-label">Categoria</label>
|
|
<select class="form-select" id="category" name="category" required>
|
|
<option value="">Selecione...</option>
|
|
<?php foreach ($categories as $cat): ?>
|
|
<option value="<?php echo htmlspecialchars($cat['slug']); ?>"><?php echo htmlspecialchars($cat['nome']); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="expense_date" class="form-label">Data da Despesa</label>
|
|
<input type="date" class="form-control" id="expense_date" name="expense_date" required>
|
|
</div>
|
|
<div class="d-grid">
|
|
<button type="submit" class="btn btn-primary">Adicionar Despesa</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Coluna para Listar e Filtrar Despesas -->
|
|
<div class="col-md-8">
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<h4 class="card-title">Minhas Despesas</h4>
|
|
|
|
<!-- Mensagens de feedback -->
|
|
<?php
|
|
if (isset($_SESSION['success_message'])) {
|
|
echo '<div class="alert alert-success">' . htmlspecialchars($_SESSION['success_message']) . '</div>';
|
|
unset($_SESSION['success_message']);
|
|
}
|
|
if (isset($_SESSION['error_message'])) {
|
|
echo '<div class="alert alert-danger">' . htmlspecialchars($_SESSION['error_message']) . '</div>';
|
|
unset($_SESSION['error_message']);
|
|
}
|
|
?>
|
|
|
|
<!-- Formulário de Filtro -->
|
|
<form method="GET" action="expenses.php" class="mb-4 p-3 bg-light rounded">
|
|
<div class="row g-3 align-items-end">
|
|
<div class="col-md-4">
|
|
<label for="start_date" class="form-label">De</label>
|
|
<input type="date" class="form-control" id="start_date" name="start_date" value="<?php echo htmlspecialchars($filter_start_date); ?>">
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label for="end_date" class="form-label">Até</label>
|
|
<input type="date" class="form-control" id="end_date" name="end_date" value="<?php echo htmlspecialchars($filter_end_date); ?>">
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label for="filter_category" class="form-label">Categoria</label>
|
|
<select class="form-select" id="filter_category" name="category">
|
|
<option value="">Todas</option>
|
|
<?php foreach ($categories as $cat): ?>
|
|
<option value="<?php echo htmlspecialchars($cat['slug']); ?>" <?php echo ($filter_category === $cat['slug']) ? 'selected' : ''; ?>><?php echo htmlspecialchars($cat['nome']); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-12 d-flex justify-content-end mt-3">
|
|
<a href="expenses.php" class="btn btn-secondary me-2">Limpar</a>
|
|
<button type="submit" class="btn btn-primary me-2">Filtrar</button>
|
|
<a href="#" id="export-csv" class="btn btn-success"><i class="bi bi-download me-2"></i>Exportar CSV</a>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
|
|
<!-- Resumo dos Filtros -->
|
|
<div class="alert alert-info">
|
|
<strong>Total Filtrado:</strong> R$ <?php echo number_format($total_filtered_amount, 2, ',', '.'); ?>
|
|
</div>
|
|
|
|
<!-- Tabela de Despesas -->
|
|
<div class="table-responsive">
|
|
<table class="table table-striped">
|
|
<thead>
|
|
<tr>
|
|
<th>Descrição</th>
|
|
<th>Valor</th>
|
|
<th>Categoria</th>
|
|
<th>Data</th>
|
|
<th>Ações</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($expenses)): ?>
|
|
<tr>
|
|
<td colspan="5" class="text-center">Nenhuma despesa encontrada para os filtros aplicados.</td>
|
|
</tr>
|
|
<?php else: ?>
|
|
<?php foreach ($expenses as $expense): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($expense['description']); ?></td>
|
|
<td>R$ <?php echo number_format($expense['amount'], 2, ',', '.'); ?></td>
|
|
<td><?php echo htmlspecialchars($expense['category']); ?></td>
|
|
<td><?php echo date('d/m/Y', strtotime($expense['expense_date'])); ?></td>
|
|
<td>
|
|
<a href="edit_expense.php?id=<?php echo $expense['id']; ?>" class="btn btn-sm btn-outline-primary"><i class="bi bi-pencil-sm"></i></a>
|
|
<a href="delete_expense.php?id=<?php echo $expense['id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Tem certeza que deseja excluir esta despesa?');"><i class="bi bi-trash-sm"></i></a>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
const exportBtn = document.getElementById('export-csv');
|
|
if (exportBtn) {
|
|
exportBtn.addEventListener('click', function(e) {
|
|
e.preventDefault();
|
|
|
|
const startDate = document.getElementById('start_date').value;
|
|
const endDate = document.getElementById('end_date').value;
|
|
const category = document.getElementById('filter_category').value;
|
|
|
|
const params = new URLSearchParams();
|
|
if (startDate) params.append('start_date', startDate);
|
|
if (endDate) params.append('end_date', endDate);
|
|
if (category) params.append('category', category);
|
|
|
|
const exportUrl = 'export.php?' + params.toString();
|
|
window.location.href = exportUrl;
|
|
});
|
|
}
|
|
});
|
|
</script>
|
|
|
|
<?php include __DIR__ . '/includes/footer.php'; ?>
|