-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.php
241 lines (201 loc) · 8.98 KB
/
report.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
<?php
session_start();
include 'connection.php';
if (!isset($_SESSION['id']) || !isset($_SESSION['role'])) {
header('Location: login.php');
exit;
}
include_once('includes/header.php');
?>
<div id="page-wrapper">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header text-center">All Reports</h1>
</div>
</div>
<style>
#transactionChart,
#customerChart {
background-color: #e1f7d5;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
margin-bottom: 20px;
}
.filter-container {
background-color: #e1f7d5;
margin-bottom: 20px;
padding: 15px;
border-radius: 8px;
}
.filter-container label {
margin-right: 10px;
font-weight: bold;
color: #007bff;
}
.filter-container select {
padding: 8px;
border-radius: 4px;
border: 1px solid #ced4da;
outline: none;
transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}
</style>
<div class="row">
<div class=" mt-4">
<!-- Filter Select Box -->
<div class="filter-container">
<label for="interval">Select Interval:</label>
<select id="interval" name="filter" class="form-select">
<option value="last-7-days" <?php echo (!isset($_POST['filter']) || $_POST['filter'] == 'last-7-days') ? 'selected' : ''; ?>>Last 7 Days</option>
<option value="last-30-days" <?php echo (isset($_POST['filter']) && $_POST['filter'] == 'last-30-days') ? 'selected' : ''; ?>>Last 30 Days</option>
<option value="monthly" <?php echo (isset($_POST['filter']) && $_POST['filter'] == 'monthly') ? 'selected' : ''; ?>>Monthly</option>
<option value="yearly" <?php echo (isset($_POST['filter']) && $_POST['filter'] == 'yearly') ? 'selected' : ''; ?>>Yearly</option>
<!-- <option value="all-time" <?php echo (isset($_POST['filter']) && $_POST['filter'] == 'all-time') ? 'selected' : ''; ?>>All Time</option> -->
</select>
</div>
<!-- Stylish Canvas for Chart.js -->
<div class="card">
<div class="card-body">
<canvas id="myChart" width="400" height="150"></canvas>
</div>
</div>
<?php
function fetchDataForChart($interval, $format, $conn)
{
$sql = "SELECT DATE(b.date) AS booking_date, SUM(t.total) AS total_income
FROM booking b
LEFT JOIN transaction t ON b.transaction_id = t.id";
switch ($interval) {
case 'last-7-days':
$sql .= " WHERE b.date >= CURDATE() - INTERVAL 6 DAY AND b.date <= NOW()";
break;
case 'last-30-days':
$sql .= " WHERE b.date >= CURDATE() - INTERVAL 29 DAY AND b.date <= NOW()";
break;
case 'monthly':
$sql .= " WHERE b.date >= DATE_SUB(NOW(), INTERVAL 1 MONTH) AND b.date <= NOW()";
break;
case 'yearly':
$sql .= " WHERE b.date >= DATE_SUB(NOW(), INTERVAL 1 YEAR) AND b.date <= NOW()";
break;
// Add more cases for other intervals if needed
}
$sql .= " GROUP BY booking_date";
$result = mysqli_query($conn, $sql);
// Check for errors
if (!$result) {
die("Error in SQL: " . mysqli_error($conn));
}
$data = array();
// Fetch data and group by the specified interval from the booking table
while ($row = mysqli_fetch_assoc($result)) {
// Use the booking date for grouping
$date = date_create($row['booking_date']);
$groupKey = date_format($date, $format);
if (!isset($data[$groupKey])) {
$data[$groupKey] = 0;
}
// Sum up the total amount for each interval
$data[$groupKey] += $row['total_income'];
}
return array('totalIncome' => $data);
}
// Display data by default (last 30 days)
$data = fetchDataForChart('last-7-days', 'Y-m-d', $conn);
?>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script>
var myChart; // Variable to hold the Chart.js instance
// Function to create a stylish Chart.js bar chart
function createChart(data) {
var ctx = document.getElementById('myChart').getContext('2d');
// Check if a chart instance exists, destroy it before creating a new one
if (window.myChart) {
window.myChart.destroy();
}
var labels = [];
var values = [];
// Extract keys (labels) and values from the data.totalIncome array
for (var key in data.totalIncome) {
labels.push(key);
values.push(data.totalIncome[key]);
}
// Define vibrant and deeper colors for the bars
var barColors = [
'rgba(75, 192, 192, 0.8)',
'rgba(255, 99, 132, 0.8)',
'rgba(54, 162, 235, 0.8)',
'rgba(255, 206, 86, 0.8)',
'rgba(153, 102, 255, 0.8)',
'rgba(255, 159, 64, 0.8)',
];
window.myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: 'Total Amount',
data: values,
backgroundColor: barColors,
borderColor: barColors.map(color => color.replace('0.8', '1')),
borderWidth: 2
}]
},
options: {
scales: {
y: {
beginAtZero: true,
grid: {
color: 'rgba(0, 0, 0, 0.1)',
}
},
x: {
grid: {
color: 'rgba(0, 0, 0, 0)',
}
}
},
plugins: {
legend: {
display: true,
position: 'top',
}
}
}
});
}
// Function to update the chart
function updateChart() {
// Fetch data using jQuery AJAX
var selectedFilter = $('#interval').val();
$.ajax({
method: 'POST',
url: 'update_chart_data.php',
data: {
filter: selectedFilter
},
dataType: 'json',
cache: false, // Add this line to prevent caching
success: function(response) {
if (response.error) {
console.error('Error:', response.error);
} else {
// Call the function to update the chart
createChart(response.data);
}
},
error: function(xhr, status, error) {
console.error('AJAX Error:', error);
}
});
}
// Bind the updateChart function to the change event of the select box
$('#interval').change(updateChart);
// Initial chart creation
createChart(<?php echo json_encode($data); ?>);
</script>
</div>
</div>
</div>
<?php include_once('includes/footer.php'); ?>