-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsales-analytics.php
641 lines (554 loc) · 22.9 KB
/
sales-analytics.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
<?php
session_start();
include('config.php');
try {
$connection = new mysqli($hostname, $username, $password, $database);
if ($connection->connect_error) {
throw new Exception("Error: " . $connection->connect_error);
}
} catch (Exception $e) {
exit($e->getMessage());
}
$Username = $_SESSION['Username'];
// Fetch category names and IDs
$query = "SELECT id_category, categoryname FROM category";
$result = $connection->query($query);
$categoryNames = array();
while ($row = $result->fetch_assoc()) {
$categoryNames[$row['id_category']] = $row['categoryname'];
}
// Fetch user records based on their Username and group by category
$query = "SELECT category.categoryname AS categoryname, GROUP_CONCAT(sales.Product) AS ProductNames,
SUM(sales.InventoryQty) AS TotalInventoryQty, SUM(sales.DailySalesQty) AS TotalDailySalesQty,
SUM(sales.MonthlySalesQty) AS TotalMonthlySalesQty, SUM(sales.WeeklySalesQty) AS TotalWeeklySalesQty,
SUM(sales.YearlySalesQty) AS TotalYearlySalesQty,
SUM(sales.AnnualRevenue) AS AnnualSales
FROM sales
INNER JOIN category ON sales.sales_categoryid = category.id_category
WHERE sales.Username = ?
GROUP BY categoryname";
$stmt = $connection->prepare($query);
$stmt->bind_param('s', $Username);
$stmt->execute();
$result = $stmt->get_result();
// Fetch the data and store it in an associative array
$data = array();
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
// Fetch and store the sales data
$query = "SELECT
s.Product,
c.categoryname,
SUM(s.DailySalesQty) AS DailySalesQty,
SUM(s.WeeklySalesQty) AS WeeklySalesQty,
SUM(s.MonthlySalesQty) AS MonthlySalesQty,
SUM(s.YearlySalesQty) AS YearlySalesQty,
SUM(s.InventoryQty) AS 'Total Stock'
FROM
sales AS s
INNER JOIN
category AS c ON s.sales_categoryid = c.id_category
WHERE
s.Username = ?
GROUP BY
s.Product, c.categoryname";
$stmt = $connection->prepare($query);
// Use bind_param to bind parameters safely
$stmt->bind_param('s', $Username);
// Execute the query
$stmt->execute();
// Get the result set
$result = $stmt->get_result();
// Initialize an array to store the fetched data
$salesData = array();
// Fetch data and store it in the array
while ($row = $result->fetch_assoc()) {
$salesData[] = $row;
}
$pieChartLabels = array();
$pieChartData = array();
// Create an associative array to store category-wise total revenue
$categoryTotalSales = array();
foreach ($data as $row) {
$category_name = $row['categoryname']; // Get the category name
$total_revenue = $row['AnnualSales'];
// If the category is not in the list of labels, add it
if (!in_array($category_name, $pieChartLabels)) {
$pieChartLabels[] = $category_name;
}
// If the category is not in the list of category revenues, initialize it
if (!isset($categoryTotalSales[$category_name])) {
$categoryTotalSales[$category_name] = 0;
}
// Sum the TotalInventoryQty for products in the same category
$categoryTotalSales[$category_name] += $total_revenue;
}
// Get the total revenue for each category and maintain the original order
foreach ($pieChartLabels as $category_name) {
$pieChartData[] = $categoryTotalSales[$category_name];
}
$query2 = "SELECT category.categoryname AS categoryname, SUM(sales.YearlySalesQty) AS TotalYearlySalesQty
FROM sales
INNER JOIN category ON sales.sales_categoryid = category.id_category
WHERE sales.Username = ?
GROUP BY categoryname";
$stmt2 = $connection->prepare($query2);
$stmt2->bind_param('s', $Username);
$stmt2->execute();
$result2 = $stmt2->get_result();
$barChartLabels = array();
$barChartData = array();
// Create an associative array to store category-wise total sales quantity
$categoryDailySalesQty = array();
foreach ($result2 as $row) { // Use $result2 instead of $data to fetch the results
$category_name = $row['categoryname']; // Get the category name
$total_sales_qty = $row['TotalYearlySalesQty'];
// If the category is not in the list of labels, add it
if (!in_array($category_name, $barChartLabels)) {
$barChartLabels[] = $category_name;
}
// If the category is not in the list of category sales quantities, initialize it
if (!isset($categoryDailySalesQty[$category_name])) {
$categoryDailySalesQty[$category_name] = 0;
}
// Sum the DailySalesQty for products in the same category
$categoryDailySalesQty[$category_name] += $total_sales_qty;
}
// Get the total sales quantity for each category and maintain the original order
foreach ($barChartLabels as $category_name) {
$barChartData[] = $categoryDailySalesQty[$category_name];
}
$connection->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Sales - Analytics</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:400,700">
<!-- https://fonts.google.com/specimen/Roboto -->
<link rel="stylesheet" href="http://localhost/WEB/css/fontawesome.min.css">
<link rel="stylesheet" href="http://localhost/WEB/css/bootstrap.min.css">
<link rel="stylesheet" href="http://localhost/WEB/css/templatemo-style.css">
<link rel="icon" type="image/png" href="http://localhost/WEB/newlogo.png">
<style>
/* Add your custom styles here to handle overlapping issues */
.tm-content-row {
display: flex;
flex-wrap: wrap;
}
.tm-block-col {
flex: 1 1 100%;
margin: 0 10px 20px 0;
}
#pieChartContainer {
width: 100%;
text-align: center;
}
#pieChart {
max-width: 100%;
height: auto;
}
/* Block title */
.tm-content-row .tm-block-col .tm-block-title{
min-height:22px;
}
/* Block taller */
#home .tm-block-col:nth-child(1) .tm-block-taller{
transform:translatex(589px) translatey(50px);
position:relative;
top:21px;
left:156px !important;
}
/* Block taller */
#home .tm-content-row .tm-block-col:nth-child(1) .tm-block-taller{
width:82% !important;
right:auto !important;
top:-29px !important;
bottom:auto !important;
}
#home .tm-content-row .tm-block-col:nth-child(1) .tm-block-taller .tm-block-title{
bottom:auto !important;
top:-18px !important;
left:-18px;
}
/* Block taller */
#home .tm-block-col:nth-child(2) .tm-block-taller{
bottom:auto !important;
top:-23px !important;
transform:translatex(25px) translatey(-362px) !important;
}
/* Pie chart */
#pieChart{
width:100% !important;
transform:translatex(36px) translatey(-48px);
height:393px !important;
left:-35px;
}
/* Block taller */
#home .tm-content-row .tm-block-col:nth-child(2) .tm-block-taller{
right:auto !important;
left:-18px !important;
width:81% !important;
transform:translatex(28px) translatey(-431px) !important;
top:-23px !important;
bottom:auto !important;
}
/* Block */
#home .tm-block-col:nth-child(3) .tm-block{
transform:translatex(800px) translatey(-521px);
top:60px;
left:-5px;
width:473px !important;
height: 500px !important;
}
/* Bar chart */
#barChart{
transform:translatex(-30px) translatey(-26px);
width:116% !important;
height:80% !important;
position:relative;
left:-5px;
}
/* Block taller */
#home .tm-block-col:nth-child(4) .tm-block-taller{
width: 780px;
padding-right:30px;
z-index: 1;
transform:translatex(-5px) translatey(-1000px);
}
/* Block title */
#home .tm-block .tm-block-title{
color:#221f1f;
position:relative;
top:-1px !important;
text-align:center;
}
/* Block title */
#home .tm-block-taller .tm-block-title{
text-align:center;
}
/* Import Google Fonts */
@import url("//fonts.googleapis.com/css2?family=Roboto+Condensed:ital,wght@0,300;0,400;0,500;0,600;0,700;1,300;1,400;1,500;1,600;1,700&display=swap");
@import url("//fonts.googleapis.com/css2?family=TimesNewRoman:ital,wght@0,300;0,400;0,500;0,600;0,700;1,300;1,400;1,500;1,600;1,700&display=swap");
@import url("//fonts.googleapis.com/css2?family=Ribeye+Marrow:ital,wght@0,300;0,400;0,500;0,600;0,700;1,300;1,400;1,500;1,600;1,700&display=swap");
/* Font Icon */
#navbarSupportedContent .active i{
position:relative;
top:2px;
left:-3px;
}
/* Font Icon */
#navbarDropdown .fa-file-alt{
position:relative;
top:2px;
left:-3px;
}
/* Font Icon */
#navbarSupportedContent .nav-item .fa-shopping-cart{
position:relative;
top:2px;
left:-2px;
}
/* Font Icon */
#navbarSupportedContent .nav-item .fa-user{
position:relative;
top:2px;
left:-3px;
}
/* Heading */
.navbar a h1{
text-transform:capitalize;
font-style:normal;
font-weight:600;
font-size:26px;
font-family:'Ribeye Marrow', display;
color:#f5f3f3;
}
/* Nav link */
#navbarSupportedContent .nav-item .nav-link{
color:#121111;
font-size:20px;
font-weight:500;
font-size:15px;
font-family:'TimesNewRoman','Times New Roman',Times,Baskerville,Georgia,serif;
font-style:normal;
text-transform:none;
color:#e8e2e2;
}
/* Paragraph */
.tm-mt-small p{
position:relative;
top:-800px;
color:#070707 !important;
font-weight:500;
}
</style>
</head>
<body id="reportsPage">
<div class="" id="home">
<nav class="navbar navbar-expand-xl">
<div class="container h-100">
<a class="navbar-brand" href="sales-dashboard.php">
<h1 class="tm-site-title mb-0">Sales Overview</h1>
</a>
<button class="navbar-toggler ml-auto mr-0" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<i class="fas fa-bars tm-nav-icon"></i>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mx-auto h-200">
<li class="nav-item">
<a class="nav-link active" href="sales-analytics.php">
<i class="fas fa-tachometer-alt"></i>
Analytics
<span class="sr-only">(current)</span>
</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="report.php" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="far fa-file-alt"></i>
Reports
<span> <i class="fas fa-angle-down"></i></span>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="daily-sales.php">Daily</a>
<a class="dropdown-item" href="weekly-sales.php">Weekly</a>
<a class="dropdown-item" href="monthly-sales.php">Monthly</a>
<a class="dropdown-item" href="yearly-sales.php">Annual</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="sales-dashboard.php">
<i class="fas fa-shopping-cart"></i>
Sales
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="profile.php">
<i class="far fa-user"></i>
Accounts
</a>
</li>
</ul>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link d-block" href="loginpage.php">
<?php
$Username = $_SESSION['Username'];
echo "Welcome, $Username <b>Logout</b>";
?>
</a>
</li>
</ul>
</div>
</div>
</nav>
<!-- row -->
<div class="row tm-content-row">
<div class="col-sm-8 col-md-12 col-lg-6 col-xl-6 tm-block-col">
<div class="tm-bg-primary-dark tm-block tm-block-taller">
<h2 class="tm-block-title">Total Revenue by Category ($)</h2>
<div id="pieChartContainer">
<canvas id="pieChart" class="chartjs-render-monitor"></canvas>
</div>
</div>
</div>
<div class="col-8 tm-block-col">
<div class="tm-bg-primary-dark tm-block tm-block-taller tm-block-scroll">
<h2 class="tm-block-title">Products & Category</h2>
<table class="table">
<thead>
<tr>
<th scope="col">Category</th>
<th scope="col">Items</th>
<th scope="col">Product Names</th>
</tr>
</thead>
<tbody>
<?php
foreach ($data as $item) {
echo "<tr>";
echo "<td>" . $categoryNames[$item['categoryname']] . "</td>";
echo "<td>" . count(explode(",", $item['ProductNames'])) . "</td>";
echo "<td>" . $item['ProductNames'] . "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
</div>
</div>
<div class="col-sm-8 col-md-12 col-lg-6 col-xl-6 tm-block-col">
<div class="tm-bg-primary-dark tm-block ">
<h2 class="tm-block-title">Total Sales by Category</h2>
<canvas id="barChart"></canvas>
</div>
</div>
<div class="col-10 tm-block-col">
<div class="tm-bg-primary-dark tm-block tm-block-taller tm-block-scroll">
<h2 class="tm-block-title">Products Sold </h2>
<table class="table">
<thead>
<tr>
<th scope="col">Product</th>
<th scope="col">Category</th>
<th scope="col">Daily Sales</th>
<th scope="col">Weekly Sales</th>
<th scope="col">Monthly Sales</th>
<th scope="col">Yearly Sales</th>
</tr>
</thead>
<tbody>
<?php
foreach ($salesData as $item) {
echo "<tr>";
echo "<td>" . $item['Product'] . "</td>";
echo "<td>" . $categoryNames[$item['categoryname']] . "</td>"; // Use categoryNames array to get category name
echo "<td>" . $item['DailySalesQty'] . "</td>";
echo "<td>" . $item['WeeklySalesQty'] . "</td>";
echo "<td>" . $item['MonthlySalesQty'] . "</td>";
echo "<td>" . $item['YearlySalesQty'] . "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<footer class="tm-footer row tm-mt-small">
<div class="col-12 font-weight-light">
<p class="text-center text-white mb-0 px-4 small">
Copyright © <b>2023</b> All rights reserved.
Design: <a>Phemcode - Sales Pilot</a>
</p>
</div>
</footer>
</div>
<script src="http://localhost/WEB/js/jquery-3.3.1.min.js"></script>
<!-- https://jquery.com/download/ -->
<script src="http://localhost/WEB/js/moment.min.js"></script>
<!-- https://momentjs.com/ -->
<script src="http://localhost/WEB/js/Chart.min.js"></script>
<script src="http://localhost/WEB/js/bootstrap.min.js"></script>
<script>
const width_threshold = 600;
function drawBarChart() {
if ($("#barChart").length) {
const ctxBar = document.getElementById("barChart").getContext("2d");
// Define your PHP data variables for the bar chart here
const barChartLabels = [
"Tech Devices",
"Clothing Items",
"Food Items",
"Home Goods",
"Health",
"Automotives",
"Sports",
"Other",
"Kids",
"Offices"
];
const barChartData = <?php echo json_encode($barChartData); ?>;
const barChartColors = ["#F7604D", "#4ED6B8", "#A8D582", "#FF5733", "#1E90FF", "#FF1493", "#32CD32", "#8A2BE2", "#FFD700", "#FF8C00"];
const optionsBar = {
responsive: true,
scales: {
yAxes: [
{
barPercentage: 0.2,
ticks: {
beginAtZero: true,
},
scaleLabel: {
display: true,
labelString: "Category",
},
},
],
},
};
optionsBar.maintainAspectRatio =
$(window).width() < width_threshold ? false : true;
const configBar = {
type: "horizontalBar",
data: {
labels: barChartLabels,
datasets: [
{
label: "Total Sales Qty",
data: barChartData,
backgroundColor: barChartColors,
borderWidth: 0,
},
],
},
options: optionsBar,
};
const barChart = new Chart(ctxBar, configBar);
}
}
function drawPieChart() {
if ($("#pieChart").length) {
const chartHeight = 400;
$("#pieChartContainer").css("height", chartHeight + "px");
const ctxPie = document.getElementById("pieChart").getContext("2d");
// Define your PHP data variables for the pie chart here
const pieChartData = <?php echo json_encode($pieChartData); ?>;
const pieChartLabels = [
"Tech Devices",
"Clothing Items",
"Food Items",
"Home Goods",
"Wellness Product",
"Automotives",
"Sports & Games",
"Other",
"Kids",
"Office Supplies"
];
const pieChartColors = ["#F7604D", "#4ED6B8", "#A8D582", "#FF5733", "#1E90FF", "#FF1493", "#32CD32", "#8A2BE2", "#FFD700", "#FF8C00"];
const optionsPie = {
responsive: true,
maintainAspectRatio: false,
layout: {
padding: {
left: 10,
right: 10,
top: 10,
bottom: 10,
},
},
legend: {
position: "top",
},
};
const configPie = {
type: "pie",
data: {
datasets: [
{
data: pieChartData,
backgroundColor: pieChartColors,
label: "Total Revenue ($)",
},
],
labels: pieChartLabels,
},
options: optionsPie,
};
const pieChart = new Chart(ctxPie, configPie);
}
}
// Call the chart functions
drawPieChart();
drawBarChart();
</script>
</body>
</html>