ليس لديك صلاحية للوصول إلى هذه الصفحة."; 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') { $action = $_POST['action'] ?? ''; 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 * 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; $journal_id = $current['journal_id']; try { $db = db(); // Update Payroll Record $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]); // Accounting Integration if ($status == 'paid') { // Create or Update Journal Entry $salary_account = 'مصروفات الرواتب'; // Account ID 13 $cash_account = 'النقدية'; // Account ID 17 $entries = [ ['account' => $salary_account, 'debit' => $net, 'credit' => 0], ['account' => $cash_account, 'debit' => 0, 'credit' => $net] ]; // Get Employee Name for Description $stmt_emp = $db->prepare("SELECT first_name, last_name FROM hr_employees WHERE id = ?"); $stmt_emp->execute([$current['employee_id']]); $emp_name = $stmt_emp->fetch(PDO::FETCH_ASSOC); $emp_full_name = $emp_name ? ($emp_name['first_name'] . ' ' . $emp_name['last_name']) : "Employee #{$current['employee_id']}"; $description = "Salary Payment: $emp_full_name ({$current['month']}/{$current['year']})"; $reference = "PAY-{$current['year']}-{$current['month']}-{$current['employee_id']}"; if ($journal_id) { edit_journal_entry($journal_id, $payment_date, $description, $reference, $entries); } else { $jid = add_journal_entry($payment_date, $description, $reference, $entries); if ($jid) { $db->prepare("UPDATE hr_payroll SET journal_id = ? WHERE id = ?")->execute([$jid, $id]); } } } elseif ($status == 'pending' && $journal_id) { // If changing back to pending, delete the journal entry delete_journal_entry($journal_id); $db->prepare("UPDATE hr_payroll SET journal_id = NULL WHERE id = ?")->execute([$id]); } $success = "تم تحديث الراتب."; } catch (Exception $e) { $error = "حدث خطأ: " . $e->getMessage(); } } } } } // 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']; ?>

مسير الرواتب

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

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