CAllCrmCompany::Delete

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

	$ID = intval($ID);
	if(!is_array($arOptions))
	{
		$arOptions = [];
	}

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

	$dbResult = \CCrmCompany::GetListEx(
		[],
		array('=ID' => $ID, 'CHECK_PERMISSIONS' => 'N')
	);
	$arFields = is_object($dbResult) ? $dbResult->Fetch() : null;
	if(!is_array($arFields))
	{
		return false;
	}

	$isMyCompanyFlag = $arFields['IS_MY_COMPANY'] ?? 'N';
	if ($this->isUseOperation() && ($isMyCompanyFlag !== 'Y'))
	{
		return $this->getCompatibilityAdapter()->performDelete($ID, $arOptions);
	}

	$assignedByID = isset($arFields['ASSIGNED_BY_ID']) ? (int)$arFields['ASSIGNED_BY_ID'] : 0;
	$categoryId = (int)($arFields['CATEGORY_ID'] ?? 0);

	$permissionEntityType = (new PermissionEntityTypeHelper(CCrmOwnerType::Company))
		->getPermissionEntityTypeForCategory($categoryId)
	;

	$sWherePerm = '';
	if ($this->bCheckPermission)
	{
		$arEntityAttr = $this->cPerms->GetEntityAttr($permissionEntityType, $ID);
		$sEntityPerm = $this->cPerms->GetPermType($permissionEntityType, 'DELETE', $arEntityAttr[$ID]);
		if ($sEntityPerm == BX_CRM_PERM_NONE)
			return false;
		else if ($sEntityPerm == BX_CRM_PERM_SELF)
			$sWherePerm = " AND ASSIGNED_BY_ID = {$iUserId}";
		else if ($sEntityPerm == BX_CRM_PERM_OPEN)
			$sWherePerm = " AND (OPENED = 'Y' OR ASSIGNED_BY_ID = {$iUserId})";
	}

	$APPLICATION->ResetException();
	$events = GetModuleEvents('crm', 'OnBeforeCrmCompanyDelete');
	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);
			return false;
		}

	$enableDeferredMode = isset($arOptions['ENABLE_DEFERRED_MODE'])
		? (bool)$arOptions['ENABLE_DEFERRED_MODE']
		: \Bitrix\Crm\Settings\CompanySettings::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('COMPANY');
		$bizproc->ProcessDeletion($ID);
	}

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

	$obRes = $DB->Query("DELETE FROM b_crm_company WHERE ID = {$ID}{$sWherePerm}", false, 'FILE: '.__FILE__.'
LINE: '.__LINE__); if (is_object($obRes) && $obRes->AffectedRowsCount() > 0) { if(defined('BX_COMP_MANAGED_CACHE')) { $GLOBALS['CACHE_MANAGER']->CleanDir('b_crm_company'); } if(!$enableRecycleBin) { self::ReleaseExternalResources($arFields); } Container::getInstance()->getFactory(CCrmOwnerType::Company)->clearItemCategoryCache((int)$ID); CCrmSearch::DeleteSearch('COMPANY', $ID); Bitrix\Crm\Search\SearchContentBuilderFactory::create( CCrmOwnerType::Company )->removeShortIndex($ID); Crm\Security\Manager::getEntityController(CCrmOwnerType::Company) ->unregister($permissionEntityType, $ID) ; $GLOBALS['USER_FIELD_MANAGER']->Delete(self::$sUFEntityID, $ID); \Bitrix\Crm\Binding\ContactCompanyTable::unbindAllContacts($ID); if(!$enableDeferredMode) { $CCrmEvent = new CCrmEvent(); $CCrmEvent->DeleteByElement('COMPANY', $ID); } else { Bitrix\Crm\Cleaning\CleaningManager::register(CCrmOwnerType::Company, $ID); } $enableDupIndexInvalidation = isset($arOptions['ENABLE_DUP_INDEX_INVALIDATION']) ? (bool)$arOptions['ENABLE_DUP_INDEX_INVALIDATION'] : true; if($enableDupIndexInvalidation) { DuplicateManager::markDuplicateIndexAsJunk(CCrmOwnerType::Company, $ID); } $duplicateCriterionRegistrar = DuplicateManager::getCriterionRegistrar(\CCrmOwnerType::Company); $data = (new Crm\Integrity\CriterionRegistrar\Data()) ->setEntityTypeId(\CCrmOwnerType::Company) ->setEntityId($ID) ; $duplicateCriterionRegistrar->unregister($data); DuplicateIndexMismatch::unregisterEntity(CCrmOwnerType::Company, $ID); //Statistics & History --> $leadID = isset($arFields['LEAD_ID']) ? (int)$arFields['LEAD_ID'] : 0; if($leadID) { \Bitrix\Crm\Statistics\LeadConversionStatisticsEntry::processBindingsChange($leadID); } \Bitrix\Crm\Statistics\CompanyGrowthStatisticEntry::unregister($ID); //<-- Statistics & History \Bitrix\Crm\Counter\Monitor::getInstance()->onEntityDelete(CCrmOwnerType::Company, $arFields); CCrmActivity::DeleteByOwner(CCrmOwnerType::Company, $ID); if(!$enableRecycleBin) { //todo check that all necesary deletion is done $CCrmFieldMulti = new CCrmFieldMulti(); $CCrmFieldMulti->DeleteByElement('COMPANY', $ID); EntityAddress::unregister(CCrmOwnerType::Company, $ID, EntityAddressType::Primary); EntityAddress::unregister(CCrmOwnerType::Company, $ID, EntityAddressType::Registered); \Bitrix\Crm\Timeline\TimelineEntry::deleteByOwner(CCrmOwnerType::Company, $ID); self::getCommentsAdapter()->performDelete((int)$ID, $arOptions); $requisite = new \Bitrix\Crm\EntityRequisite(); $requisite->deleteByEntity(CCrmOwnerType::Company, $ID); unset($requisite); CCrmSonetSubscription::UnRegisterSubscriptionByEntity(CCrmOwnerType::Company, $ID); CCrmLiveFeed::DeleteLogEvents( array( 'ENTITY_TYPE_ID' => CCrmOwnerType::Company, 'ENTITY_ID' => $ID ) ); UtmTable::deleteEntityUtm(CCrmOwnerType::Company, $ID); Tracking\Entity::deleteTrace(CCrmOwnerType::Company, $ID); } CCrmContact::ProcessCompanyDeletion($ID); CCrmLead::ProcessCompanyDeletion($ID); CCrmDeal::ProcessCompanyDeletion($ID); if (Main\Loader::includeModule('sale')) { //todo unbind order (new \Bitrix\Crm\Order\ContactCompanyBinding(\CCrmOwnerType::Company))->unbind($ID); } (new Contractor\StoreDocumentContactCompanyBinding(\CCrmOwnerType::Company))->unbind($ID); (new Contractor\AgentContractContactCompanyBinding(\CCrmOwnerType::Company))->unbind($ID); \Bitrix\Crm\Timeline\CompanyController::getInstance()->onDelete( $ID, array('FIELDS' => $arFields) ); if(Bitrix\Crm\Settings\HistorySettings::getCurrent()->isCompanyDeletionEventEnabled()) { CCrmEvent::RegisterDeleteEvent(CCrmOwnerType::Company, $ID, 0, array('FIELDS' => $arFields)); } if(defined("BX_COMP_MANAGED_CACHE")) { $GLOBALS["CACHE_MANAGER"]->ClearByTag("crm_entity_name_".CCrmOwnerType::Company."_".$ID); } $afterEvents = GetModuleEvents('crm', 'OnAfterCrmCompanyDelete'); while ($arEvent = $afterEvents->Fetch()) { ExecuteModuleEventEx($arEvent, array($ID)); } } $item = $this->createPullItem($arFields); Crm\Integration\PullManager::getInstance()->sendItemDeletedEvent( $item, [ 'TYPE' => self::$TYPE_NAME, 'SKIP_CURRENT_USER' => false, 'EVENT_ID' => ($arOptions['eventId'] ?? null), 'CATEGORY_ID' => ($arFields['CATEGORY_ID'] ?? 0), ] ); return true; }

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