ليس لديك صلاحية للوصول إلى هذه الصفحة."; require_once 'includes/footer.php'; exit; } $month = $_GET['month'] ?? date('m'); $year = $_GET['year'] ?? date('Y'); $error = ''; $success = ''; // Handle Payroll Actions if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (isset($_POST['generate_payroll'])) { if (!canAdd('hr_payroll')) { $error = "لا تملك صلاحية التوليد."; } else { $gen_month = $_POST['month']; $gen_year = $_POST['year']; // Get all active employees $employees = db()->query("SELECT id, basic_salary FROM hr_employees WHERE status = 'active'")->fetchAll(); $count = 0; foreach ($employees as $emp) { // Check if already exists $stmt = db()->prepare("SELECT id FROM hr_payroll WHERE employee_id = ? AND month = ? AND year = ?"); $stmt->execute([$emp['id'], $gen_month, $gen_year]); if ($stmt->fetch()) continue; // Skip if exists // Calculate Absent Deductions $stmt = db()->prepare("SELECT COUNT(*) FROM hr_attendance WHERE employee_id = ? AND status = 'absent' AND MONTH(date) = ? AND YEAR(date) = ?"); $stmt->execute([$emp['id'], $gen_month, $gen_year]); $absent_days = $stmt->fetchColumn(); $daily_rate = $emp['basic_salary'] / 30; $deductions = round($absent_days * $daily_rate, 2); $net = $emp['basic_salary'] - $deductions; $stmt = db()->prepare("INSERT INTO hr_payroll (employee_id, month, year, basic_salary, deductions, net_salary, status) VALUES (?, ?, ?, ?, ?, ?, 'pending')"); $stmt->execute([$emp['id'], $gen_month, $gen_year, $emp['basic_salary'], $deductions, $net]); $count++; } $success = "تم توليد الرواتب لـ $count موظف."; } } elseif (isset($_POST['update_payroll'])) { if (!canEdit('hr_payroll')) { $error = "لا تملك صلاحية التعديل."; } else { $id = $_POST['id']; $bonuses = floatval($_POST['bonuses']); $deductions = floatval($_POST['deductions']); $status = $_POST['status']; // Recalculate Net $stmt = db()->prepare("SELECT basic_salary FROM hr_payroll WHERE id = ?"); $stmt->execute([$id]); $current = $stmt->fetch(); if ($current) { $net = $current['basic_salary'] + $bonuses - $deductions; $payment_date = ($status == 'paid') ? date('Y-m-d') : null; $stmt = db()->prepare("UPDATE hr_payroll SET bonuses = ?, deductions = ?, net_salary = ?, status = ?, payment_date = ? WHERE id = ?"); $stmt->execute([$bonuses, $deductions, $net, $status, $payment_date, $id]); $success = "تم تحديث الراتب."; } } } } // Fetch Payroll Records $sql = "SELECT p.*, e.first_name, e.last_name, e.job_title FROM hr_payroll p JOIN hr_employees e ON p.employee_id = e.id WHERE p.month = ? AND p.year = ? ORDER BY e.first_name"; $stmt = db()->prepare($sql); $stmt->execute([$month, $year]); $payrolls = $stmt->fetchAll(); // Calculate Totals $total_salaries = 0; foreach ($payrolls as $p) $total_salaries += $p['net_salary']; ?>

مسير الرواتب

إجمالي الرواتب للشهر

الموظف الراتب الأساسي إضافي خصومات الصافي الحالة إجراءات
لا توجد بيانات لهذا الشهر. اضغط على "توليد الرواتب" للبدء.