• Модуль: imopenlines
  • Путь к файлу: ~/bitrix/modules/imopenlines/lib/crm.php
  • Класс: BitrixImOpenLinesCrm
  • Вызов: Crm::updateFlags
protected function updateFlags(): Result
{
	$result = new Result;
	$fields = $this->getFields();
	$session = $fields->getSession();
	$updateSession = [];
	$updateChat = [];

	if (!empty($session))
	{
		if ($this->activityId > 0)
		{
			//update session
			$updateSession['CRM_ACTIVITY_ID'] = $this->activityId;

			if (!empty($this->registeredEntites))
			{
				foreach ($this->registeredEntites as $entity)
				{
					switch ($entity['ENTITY_TYPE'])
					{
						case CCrmOwnerType::LeadName:
							if (empty($updateChat['LEAD']))
							{
								$updateChat['LEAD'] = $entity['ENTITY_ID'];
								$updateSession['CRM_CREATE_LEAD'] = 'Y';
							}
							break;

						case CCrmOwnerType::DealName:
							if (empty($updateChat['DEAL']))
							{
								$updateChat['DEAL'] = $entity['ENTITY_ID'];
								$updateSession['CRM_CREATE_DEAL'] = 'Y';
							}
							break;

						case CCrmOwnerType::ContactName:
							if (empty($updateChat['CONTACT']))
							{
								$updateChat['CONTACT'] = $entity['ENTITY_ID'];
								$updateSession['CRM_CREATE_CONTACT'] = 'Y';
							}
							break;

						case CCrmOwnerType::CompanyName:
							if (empty($updateChat['COMPANY']))
							{
								$updateChat['COMPANY'] = $entity['ENTITY_ID'];
								$updateSession['CRM_CREATE_COMPANY'] = 'Y';
							}
							break;
					}
				}
			}

			//update chat
			foreach ($this->updateEntites as $entity)
			{
				switch ($entity['ENTITY_TYPE'])
				{
					case CCrmOwnerType::LeadName:
						if (empty($updateChat['LEAD']))
						{
							$updateChat['LEAD'] = $entity['ENTITY_ID'];
						}
						break;

					case CCrmOwnerType::DealName:
						if (empty($updateChat['DEAL']))
						{
							$updateChat['DEAL'] = $entity['ENTITY_ID'];
						}
						break;

					case CCrmOwnerType::ContactName:
						if (empty($updateChat['CONTACT']))
						{
							$updateChat['CONTACT'] = $entity['ENTITY_ID'];
						}
						break;

					case CCrmOwnerType::CompanyName:
						if (empty($updateChat['COMPANY']))
						{
							$updateChat['COMPANY'] = $entity['ENTITY_ID'];
						}
						break;
				}
			}

			//For backward compatibility, the most up-to-date entity.
			if (!empty($updateChat))
			{
				if (!empty($updateChat['DEAL']))
				{
					$updateChat['ENTITY_TYPE'] = CCrmOwnerType::DealName;
					$updateChat['ENTITY_ID'] = $updateChat['DEAL'];
				}
				elseif(!empty($updateChat['LEAD']))
				{
					$updateChat['ENTITY_TYPE'] = CCrmOwnerType::LeadName;
					$updateChat['ENTITY_ID'] = $updateChat['LEAD'];
				}
				elseif(!empty($updateChat['COMPANY']))
				{
					$updateChat['ENTITY_TYPE'] = CCrmOwnerType::CompanyName;
					$updateChat['ENTITY_ID'] = $updateChat['COMPANY'];
				}
				elseif(!empty($updateChat['CONTACT']))
				{
					$updateChat['ENTITY_TYPE'] = CCrmOwnerType::ContactName;
					$updateChat['ENTITY_ID'] = $updateChat['CONTACT'];
				}

				$updateChat['CRM'] = 'Y';
			}
		}

		if (!empty($updateSession))
		{
			$session->updateCrmFlags($updateSession);
		}
		if (!empty($updateChat))
		{
			$session->getChat()->setCrmFlag($updateChat);
		}
	}
	else
	{
		$result->addError(new Error(Loc::getMessage('IMOL_CRM_ERROR_NO_SESSION'), self::ERROR_IMOL_NO_SESSION, __METHOD__));
	}

	return $result;
}