• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/merger/leadmerger.php
  • Класс: Bitrix\Crm\Merger\LeadMerger
  • Вызов: LeadMerger::innerPrepareEntityFieldMergeData
protected function innerPrepareEntityFieldMergeData($fieldID, array $fieldParams,  array $seeds, array $targ, array $options = null)
{
	if($fieldID === 'CONTACT_IDS')
	{
		$enabledIdsMap = null;
		if(isset($options['enabledIds']) && is_array($options['enabledIds']))
		{
			$enabledIdsMap = array_fill_keys($options['enabledIds'], true);
		}

		$sourceEntityIDs = array();
		$resultContactBindings = array();
		foreach($seeds as $seed)
		{
			$seedID = (int)$seed['ID'];
			if(is_null($enabledIdsMap) || isset($enabledIdsMap[$seedID]))
			{
				$seedContactBindings = Binding\LeadContactTable::getLeadBindings($seedID);
				if(!empty($seedContactBindings))
				{
					$sourceEntityIDs[] = $seedID;
					self::mergeEntityBindings(
						\CCrmOwnerType::Contact,
						$seedContactBindings,
						$resultContactBindings
					);
				}
			}
		}

		$targID = (int)$targ['ID'];
		if(is_null($enabledIdsMap) || isset($enabledIdsMap[$targID]))
		{
			$targContactBindings = Binding\LeadContactTable::getLeadBindings($targID);
			if(!empty($targContactBindings))
			{
				$sourceEntityIDs[] = $targID;
				self::mergeEntityBindings(
					\CCrmOwnerType::Contact,
					$targContactBindings,
					$resultContactBindings
				);
			}
		}

		return array(
			'FIELD_ID' => 'CONTACT_IDS',
			'TYPE' => 'crm_contact',
			'IS_MERGED' => true,
			'IS_MULTIPLE' => true,
			'SOURCE_ENTITY_IDS' => array_unique($sourceEntityIDs, SORT_NUMERIC),
			'VALUE' => Binding\EntityBinding::prepareEntityIDs(\CCrmOwnerType::Contact, $resultContactBindings),
		);
	}
	// check all address components
	if($fieldID === 'ADDRESS')
	{
		$addressFields = [];
		$result = parent::innerPrepareEntityFieldMergeData(
			'ADDRESS_LOC_ADDR_ID',
			$fieldParams,
			$seeds,
			$targ,
			$options
		);
		if ($result['VALUE'] > 0)
		{
			$addressFields['LOC_ADDR_ID'] = $result['VALUE'];
		}
		else
		{
			$result = parent::innerPrepareEntityFieldMergeData(
				$fieldID,
				$fieldParams,
				$seeds,
				$targ,
				$options
			);
			foreach (self::getBaseAddressFieldNames() as $addrFieldId)
			{
				if ($addrFieldId === $fieldID)
				{
					continue;
				}
				$extraFieldMergeResult = parent::innerPrepareEntityFieldMergeData(
					$addrFieldId,
					$fieldParams,
					$seeds,
					$targ,
					$options
				);

				if (empty($result['SOURCE_ENTITY_IDS']))
				{
					$result = $extraFieldMergeResult;
				}

				$result['IS_MERGED'] = $result['IS_MERGED'] && $extraFieldMergeResult['IS_MERGED'];
				if (!$result['IS_MERGED'])
				{
					break;
				}
			}

			$addressSourceId = $result['SOURCE_ENTITY_IDS'][0] ?? null;
			if ($addressSourceId)
			{
				$addressSource = null;
				if ($targ['ID'] === $addressSourceId)
				{
					$addressSource = $targ;
				}
				else
				{
					foreach ($seeds as $seed)
					{
						if ($seed['ID'] === $addressSourceId)
						{
							$addressSource = $seed;
							break;
						}
					}
				}
				if ($addressSource)
				{
					foreach (self::getBaseAddressFieldNames() as $addrFieldId)
					{
						$addressValue = (string)$addressSource[$addrFieldId];
						if ($addressValue !== '')
						{
							if ($addrFieldId !== 'ADDRESS' && $addrFieldId !== 'ADDRESS_2')
							{
								$addrFieldId = str_replace('ADDRESS_', '', $addrFieldId);
							}
							if ($addrFieldId === 'ADDRESS')
							{
								$addrFieldId = 'ADDRESS_1';
							}
							$addressFields[$addrFieldId] = $addressValue;
						}
					}
				}
			}
		}

		if (Main\Loader::includeModule('location') && !empty($addressFields))
		{
			$address = \Bitrix\Crm\EntityAddress::makeLocationAddressByFields($addressFields);
			if ($address)
			{
				$result['VALUE'] = $address->toJson();
			}
		}

		return $result;
	}
	return parent::innerPrepareEntityFieldMergeData($fieldID, $fieldParams, $seeds, $targ, $options);
}