role ?? 'viewer'; $assignedCompanyId = $user->assigned_company_id ?? null; if ($role === 'super_admin' || $role === 'admin') { $stmt = $db->prepare("SELECT i.*, c.name as company_name FROM invoices i JOIN companies c ON i.company_id = c.id WHERE i.tenant_id = ? AND i.deleted_at IS NULL ORDER BY i.created_at DESC"); $stmt->execute([$tenantId]); } else { $stmt = $db->prepare("SELECT i.*, c.name as company_name FROM invoices i JOIN companies c ON i.company_id = c.id WHERE i.tenant_id = ? AND i.company_id = ? AND i.deleted_at IS NULL ORDER BY i.created_at DESC"); $stmt->execute([$tenantId, $assignedCompanyId]); } return $stmt->fetchAll() ?: []; } }