• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/exclusion/store.php
  • Класс: Bitrix\Crm\Exclusion\Store
  • Вызов: Store::addFromEntities
static function addFromEntities($entityTypeId, array $entities, $comment = null)
{
	$batch = [];

	$list = [];
	$entityTypeId = (int) $entityTypeId;
	switch ($entityTypeId)
	{
		case \CCrmOwnerType::Contact:
		case \CCrmOwnerType::Company:
			$batch[$entityTypeId] = $entities;
			break;

		case \CCrmOwnerType::Lead:
			$leads = LeadTable::getList([
				'select' => [
					'ID', 'IS_RETURN_CUSTOMER', 'COMPANY_ID', 'CONTACT_ID',
					'HAS_EMAIL', 'HAS_PHONE'
				],
				'filter' => ['=ID' => $entities]
			]);
			foreach ($leads as $entity)
			{
				if ($entity['IS_RETURN_CUSTOMER'] === 'Y')
				{
					$list[] = [
						'CONTACT_ID' => $entity['CONTACT_ID'],
						'COMPANY_ID' => $entity['COMPANY_ID'],
					];
				}
				elseif ($entity['HAS_EMAIL'] === 'Y' || $entity['HAS_PHONE'] === 'Y')
				{
					$batch[\CCrmOwnerType::Lead][] = $entity['ID'];
				}
			}
			break;

		case \CCrmOwnerType::Deal:
			$list = DealTable::getList([
				'select' => ['COMPANY_ID', 'CONTACT_ID'],
				'filter' => ['=ID' => $entities]
			]);
			break;

		case \CCrmOwnerType::Quote:
			$list = QuoteTable::getList([
				'select' => ['COMPANY_ID', 'CONTACT_ID'],
				'filter' => ['=ID' => $entities]
			]);
			break;

		default:
			throw new NotSupportedException("Entity type ID `$entityTypeId` not supported.");
	}

	foreach ($list as $entity)
	{
		if ($entity['CONTACT_ID'])
		{
			$batch[\CCrmOwnerType::Contact][] = $entity['CONTACT_ID'];
		}
		if ($entity['COMPANY_ID'])
		{
			$batch[\CCrmOwnerType::Company][] = $entity['COMPANY_ID'];
		}
	}


	if (empty($batch))
	{
		return;
	}


	$typeMap = [
		\CCrmFieldMulti::PHONE => Communication\Type::PHONE,
		\CCrmFieldMulti::EMAIL => Communication\Type::EMAIL,
	];
	foreach ($batch as $entityTypeId => $list)
	{
		$list = FieldMultiTable::getList([
			'select' => ['TYPE_ID', 'VALUE'],
			'filter' => [
				'=ENTITY_ID' => \CCrmOwnerType::resolveName($entityTypeId),
				'=ELEMENT_ID' => $list,
				'=TYPE_ID' => array_keys($typeMap)
			],
		]);
		foreach ($list as $item)
		{
			self::add($typeMap[$item['TYPE_ID']], $item['VALUE'], $comment);
		}
	}
}