• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/attribute/fieldattributemanager.php
  • Класс: Bitrix\Crm\Attribute\FieldAttributeManager
  • Вызов: FieldAttributeManager::processPhaseCreation
static function processPhaseCreation(
	string $phaseID,
	int $entityTypeID,
	string $entityScope,
	array $phases
): void
{
	// If there is a range with a finish stage that comes before the created one,
	// then the range is expanded, or a new range is added if the semantics are final.

	$phases = static::sortPhasesBySortAscAndIdDesc($phases);

	$phaseID = is_string($phaseID) ? $phaseID : '';
	if ($phaseID === '')
	{
		return;
	}
	if (!CCrmOwnerType::IsDefined($entityTypeID))
	{
		return;
	}

	$isPrevPhaseExists = false;
	$curPhaseId = '';
	$prevPhaseId = '';
	$prevPhaseSemantics = '';
	foreach ($phases as $curPhaseId => $phase)
	{
		$curPhaseSemantics = $phase['SEMANTICS'] ?? '';
		if (
			$curPhaseId === $phaseID
			&& $prevPhaseId !== ''
			&& $curPhaseSemantics === $prevPhaseSemantics
		)
		{
			$isPrevPhaseExists = true;
			break;
		}
		$prevPhaseId = $curPhaseId;
		$prevPhaseSemantics = $curPhaseSemantics;
	}
	if ($isPrevPhaseExists)
	{
		$res = FieldAttributeTable::getList(
			[
				'filter' => [
					'=ENTITY_TYPE_ID' => $entityTypeID,
					'=ENTITY_SCOPE' => $entityScope,
					'=TYPE_ID' => FieldAttributeType::REQUIRED,
				]
			]
		);
		$addRecords = [];
		while ($row = $res->fetch())
		{
			if ($row['FINISH_PHASE'] === $prevPhaseId)
			{
				if ($prevPhaseSemantics === PhaseSemantics::FAILURE)
				{
					$record = $row;
					unset($record['ID']);
					$record['CREATED_TIME'] = new Main\Type\DateTime();
					$record['START_PHASE'] = $curPhaseId;
					$record['FINISH_PHASE'] = $curPhaseId;
					$addRecords[] = $record;
				}
				else
				{
					Entity\FieldAttributeTable::update(
						(int)$row['ID'],
						[
							'CREATED_TIME' => new Main\Type\DateTime(),
							'FINISH_PHASE' => $curPhaseId,
						]
					);
				}
			}
		}
		if (!empty($addRecords))
		{
			foreach ($addRecords as $data)
			{
				Entity\FieldAttributeTable::add($data);
			}
		}
	}
}