CAllCrmLead::Delete

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CAllCrmLead
  4. Delete
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_lead.php
  • Класс: \CAllCrmLead
  • Вызов: CAllCrmLead::Delete
public function Delete($ID, $arOptions = array())
{
	global $DB, $APPLICATION;
	$ID = intval($ID);

	$this->LAST_ERROR = '';
	$APPLICATION->ResetException();

	if(!is_array($arOptions))
	{
		$arOptions = array();
	}

	if ($this->isUseOperation())
	{
		return $this->getCompatibilityAdapter()->performDelete($ID, $arOptions);
	}

	if(isset($arOptions['CURRENT_USER']))
	{
		$iUserId = intval($arOptions['CURRENT_USER']);
	}
	else
	{
		$iUserId = CCrmSecurityHelper::GetCurrentUserID();
	}

	$dbRes = self::GetListEx(
		array(),
		array('=ID' => $ID, 'CHECK_PERMISSIONS' => 'N'),
		false,
		false,
		array('*')
	);
	$arFields = $dbRes ? $dbRes->Fetch() : null;
	if(!is_array($arFields))
	{
		$err = GetMessage('CRM_LEAD_DELETION_NOT_FOUND');
		$this->LAST_ERROR = $err;
		$APPLICATION->throwException($err);
		return false;
	}

	$assignedByID = isset($arFields['ASSIGNED_BY_ID']) ? (int)$arFields['ASSIGNED_BY_ID'] : 0;
	$isConverted = isset($arFields['STATUS_ID']) && $arFields['STATUS_ID'] === 'CONVERTED';
	$companyID =  isset($arFields['COMPANY_ID']) ? (int)$arFields['COMPANY_ID'] : 0;
	$contactID = isset($arFields['CONTACT_ID']) ? (int)$arFields['CONTACT_ID'] : 0;

	if($companyID > 0 && !CCrmCompany::Exists($companyID))
	{
		$companyID = 0;
	}

	if($contactID > 0 && !CCrmContact::Exists($contactID))
	{
		$contactID = 0;
	}

	$hasDeletePerm = \Bitrix\Crm\Service\Container::getInstance()
		->getUserPermissions($iUserId)
		->checkDeletePermissions(CCrmOwnerType::Lead, $ID);

	if (
		$this->bCheckPermission
		&& !$hasDeletePerm
	)
	{
		return false;
	}

	$events = GetModuleEvents('crm', 'OnBeforeCrmLeadDelete');
	while ($arEvent = $events->Fetch())
	{
		if(ExecuteModuleEventEx($arEvent, array($ID))===false)
		{
			$err = GetMessage("MAIN_BEFORE_DEL_ERR").' '.$arEvent['TO_NAME'];
			if($ex = $APPLICATION->GetException())
				$err .= ': '.$ex->GetString();
			$APPLICATION->throwException($err);
			$this->LAST_ERROR = $err;
			return false;
		}
	}

	$enableDeferredMode = isset($arOptions['ENABLE_DEFERRED_MODE'])
		? (bool)$arOptions['ENABLE_DEFERRED_MODE']
		: \Bitrix\Crm\Settings\LeadSettings::getCurrent()->isDeferredCleaningEnabled();

	//By default we need to clean up related bizproc entities
	$processBizproc = isset($arOptions['PROCESS_BIZPROC']) ? (bool)$arOptions['PROCESS_BIZPROC'] : true;
	if($processBizproc)
	{
		$bizproc = new CCrmBizProc('LEAD');
		$bizproc->ProcessDeletion($ID);
	}

	$enableRecycleBin = \Bitrix\Crm\Recycling\LeadController::isEnabled()
		&& \Bitrix\Crm\Settings\LeadSettings::getCurrent()->isRecycleBinEnabled();
	if($enableRecycleBin)
	{
		\Bitrix\Crm\Recycling\LeadController::getInstance()->moveToBin($ID, array('FIELDS' => $arFields));
	}

	$tableName = CCrmLead::TABLE_NAME;
	$sSql = "DELETE FROM {$tableName} WHERE ID = {$ID}";
	$obRes = $DB->Query($sSql, false, 'FILE: '.__FILE__.'
LINE: '.__LINE__); if (is_object($obRes) && $obRes->AffectedRowsCount() > 0) { if(defined('BX_COMP_MANAGED_CACHE')) { $GLOBALS['CACHE_MANAGER']->CleanDir('b_crm_lead'); $GLOBALS['CACHE_MANAGER']->ClearByTag('b_crm_lead'); } CCrmSearch::DeleteSearch('LEAD', $ID); Bitrix\Crm\Search\SearchContentBuilderFactory::create( CCrmOwnerType::Lead )->removeShortIndex($ID); Bitrix\Crm\Kanban\SortTable::clearEntity($ID, \CCrmOwnerType::LeadName); Crm\Security\Manager::getEntityController(CCrmOwnerType::Lead) ->unregister(self::$TYPE_NAME, $ID) ; $GLOBALS['USER_FIELD_MANAGER']->Delete(self::$sUFEntityID, $ID); LeadContactTable::unbindAllContacts($ID); if(!$enableDeferredMode) { $CCrmEvent = new CCrmEvent(); $CCrmEvent->DeleteByElement('LEAD', $ID); } else { Bitrix\Crm\Cleaning\CleaningManager::register(CCrmOwnerType::Lead, $ID); } Bitrix\Crm\History\LeadStatusHistoryEntry::unregister($ID); Bitrix\Crm\Statistics\LeadSumStatisticEntry::unregister($ID); Bitrix\Crm\Statistics\LeadActivityStatisticEntry::unregister($ID); //Bitrix\Crm\Statistics\LeadProcessStatisticsEntry::unregister($ID); LeadChannelBinding::unregisterAll($ID); if($isConverted) { Bitrix\Crm\Statistics\LeadConversionStatisticsEntry::unregister($ID); } $enableDupIndexInvalidation = is_array($arOptions) && isset($arOptions['ENABLE_DUP_INDEX_INVALIDATION']) ? (bool)$arOptions['ENABLE_DUP_INDEX_INVALIDATION'] : true; if($enableDupIndexInvalidation) { DuplicateManager::markDuplicateIndexAsJunk(CCrmOwnerType::Lead, $ID); } $duplicateCriterionRegistrar = DuplicateManager::getCriterionRegistrar(\CCrmOwnerType::Lead); $data = (new Crm\Integrity\CriterionRegistrar\Data()) ->setEntityTypeId(\CCrmOwnerType::Lead) ->setEntityId($ID) ; $duplicateCriterionRegistrar->unregister($data); DuplicateIndexMismatch::unregisterEntity(CCrmOwnerType::Lead, $ID); \Bitrix\Crm\Counter\Monitor::getInstance()->onEntityDelete(CCrmOwnerType::Lead, $arFields); if($isConverted) { if($contactID > 0) { CCrmActivity::ChangeOwner(CCrmOwnerType::Lead, $ID, CCrmOwnerType::Contact, $contactID); } elseif($companyID> 0) { CCrmActivity::ChangeOwner(CCrmOwnerType::Lead, $ID, CCrmOwnerType::Company, $companyID); } } CCrmActivity::DeleteByOwner(CCrmOwnerType::Lead, $ID); if(!$enableRecycleBin) { $CCrmFieldMulti = new CCrmFieldMulti(); $CCrmFieldMulti->DeleteByElement('LEAD', $ID); CCrmProductRow::DeleteByOwner('L', $ID); CCrmProductRow::DeleteSettings('L', $ID); Crm\EntityAddress::unregister(CCrmOwnerType::Lead, $ID, EntityAddressType::Primary); Crm\Timeline\TimelineEntry::deleteByOwner(CCrmOwnerType::Lead, $ID); Crm\Pseudoactivity\WaitEntry::deleteByOwner(CCrmOwnerType::Lead, $ID); Crm\Observer\ObserverManager::deleteByOwner(CCrmOwnerType::Lead, $ID); Crm\Ml\Scoring::onEntityDelete(CCrmOwnerType::Lead, $ID); self::getCommentsAdapter()->performDelete((int)$ID, $arOptions); Crm\Integration\Im\Chat::deleteChat( array( 'ENTITY_TYPE_ID' => \CCrmOwnerType::Lead, 'ENTITY_ID' => $ID ) ); CCrmSonetSubscription::UnRegisterSubscriptionByEntity(CCrmOwnerType::Lead, $ID); CCrmLiveFeed::DeleteLogEvents( array( 'ENTITY_TYPE_ID' => CCrmOwnerType::Lead, 'ENTITY_ID' => $ID ), array( 'UNREGISTER_RELATION' => true, 'UNREGISTER_SUBSCRIPTION' => true ) ); UtmTable::deleteEntityUtm(CCrmOwnerType::Lead, $ID); Tracking\Entity::deleteTrace(CCrmOwnerType::Lead, $ID); } CCrmContact::ProcessLeadDeletion($ID); CCrmCompany::ProcessLeadDeletion($ID); CCrmDeal::ProcessLeadDeletion($ID); \Bitrix\Crm\Timeline\LeadController::getInstance()->onDelete( $ID, array('FIELDS' => $arFields) ); if(Bitrix\Crm\Settings\HistorySettings::getCurrent()->isLeadDeletionEventEnabled()) { CCrmEvent::RegisterDeleteEvent(CCrmOwnerType::Lead, $ID, $iUserId, array('FIELDS' => $arFields)); } if(defined("BX_COMP_MANAGED_CACHE")) { $GLOBALS["CACHE_MANAGER"]->ClearByTag("crm_entity_name_".CCrmOwnerType::Lead."_".$ID); } $afterEvents = GetModuleEvents('crm', 'OnAfterCrmLeadDelete'); while ($arEvent = $afterEvents->Fetch()) { ExecuteModuleEventEx($arEvent, array($ID)); } } $item = Crm\Kanban\Entity::getInstance(self::$TYPE_NAME) ->createPullItem($arFields); PullManager::getInstance()->sendItemDeletedEvent( $item, [ 'TYPE' => self::$TYPE_NAME, 'SKIP_CURRENT_USER' => false, 'EVENT_ID' => ($arOptions['eventId'] ?? null), ] ); return true; }

Добавить комментарий