prepare($query); $stmt->execute($params); return $stmt->fetchColumn() ?: 0; } catch (Exception $e) { return 0; } } function getSafeValue($query, $params = []) { try { $stmt = db()->prepare($query); $stmt->execute($params); return $stmt->fetchColumn() ?: 0; } catch (Exception $e) { return 0; } } // 1. Mail & Tasks $total_inbound = canView('inbound') ? getSafeCount("SELECT COUNT(*) FROM inbound_mail") : 0; $total_outbound = canView('outbound') ? getSafeCount("SELECT COUNT(*) FROM outbound_mail") : 0; $total_internal = canView('internal') ? getSafeCount("SELECT COUNT(*) FROM internal_mail") : 0; $in_progress_count = 0; $overdue_count = 0; try { $statuses_data = db()->query("SELECT * FROM mailbox_statuses")->fetchAll(PDO::FETCH_UNIQUE); $in_progress_id = null; foreach ($statuses_data as $id => $s) { if ($s['name'] == 'in_progress') { $in_progress_id = $id; break; } } if ($in_progress_id) { if (canView('inbound')) $in_progress_count += getSafeCount("SELECT COUNT(*) FROM inbound_mail WHERE status_id = ?", [$in_progress_id]); if (canView('outbound')) $in_progress_count += getSafeCount("SELECT COUNT(*) FROM outbound_mail WHERE status_id = ?", [$in_progress_id]); if (canView('internal')) $in_progress_count += getSafeCount("SELECT COUNT(*) FROM internal_mail WHERE status_id = ?", [$in_progress_id]); } if (canView('reports')) { $overdue_count += getSafeCount("SELECT COUNT(*) FROM inbound_mail WHERE due_date < CURDATE() AND status_id NOT IN (SELECT id FROM mailbox_statuses WHERE name IN ('closed', 'مكتمل', 'مؤرشف', 'مؤرشفة'))"); $overdue_count += getSafeCount("SELECT COUNT(*) FROM outbound_mail WHERE due_date < CURDATE() AND status_id NOT IN (SELECT id FROM mailbox_statuses WHERE name IN ('closed', 'مكتمل', 'مؤرشف', 'مؤرشفة'))"); $overdue_count += getSafeCount("SELECT COUNT(*) FROM internal_mail WHERE due_date < CURDATE() AND status_id NOT IN (SELECT id FROM mailbox_statuses WHERE name IN ('closed', 'مكتمل', 'مؤرشف', 'مؤرشفة'))"); } } catch (Exception $e) {} // 2. HR & Employees $total_employees = canView('hr_employees') ? getSafeCount("SELECT COUNT(*) FROM hr_employees") : 0; $pending_leaves = canView('hr_leaves') ? getSafeCount("SELECT COUNT(*) FROM hr_leaves WHERE status = 'pending'") : 0; $present_today = canView('hr_attendance') ? getSafeCount("SELECT COUNT(DISTINCT employee_id) FROM hr_attendance WHERE date = CURDATE() AND status = 'present'") : 0; // 3. Accounting & Expenses $total_expenses_month = canView('expenses') ? getSafeValue("SELECT SUM(amount) FROM expenses WHERE MONTH(date) = MONTH(CURRENT_DATE()) AND YEAR(date) = YEAR(CURRENT_DATE())") : 0; $total_accounts = canView('accounting') ? getSafeCount("SELECT COUNT(*) FROM accounting_accounts") : 0; // 4. Stock & Inventory $total_stock_items = canView('stock') ? getSafeCount("SELECT COUNT(*) FROM stock_items") : 0; $low_stock = canView('stock') ? getSafeCount("SELECT COUNT(*) FROM (SELECT si.id FROM stock_items si LEFT JOIN stock_quantities sq ON si.id = sq.item_id GROUP BY si.id, si.min_quantity HAVING COALESCE(SUM(sq.quantity), 0) <= si.min_quantity) AS t") : 0; // 5. Charity & Committees $total_charity_members = canView('charity_members') ? getSafeCount("SELECT COUNT(*) FROM charity_members") : 0; $total_committees = canView('committees') ? getSafeCount("SELECT COUNT(*) FROM committees") : 0; // 6. Events & Meetings $upcoming_meetings = canView('meetings') ? getSafeCount("SELECT COUNT(*) FROM meetings WHERE DATE(start_time) >= CURRENT_DATE() AND status != 'completed'") : 0; $upcoming_events = canView('events') ? getSafeCount("SELECT COUNT(*) FROM events WHERE event_date >= CURRENT_DATE()") : 0; // 7. Users $total_users = canView('users') ? getSafeCount("SELECT COUNT(*) FROM users") : 0; ?>