- Модуль: crm
- Путь к файлу: ~/bitrix/modules/crm/lib/tracking/analytics/ad.php
- Класс: Bitrix\Crm\Tracking\Analytics\Ad
- Вызов: Ad::getExpenses
public function getExpenses(Main\Type\Date $dateFrom = null, Main\Type\Date $dateTo = null)
{
self::updateAccountIdCompatible();
$defaultResult = [
'impressions' => 0,
'actions' => 0,
'spend' => 0,
'currency' => '',
];
if (!$this->isConnected())
{
return $defaultResult;
}
if ($this->account->hasAccounts())
{
if (!$this->accountId)
{
return $defaultResult;
}
}
$cacheDir = '/crm/tracking/ad/expenses/';
$cacheTtl = (int) Main\Config\Option::get('crm', 'crm_tracking_expenses_cache_ttl') ?: self::CacheTtl;
$cacheId = $this->code
. '|' . $this->clientId . '|' . $this->accountId
. '|' . $dateFrom->getTimestamp() . '|' . $dateTo->getTimestamp();
$cache = Main\Data\Cache::createInstance();
if ($cache->initCache($cacheTtl, $cacheId, $cacheDir))
{
return $cache->getVars()['expenses'] + $defaultResult;
}
$expenses = $defaultResult;
$result = $this->account->getExpenses($this->accountId, $dateFrom, $dateTo)->fetch();
if (!empty($result['EXPENSES']))
{
$result = $result['EXPENSES'];
/** @var $result Seo\Analytics\Internals\Expenses */
$currencyId = $result->getCurrency();
if ($currencyId === 'BYN' && \CCrmCurrency::getAccountCurrencyID() === 'BYR')
{
$currencyId = 'BYR';
}
$expenses = [
'impressions' => $result->getImpressions(),
'actions' => $result->getActions(),
'spend' => ($result->getSpend() && $result->getCurrency())
? \CCrmCurrency::convertMoney(
$result->getSpend(),
$currencyId,
\CCrmCurrency::GetAccountCurrencyID()
)
: $result->getSpend(),
'currency' => \CCrmCurrency::GetAccountCurrencyID(),
];
}
if (!empty($expenses['spend']))
{
$cache->startDataCache($cacheTtl, $cacheId, $cacheDir);
$cache->endDataCache(['expenses' => $expenses]);
}
return $expenses;
}