-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransaction.php
More file actions
215 lines (205 loc) · 7.02 KB
/
Copy pathtransaction.php
File metadata and controls
215 lines (205 loc) · 7.02 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
session_start();
if (!isset($_SESSION["user"]) || $_SESSION["role"] !== "admin") {
header("Location: index.php");
exit();
}
try {
$dbPath = __DIR__ . DIRECTORY_SEPARATOR . "inventory.db";
$db = new PDO("sqlite:$dbPath");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Database connection failed: " . $e->getMessage());
}
// Get filter parameter
$filter = $_GET['filter'] ?? 'day';
// Prepare SQL based on filter
switch($filter) {
case 'week':
$dateFormat = '%Y-W%W';
$groupBy = "strftime('%Y-W%W', sale_date)";
$labelFormat = "Week";
break;
case 'month':
$dateFormat = '%Y-%m';
$groupBy = "strftime('%Y-%m', sale_date)";
$labelFormat = "Month";
break;
case 'year':
$dateFormat = '%Y';
$groupBy = "strftime('%Y', sale_date)";
$labelFormat = "Year";
break;
default: // day
$dateFormat = '%Y-%m-%d';
$groupBy = "DATE(sale_date)";
$labelFormat = "Day";
}
// Get sales data
$salesQuery = "
SELECT
$groupBy as period,
SUM(total_amount) as revenue,
COUNT(*) as transactions
FROM sales
WHERE status = 'completed'
GROUP BY period
ORDER BY period DESC
LIMIT 30
";
$salesData = $db->query($salesQuery)->fetchAll(PDO::FETCH_ASSOC);
$salesData = array_reverse($salesData);
// Get top products
$topProductsQuery = "
SELECT
product_name,
SUM(quantity) as units_sold,
SUM(subtotal) as revenue
FROM sales_items
GROUP BY product_name
ORDER BY units_sold DESC
LIMIT 10
";
$topProducts = $db->query($topProductsQuery)->fetchAll(PDO::FETCH_ASSOC);
// Get summary stats
$statsQuery = "
SELECT
SUM(total_amount) as total_revenue,
COUNT(*) as total_transactions,
AVG(total_amount) as avg_transaction
FROM sales
WHERE status = 'completed'
";
$stats = $db->query($statsQuery)->fetch(PDO::FETCH_ASSOC);
// Prepare data for charts
$labels = array_column($salesData, 'period');
$revenues = array_column($salesData, 'revenue');
$transactions = array_column($salesData, 'transactions');
$productLabels = array_column($topProducts, 'product_name');
$productSales = array_column($topProducts, 'units_sold');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sales Report</title>
<link rel="stylesheet" href="styles/report.css">
<link rel="icon" type="image/svg+xml" href="assets/logo.svg">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<div class="report-container">
<img src="assets/logo.svg" class="report-logo">
<div class="transactions-section">
<div class="header-wrapper">
<h2>Recent Transactions</h2>
<button class="back-btn"><a href="report.php">↩</a></button>
</div>
<div class="table-wrapper">
<table>
<thead>
<tr>
<th>Transaction ID</th>
<th>Date</th>
<th>Total</th>
<th>Cashier</th>
</tr>
</thead>
<tbody>
<?php
$recentQuery = "SELECT * FROM sales ORDER BY sale_date DESC LIMIT 20";
$recent = $db->query($recentQuery)->fetchAll(PDO::FETCH_ASSOC);
foreach($recent as $sale):
?>
<tr>
<td><?php echo htmlspecialchars($sale['transaction_id']); ?></td>
<td><?php echo date('M d, Y H:i', strtotime($sale['sale_date'])); ?></td>
<td>₱<?php echo number_format($sale['total_amount'], 2); ?></td>
<td><?php echo htmlspecialchars($sale['cashier_name']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<script>
// Revenue Line Chart
const revenueCtx = document.getElementById('revenueChart').getContext('2d');
new Chart(revenueCtx, {
type: 'line',
data: {
labels: <?php echo json_encode($labels); ?>,
datasets: [{
label: 'Revenue (₱)',
data: <?php echo json_encode($revenues); ?>,
borderColor: '#67a6ff',
backgroundColor: 'rgba(103, 166, 255, 0.1)',
borderWidth: 3,
fill: true,
tension: 0.4
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
labels: { color: 'white', font: { size: 14 } }
}
},
scales: {
y: {
beginAtZero: true,
ticks: { color: 'white' },
grid: { color: 'rgba(255, 255, 255, 0.1)' }
},
x: {
ticks: { color: 'white' },
grid: { color: 'rgba(255, 255, 255, 0.1)' }
}
}
}
});
// Products Bar Chart
const productsCtx = document.getElementById('productsChart').getContext('2d');
new Chart(productsCtx, {
type: 'bar',
data: {
labels: <?php echo json_encode($productLabels); ?>,
datasets: [{
label: 'Units Sold',
data: <?php echo json_encode($productSales); ?>,
backgroundColor: '#67a6ff',
borderColor: '#0466c8',
borderWidth: 1
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
labels: { color: 'white', font: { size: 14 } }
}
},
scales: {
y: {
beginAtZero: true,
ticks: { color: 'white' },
grid: { color: 'rgba(255, 255, 255, 0.1)' }
},
x: {
ticks: { color: 'white' },
grid: { color: 'rgba(255, 255, 255, 0.1)' }
}
}
}
});
// Print Report Function
function printReport() {
window.print();
}
</script>
</body>
</html>