-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager_dashboard.php
More file actions
244 lines (215 loc) · 7.74 KB
/
Copy pathmanager_dashboard.php
File metadata and controls
244 lines (215 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
244
<?php
include 'connect.php';
if (!isset($_SESSION['user_id']) || $_SESSION['role'] != 'manager') {
header("Location: login.php");
exit;
}
// Ambil parameter
$month = isset($_GET['month']) ? (int)$_GET['month'] : (int)date('n');
$year = isset($_GET['year']) ? (int)$_GET['year'] : (int)date('Y');
$search = isset($_GET['search']) ? trim($_GET['search']) : '';
// Month names
$months = [
1 => 'January',
2 => 'February',
3 => 'March',
4 => 'April',
5 => 'May',
6 => 'June',
7 => 'July',
8 => 'August',
9 => 'September',
10 => 'October',
11 => 'November',
12 => 'December'
];
$start_date = "$year-$month-01";
$end_date = date("Y-m-t", strtotime($start_date));
$search_param = "%" . strtolower($search) . "%";
// SQL
$sql = "SELECT ot.*, u.name AS staff_name
FROM ot_attendance ot
JOIN users u ON ot.staff_id = u.id
WHERE ot.date BETWEEN ? AND ?
AND LOWER(u.name) LIKE ?
ORDER BY ot.date DESC";
$stmt = $conn->prepare($sql);
$stmt->bind_param("sss", $start_date, $end_date, $search_param);
$stmt->execute();
$result = $stmt->get_result();
// Function kira duration (decimal hour) yang betul
function calculateDuration($check_in, $check_out, $date)
{
if (!$check_in || !$check_out) return 0;
// Gabungkan date dengan time untuk dapat datetime lengkap
$start = strtotime($date . ' ' . $check_in);
$end = strtotime($date . ' ' . $check_out);
// Jika check_out lebih awal dari check_in, tambah 1 hari (bermaksud lepas tengah malam)
if ($end < $start) {
$end = strtotime('+1 day', $end);
}
return ($end - $start) / 3600; // jam decimal
}
// Function format duration (xh ym)
function formatDuration($hours_decimal)
{
$hours = floor($hours_decimal);
$minutes = round(($hours_decimal - $hours) * 60);
return "{$hours}h {$minutes}m";
}
// Function format masa
function formatTime($time)
{
return $time ? date("H:i", strtotime($time)) : '-';
}
// Simpan OT rows + chart data
$ot_rows = [];
$chart_data = [];
while ($row = $result->fetch_assoc()) {
$row['duration_hours'] = calculateDuration($row['check_in_time'], $row['check_out_time'], $row['date']);
$ot_rows[] = $row;
if (!isset($chart_data[$row['staff_name']])) $chart_data[$row['staff_name']] = 0;
$chart_data[$row['staff_name']] += $row['duration_hours'];
}
// Chart labels & values
$chart_data = array_map(function ($hours) {
return round($hours, 2);
}, $chart_data);
$chart_labels = array_keys($chart_data);
$chart_values = array_values($chart_data);
?>
<!DOCTYPE html>
<html>
<head>
<title>Manager Dashboard - OT System</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<link rel="stylesheet" href="manager_dashboard.css">
</head>
<body>
<img src="images/tm-logo.png" alt="TM Logo" class="tm-logo">
<h1>Manager OT Dashboard - TM Raub</h1>
<!-- Filter Form -->
<form method="get" action="" class="filter-form">
<select name="year">
<?php for ($y = date('Y') - 5; $y <= date('Y') + 1; $y++): ?>
<option value="<?= $y ?>" <?= $y == $year ? 'selected' : '' ?>><?= $y ?></option>
<?php endfor; ?>
</select>
<select name="month">
<?php foreach ($months as $num => $name): ?>
<option value="<?= $num ?>" <?= $num == $month ? 'selected' : '' ?>><?= $name ?></option>
<?php endforeach; ?>
</select>
<input type="text" name="search" placeholder="Staff name" value="<?= htmlspecialchars($search) ?>">
<button type="submit">Search</button>
</form>
<!-- Download Excel -->
<div class="download-btn-container">
<a href="export_ot_report.php?year=<?= $year ?>&month=<?= $month ?>&search=<?= urlencode($search) ?>"
class="download-excel-btn">
Download Excel
</a>
</div>
<!-- Button Bar Chart -->
<div class="chart-button-container">
<button id="openChartBtn" class="chart-button">View OT Bar Chart</button>
</div>
<!-- OT Table -->
<table class="ot-table">
<tr class="table-header">
<th>Date</th>
<th>Day</th>
<th>Staff Name</th>
<th>Location</th>
<th>Remarks</th>
<th>Check In</th>
<th>Check Out</th>
<th>Duration</th>
</tr>
<?php if (empty($ot_rows)): ?>
<tr>
<td colspan="8" class="no-records-cell">
<div class="no-records">No records found</div>
</td>
</tr>
<?php else: ?>
<?php foreach ($ot_rows as $row): ?>
<tr>
<td><?= $row['date'] ?></td>
<td><?= date('l', strtotime($row['date'])) ?></td>
<td><?= htmlspecialchars($row['staff_name']) ?></td>
<td><?= htmlspecialchars($row['location']) ?></td>
<td><?= htmlspecialchars($row['remarks']) ?></td>
<td><?= formatTime($row['check_in_time']) ?></td>
<td><?= formatTime($row['check_out_time']) ?></td>
<td><?= formatDuration($row['duration_hours']) ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</table>
<a href="logout.php" class="logout-link">Logout</a>
<!-- Modal Bar Chart -->
<div id="chartModal" class="chart-modal">
<div class="modal-content">
<span id="closeModal" class="close-modal">×</span>
<h2>Total OT Hours per Staff - <?= $months[$month] ?> <?= $year ?></h2>
<canvas id="otBarChart"></canvas>
</div>
</div>
<script>
// Data dari PHP
const labels = <?= json_encode($chart_labels) ?>;
const data = <?= json_encode(array_map('round', $chart_values)) ?>;
const ctx = document.getElementById('otBarChart').getContext('2d');
const otBarChart = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: 'Total OT Hours',
data: data,
backgroundColor: 'rgba(255,140,0,0.7)',
borderColor: 'rgba(255,140,0,1)',
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true,
title: {
display: true,
text: 'Hours'
},
ticks: {
precision: 0
}
},
x: {
title: {
display: true,
text: 'Staff'
}
}
},
plugins: {
legend: {
display: false
}
}
}
});
// Modal
const modal = document.getElementById('chartModal');
document.getElementById('openChartBtn').onclick = () => {
modal.style.display = 'flex';
};
document.getElementById('closeModal').onclick = () => {
modal.style.display = 'none';
};
window.onclick = (event) => {
if (event.target == modal) modal.style.display = 'none';
};
</script>
</body>
</html>