• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/order/builder/orderbuilderrest.php
  • Класс: Bitrix\Crm\Order\Builder\OrderBuilderRest
  • Вызов: OrderBuilderRest::buildRequisiteLink
public function buildRequisiteLink()
{
	$requisite = new EntityRequisite();

	if(isset($this->formData["REQUISITE_LINK"]) && empty($this->formData["REQUISITE_LINK"]))
	{
		if(!$this->removeRequisiteLink())
		{
			return $this;
		}
	}

	if(isset($this->formData["REQUISITE_LINK"]) && is_array($this->formData["REQUISITE_LINK"]))
	{
		$hasCompany = false;
		$requisiteIdLink = $this->formData["REQUISITE_LINK"]['REQUISITE_ID'];

		$contactCompanyCollection = $this->getOrder()->getContactCompanyCollection();
		/** @var ContactCompanyEntity $client */
		foreach ($contactCompanyCollection as $client)
		{
			if($client->getField('ENTITY_TYPE_ID') == \CCrmOwnerType::Company)
			{
				$hasCompany = true;
				$requisiteCount = $requisite->getCountByFilter(
					array(
						'ENTITY_TYPE_ID' => \CCrmOwnerType::Company,
						'ENTITY_ID' => $client->getField('ENTITY_ID'),
						'ID' => $requisiteIdLink
					)
				);
				if($requisiteCount === 0)
				{
					$this->getErrorsContainer()->addError(new Error('requisite by company - is not exists ['.$requisiteIdLink.']'));
					throw new BuildingException();
				}
			}
		}

		if($hasCompany === false)
		{
			foreach ($contactCompanyCollection as $client)
			{
				if($client->getField('ENTITY_TYPE_ID') == \CCrmOwnerType::Contact)
				{
					$requisiteCount = $requisite->getCountByFilter(
						array(
							'ENTITY_TYPE_ID' => \CCrmOwnerType::Contact,
							'ENTITY_ID' => $client->getField('ENTITY_ID'),
							'ID' => $requisiteIdLink
						)
					);
					if($requisiteCount === 0)
					{
						$this->getErrorsContainer()->addError(new Error('requisite by contact - is not exists ['.$requisiteIdLink.']'));
						throw new BuildingException();
					}
				}
			}
		}

		$this->getOrder()->setRequisiteLink($this->formData["REQUISITE_LINK"]);
	}

	return $this;
}