CAllCrmActivity::Delete

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CAllCrmActivity
  4. Delete
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_activity.php
  • Класс: \CAllCrmActivity
  • Вызов: CAllCrmActivity::Delete
static function Delete($ID, $checkPerms = true, $regEvent = true, $options = array())
{
	global $USER_FIELD_MANAGER;
	$ID = intval($ID);
	if(!is_array($options))
	{
		$options = array();
	}

	$movedToRecycleBin = isset($options['MOVED_TO_RECYCLE_BIN']) && $options['MOVED_TO_RECYCLE_BIN'];

	$events = GetModuleEvents('crm', 'OnBeforeActivityDelete');
	while ($event = $events->Fetch())
	{
		if (ExecuteModuleEventEx($event, array($ID)) === false)
		{
			return false;
		}
	}

	$ary = (isset($options['ACTUAL_ITEM']) && is_array($options['ACTUAL_ITEM']))
		? $options['ACTUAL_ITEM']
		: self::GetByID($ID, $checkPerms);

	if(!is_array($ary))
	{
		return false; //is not found
	}

	$incomingChannel = \Bitrix\Crm\Activity\IncomingChannel::getInstance();
	$isIncomingChannel = $incomingChannel->isIncomingChannel($ID);
	$ary['IS_INCOMING_CHANNEL'] = $isIncomingChannel ? 'Y' : 'N';
	$pingOffset = PingOffset::getInstance();
	$pingQueue = PingQueue::getInstance();
	if(!$movedToRecycleBin && \Bitrix\Crm\Recycling\ActivityController::isEnabled())
	{
		$enableRecycleBin = (isset($options['ENABLE_RECYCLE_BIN']) && $options['ENABLE_RECYCLE_BIN'])
			|| \Bitrix\Crm\Settings\ActivitySettings::getCurrent()->isRecycleBinEnabled();

		if($enableRecycleBin)
		{
			$recycleBinResult = \Bitrix\Crm\Recycling\ActivityController::getInstance()->moveToBin(
				$ID,
				array('FIELDS' => $ary)
			);

			if($recycleBinResult->isSuccess())
			{
				$movedToRecycleBin = true;

				$resultData = $recycleBinResult->getData();
				if(is_array($resultData) && isset($resultData['isDeleted']) && $resultData['isDeleted'])
				{
					$incomingChannel->unregister($ID);
					$pingOffset->unregister($ID);
					$pingQueue->unregister($ID);

					return true;
				}
			}
		}
	}

	if($movedToRecycleBin)
	{
		$options['SKIP_FILES'] = true;
	}

	$arBindings = isset($options['ACTUAL_BINDINGS']) && is_array($options['ACTUAL_BINDINGS'])
		? $options['ACTUAL_BINDINGS']
		: self::GetBindings($ID);

	if (is_array($arBindings))
	{
		$monitor = Crm\Service\Timeline\Monitor::getInstance();
		foreach ($arBindings as $singleBinding)
		{
			$monitor->onActivityRemoveIfSuitable(new Crm\ItemIdentifier((int)$singleBinding['OWNER_TYPE_ID'], (int)$singleBinding['OWNER_ID']), $ID);
		}
	}

	if(!self::InnerDelete($ID, $options))
	{
		return false;
	}

	$USER_FIELD_MANAGER->Delete(static::UF_ENTITY_TYPE, $ID);
	$incomingChannel->unregister($ID);
	$pingOffset->unregister($ID);
	$pingQueue->unregister($ID);

	$responsibleID = isset($ary['RESPONSIBLE_ID']) ? (int)$ary['RESPONSIBLE_ID'] : 0;

	if (is_array($arBindings))
	{

		$oldLightTimeDate = self::queryLightTimeDateByActId($ID);

		(new FillActLightCounter())->onActDelete($ary);

		\Bitrix\Crm\Activity\UncompletedActivity::synchronizeForUncompletedActivityChange(
			\Bitrix\Crm\Activity\UncompletedActivityChange::create(
				$ID,
				$ary,
				$arBindings,
				[],
				[],
				$oldLightTimeDate,
				null
			)
		);
	}
	// Synchronize user activity -->
	$skipUserActivitySync = isset($options['SKIP_USER_ACTIVITY_SYNC']) ? $options['SKIP_USER_ACTIVITY_SYNC'] : false;
	if(!$skipUserActivitySync && is_array($arBindings))
	{
		foreach($arBindings as $arBinding)
		{
			if($responsibleID > 0)
			{
				self::SynchronizeUserActivity($arBinding['OWNER_TYPE_ID'], $arBinding['OWNER_ID'], $responsibleID);
			}
			self::SynchronizeUserActivity($arBinding['OWNER_TYPE_ID'], $arBinding['OWNER_ID'], 0);
		}
	}
	// <-- Synchronize user activity

	if(is_array($arBindings))
	{
		if($regEvent)
		{
			foreach($arBindings as $arBinding)
			{
				self::RegisterRemoveEvent(
					$arBinding['OWNER_TYPE_ID'],
					$arBinding['OWNER_ID'],
					$ary,
					$checkPerms,
					CCrmSecurityHelper::GetCurrentUserID()
				);
			}
			unset($arBinding);
		}

		$skipStatistics = isset($options['SKIP_STATISTICS']) ? $options['SKIP_STATISTICS'] : false;
		if(!$skipStatistics)
		{
			$completed = isset($ary['COMPLETED']) && $ary['COMPLETED'] === 'Y';
			$deadline = isset($ary['DEADLINE']) ? $ary['DEADLINE'] : '';
			if($completed && $deadline)
			{
				$deadline = \Bitrix\Main\Type\DateTime::createFromUserTime($deadline);
				$deadline->setTime(0, 0, 0);
				foreach($arBindings as $arBinding)
				{
					$curOwnerTypeID = isset($arBinding['OWNER_TYPE_ID']) ? intval($arBinding['OWNER_TYPE_ID']) : 0;
					$curOwnerID = isset($arBinding['OWNER_ID']) ? intval($arBinding['OWNER_ID']) : 0;
					if($curOwnerID > 0)
					{
						if($curOwnerTypeID === CCrmOwnerType::Deal)
						{
							Bitrix\Crm\Statistics\DealActivityStatisticEntry::register($curOwnerID, null, array('DATE' => $deadline));
						}
						elseif($curOwnerTypeID === CCrmOwnerType::Lead)
						{
							Bitrix\Crm\Statistics\LeadActivityStatisticEntry::register($curOwnerID, null, array('DATE' => $deadline));
							//Bitrix\Crm\Statistics\LeadProcessStatisticsEntry::register($curOwnerID, null, array('IS_NEW' => false, 'DATE' => $deadline));
						}
					}
				}
				unset($arBinding);
			}

			$ary['BINDINGS'] = $arBindings;
			\Bitrix\Crm\Activity\CommunicationStatistics::unregisterActivity($ary);
		}
	}

	$skipAssocEntity = isset($options['SKIP_ASSOCIATED_ENTITY']) ? (bool)$options['SKIP_ASSOCIATED_ENTITY'] : false;
	$associatedEntityId = isset($ary['ASSOCIATED_ENTITY_ID']) ? (int)$ary['ASSOCIATED_ENTITY_ID'] : 0;
	$providerTypeId = isset($ary['PROVIDER_TYPE_ID']) ? (string) $ary['PROVIDER_TYPE_ID'] : null;
	$provider = self::GetActivityProvider($ary);

	if(!$skipAssocEntity && $provider !== null && $associatedEntityId > 0)
	{
		$provider::deleteAssociatedEntity($associatedEntityId, $ary, $options);
	}

	if ($provider !== null && $provider::canUseCalendarEvents($providerTypeId))
	{
		$skipCalendarEvent = isset($options['SKIP_CALENDAR_EVENT']) ? (bool)$options['SKIP_CALENDAR_EVENT'] : null;

		if (!$skipCalendarEvent)
		{
			self::DeleteCalendarEvent($ary);
		}
	}

	\Bitrix\Crm\Statistics\ActivityStatisticEntry::unregister($ID);
	\Bitrix\Crm\Integration\Channel\ActivityChannelBinding::unregisterAll($ID);

	if (isset($recycleBinResult))
	{
		$recycleBinEntityId = $recycleBinResult->getData()['recyclingEntityId'];
	}
	else
	{
		$recycleBinEntityId = Crm\Recycling\ActivityController::getInstance()->getRecyclingEntityId($ID);
	}

	\Bitrix\Crm\Timeline\ActivityController::getInstance()->onDelete(
		$ID,
		[
			'FIELDS' => $ary,
			'BINDINGS' => $arBindings,
			'MOVED_TO_RECYCLE_BIN' => $movedToRecycleBin,
			'RECYCLE_BIN_ENTITY_ID' => $recycleBinEntityId,
		]
	);
	if (is_array($arBindings))
	{
		\Bitrix\Crm\Counter\Monitor::getInstance()->onActivityDelete($ary, $arBindings, $oldLightTimeDate);
	}

	\Bitrix\Crm\Ml\Scoring::onActivityDelete($ID);

	if(!$movedToRecycleBin)
	{
		self::UnregisterLiveFeedEvent($ID);
		CCrmSonetSubscription::UnRegisterSubscriptionByEntity(CCrmOwnerType::Activity, $ID);
	}

	if ($provider !== null)
	{
		$provider::onAfterDelete($ID, $ary);
	}

	$rsEvents = GetModuleEvents('crm', 'OnActivityDelete');
	while ($arEvent = $rsEvents->Fetch())
	{
		ExecuteModuleEventEx($arEvent, array($ID));
	}

	if(defined("BX_COMP_MANAGED_CACHE"))
	{
		$GLOBALS["CACHE_MANAGER"]->ClearByTag("CRM_ACTIVITY_".$ID);
	}

	return true;
}

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