• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/businessvalue.php
  • Класс: BitrixSaleBusinessValue
  • Вызов: BusinessValue::setMapping
static function setMapping($codeKey, $consumerKey, $personTypeId, array $mapping, $withCommon = false)
{
	$codeKey = ToUpper($codeKey);

	$oldMapping = self::getMapping($codeKey, $consumerKey, $personTypeId, ['MATCH' => self::MATCH_EXACT]);

	if (! $consumerKey || $consumerKey === BusinessValueTable::COMMON_CONSUMER_KEY)
		$consumerKey = null;

	if (! $personTypeId || $personTypeId === BusinessValueTable::COMMON_PERSON_TYPE_ID)
		$personTypeId = null;

	if (!array_key_exists('PROVIDER_KEY', $mapping) || !array_key_exists('PROVIDER_VALUE', $mapping))
		$mapping = array();

	$primary = array(
		'CODE_KEY'       => $codeKey,
		// TODO remove save_data_modification hack
		'CONSUMER_KEY'   => $consumerKey ?: BusinessValueTable::COMMON_CONSUMER_KEY,
		'PERSON_TYPE_ID' => $personTypeId ?: BusinessValueTable::COMMON_PERSON_TYPE_ID,
	);

	$consumerCodePersonMapping = self::getConsumerCodePersonMapping();

	if (isset($consumerCodePersonMapping[$consumerKey][$codeKey][$personTypeId]))
	{
		if ($mapping)
		{
			$savedMapping = $consumerCodePersonMapping[$consumerKey][$codeKey][$personTypeId];

			if (! ($mapping['PROVIDER_KEY'] == $savedMapping['PROVIDER_KEY']
				&& $mapping['PROVIDER_VALUE'] == $savedMapping['PROVIDER_VALUE']))
				$result = BusinessValueTable::update($primary, $mapping);
		}
		else
		{
			$result = BusinessValueTable::delete($primary);
		}
	}
	elseif ($mapping)
	{
		if ($withCommon
			&& $consumerKey
			&& !isset($consumerCodePersonMapping[''][$codeKey][''])
		)
		{
			$consumerKey = null;
			$personTypeId = null;
			$primary['CONSUMER_KEY'] = BusinessValueTable::COMMON_CONSUMER_KEY;
			$primary['PERSON_TYPE_ID'] = BusinessValueTable::COMMON_PERSON_TYPE_ID;
		}

		$result = BusinessValueTable::add($primary + $mapping);
	}

	if (isset($result))
	{
		if ($result->isSuccess())
		{
			if ($mapping)
			{
				self::$consumerCodePersonMapping[$consumerKey][$codeKey][$personTypeId] = $mapping;
			}
			else
			{
				unset(self::$consumerCodePersonMapping[$consumerKey][$codeKey][$personTypeId]);
			}

			$eventParams = [
				'CODE_KEY' => $codeKey,
				'CONSUMER_KEY' => $consumerKey,
				'PERSON_TYPE_ID' => $personTypeId,
				'OLD_MAPPING' => $oldMapping,
				'NEW_MAPPING' => $mapping,
			];
			$onSetMappingEvent = new Event('sale', self::EVENT_ON_BUSINESS_VALUE_SET_MAPPING, $eventParams);
			EventManager::getInstance()->send($onSetMappingEvent);
		}
	}
	else
	{
		$result = new BitrixMainEntityResult;
	}

	return $result;
}