38808-vm/hr_leaves.php
2026-03-27 03:32:55 +00:00

275 lines
14 KiB
PHP

<?php
require_once 'includes/header.php';
if (!canView('hr_leaves')) {
echo "<div class='alert alert-danger'>ليس لديك صلاحية للوصول إلى هذه الصفحة.</div>";
require_once 'includes/footer.php';
exit;
}
$tab = $_GET['tab'] ?? 'pending';
$error = '';
$success = '';
// Handle Form Submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['request_leave'])) {
if (!canAdd('hr_leaves')) {
$error = "لا تملك صلاحية الإضافة.";
} else {
$id = $_POST['id'] ?? null; // For edit
$emp_id = $_POST['employee_id'];
$type = $_POST['leave_type'];
$start = $_POST['start_date'];
$end = $_POST['end_date'];
$reason = trim($_POST['reason']);
$start_dt = new DateTime($start);
$end_dt = new DateTime($end);
$days = $end_dt->diff($start_dt)->days + 1;
if ($days <= 0) {
$error = "تاريخ النهاية يجب أن يكون بعد تاريخ البداية.";
} else {
try {
if ($id) {
// Update existing request
$stmt = db()->prepare("UPDATE hr_leaves SET employee_id=?, leave_type=?, start_date=?, end_date=?, days_count=?, reason=? WHERE id=? AND status='pending'");
$stmt->execute([$emp_id, $type, $start, $end, $days, $reason, $id]);
$success = "تم تحديث طلب الإجازة بنجاح.";
} else {
// New request
$stmt = db()->prepare("INSERT INTO hr_leaves (employee_id, leave_type, start_date, end_date, days_count, reason, status) VALUES (?, ?, ?, ?, ?, ?, 'pending')");
$stmt->execute([$emp_id, $type, $start, $end, $days, $reason]);
$success = "تم تقديم طلب الإجازة بنجاح.";
}
} catch (PDOException $e) {
$error = "خطأ: " . $e->getMessage();
}
}
}
} elseif (isset($_POST['update_status'])) {
if (!canEdit('hr_leaves')) {
$error = "لا تملك صلاحية الاعتماد.";
} else {
$id = $_POST['id'];
$status = $_POST['status'];
$stmt = db()->prepare("UPDATE hr_leaves SET status = ?, approved_by = ? WHERE id = ?");
$stmt->execute([$status, $_SESSION['user_id'], $id]);
$success = "تم تحديث حالة الطلب.";
}
}
}
// Fetch Employees for Dropdown
$employees = db()->query("SELECT id, first_name, last_name FROM hr_employees WHERE status = 'active' ORDER BY first_name")->fetchAll();
// Fetch Leaves based on Tab
$where_clause = $tab === 'pending' ? "WHERE l.status = 'pending'" : "WHERE 1=1";
$sql = "SELECT l.*, e.first_name, e.last_name, u.full_name as approver_name
FROM hr_leaves l
JOIN hr_employees e ON l.employee_id = e.id
LEFT JOIN users u ON l.approved_by = u.id
$where_clause
ORDER BY l.created_at DESC";
$requests = db()->query($sql)->fetchAll();
?>
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">إدارة الإجازات</h1>
<div class="btn-toolbar mb-2 mb-md-0">
<?php if (canAdd('hr_leaves')): ?>
<button type="button" class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#leaveModal" onclick="resetLeaveForm()">
<i class="fas fa-plus"></i> طلب إجازة جديد
</button>
<?php endif; ?>
</div>
</div>
<?php if ($error): ?>
<div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
<?php endif; ?>
<?php if ($success): ?>
<div class="alert alert-success"><?= htmlspecialchars($success) ?></div>
<?php endif; ?>
<ul class="nav nav-tabs mb-4">
<li class="nav-item">
<a class="nav-link <?= $tab === 'pending' ? 'active' : '' ?>" href="?tab=pending">الطلبات المعلقة</a>
</li>
<li class="nav-item">
<a class="nav-link <?= $tab === 'all' ? 'active' : '' ?>" href="?tab=all">سجل الإجازات</a>
</li>
</ul>
<div class="card shadow-sm">
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover align-middle">
<thead>
<tr>
<th>الموظف</th>
<th>نوع الإجازة</th>
<th>الفترة</th>
<th>المدة</th>
<th>السبب</th>
<th>الحالة</th>
<?php if ($tab === 'all'): ?><th>المعتمد</th><?php endif; ?>
<th>إجراءات</th>
</tr>
</thead>
<tbody>
<?php if (empty($requests)): ?>
<tr><td colspan="8" class="text-center py-4 text-muted">لا توجد طلبات.</td></tr>
<?php else: ?>
<?php foreach ($requests as $req): ?>
<tr>
<td class="fw-bold"><?= htmlspecialchars($req['first_name'] . ' ' . $req['last_name']) ?></td>
<td>
<?php
$type_map = [
'annual' => 'سنوية',
'sick' => 'مرضية',
'unpaid' => 'بدون راتب',
'maternity' => 'أمومة',
'emergency' => 'طارئة',
'other' => 'أخرى'
];
echo $type_map[$req['leave_type']] ?? $req['leave_type'];
?>
</td>
<td class="small">
من <?= $req['start_date'] ?><br>إلى <?= $req['end_date'] ?>
</td>
<td><?= $req['days_count'] ?> يوم</td>
<td class="text-truncate" style="max-width: 150px;"><?= htmlspecialchars($req['reason']) ?></td>
<td>
<?php
$status_cls = match($req['status']) {
'approved' => 'success',
'rejected' => 'danger',
default => 'warning'
};
$status_txt = match($req['status']) {
'approved' => 'مقبولة',
'rejected' => 'مرفوضة',
default => 'معلقة'
};
?>
<span class="badge bg-<?= $status_cls ?>"><?= $status_txt ?></span>
</td>
<?php if ($tab === 'all'): ?>
<td class="small"><?= htmlspecialchars($req['approver_name'] ?? '-') ?></td>
<?php endif; ?>
<td>
<?php if ($req['status'] === 'pending' && canEdit('hr_leaves')): ?>
<button class="btn btn-sm btn-outline-primary"
title="تعديل الطلب"
data-bs-toggle="modal"
data-bs-target="#leaveModal"
data-id="<?= $req['id'] ?>"
data-emp="<?= $req['employee_id'] ?>"
data-type="<?= $req['leave_type'] ?>"
data-start="<?= $req['start_date'] ?>"
data-end="<?= $req['end_date'] ?>"
data-reason="<?= htmlspecialchars($req['reason']) ?>"
onclick="editLeave(this)">
<i class="fas fa-edit"></i>
</button>
<form method="post" class="d-inline">
<input type="hidden" name="id" value="<?= $req['id'] ?>">
<input type="hidden" name="status" value="approved">
<button type="submit" name="update_status" class="btn btn-sm btn-success" title="قبول"><i class="fas fa-check"></i></button>
</form>
<form method="post" class="d-inline">
<input type="hidden" name="id" value="<?= $req['id'] ?>">
<input type="hidden" name="status" value="rejected">
<button type="submit" name="update_status" class="btn btn-sm btn-danger" title="رفض" onclick="return confirm('هل أنت متأكد من الرفض؟')"><i class="fas fa-times"></i></button>
</form>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
<!-- Leave Request Modal -->
<div class="modal fade" id="leaveModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="leaveModalTitle">تقديم طلب إجازة</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="post" id="leaveForm">
<div class="modal-body">
<input type="hidden" name="id" id="leaveId">
<div class="mb-3">
<label class="form-label">الموظف</label>
<select name="employee_id" id="leaveEmp" class="form-select" required>
<option value="">-- اختر الموظف --</option>
<?php foreach ($employees as $emp): ?>
<option value="<?= $emp['id'] ?>"><?= htmlspecialchars($emp['first_name'] . ' ' . $emp['last_name']) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label class="form-label">نوع الإجازة</label>
<select name="leave_type" id="leaveType" class="form-select" required>
<option value="annual">سنوية</option>
<option value="sick">مرضية</option>
<option value="emergency">طارئة</option>
<option value="unpaid">بدون راتب</option>
<option value="maternity">أمومة</option>
<option value="other">أخرى</option>
</select>
</div>
<div class="row g-2 mb-3">
<div class="col">
<label class="form-label">من تاريخ</label>
<input type="date" name="start_date" id="leaveStart" class="form-control" required>
</div>
<div class="col">
<label class="form-label">إلى تاريخ</label>
<input type="date" name="end_date" id="leaveEnd" class="form-control" required>
</div>
</div>
<div class="mb-3">
<label class="form-label">السبب</label>
<textarea name="reason" id="leaveReason" class="form-control" rows="3" placeholder="سبب الإجازة..."></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">إلغاء</button>
<button type="submit" name="request_leave" class="btn btn-primary">حفظ الطلب</button>
</div>
</form>
</div>
</div>
</div>
<script>
function resetLeaveForm() {
document.getElementById('leaveForm').reset();
document.getElementById('leaveId').value = '';
document.getElementById('leaveModalTitle').textContent = 'تقديم طلب إجازة';
}
function editLeave(btn) {
document.getElementById('leaveModalTitle').textContent = 'تعديل طلب إجازة';
document.getElementById('leaveId').value = btn.getAttribute('data-id');
document.getElementById('leaveEmp').value = btn.getAttribute('data-emp');
document.getElementById('leaveType').value = btn.getAttribute('data-type');
document.getElementById('leaveStart').value = btn.getAttribute('data-start');
document.getElementById('leaveEnd').value = btn.getAttribute('data-end');
document.getElementById('leaveReason').value = btn.getAttribute('data-reason');
}
</script>
<?php require_once 'includes/footer.php'; ?>