public function Delete($ID, $arOptions = array())
{
global $DB, $APPLICATION;
$ID = (int)$ID;
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();
}
$dbResult = \CCrmDeal::GetListEx(
array(),
array('=ID' => $ID, 'CHECK_PERMISSIONS' => 'N')
);
$arFields = is_object($dbResult) ? $dbResult->Fetch() : null;
if(!is_array($arFields))
{
return false;
}
$assignedByID = isset($arFields['ASSIGNED_BY_ID']) ? (int)$arFields['ASSIGNED_BY_ID'] : 0;
$categoryID = isset($arFields['CATEGORY_ID']) ? (int)$arFields['CATEGORY_ID'] : 0;
$permissionEntityType = DealCategory::convertToPermissionEntityType($categoryID);
$hasDeletePerm = \Bitrix\Crm\Service\Container::getInstance()
->getUserPermissions($iUserId)
->checkDeletePermissions(CCrmOwnerType::Deal, $ID);
if (
$this->bCheckPermission
&& !$hasDeletePerm
)
{
return false;
}
$APPLICATION->ResetException();
$events = GetModuleEvents('crm', 'OnBeforeCrmDealDelete');
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();
if ($ex->GetID() === 'system')
{
$err = $ex->GetString();
}
}
$this->LAST_ERROR = $err;
$APPLICATION->ThrowException($this->LAST_ERROR);
return false;
}
$enableDeferredMode = isset($arOptions['ENABLE_DEFERRED_MODE'])
? (bool)$arOptions['ENABLE_DEFERRED_MODE']
: \Bitrix\Crm\Settings\DealSettings::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('DEAL');
$bizproc->ProcessDeletion($ID);
}
$enableRecycleBin = \Bitrix\Crm\Recycling\DealController::isEnabled()
&& \Bitrix\Crm\Settings\DealSettings::getCurrent()->isRecycleBinEnabled();
if($enableRecycleBin)
{
\Bitrix\Crm\Recycling\DealController::getInstance()->moveToBin($ID, array('FIELDS' => $arFields));
}
$dbRes = $DB->Query("DELETE FROM b_crm_deal WHERE ID = {$ID}", false, 'FILE: '.__FILE__.'
LINE: '.__LINE__);
if (is_object($dbRes) && $dbRes->AffectedRowsCount() > 0)
{
if(defined('BX_COMP_MANAGED_CACHE'))
{
$GLOBALS['CACHE_MANAGER']->CleanDir('b_crm_deal');
}
self::clearCategoryCache($ID);
self::SynchronizeCustomerData($ID, $arFields, array('ENABLE_SOURCE' => false));
CCrmSearch::DeleteSearch('DEAL', $ID);
Bitrix\Crm\Search\SearchContentBuilderFactory::create(
CCrmOwnerType::Deal
)->removeShortIndex($ID);
Bitrix\Crm\Kanban\SortTable::clearEntity($ID, \CCrmOwnerType::DealName);
Crm\Security\Manager::getEntityController(CCrmOwnerType::Deal)
->unregister(
$permissionEntityType,
$ID
)
;
$GLOBALS['USER_FIELD_MANAGER']->Delete(self::$sUFEntityID, $ID);
DealContactTable::unbindAllContacts($ID);
if(!$enableDeferredMode)
{
$CCrmEvent = new CCrmEvent();
$CCrmEvent->DeleteByElement('DEAL', $ID);
}
else
{
Bitrix\Crm\Cleaning\CleaningManager::register(CCrmOwnerType::Deal, $ID);
}
if(!isset($arOptions['REGISTER_STATISTICS']) || $arOptions['REGISTER_STATISTICS'] === true)
{
DealStageHistoryEntry::unregister($ID);
DealSumStatisticEntry::unregister($ID);
DealInvoiceStatisticEntry::unregister($ID);
DealActivityStatisticEntry::unregister($ID);
DealChannelBinding::unregisterAll($ID);
}
\Bitrix\Crm\Counter\Monitor::getInstance()->onEntityDelete(CCrmOwnerType::Deal, $arFields);
//Statistics & History -->
$leadID = isset($arFields['LEAD_ID']) ? (int)$arFields['LEAD_ID'] : 0;
if($leadID)
{
LeadConversionStatisticsEntry::processBindingsChange($leadID);
}
//<-- Statistics & History
CCrmActivity::DeleteByOwner(CCrmOwnerType::Deal, $ID);
if(!$enableRecycleBin)
{
CCrmProductRow::DeleteByOwner('D', $ID);
CCrmProductRow::DeleteSettings('D', $ID);
\Bitrix\Crm\Requisite\EntityLink::unregister(CCrmOwnerType::Deal, $ID);
\Bitrix\Crm\Timeline\TimelineEntry::deleteByOwner(CCrmOwnerType::Deal, $ID);
\Bitrix\Crm\Pseudoactivity\WaitEntry::deleteByOwner(CCrmOwnerType::Deal, $ID);
\Bitrix\Crm\Observer\ObserverManager::deleteByOwner(CCrmOwnerType::Deal, $ID);
\Bitrix\Crm\Ml\Scoring::onEntityDelete(CCrmOwnerType::Deal, $ID);
self::getCommentsAdapter()->performDelete((int)$ID, $arOptions);
Crm\Integration\Im\Chat::deleteChat(
array(
'ENTITY_TYPE_ID' => \CCrmOwnerType::Deal,
'ENTITY_ID' => $ID
)
);
CCrmSonetSubscription::UnRegisterSubscriptionByEntity(CCrmOwnerType::Deal, $ID);
CCrmLiveFeed::DeleteLogEvents(
array(
'ENTITY_TYPE_ID' => CCrmOwnerType::Deal,
'ENTITY_ID' => $ID
)
);
UtmTable::deleteEntityUtm(CCrmOwnerType::Deal, $ID);
Tracking\Entity::deleteTrace(CCrmOwnerType::Deal, $ID);
}
// Deletion of deal details
\Bitrix\Crm\Timeline\DealController::getInstance()->onDelete(
$ID,
array('FIELDS' => $arFields)
);
if ($arFields['IS_RECURRING'] === "Y")
{
$dealRecurringItem = \Bitrix\Crm\Recurring\Entity\Item\DealExist::loadByDealId($ID);
if ($dealRecurringItem)
{
$dealRecurringItem->delete();
}
}
self::PullChange('DELETE', array('ID' => $ID));
if(HistorySettings::getCurrent()->isDealDeletionEventEnabled())
{
CCrmEvent::RegisterDeleteEvent(CCrmOwnerType::Deal, $ID, $iUserId, array('FIELDS' => $arFields));
}
if(defined("BX_COMP_MANAGED_CACHE"))
{
$GLOBALS["CACHE_MANAGER"]->ClearByTag("crm_entity_name_".CCrmOwnerType::Deal."_".$ID);
}
$afterEvents = GetModuleEvents('crm', 'OnAfterCrmDealDelete');
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,
'CATEGORY_ID' => $categoryID,
'SKIP_CURRENT_USER' => false,
'EVENT_ID' => ($arOptions['eventId'] ?? null),
]
);
}
return true;
}