• Модуль: voximplant
  • Путь к файлу: ~/bitrix/modules/voximplant/lib/agent/fixer.php
  • Класс: BitrixVoximplantAgentFixer
  • Вызов: Fixer::restoreLostCrmActivities
static function restoreLostCrmActivities()
{
	if(!Loader::includeModule('crm'))
	{
		return '';
	}

	$lastId = (int)Option::get('voximplant', '~activity_fixer_last_processed_id', 0);
	$cursor = BitrixVoximplantStatisticTable::query()
		->addSelect('*')
		->whereLike('CALL_ID', 'external%')
		->where('CALL_START_DATE', '>', new BitrixMainTypeDateTime('2018-11-15', 'Y-m-d'))
		->where('INCOMING', '=', '1')
		->where(BitrixMainEntityQuery::filter()
			->logic('or')
			->where('CRM_ACTIVITY_ID', 0)
			->whereNull('CRM_ACTIVITY_ID')
		)
		->where('ID', '>', $lastId)
		->setLimit(100)
		->exec();

	$found = false;
	while ($row = $cursor->fetch())
	{
		$found = true;

		$activityId = CVoxImplantCrmHelper::AddCall($row);
		if ($activityId > 0)
		{
			BitrixVoximplantStatisticTable::update($row['ID'], [
				'CRM_ACTIVITY_ID' => $activityId
			]);
			CVoxImplantCrmHelper::AttachRecordToCall($row);

			$activity = CCrmActivity::GetByID($activityId, false);

			$correctDate = new BitrixMainTypeDateTime($activity['END_TIME'], BitrixMainTypeDate::convertFormatToPhp(FORMAT_DATETIME));
			BitrixCrmActivityTable::update($activityId, [
				'CREATED' => $correctDate,
				'LAST_UPDATED' => $correctDate
			]);

			$timelineCursor = BitrixCrmTimelineEntityTimelineTable::getList([
				'select' => ['ID'],
				'filter' => [
					'=ASSOCIATED_ENTITY_ID' => $activityId,
					'=ASSOCIATED_ENTITY_TYPE_ID' => 6
				]
			]);

			while ($timelineRow = $timelineCursor->fetch())
			{
				BitrixCrmTimelineEntityTimelineTable::update($timelineRow['ID'], [
					'CREATED' => $correctDate
				]);
			}
		}
		$lastId = $row['ID'];
	}

	if ($found)
	{
		Option::set('voximplant', '~activity_fixer_last_processed_id', $lastId);
		return 'BitrixVoximplantAgentFixer::restoreLostCrmActivities();';
	}
	else
	{
		Option::delete('voximplant', [
			'name' => '~activity_fixer_last_processed_id'
		]);
		return '';
	}
}