private function saveReportData($getPeriodicSubjectData, $subjectEntityName, array $params, array $forecastParams, $subjectId, $subjectType, $emails, $formatedTitle, $formatedForecastDate, $ifAccountLevel = false)
{
$periodData = \Scalr::getContainer()->analytics->usage->{$getPeriodicSubjectData}($subjectId, $params['period'], $params['start'], $params['end']);
$periodDataForecast = \Scalr::getContainer()->analytics->usage->{$getPeriodicSubjectData}($subjectId, $forecastParams['period'], $forecastParams['start'], $forecastParams['end']);
$subjectEntity = call_user_func($subjectEntityName . 'Entity::findPk', $subjectId);
$baseUrl = rtrim(\Scalr::getContainer()->config('scalr.endpoint.scheme') . "://" . \Scalr::getContainer()->config('scalr.endpoint.host'), '/');
if (strpos($subjectEntityName, 'Project') !== false) {
$subjects = 'projects';
$subjectIdName = 'projectId';
} else {
$subjects = 'costcenters';
$subjectIdName = 'ccId';
}
$scope = $ifAccountLevel ? 'account' : 'admin';
$periodData['detailsUrl'] = "{$baseUrl}#/{$scope}/analytics/{$subjects}?{$subjectIdName}={$subjectId}";
$periodData['period'] = $params['period'];
$periodData['forecastPeriod'] = $formatedForecastDate;
$periodData['totals']['forecastCost'] = $periodDataForecast['totals']['forecastCost'];
$periodData['name'] = $subjectEntity->name;
$periodData['jsonVersion'] = '1.0.0';
$periodData['totals']['clouds'] = $this->changeCloudNames($periodData['totals']['clouds']);
if ($params['period'] !== 'custom') {
$periodData['totals']['prevPeriodDate'] = (new \DateTime($periodData['previousStartDate'], new \DateTimeZone('UTC')))->format('M d') . " - " . (new \DateTime($periodData['previousEndDate'], new \DateTimeZone('UTC')))->format('M d');
} else {
$periodData['totals']['prevPeriodDate'] = (new \DateTime($periodData['previousEndDate'], new \DateTimeZone('UTC')))->format('M d');
}
$periodData['date'] = $formatedTitle;
$itemKey = isset($periodData['totals']['projects']) ? 'projects' : 'farms';
if (count($periodData['totals'][$itemKey] > 1)) {
uasort($periodData['totals'][$itemKey], array($this, 'sortItems'));
if (count($periodData['totals'][$itemKey] > 6)) {
array_splice($periodData['totals'][$itemKey], 6, count($periodData['totals'][$itemKey]));
}
}
if (count($periodData['totals']['clouds'] > 1)) {
usort($periodData['totals']['clouds'], array($this, 'sortItems'));
}
$entity = ReportPayloadEntity::init([$subjectType, $subjectId, $params['period']], $periodData, $params['start']);
if (!ReportPayloadEntity::findPk($entity->uuid)) {
$payload = json_decode($entity->payload, true);
$emailTemplate = strpos($subjectEntityName, 'Project') !== false ? 'project' : 'cc';
if ($periodData['period'] === 'custom') {
$subjectPeriod = 'Daily';
} else {
$subjectPeriod = ucfirst($periodData['period']) . 'ly';
}
$emailSubject = (strpos($subjectEntityName, 'Project') !== false ? 'Project' : 'Cost Center') . ' ' . $subjectEntity->name . ' ' . $subjectPeriod . ' report';
\Scalr::getContainer()->mailer->setSubject($emailSubject)->setContentType('text/html')->sendTemplate(SCALR_TEMPLATES_PATH . '/emails/report_' . $emailTemplate . '.html.php', $payload, $emails);
$this->getLogger()->info('Report email has been sent');
$payload['date'] = $entity->created->format('Y-m-d');
$entity->payload = json_encode($payload);
$entity->save();
}
}