36398-vm/mark_attendance.php
2025-11-28 03:31:58 +00:00

103 lines
4.3 KiB
PHP

<?php
require_once 'auth.php';
require_once 'db/config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
$employee_id = $_POST['employee_id'];
$action = $_POST['action'];
$current_time = date('H:i:s');
$current_date = date('Y-m-d');
if ($action === 'check-in') {
$stmt = db()->prepare("SELECT id FROM attendance WHERE employee_id = ? AND date = ?");
$stmt->execute([$employee_id, $current_date]);
$attendance_record = $stmt->fetch();
if ($attendance_record) {
$stmt = db()->prepare("UPDATE attendance SET check_in_time = ?, status = 'Present' WHERE id = ?");
$stmt->execute([$current_time, $attendance_record['id']]);
} else {
$stmt = db()->prepare("INSERT INTO attendance (employee_id, date, status, check_in_time) VALUES (?, ?, 'Present', ?)");
$stmt->execute([$employee_id, $current_date, $current_time]);
}
} elseif ($action === 'check-out') {
$stmt = db()->prepare("UPDATE attendance SET check_out_time = ? WHERE employee_id = ? AND date = ?");
$stmt->execute([$current_time, $employee_id, $current_date]);
}
header('Location: mark_attendance.php');
exit();
}
// Only allow Admin and HR to access this page
if (!isset($_SESSION['user_role']) || ($_SESSION['user_role'] !== 'Admin' && $_SESSION['user_role'] !== 'HR')) {
header('Location: index.php');
exit();
}
$pdo = db();
$today = date('Y-m-d');
// Fetch all employees (users)
$stmt = $pdo->prepare('SELECT id, username, role FROM users ORDER BY username');
$stmt->execute();
$employees = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mark Attendance</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<link rel="stylesheet" href="assets/css/custom.css">
</head>
<body>
<div class="d-flex">
<?php include 'sidebar.php'; ?>
<div class="container-fluid main-content">
<div class="card shadow-sm">
<div class="card-header bg-primary text-white">
<h1 class="h5 mb-0">Mark Daily Attendance</h1>
</div>
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead class="table-light">
<tr>
<th>Employee Name</th>
<th>Role</th>
<th class="text-center">Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($employees as $employee): ?>
<tr>
<td><?php echo htmlspecialchars($employee['username']); ?></td>
<td><?php echo htmlspecialchars($employee['role']); ?></td>
<td class="text-center">
<form action="mark_attendance.php" method="post" style="display: inline-block;">
<input type="hidden" name="employee_id" value="<?= $employee['id'] ?>">
<button type="submit" name="action" value="check-in" class="btn btn-success btn-sm">Check-in</button>
</form>
<form action="mark_attendance.php" method="post" style="display: inline-block;">
<input type="hidden" name="employee_id" value="<?= $employee['id'] ?>">
<button type="submit" name="action" value="check-out" class="btn btn-danger btn-sm">Check-out</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>