• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/history/leadstatushistorywithsupposedentry.php
  • Класс: Bitrix\Crm\History\LeadStatusHistoryWithSupposedEntry
  • Вызов: LeadStatusHistoryWithSupposedEntry::getHistoriesToAdd
static function getHistoriesToAdd($leadId)
{
	$lastSavedStatusModification = self::getLeadLastModificationFromSupposed($leadId);

	if ($lastSavedStatusModification)
	{
		$lastModificationDate = $lastSavedStatusModification['CREATED_TIME'];
		$existingHistoryToWrite = self::getLeadNewHistoryModificationsById($leadId, $lastModificationDate);
		if ($existingHistoryToWrite)
		{
			$firstModificationToWrite = $existingHistoryToWrite[0];
			/** @var DateTime $firstModificationDate */
			$firstModificationDate = $firstModificationToWrite['HISTORY_CREATED_TIME'];
			/** @var DateTime $lastSavedModificationDate */
			$lastSavedModificationDate = $lastSavedStatusModification['CREATED_TIME'];
			$spentTime = $firstModificationDate->getTimestamp() - $lastSavedModificationDate->getTimestamp();
			LeadStatusHistoryWithSupposedTable::update(
				$lastSavedStatusModification['ID'],
				[
					'SPENT_TIME' => $spentTime
				]
			);
		}
	}
	else
	{
		$existingHistoryToWrite = self::getLeadNewHistoryModificationsById($leadId);
	}

	$prepared = self::prepareHistoriesFromExistHistory($existingHistoryToWrite);
	$historiesToAdd = [];
	if (!$lastSavedStatusModification)
	{
		foreach ($prepared as $statusId => $history)
		{
			if (!empty($history))
			{
				$historiesToAdd[$statusId] = $history;
			}
		}
	}
	else
	{
		foreach ($prepared as $statusId => $history)
		{
			if ((string)$statusId === (string)$lastSavedStatusModification['STATUS_ID'])
			{
				break;
			}

			if (!empty($history))
			{
				$historiesToAdd[$statusId] = $history;
			}
		}
	}
	return $historiesToAdd;
}