• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/timeline/dealcontroller.php
  • Класс: Bitrix\Crm\Timeline\DealController
  • Вызов: DealController::onModify
public function onModify($ownerID, array $params)
{
	if (!is_int($ownerID))
	{
		$ownerID = (int)$ownerID;
	}

	if ($ownerID <= 0)
	{
		throw new Main\ArgumentException('Owner ID must be greater than zero.', 'ownerID');
	}

	$currentFields =
		isset($params['CURRENT_FIELDS']) && is_array($params['CURRENT_FIELDS'])
			? $params['CURRENT_FIELDS']
			: []
	;
	$previousFields =
		isset($params['PREVIOUS_FIELDS']) && is_array($params['PREVIOUS_FIELDS'])
			? $params['PREVIOUS_FIELDS']
			: []
	;

	$fieldsMap = $params['FIELDS_MAP'] ?? null;
	if (is_array($fieldsMap))
	{
		$currentFields = EntityFieldsHelper::replaceFieldNamesByMap($currentFields, $fieldsMap);
		$previousFields = EntityFieldsHelper::replaceFieldNamesByMap($previousFields, $fieldsMap);
	}

	$authorID = self::resolveEditorID($currentFields);

	$prevStageID = $previousFields['STAGE_ID'] ?? '';
	$curStageID = $currentFields['STAGE_ID'] ?? $prevStageID;

	$categoryID = isset($previousFields['CATEGORY_ID']) ? (int)$previousFields['CATEGORY_ID'] : -1;
	if ($categoryID < 0)
	{
		$categoryID = \CCrmDeal::GetCategoryID($ownerID);
	}

	$categoryChanged = false;
	if (isset($previousFields['CATEGORY_ID']) && isset($currentFields['CATEGORY_ID']) && $previousFields['CATEGORY_ID'] != $currentFields['CATEGORY_ID'])
	{
		$categoryChanged = true;

		$currentCategoryId = (int)$currentFields['CATEGORY_ID'];
		$prevCategoryId = (int)$previousFields['CATEGORY_ID'];
		$factory = Crm\Service\Container::getInstance()->getFactory(\CCrmOwnerType::Deal);
		$currentCategory = $factory ? $factory->getCategory($currentCategoryId) : null;
		$prevCategory = $factory ? $factory->getCategory($prevCategoryId) : null;
		$currentStage = $factory ? $factory->getStage($curStageID) : null;
		$prevStage = $factory ? $factory->getStage($prevStageID) : null;

		$historyEntryID = ModificationEntry::create(
			array(
				'ENTITY_TYPE_ID' => \CCrmOwnerType::Deal,
				'ENTITY_ID' => $ownerID,
				'AUTHOR_ID' => $authorID,
				'SETTINGS' => array(
					'FIELD' => 'CATEGORY_ID',
					'START_CATEGORY_ID' => $prevCategoryId,
					'FINISH_CATEGORY_ID' => $currentCategoryId,
					'START_CATEGORY_NAME' => $prevCategory ? $prevCategory->getName() : $prevCategoryId,
					'FINISH_CATEGORY_NAME' => $currentCategory ? $currentCategory->getName() : $currentCategoryId,
					'START_STAGE_ID' => $prevStageID,
					'FINISH_STAGE_ID' => $curStageID,
					'START_STAGE_NAME' => $prevStage ? $prevStage->getName() : $prevStageID,
					'FINISH_STAGE_NAME' => $currentStage ? $currentStage->getName() : $curStageID
				)
			)
		);
		$this->sendPullEventOnAdd(new Crm\ItemIdentifier(\CCrmOwnerType::Deal, $ownerID), $historyEntryID);
	}

	if (!$categoryChanged && $prevStageID !== $curStageID)
	{
		$stageNames = \CCrmDeal::GetStageNames($categoryID);
		$historyEntryID = ModificationEntry::create(
			array(
				'ENTITY_TYPE_ID' => \CCrmOwnerType::Deal,
				'ENTITY_ID' => $ownerID,
				'AUTHOR_ID' => $authorID,
				'SETTINGS' => array(
					'FIELD' => 'STAGE_ID',
					'START' => $prevStageID,
					'FINISH' => $curStageID,
					'START_NAME' => $stageNames[$prevStageID] ?? $prevStageID,
					'FINISH_NAME' => $stageNames[$curStageID] ?? $curStageID
				)
			)
		);
		$this->sendPullEventOnAdd(new Crm\ItemIdentifier(\CCrmOwnerType::Deal, $ownerID), $historyEntryID);
	}

	$this->createManualOpportunityModificationEntryIfNeeded($ownerID, $authorID, $currentFields, $previousFields);
}