218 lines
10 KiB
PHP
218 lines
10 KiB
PHP
<?php $pdo = $db; ?>
|
|
<?php
|
|
if (!is_admin()) {
|
|
// header('Location: index.php'); exit;
|
|
}
|
|
|
|
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
|
|
$limit = 15;
|
|
$offset = ($page - 1) * $limit;
|
|
|
|
$where = "1=1";
|
|
$params = [];
|
|
|
|
if (!empty($_GET['status'])) {
|
|
$where .= " AND l.status = ?";
|
|
$params[] = $_GET['status'];
|
|
}
|
|
|
|
$logs = $pdo->prepare("
|
|
SELECT l.*, e.name_en
|
|
FROM leave_requests l
|
|
JOIN employees e ON l.employee_id = e.id
|
|
WHERE $where
|
|
ORDER BY l.created_at DESC
|
|
LIMIT $limit OFFSET $offset
|
|
");
|
|
$logs->execute($params);
|
|
$requests = $logs->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$total_logs = $pdo->prepare("SELECT COUNT(*) FROM leave_requests l WHERE $where");
|
|
$total_logs->execute($params);
|
|
$total_rows = $total_logs->fetchColumn();
|
|
$total_pages = ceil($total_rows / $limit);
|
|
|
|
$employees = $pdo->query("SELECT id, name_en FROM employees ORDER BY name_en")->fetchAll(PDO::FETCH_KEY_PAIR);
|
|
|
|
// Handle Actions
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
if (isset($_POST['add_leave'])) {
|
|
$emp_id = $_POST['employee_id'];
|
|
$type = $_POST['leave_type'];
|
|
$start = $_POST['start_date'];
|
|
$end = $_POST['end_date'];
|
|
$reason = $_POST['reason'];
|
|
|
|
$diff = strtotime($end) - strtotime($start);
|
|
$days = round($diff / (60 * 60 * 24)) + 1;
|
|
|
|
$stmt = $pdo->prepare("INSERT INTO leave_requests (employee_id, leave_type, start_date, end_date, days, reason, status) VALUES (?, ?, ?, ?, ?, ?, 'Pending')");
|
|
$stmt->execute([$emp_id, $type, $start, $end, $days, $reason]);
|
|
} elseif (isset($_POST['approve_leave'])) {
|
|
$stmt = $pdo->prepare("UPDATE leave_requests SET status = 'Approved' WHERE id = ?");
|
|
$stmt->execute([$_POST['id']]);
|
|
} elseif (isset($_POST['reject_leave'])) {
|
|
$stmt = $pdo->prepare("UPDATE leave_requests SET status = 'Rejected' WHERE id = ?");
|
|
$stmt->execute([$_POST['id']]);
|
|
} elseif (isset($_POST['delete_leave'])) {
|
|
$stmt = $pdo->prepare("DELETE FROM leave_requests WHERE id = ?");
|
|
$stmt->execute([$_POST['id']]);
|
|
}
|
|
|
|
echo "<script>window.location.href='hr_leaves.php';</script>";
|
|
}
|
|
?>
|
|
|
|
<div class="container-fluid">
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<h1 class="h3 mb-0 text-gray-800">Leave Requests</h1>
|
|
<button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#addLeaveModal">
|
|
<i class="fas fa-plus"></i> New Request
|
|
</button>
|
|
</div>
|
|
|
|
<!-- Filter -->
|
|
<div class="card shadow mb-4">
|
|
<div class="card-body">
|
|
<form method="GET" class="form-inline">
|
|
<select name="status" class="form-control mr-2 mb-2">
|
|
<option value="">All Statuses</option>
|
|
<option value="Pending" <?php echo (isset($_GET['status']) && $_GET['status'] == 'Pending') ? 'selected' : ''; ?>>Pending</option>
|
|
<option value="Approved" <?php echo (isset($_GET['status']) && $_GET['status'] == 'Approved') ? 'selected' : ''; ?>>Approved</option>
|
|
<option value="Rejected" <?php echo (isset($_GET['status']) && $_GET['status'] == 'Rejected') ? 'selected' : ''; ?>>Rejected</option>
|
|
</select>
|
|
<button type="submit" class="btn btn-primary mb-2">Filter</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Table -->
|
|
<div class="card shadow mb-4">
|
|
<div class="card-body">
|
|
<div class="table-responsive">
|
|
<table class="table table-bordered" width="100%" cellspacing="0">
|
|
<thead>
|
|
<tr>
|
|
<th>Employee</th>
|
|
<th>Type</th>
|
|
<th>Duration</th>
|
|
<th>Days</th>
|
|
<th>Reason</th>
|
|
<th>Status</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($requests as $req): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($req['name_en']); ?></td>
|
|
<td><?php echo htmlspecialchars($req['leave_type']); ?></td>
|
|
<td>
|
|
<?php echo $req['start_date']; ?> to <?php echo $req['end_date']; ?>
|
|
</td>
|
|
<td><?php echo $req['days']; ?></td>
|
|
<td><?php echo htmlspecialchars($req['reason']); ?></td>
|
|
<td>
|
|
<span class="badge badge-<?php
|
|
echo $req['status'] == 'Approved' ? 'success' :
|
|
($req['status'] == 'Pending' ? 'warning' : 'danger');
|
|
?>">
|
|
<?php echo $req['status']; ?>
|
|
</span>
|
|
</td>
|
|
<td>
|
|
<?php if($req['status'] == 'Pending'): ?>
|
|
<form method="POST" style="display:inline;">
|
|
<input type="hidden" name="id" value="<?php echo $req['id']; ?>">
|
|
<button type="submit" name="approve_leave" class="btn btn-success btn-sm" title="Approve"><i class="fas fa-check"></i></button>
|
|
</form>
|
|
<form method="POST" style="display:inline;">
|
|
<input type="hidden" name="id" value="<?php echo $req['id']; ?>">
|
|
<button type="submit" name="reject_leave" class="btn btn-danger btn-sm" title="Reject"><i class="fas fa-times"></i></button>
|
|
</form>
|
|
<?php endif; ?>
|
|
<form method="POST" style="display:inline;" onsubmit="return confirm('Delete this request?');">
|
|
<input type="hidden" name="id" value="<?php echo $req['id']; ?>">
|
|
<button type="submit" name="delete_leave" class="btn btn-secondary btn-sm" title="Delete"><i class="fas fa-trash"></i></button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php if (empty($requests)): ?>
|
|
<tr><td colspan="7" class="text-center">No requests found</td></tr>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- Pagination -->
|
|
<?php if ($total_pages > 1): ?>
|
|
<nav aria-label="Page navigation">
|
|
<ul class="pagination justify-content-center">
|
|
<?php for ($i = 1; $i <= $total_pages; $i++): ?>
|
|
<li class="page-item <?php echo $i == $page ? 'active' : ''; ?>">
|
|
<a class="page-link" href="?page=<?php echo $i; ?>&status=<?php echo $_GET['status'] ?? ''; ?>">
|
|
<?php echo $i; ?>
|
|
</a>
|
|
</li>
|
|
<?php endfor; ?>
|
|
</ul>
|
|
</nav>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Add Modal -->
|
|
<div class="modal fade" id="addLeaveModal" tabindex="-1" role="dialog" aria-hidden="true">
|
|
<div class="modal-dialog" role="document">
|
|
<form method="POST">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title">Request Leave</h5>
|
|
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
|
<span aria-hidden="true">×</span>
|
|
</button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<input type="hidden" name="add_leave" value="1">
|
|
<div class="form-group">
|
|
<label>Employee</label>
|
|
<select name="employee_id" class="form-control" required>
|
|
<?php foreach ($employees as $id => $name): ?>
|
|
<option value="<?php echo $id; ?>"><?php echo htmlspecialchars($name); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Leave Type</label>
|
|
<select name="leave_type" class="form-control">
|
|
<option value="Annual">Annual</option>
|
|
<option value="Sick">Sick</option>
|
|
<option value="Casual">Casual</option>
|
|
<option value="Unpaid">Unpaid</option>
|
|
</select>
|
|
</div>
|
|
<div class="form-group row">
|
|
<div class="col-6">
|
|
<label>Start Date</label>
|
|
<input type="date" name="start_date" class="form-control" required>
|
|
</div>
|
|
<div class="col-6">
|
|
<label>End Date</label>
|
|
<input type="date" name="end_date" class="form-control" required>
|
|
</div>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Reason</label>
|
|
<textarea name="reason" class="form-control" rows="2"></textarea>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
|
|
<button type="submit" class="btn btn-primary">Submit</button>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|