51 lines
1.4 KiB
PHP
51 lines
1.4 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'];
|
|
$totalBilling = 0;
|
|
foreach ($billingData as $row) {
|
|
$header[] = date("M Y", strtotime($row['month']));
|
|
$totalBilling += $row['amount'];
|
|
}
|
|
$header[] = 'Total';
|
|
fputcsv($output, $header);
|
|
|
|
// Data row
|
|
$data = ['Billing'];
|
|
foreach ($billingData as $row) {
|
|
$data[] = $row['amount'];
|
|
}
|
|
$data[] = $totalBilling;
|
|
fputcsv($output, $data);
|
|
|
|
fclose($output);
|
|
exit();
|
|
|
|
} catch (PDOException $e) {
|
|
die("Database error: " . $e->getMessage());
|
|
}
|