• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/tracking/ad/index/indexbuilder.php
  • Класс: Bitrix\Crm\Tracking\Ad\Index\IndexBuilder
  • Вызов: IndexBuilder::build
protected function build()
{
	if (!$this->sourceId)
	{
		$this->errorCollection->setError(new Main\Error('Source ID is empty.'));
		return;
	}

	$sources = Tracking\Provider::getReadySources();
	$sources = array_combine(
		array_column($sources, 'ID'),
		array_values($sources)
	);
	if (!$sources[$this->sourceId])
	{
		$this->errorCollection->setError(new Main\Error('Source not found by ID.'));
		return;
	}


	$ad = new Tracking\Analytics\Ad($sources[$this->sourceId]);
	if (!$ad->isSupportExpensesReport())
	{
		$this->errorCollection->setError(new Main\Error('Source expenses report not supported.'));
		return;
	}

	if (!(list($dateFrom, $dateTo) = $this->getFetchDates()))
	{
		return;
	}
	$result = $ad->getExpensesReport($dateFrom, $dateTo);
	if (!$result->isSuccess())
	{
		$this->errorCollection->add($result->getErrors());
		return;
	}

	$data = $result->getData();
	unset($result);

	// import structure of campaigns, groups, keywords
	$map = $this->importStructure($data);

	// import daily expenses
	if (empty($map['KEYWORDS']))
	{
		$map['KEYWORDS'] = [];
	}
	$packId = $this->importDailyExpenses($map['KEYWORDS'], $data);

	// insert zero expenses for actual dates if no data imported
	$this->closeDailyExpenses($dateFrom, $dateTo, $packId);
}