• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/clientinfo.php
  • Класс: Bitrix\Crm\ClientInfo
  • Вызов: ClientInfo::createFromOwner
static function createFromOwner(int $ownerTypeId, int $ownerId): self
{
	$withOwner = false;

	$companyId = null;
	$contactIds = [];

	if ($ownerTypeId === CCrmOwnerType::Lead)
	{
		$lead = LeadTable::getById($ownerId)->fetch();
		if ($lead)
		{
			$companyId = (int)$lead['COMPANY_ID'];
			$contactIds = LeadContactTable::getLeadContactIDs($ownerId);
		}
	}
	elseif ($ownerTypeId === CCrmOwnerType::Deal)
	{
		$deal = DealTable::getById($ownerId)->fetch();
		if ($deal)
		{
			$contactIds = DealContactTable::getDealContactIDs($ownerId);
			$companyId = (int)$deal['COMPANY_ID'];
			$withOwner = true;
		}
	}
	elseif ($ownerTypeId === CCrmOwnerType::Contact)
	{
		$contactIds = [(int)$ownerId];
	}
	elseif ($ownerTypeId === CCrmOwnerType::Company)
	{
		$companyId = (int)$ownerId;
	}
	elseif ($ownerTypeId === CCrmOwnerType::Order)
	{
		$order = Order::load($ownerId);
		if ($order)
		{
			$collection = $order->getContactCompanyCollection();
			$company = $collection->getPrimaryCompany();
			if ($company)
			{
				$companyId = (int)$company->getField('ENTITY_ID');
			}

			$contacts = $collection->getContacts();
			foreach ($contacts as $contact)
			{
				$contactIds[] = (int)$contact->getField('ENTITY_ID');
			}
		}
	}
	elseif ($ownerTypeId === CCrmOwnerType::Invoice)
	{
		$invoice = CCrmInvoice::GetByID($ownerId);
		if ($invoice)
		{
			$companyID = $invoice['UF_COMPANY_ID'] ?? 0;
			if ($companyID)
			{
				$companyId = $companyID;
			}

			$contactID = $invoice['UF_CONTACT_ID'] ?? 0;
			if ($contactID)
			{
				$contactIds[] = $contactID;
			}
		}
	}
	else
	{
		$factory = Container::getInstance()->getFactory($ownerTypeId);
		if ($factory)
		{
			$item = $factory->getItem($ownerId);
			if ($item && $item->getCompanyId())
			{
				$companyId = (int)$item->getCompanyId();
			}

			if ($item && $item->getContactId())
			{
				$contactIds = [(int)$item->getContactId()];
			}
		}
		$withOwner = true;
	}

	$self = new static(
		$companyId,
		$contactIds
	);
	if ($withOwner)
	{
		$self->ownerId = $ownerId;
		$self->ownerTypeId = $ownerTypeId;
	}

	return $self;
}