• Модуль: imopenlines
  • Путь к файлу: ~/bitrix/modules/imopenlines/lib/crm.php
  • Класс: BitrixImOpenLinesCrm
  • Вызов: Crm::registerTouch
protected function registerTouch(): Result
{
	$result = new Result;

	$facility = $this->getEntityManageFacility();
	$fields = $this->getFields();
	$session = $fields->getSession();

	if (Loader::includeModule('crm'))
	{
		if ($session !== null)
		{
			if ($this->isSkipCreate())
			{
				$facility->setRegisterMode($facility::REGISTER_MODE_ONLY_UPDATE);
			}

			$facility->setUpdateClientMode($facility::UPDATE_MODE_NONE);

			$isCorrectEntity = $this->isFieldsCrmEntityCorrect();
			if ($isCorrectEntity->isSuccess())
			{
				$oldRegisterMode = $facility->getRegisterMode();

				//The creation mode of the deal
				if ($session->getConfig('CRM_CREATE') === Config::CRM_CREATE_DEAL)
				{
					$crmOwnerType = CCrmOwnerType::Deal;
					$contactId = $facility->getSelector()->getContactId();

					if (!$contactId)
					{
						$fieldsContactAdd = $this->getFieldsAddContact()->getData();

						$isRegisterContact = $facility->registerContact($fieldsContactAdd, true, [
							'CURRENT_USER' => $this->getResponsibleCrmId(),
							'DISABLE_USER_FIELD_CHECK' => true
						]);

						if (
							$isRegisterContact &&
							$facility->getRegisteredId() &&
							$facility->getRegisteredTypeId() === CCrmOwnerType::Contact
						)
						{
							$contactId = $facility->getRegisteredId();
						}
						elseif ($facility->hasErrors())
						{
							$errorDescriptions = implode(';', $facility->getErrorMessages());
							$result->addError(new Error($errorDescriptions, self::ERROR_IMOL_CREATING_CRM_ENTITY, __METHOD__, $fieldsContactAdd));
						}
					}

					if ($contactId)
					{
						$facility->getSelector()->setEntity(CCrmOwnerType::Contact, $contactId);

						$fieldsAdd = $this->getFieldsAddDeal()->getData();

						if ($session->getConfig('CRM_CREATE_THIRD') === 'N')
						{
							$facility->setRegisterMode($facility::REGISTER_MODE_ALWAYS_ADD);
						}
					}
				}
				//Mode for creating leads. By default.
				else
				{
					$crmOwnerType = CCrmOwnerType::Lead;
					$fieldsAdd = $this->getFieldsAddLead()->getData();
				}

				if (!empty($fieldsAdd))
				{
					$isRegisterEntity = $facility->registerTouch($crmOwnerType, $fieldsAdd, true, [
						'CURRENT_USER' => $this->getResponsibleCrmId(),
						'DISABLE_USER_FIELD_CHECK' => true
					]);

					if (
						$isRegisterEntity !== true &&
						$facility->hasErrors()
					)
					{
						$errorDescriptions = implode(';', $facility->getErrorMessages());
						$result->addError(new Error($errorDescriptions, self::ERROR_IMOL_CREATING_CRM_ENTITY, __METHOD__, $fieldsAdd));
					}
				}
				else
				{
					$result->addError(new Error(self::ERROR_IMOL_CREATING_CRM_ENTITY, '', __METHOD__));
				}

				//Resetting the entity registration mode
				if ($oldRegisterMode !== $facility->getRegisterMode())
				{
					$facility->setRegisterMode($oldRegisterMode);
				}
			}
		}
		else
		{
			$result->addError(new Error(Loc::getMessage('IMOL_CRM_ERROR_NO_SESSION'), self::ERROR_IMOL_NO_SESSION, __METHOD__));
		}
	}
	else
	{
		$result->addError(new Error(Loc::getMessage('IMOL_CRM_ERROR_NOT_LOAD_CRM'), self::ERROR_IMOL_NOT_LOAD_CRM, __METHOD__));
	}

	return $result;
}