60 lines
1.4 KiB
PHP
60 lines
1.4 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
// Check if user is logged in
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header('Location: login.php');
|
|
exit();
|
|
}
|
|
|
|
$client_id = $_SESSION['client_id'];
|
|
$pdo = db();
|
|
|
|
// Build query with filters
|
|
$sql = "SELECT * FROM expenses WHERE client_id = :client_id";
|
|
$params = ['client_id' => $client_id];
|
|
|
|
if (!empty($_GET['start_date'])) {
|
|
$sql .= " AND expense_date >= :start_date";
|
|
$params['start_date'] = $_GET['start_date'];
|
|
}
|
|
if (!empty($_GET['end_date'])) {
|
|
$sql .= " AND expense_date <= :end_date";
|
|
$params['end_date'] = $_GET['end_date'];
|
|
}
|
|
if (!empty($_GET['category'])) {
|
|
$sql .= " AND category = :category";
|
|
$params['category'] = $_GET['category'];
|
|
}
|
|
|
|
$sql .= " ORDER BY expense_date DESC";
|
|
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute($params);
|
|
$expenses = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Set headers for CSV download
|
|
header('Content-Type: text/csv; charset=utf-8');
|
|
header('Content-Disposition: attachment; filename="despesas.csv"');
|
|
|
|
// Open output stream
|
|
$output = fopen('php://output', 'w');
|
|
|
|
// Write CSV header
|
|
fputcsv($output, ['Data', 'Descricao', 'Valor', 'Categoria']);
|
|
|
|
// Write data
|
|
if ($expenses) {
|
|
foreach ($expenses as $expense) {
|
|
fputcsv($output, [
|
|
$expense['expense_date'],
|
|
$expense['description'],
|
|
$expense['amount'],
|
|
$expense['category']
|
|
]);
|
|
}
|
|
}
|
|
|
|
fclose($output);
|
|
exit(); |