-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_sales_csv.php
More file actions
36 lines (29 loc) · 975 Bytes
/
Copy pathexport_sales_csv.php
File metadata and controls
36 lines (29 loc) · 975 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
session_start();
include 'db.php';
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
// Set headers for CSV download
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=sales_report.csv');
// Open file pointer
$output = fopen('php://output', 'w');
// Write column headers
fputcsv($output, ['Product Name', 'Total Sold', 'Total Revenue (₹)']);
// Fetch Sales Data
$sales_sql = "SELECT p.name, SUM(o.quantity) AS total_sold, SUM(o.total_price) AS total_revenue
FROM orders o
JOIN products p ON o.product_id = p.id
WHERE o.status = 'Completed'
GROUP BY p.id";
$sales_result = $conn->query($sales_sql);
// Write rows to CSV
while ($row = $sales_result->fetch_assoc()) {
fputcsv($output, [$row['name'], $row['total_sold'], number_format($row['total_revenue'], 2)]);
}
// Close output stream
fclose($output);
exit();
?>