CAllCrmLead::RebuildStatistics

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CAllCrmLead
  4. RebuildStatistics
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_lead.php
  • Класс: \CAllCrmLead
  • Вызов: CAllCrmLead::RebuildStatistics
static function RebuildStatistics(array $IDs, array $options = null)
{
	$dbResult = self::GetListEx(
		array(),
		array('@ID' => $IDs, 'CHECK_PERMISSIONS' => 'N'),
		false,
		false,
		array(
			'ID', 'DATE_CREATE', 'DATE_MODIFY', 'STATUS_ID', 'SOURCE_ID',
			'ASSIGNED_BY_ID' , 'CURRENCY_ID', 'OPPORTUNITY', 'UF_*'
		)
	);

	if(!is_object($dbResult))
	{
		return;
	}

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

	$forced = isset($options['FORCED']) ? $options['FORCED'] : false;
	$enableHistory = isset($options['ENABLE_HISTORY']) ? $options['ENABLE_HISTORY'] : true;
	$enableSumStatistics = isset($options['ENABLE_SUM_STATISTICS']) ? $options['ENABLE_SUM_STATISTICS'] : true;
	$enableActivityStatistics = isset($options['ENABLE_ACTIVITY_STATISTICS']) ? $options['ENABLE_ACTIVITY_STATISTICS'] : true;
	$enableConversionStatistics = isset($options['ENABLE_CONVERSION_STATISTICS']) ? $options['ENABLE_CONVERSION_STATISTICS'] : true;

	while($fields = $dbResult->Fetch())
	{
		$ID = (int)$fields['ID'];
		$statusID = isset($fields['STATUS_ID']) ? $fields['STATUS_ID'] : '';
		if($statusID !== '')
		{
			$created = isset($fields['DATE_CREATE']) ? $fields['DATE_CREATE'] : '';
			$createdTime = null;
			try
			{
				$createdTime = new Bitrix\Main\Type\DateTime(
					$created,
					Bitrix\Main\Type\DateTime::convertFormatToPhp(FORMAT_DATETIME));
			}
			catch(Bitrix\Main\ObjectException $e)
			{
			}

			if($createdTime === null)
			{
				$createdTime = new Bitrix\Main\Type\DateTime();
			}

			$modified = isset($fields['DATE_MODIFY']) ? $fields['DATE_MODIFY'] : '';
			$modifiedTime = null;
			if($modified !== '')
			{
				try
				{
					$modifiedTime = new Bitrix\Main\Type\DateTime(
						$modified,
						Bitrix\Main\Type\DateTime::convertFormatToPhp(FORMAT_DATETIME));
				}
				catch(Bitrix\Main\ObjectException $e)
				{
				}
			}

			if($modifiedTime === null)
			{
				$modifiedTime = $createdTime;
			}

			$isNew = $createdTime->getTimestamp() === $modifiedTime->getTimestamp();

			//--> History
			if($enableHistory && ($forced || !Bitrix\Crm\History\LeadStatusHistoryEntry::isRegistered($ID)))
			{

				Bitrix\Crm\History\LeadStatusHistoryEntry::register(
					$ID,
					$fields,
					array('IS_NEW' => $isNew, 'TIME' => $isNew ? $createdTime : $modifiedTime)
				);
			}
			//<-- History

			//--> Statistics
			if($enableConversionStatistics && $statusID === 'CONVERTED')
			{
				$isRegistered = false;
				if($forced)
				{
					Bitrix\Crm\Statistics\LeadConversionStatisticsEntry::unregister($ID);
				}
				else
				{
					$isRegistered = Bitrix\Crm\Statistics\LeadConversionStatisticsEntry::isRegistered($ID);
				}

				$statusHistory = \Bitrix\Crm\History\LeadStatusHistoryEntry::getLatest($ID);
				if($statusHistory && ($forced || !$isRegistered))
				{
					Bitrix\Crm\Statistics\LeadConversionStatisticsEntry::register(
						$ID,
						$fields,
						array(
							'IS_NEW' => $isNew,
							'DATE' => $statusHistory['CREATED_DATE']
						)
					);
				}
			}
			//<-- Statistics
		}

		//--> Statistics
		if($enableSumStatistics && ($forced || !Bitrix\Crm\Statistics\LeadSumStatisticEntry::isRegistered($ID)))
		{
			Bitrix\Crm\Statistics\LeadSumStatisticEntry::register($ID, $fields, array('FORCED' => $forced));
		}

		if($enableActivityStatistics)
		{
			$timeline = Bitrix\Crm\Statistics\LeadActivityStatisticEntry::prepareTimeline($ID);
			foreach($timeline as $date)
			{
				Bitrix\Crm\Statistics\LeadActivityStatisticEntry::register($ID, $fields, array('DATE' => $date));
			}
		}
		//<-- Statistics
	}
}

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