prepare("SELECT tenant_id FROM users WHERE id = ?"); $stmt->execute([$userId]); $userTenant = $stmt->fetchColumn(); if ($userTenant !== $decoded['tenant_id']) { json_error('User does not belong to your office', 403); } } $stmt = $db->prepare(" INSERT INTO user_company_assignments (id, user_id, company_id, is_active, created_at) VALUES (?, ?, ?, 1, ?) ON DUPLICATE KEY UPDATE is_active = 1 "); $stmt->execute([ Database::generateUuid(), $userId, $companyId, date('Y-m-d H:i:s') ]); json_success(null, 'تم تخصيص المستخدم للشركة بنجاح'); } catch (\Exception $e) { json_error('حدث خطأ أثناء التخصيص: ' . $e->getMessage(), 500); }