-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.php
More file actions
243 lines (226 loc) · 7.74 KB
/
Copy pathreport.php
File metadata and controls
243 lines (226 loc) · 7.74 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?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="report-header">
<h1>Sales Analytics Report</h1>
<div class="header-actions">
<a href="dashboard.php" class="btn-secondary">Dashboard</a>
<a href="pos.php" class="btn-secondary">POS</a>
<a href="inventory.php" class="btn-secondary">Inventory</a>
<a href="logout.php" class="btn-logout">Logout</a>
</div>
</div>
<!-- Filter Buttons -->
<div class="filter-section">
<h3>Filter by:</h3>
<div class="filter-buttons">
<a href="?filter=day" class="filter-btn <?php echo $filter === 'day' ? 'active' : ''; ?>">Day</a>
<a href="?filter=week" class="filter-btn <?php echo $filter === 'week' ? 'active' : ''; ?>">Week</a>
<a href="?filter=month" class="filter-btn <?php echo $filter === 'month' ? 'active' : ''; ?>">Month</a>
<a href="?filter=year" class="filter-btn <?php echo $filter === 'year' ? 'active' : ''; ?>">Year</a>
</div>
<div class="print-transac">
<button onclick="" class="transac-btn">
<a href="transaction.php">Transaction Report</a>
</button>
<button onclick="printReport()" class="print-btn">
<span>📄</span> Print / Export PDF
</button>
</div>
</div>
<!-- Summary Cards -->
<div class="stats-grid">
<div class="stat-card">
<h3>Total Revenue</h3>
<p class="stat-value">₱<?php echo number_format($stats['total_revenue'], 2); ?></p>
</div>
<div class="stat-card">
<h3>Total Transactions</h3>
<p class="stat-value"><?php echo number_format($stats['total_transactions']); ?></p>
</div>
<div class="stat-card">
<h3>Average Sale</h3>
<p class="stat-value">₱<?php echo number_format($stats['avg_transaction'], 2); ?></p>
</div>
</div>
<!-- Charts -->
<div class="charts-grid">
<div class="chart-container">
<h2>Revenue Trend (<?php echo ucfirst($filter); ?>)</h2>
<canvas id="revenueChart"></canvas>
</div>
<div class="chart-container">
<h2>Top 10 Products</h2>
<canvas id="productsChart"></canvas>
</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>