-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_returns_csv.php
52 lines (41 loc) · 1.46 KB
/
export_returns_csv.php
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
// Start a new session
require('layouts/session.php');
$user_email = $_SESSION['email']; // User's email
// Database connection
include('conn.php');
// Establish the connection to the user's database
$conn = connectMainDB();
// Set headers for CSV download
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=sales_returns_report.csv');
// Create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
// Output the column headings
fputcsv($output, ['Customer', 'Reference', 'Status', 'Grand Total', 'Amount', 'Date', 'Generated On']);
// Get current timestamp
$currentTimestamp = date('Y-m-d H:i:s a');
// Fetch sales return data
$query = "SELECT date, customer, reference, status, grand_total_returned, amount_returned
FROM sales_return
WHERE user_email = '$user_email'";
$result = $conn->query($query);
if ($result->num_rows > 0) {
// Fetch data and write to the CSV
while ($row = $result->fetch_assoc()) {
fputcsv($output, [
$row['customer'],
$row['reference'],
$row['status'],
$row['grand_total_returned'],
$row['amount_returned'],
$row['date'],
$currentTimestamp // Adding the timestamp to each row
]);
}
} else {
// If no data, add a single line indicating that
fputcsv($output, ['No records found.']);
}
// Close the output stream
fclose($output);