-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs.php
333 lines (290 loc) · 13 KB
/
logs.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
<?php
session_start(); // Start session management
// Enable error reporting for debugging
error_reporting(E_ALL); // Report all types of errors
ini_set('display_errors', 1); // Display errors on the page
// Check if the user is logged in
$isLoggedIn = isset($_SESSION['user_logged_in']) && $_SESSION['user_logged_in'] === true;
if (!$isLoggedIn) {
// Store the intended URL (current URL) in session
$_SESSION['redirect_after_login'] = $_SERVER['REQUEST_URI'];
header("Location: index.php"); // Redirect to login
exit;
}
require 'db.php'; // Include database connection
// Pagination settings
$limit = 15; // Number of logs per page
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $limit;
// Search settings
$searchTerm = '';
$userId = '';
$date = '';
// Process the search form submission
if (isset($_POST['search'])) {
$userId = $_POST['user_id'] ?? '';
$date = $_POST['date'] ?? ''; // Single date input
$searchTerm = trim($_POST['product_name'] ?? '');
}
// Fetch users for the dropdown
$usersQuery = "SELECT id, username FROM users ORDER BY username";
$usersResult = $conn->query($usersQuery);
// Check for errors in the query
if (!$usersResult) {
die("Users Query failed: " . $conn->error);
}
// Fetch stock logs from the database with product name based on search criteria
$stockLogsQuery = "
SELECT sl.created_at, u.username, sl.user_id, sl.action, p.id AS product_id, p.name, sl.quantity_change
FROM stock_logs sl
JOIN users u ON sl.user_id = u.id
JOIN products p ON sl.product_id = p.id
WHERE (u.id = '$userId' OR '$userId' = '')
AND (DATE(sl.created_at) = ? OR '$date' = '')
AND (p.name LIKE ? OR '$searchTerm' = '')
ORDER BY sl.created_at DESC
LIMIT $limit OFFSET $offset
";
// Prepare the statement
$stmt = $conn->prepare($stockLogsQuery);
$likeSearchTerm = '%' . $searchTerm . '%'; // Prepare like query
$stmt->bind_param("ss", $date, $likeSearchTerm); // Bind parameters
$stmt->execute();
$stockLogsResult = $stmt->get_result();
// Check for errors in the query
if (!$stockLogsResult) {
die("Stock Logs Query failed: " . $conn->error);
}
// Count total logs for pagination
$totalLogsQuery = "SELECT COUNT(*) as count FROM stock_logs sl
JOIN users u ON sl.user_id = u.id
JOIN products p ON sl.product_id = p.id
WHERE (u.id = '$userId' OR '$userId' = '')
AND (DATE(sl.created_at) = ? OR '$date' = '')
AND (p.name LIKE ? OR '$searchTerm' = '')";
$stmtTotal = $conn->prepare($totalLogsQuery);
$stmtTotal->bind_param("ss", $date, $likeSearchTerm);
$stmtTotal->execute();
$totalLogsResult = $stmtTotal->get_result();
$totalLogs = $totalLogsResult->fetch_assoc()['count'];
$totalPages = ceil($totalLogs / $limit);
// Fetch product logs for the product logs tab with search and pagination
$productLogsQuery = "
SELECT pl.created_at, u.username, pl.user_id, pl.action, p.name, pl.description
FROM product_logs pl
JOIN users u ON pl.user_id = u.id
LEFT JOIN products p ON pl.product_id = p.id
WHERE (u.id = '$userId' OR '$userId' = '')
AND (p.name LIKE ? OR '$searchTerm' = '')
";
// Append date condition if it's not empty
if (!empty($date)) {
$productLogsQuery .= " AND (DATE(pl.created_at) = ?)";
}
$productLogsQuery .= " ORDER BY pl.created_at DESC LIMIT $limit OFFSET $offset";
// Prepare the statement
$stmtProduct = $conn->prepare($productLogsQuery);
if (!empty($date)) {
$stmtProduct->bind_param("ss", $likeSearchTerm, $date);
} else {
$stmtProduct->bind_param("s", $likeSearchTerm);
}
$stmtProduct->execute();
$productLogsResult = $stmtProduct->get_result();
// Check for errors in the query
if (!$productLogsResult) {
die("Product Logs Query failed: " . $conn->error);
}
// Count total product logs for pagination
$totalProductLogsQuery = "SELECT COUNT(*) as count FROM product_logs pl
JOIN users u ON pl.user_id = u.id
JOIN products p ON pl.product_id = p.id
WHERE (u.id = '$userId' OR '$userId' = '')
AND (p.name LIKE ? OR '$searchTerm' = '')";
// Append date condition if it's not empty
if (!empty($date)) {
$totalProductLogsQuery .= " AND (DATE(pl.created_at) = ?)";
}
$stmtTotalProduct = $conn->prepare($totalProductLogsQuery);
if (!empty($date)) {
$stmtTotalProduct->bind_param("ss", $likeSearchTerm, $date);
} else {
$stmtTotalProduct->bind_param("s", $likeSearchTerm);
}
$stmtTotalProduct->execute();
$totalProductLogsResult = $stmtTotalProduct->get_result();
$totalProductLogs = $totalProductLogsResult->fetch_assoc()['count'];
$totalProductPages = ceil($totalProductLogs / $limit);
?>
<style>
.action-added {
color: green;
font-weight: bold;
}
.action-removed {
color: red;
font-weight: bold;
}
.quantity-added {
color: green;
font-weight: bold;
}
.quantity-removed {
color: red;
font-weight: bold;
}
th, td {
border: 1px solid #ddd; /* Border for action and description columns */
}
td {
padding: 8px; /* Padding for table cells */
}
.bold {
font-weight: bold; /* Bold for product name */
}
</style>
</head>
<body>
<?php require 'header.php'; require 'nav.php'; ?>
<div class="container mt-5">
<h2 class="mb-4">Change Logs</h2>
<!-- Search Form -->
<form method="POST" class="mb-4">
<div class="row">
<div class="col-md-3">
<label for="user_id" class="form-label">User</label>
<select name="user_id" id="user_id" class="form-select">
<option value="">Select User</option>
<?php while ($user = $usersResult->fetch_assoc()): ?>
<option value="<?php echo $user['id']; ?>" <?php echo ($user['id'] == $userId) ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($user['username']); ?>
</option>
<?php endwhile; ?>
</select>
</div>
<div class="col-md-3">
<label for="date" class="form-label">Date</label>
<input type="text" name="date" id="date" class="form-control" placeholder="YYYY-MM-DD" value="<?php echo htmlspecialchars($date); ?>">
</div>
<div class="col-md-3">
<label for="product_name" class="form-label">Product</label>
<input type="text" name="product_name" id="product_name" class="form-control" placeholder="Product Name" value="<?php echo htmlspecialchars($searchTerm); ?>">
</div>
</div>
<button class="btn btn-primary mt-3" type="submit" name="search">Search</button>
</form>
<!-- Nav tabs -->
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item" role="presentation">
<a class="nav-link active" id="stock-logs-tab" data-bs-toggle="tab" href="#stock-logs" role="tab" aria-controls="stock-logs" aria-selected="true">Stock Logs</a>
</li>
<li class="nav-item" role="presentation">
<a class="nav-link" id="product-logs-tab" data-bs-toggle="tab" href="#product-logs" role="tab" aria-controls="product-logs" aria-selected="false">Product Logs</a>
</li>
<li class="nav-item" role="presentation">
<a class="nav-link" id="user-logs-tab" data-bs-toggle="tab" href="#user-logs" role="tab" aria-controls="user-logs" aria-selected="false">User Logs</a>
</li>
</ul>
<!-- Tab content -->
<div class="tab-content mt-3" id="myTabContent">
<!-- Stock Logs Tab -->
<div class="tab-pane fade show active" id="stock-logs" role="tabpanel" aria-labelledby="stock-logs-tab">
<table class="table table-striped table-bordered">
<thead class="table-dark">
<tr>
<th>Date</th>
<th>User</th>
<th>Action</th>
<th>Product Name</th>
<th>Quantity</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php
// Check if there are results and display stock logs
if ($stockLogsResult->num_rows > 0) {
while ($row = $stockLogsResult->fetch_assoc()) {
// Format the date
$dateFormatted = date('d F Y - H:i', strtotime($row['created_at']));
// Determine the classes for action and quantity
$actionClass = ($row['action'] === 'added') ? 'action-added' : 'action-removed';
$quantityClass = ($row['quantity_change'] > 0) ? 'quantity-added' : 'quantity-removed';
// Create a description string
$description = "{$row['username']} <span class='bold'>{$row['action']}</span> <span class='{$quantityClass}'>{$row['quantity_change']}</span> qty for product ID {$row['product_id']} (<span class='bold'>{$row['name']}</span>)";
echo "<tr>
<td>{$dateFormatted}</td>
<td>{$row['username']}</td>
<td><span class='$actionClass'>" . ucfirst($row['action']) . "</span></td>
<td class='bold'>{$row['name']}</td>
<td><span class='$quantityClass'>{$row['quantity_change']}</span></td>
<td>{$description}</td>
</tr>";
}
} else {
echo "<tr><td colspan='6' class='text-center'>No logs found</td></tr>";
}
?>
</tbody>
</table>
<!-- Pagination -->
<nav aria-label="Page navigation">
<ul class="pagination">
<?php for ($i = 1; $i <= $totalPages; $i++): ?>
<li class="page-item <?php echo ($i == $page) ? 'active' : ''; ?>">
<a class="page-link" href="?page=<?php echo $i; ?>&user_id=<?php echo htmlspecialchars($userId); ?>&date=<?php echo htmlspecialchars($date); ?>&product_name=<?php echo htmlspecialchars($searchTerm); ?>">
<?php echo $i; ?>
</a>
</li>
<?php endfor; ?>
</ul>
</nav>
</div>
<!-- Product Logs Tab -->
<div class="tab-pane fade" id="product-logs" role="tabpanel" aria-labelledby="product-logs-tab">
<table class="table table-striped table-bordered">
<thead class="table-dark">
<tr>
<th>Date</th>
<th>User</th>
<th>Action</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php while ($log = $productLogsResult->fetch_assoc()): ?>
<tr>
<td><?php echo date('Y-m-d H:i:s', strtotime($log['created_at'])); ?></td>
<td><?php echo htmlspecialchars($log['username']); ?></td>
<td><?php echo htmlspecialchars(ucfirst($log['action'])); ?></td>
<td><?php echo htmlspecialchars($log['description']); ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<!-- Pagination for Product Logs -->
<nav aria-label="Page navigation">
<ul class="pagination">
<?php for ($i = 1; $i <= $totalProductPages; $i++): ?>
<li class="page-item <?php echo ($i === $page) ? 'active' : ''; ?>">
<a class="page-link" href="?page=<?php echo $i; ?>"><?php echo $i; ?></a>
</li>
<?php endfor; ?>
</ul>
</nav>
</div>
<!-- User Logs Tab -->
<div class="tab-pane fade" id="user-logs" role="tabpanel" aria-labelledby="user-logs-tab">
<p>User logs will be displayed here.</p>
</div>
</div>
</div>
<script>
$(function() {
// Initialize the datepicker
$("#date").datepicker({
dateFormat: "yy-mm-dd" // Format for the date picker
});
});
</script>
<?php require 'footer.php'; ?>