• Модуль: voximplant
  • Путь к файлу: ~/bitrix/modules/voximplant/classes/general/vi_crm_helper.php
  • Класс: CVoxImplantCrmHelper
  • Вызов: CVoxImplantCrmHelper::registerCallInCrm
static function registerCallInCrm(VICall $call, $config = null)
{
	if(!BitrixMainLoader::includeModule('crm'))
	{
		static::$lastError = 'CRM module is not installed';
		return false;
	}

	if($call->getCallerId() == '')
	{
		static::$lastError = 'Can not register in CRM call without caller id';
		return false;
	}

	$isCallRegisteredInCrmEventSent = false;
	if (!empty($call->getCrmBindings()) && static::isNewCallScenarioEnabled())
	{
		static::sendCallRegisteredInCrmEvent($call);

		$isCallRegisteredInCrmEventSent = true;
	}

	$shouldCreateLead = true;
	if(!is_array($config))
	{
		$config = $call->getConfig();
	}

	if(is_array($config))
	{
		$shouldCreateLead = static::shouldCreateLead($call, $config);
	}
	if(!$shouldCreateLead)
	{
		return true;
	}

	$arFields = static::getLeadFields([
		'USER_ID' => $call->getUserId(),
		'PHONE_NUMBER' => $call->getCallerId(),
		'SEARCH_ID' => $call->getPortalNumber(),
		'EXTERNAL_LINE_ID' => $call->getExternalLineId(),
		'CRM_SOURCE' => $config['CRM_SOURCE'],
		'INCOMING' => $call->getIncoming(),
	]);

	if(!$arFields)
	{
		return false;
	}

	$entityManager = VIIntegrationCrmEntityManagerRegistry::getWithCall($call);
	if(!$entityManager)
	{
		return false;
	}

	if ($call->getIncoming() == CVoxImplantMain::CALL_OUTGOING && !empty($entityManager->getActivityBindings()))
	{
		$entityManager->setRegisterMode($entityManager::REGISTER_MODE_ONLY_UPDATE);
	}

	$entityManager->setDirection(
		($call->getIncoming() == CVoxImplantMain::CALL_INCOMING || $call->getIncoming() == CVoxImplantMain::CALL_INCOMING_REDIRECT)
		?
		$entityManager::DIRECTION_INCOMING
		:
		$entityManager::DIRECTION_OUTGOING
	);

	$entityManager->setTrace(
		TrackingTrace::create()->addChannel(
			new TrackingChannelCall($call->getPortalNumber())
		)
	);

	$isSuccessful = $entityManager->registerTouch(
		CCrmOwnerType::Lead,
		$arFields,
		true,
		[
			'CURRENT_USER' => $call->getUserId(),
			'DISABLE_USER_FIELD_CHECK' => true
		]
	);

	if(!$isSuccessful)
	{
		if($entityManager->hasErrors())
		{
			$errors = $entityManager->getErrorMessages();
			static::$lastError = end($errors);
			CVoxImplantHistory::WriteToLog(join(';', $entityManager->getErrorMessages()), 'ERROR CREATING LEAD');
		}
		return false;
	}

	if ($entityManager->getRegisteredTypeId() == CCrmOwnerType::Lead)
	{
		BitrixCrmIntegrationChannelVoxImplantTracker::getInstance()->registerLead($entityManager->getRegisteredId());

		if(CVoxImplantConfig::GetLeadWorkflowExecution() == CVoxImplantConfig::WORKFLOW_START_IMMEDIATE)
		{
			self::StartLeadWorkflow($entityManager->getRegisteredId());
		}
	}
	else if ($entityManager->getRegisteredTypeId() == CCrmOwnerType::Deal)
	{
		BitrixCrmIntegrationChannelVoxImplantTracker::getInstance()->registerDeal($entityManager->getRegisteredId());
	}

	$registeredEntites = [];
	/** @var BitrixCrmEntityIdentificatorComplex $registeredEntity */
	foreach ($entityManager->getRegisteredEntities() as $registeredEntity)
	{
		$registeredEntites[] = [
			'ENTITY_TYPE' => CCrmOwnerType::ResolveName($registeredEntity->getTypeId()),
			'ENTITY_ID' => $registeredEntity->getId(),
			'IS_CREATED' => 'Y',
			'IS_PRIMARY' => ($registeredEntity->getTypeId() == $entityManager->getPrimaryTypeId() && $registeredEntity->getId() == $entityManager->getPrimaryId()) ? 'Y' : 'N'
		];
	}

	CVoxImplantHistory::WriteToLog($registeredEntites, "Created CRM entities");

	$call->addCrmEntities($registeredEntites);
	if(!empty($registeredEntites))
	{
		$call->updateCrmBindings($entityManager->getActivityBindings());
	}

	if (!$isCallRegisteredInCrmEventSent && static::isNewCallScenarioEnabled())
	{
		static::sendCallRegisteredInCrmEvent($call);
	}

	return true;
}