0 && $tenantId > 0) { if (!isset($userTenantAssignments[$userId])) { $userTenantAssignments[$userId] = []; } $userTenantAssignments[$userId][] = $tenantId; } } } // Load tenant-department assignments $tenantDepartmentAssignments = []; $tenantDepartmentRows = DB::select('select tenant_id, department_id from tenant_departments'); if (is_array($tenantDepartmentRows)) { foreach ($tenantDepartmentRows as $row) { $data = $row['tenant_departments'] ?? $row; $tenantId = (int) ($data['tenant_id'] ?? 0); $departmentId = (int) ($data['department_id'] ?? 0); if ($tenantId > 0 && $departmentId > 0) { if (!isset($tenantDepartmentAssignments[$tenantId])) { $tenantDepartmentAssignments[$tenantId] = []; } $tenantDepartmentAssignments[$tenantId][] = $departmentId; } } } foreach ($tenants as $tenant) { $tenantId = (int) ($tenant['id'] ?? 0); if ($tenantId === 0) { continue; } // Count departments for this tenant $deptCount = 0; $tenantDepartmentIds = $tenantDepartmentAssignments[$tenantId] ?? []; foreach ($departments as $dept) { $deptId = (int) ($dept['id'] ?? 0); if ($deptId > 0 && in_array($deptId, $tenantDepartmentIds, true)) { $deptCount++; } } // Count users for this tenant $totalUsers = 0; $activeUsers = 0; $inactiveUsers = 0; foreach ($users as $user) { $userId = (int) ($user['id'] ?? 0); if ($userId === 0) { continue; } // Check if user is assigned to this tenant $userTenantIds = $userTenantAssignments[$userId] ?? []; if (in_array($tenantId, $userTenantIds, true)) { $totalUsers++; $isActive = (int) ($user['active'] ?? 1); if ($isActive === 1) { $activeUsers++; } else { $inactiveUsers++; } } } $tenantBreakdown[] = [ 'id' => $tenantId, 'uuid' => $tenant['uuid'] ?? '', 'description' => $tenant['description'] ?? '', 'departments' => $deptCount, 'total_users' => $totalUsers, 'active_users' => $activeUsers, 'inactive_users' => $inactiveUsers, ]; } // Sort by description usort($tenantBreakdown, function ($a, $b) { return strcmp((string) ($a['description'] ?? ''), (string) ($b['description'] ?? '')); }); // Mail log stats $mailLogTotalCount = DB::selectValue('select count(*) from mail_log'); $mailLogTotalCount = $mailLogTotalCount ? (int) $mailLogTotalCount : 0; $mailLogSentCount = DB::selectValue("select count(*) from mail_log where status = 'sent'"); $mailLogSentCount = $mailLogSentCount ? (int) $mailLogSentCount : 0; $mailLogFailedCount = DB::selectValue("select count(*) from mail_log where status = 'failed'"); $mailLogFailedCount = $mailLogFailedCount ? (int) $mailLogFailedCount : 0; $mailLogQueuedCount = DB::selectValue("select count(*) from mail_log where status = 'queued'"); $mailLogQueuedCount = $mailLogQueuedCount ? (int) $mailLogQueuedCount : 0; // Mail log stats (last 24h) $mailLogTotal24hCount = DB::selectValue("select count(*) from mail_log where created_at >= NOW() - INTERVAL 24 HOUR"); $mailLogTotal24hCount = $mailLogTotal24hCount ? (int) $mailLogTotal24hCount : 0; $mailLogSent24hCount = DB::selectValue("select count(*) from mail_log where status = 'sent' and created_at >= NOW() - INTERVAL 24 HOUR"); $mailLogSent24hCount = $mailLogSent24hCount ? (int) $mailLogSent24hCount : 0; $mailLogFailed24hCount = DB::selectValue("select count(*) from mail_log where status = 'failed' and created_at >= NOW() - INTERVAL 24 HOUR"); $mailLogFailed24hCount = $mailLogFailed24hCount ? (int) $mailLogFailed24hCount : 0; $mailLogQueued24hCount = DB::selectValue("select count(*) from mail_log where status = 'queued' and created_at >= NOW() - INTERVAL 24 HOUR"); $mailLogQueued24hCount = $mailLogQueued24hCount ? (int) $mailLogQueued24hCount : 0; // Calculate date range for last 24 hours $date24hAgo = gmdate('Y-m-d', strtotime('-24 hours')); $dateToday = gmdate('Y-m-d'); Buffer::set('title', t('Statistics'));