17 private ?
string $currency =
null;
21 $this->sender = $sender;
23 $this->initDefaultPeriod();
26 private function getSender():
Sender
36 private function initDefaultPeriod(): void
41 $this->dateFrom->add(
'-1 week');
48 $this->dateFrom = $dateFrom;
53 $this->dateTo = $dateTo;
59 public function buildDailyExpensesReport():
Main\
Result
66 $this->currency = $this->getSender()->getCurrency();
68 $reportResult = $this->loadReports();
69 if (!$reportResult->isSuccess())
71 return $result->addErrors($reportResult->getErrors());
74 $campaignIds = $this->reportCollection->getUniqCampaignIds();
76 $adResult = $this->loadAds($campaignIds);
77 if (!$adResult->isSuccess())
79 $result->addErrors($adResult->getErrors());
86 foreach ($this->reportCollection as $report)
91 $result->setData([
'expenses' => $expensesCollection]);
101 $reportResult = $this->getSender()->getDailyExpensesReport($this->dateFrom, $this->dateTo);
102 if ($reportResult->isSuccess())
104 foreach ($this->parseReportData((
string)$reportResult->getResponse()) as $report)
106 $this->reportCollection->addItem(Dto\Report::makeFromArray($report));
111 $result->addErrors($reportResult->getErrors());
117 private function loadAds(
array $campaignIds): Main\Result
121 $campaignIdsForRequest = [];
123 $cache = Main\Data\Cache::createInstance();
124 $cacheDir =
'/biconnector/integration/crm/dailyexpenses/yandex/ads';
127 foreach ($campaignIds as $campaignId)
129 $cacheName = (string)$campaignId;
131 if ($cache->initCache($cacheTtl, $cacheName, $cacheDir .
'/' . $cacheName))
133 $cacheDataAds = $cache->getVars();
134 foreach ($cacheDataAds as $cacheDataAd)
136 $this->adCollection->set((
int)$cacheDataAd[
'Id'], Dto\Ad::makeFromArray($cacheDataAd));
141 $campaignIdsForRequest[] = $campaignId;
147 foreach (array_chunk($campaignIdsForRequest, 10) as $chunkCampaignIds)
149 $adResult = $this->getSender()->getAds($chunkCampaignIds);
150 if ($adResult->isSuccess())
155 foreach (
$response[
'result'][
'Ads'] as $ad)
157 $this->adCollection->set((
int)$ad[
'Id'], Dto\Ad::makeFromArray($ad));
158 $adsForCache[(int)$ad[
'CampaignId']][(
int)$ad[
'Id']] = $ad;
164 $result->addErrors($adResult->getErrors());
170 foreach ($campaignIdsForRequest as $campaignId)
172 $cacheName = (string)$campaignId;
174 $cache->initCache($cacheTtl, $cacheName, $cacheDir .
'/' . $cacheName);
175 $cache->startDataCache();
176 $cache->endDataCache($adsForCache[$campaignId] ?? []);
182 private function parseReportData(
string $data): \Generator
190 $strings = explode(
"\n",
$data);
191 foreach ($strings as $number => $string)
195 $titles = explode(
"\t", $string);
197 elseif (!empty($string) && !str_starts_with($string,
'Total'))
199 yield array_combine($titles, explode(
"\t", $string));
204 private function formatExpensesData(Dto\Report $report):
array
207 $ad = $this->adCollection->get($report->adId);
214 if ($report->impressions > 0)
216 $cpm = round(($report->cost / $report->impressions) * 1000, 2);
220 'impressions' => $report->impressions,
221 'campaignName' => $report->campaignName,
222 'adId' => $report->adId,
223 'adName' => $ad?->title,
224 'groupId' => $report->adGroupId,
225 'groupName' => $report->adGroupName,
226 'campaignId' => $report->campaignId,
227 'clicks' => $report->clicks,
228 'actions' => $report->conversions + $report->clicks,
229 'spend' => $report->cost,
230 'cpc' => $report->avgCpc,
231 'date' => $report->date,
234 'utmSource' => $report->utmSource,
235 'utmMedium' => $report->utmMedium,
236 'utmCampaign' => $report->utmCampaign,
237 'utmContent' => $report->utmContent,
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)