• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/statustable.php
  • Класс: Bitrix\Crm\StatusTable
  • Вызов: StatusTable::onBeforeDelete
static function onBeforeDelete(Event $event): EventResult
{
	$data = static::getByPrimary($event->getParameter('id'))->fetch();

	$result = new EventResult();
	if (!$data)
	{
		return $result;
	}

	static::getTemporaryStorage()->saveData($event->getParameter('id'), $data);

	$entityId = $data['ENTITY_ID'];
	$entity = \CCrmStatus::GetEntityTypes()[$entityId] ?? null;
	if (!$entity)
	{
		return $result;
	}

	$factory = static::getFactoryByStagesEntityId($entityId);
	if (!$factory)
	{
		return $result;
	}

	$itemsOnStageCount = $factory->getItemsCount([
		'=' . Item::FIELD_NAME_STAGE_ID => $data['STATUS_ID'],
	]);

	if ($itemsOnStageCount > 0)
	{
		$result->addError(new ORM\EntityError(Loc::getMessage('CRM_STATUS_STAGE_WITH_ITEMS_ERROR')));
	}

	$entityTypeId = $entity['ENTITY_TYPE_ID'] ?? null;
	if ($entityTypeId)
	{
		FieldAttributeManager::processPhaseDeletion(
			$data['STATUS_ID'],
			$entityTypeId,
			$entity['FIELD_ATTRIBUTE_SCOPE'] ?? '',
			static::getStatusesByEntityId($entityId)
		);
	}

	return $result;
}