ليس لديك صلاحية للوصول إلى هذه الصفحة."; 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(); ?>
| الموظف | نوع الإجازة | الفترة | المدة | السبب | الحالة | المعتمد | إجراءات |
|---|---|---|---|---|---|---|---|
| لا توجد طلبات. | |||||||
| = htmlspecialchars($req['first_name'] . ' ' . $req['last_name']) ?> | 'سنوية', 'sick' => 'مرضية', 'unpaid' => 'بدون راتب', 'maternity' => 'أمومة', 'emergency' => 'طارئة', 'other' => 'أخرى' ]; echo $type_map[$req['leave_type']] ?? $req['leave_type']; ?> |
من = $req['start_date'] ?> إلى = $req['end_date'] ?> |
= $req['days_count'] ?> يوم | = htmlspecialchars($req['reason']) ?> | 'success', 'rejected' => 'danger', default => 'warning' }; $status_txt = match($req['status']) { 'approved' => 'مقبولة', 'rejected' => 'مرفوضة', default => 'معلقة' }; ?> = $status_txt ?> | = htmlspecialchars($req['approver_name'] ?? '-') ?> | |