36293-vm/export_billing.php
2025-11-26 09:02:25 +00:00

47 lines
1.3 KiB
PHP

<?php
require_once __DIR__ . '/db/config.php';
$projectId = $_GET['id'] ?? null;
if (!$projectId) {
die("Project ID is required.");
}
try {
$pdo = db();
$stmt = $pdo->prepare("SELECT name FROM projects WHERE id = :id");
$stmt->execute([':id' => $projectId]);
$project = $stmt->fetch(PDO::FETCH_ASSOC);
$projectName = $project ? $project['name'] : 'project';
$stmt = $pdo->prepare("SELECT month, amount FROM billingMonthly WHERE projectId = :projectId ORDER BY month");
$stmt->execute([':projectId' => $projectId]);
$billingData = $stmt->fetchAll(PDO::FETCH_ASSOC);
$filename = "billing_" . strtolower(str_replace(' ', '_', $projectName)) . ".csv";
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '"');
$output = fopen('php://output', 'w');
// Header row
$header = ['Billing'];
foreach ($billingData as $row) {
$header[] = date("M Y", strtotime($row['month']));
}
fputcsv($output, $header);
// Data row
$data = ['Billing'];
foreach ($billingData as $row) {
$data[] = $row['amount'];
}
fputcsv($output, $data);
fclose($output);
exit();
} catch (PDOException $e) {
die("Database error: " . $e->getMessage());
}