Files
kupshop/bundles/KupShop/AdminBundle/Util/Stats/StatsDataHelper.php
2025-08-02 16:30:27 +02:00

152 lines
4.4 KiB
PHP

<?php
namespace KupShop\AdminBundle\Util\Stats;
trait StatsDataHelper
{
public function countDays()
{
$d1 = $this->from;
$d2 = $this->to;
return $d1->diff($d2)->days + 1;
}
public function countMonths()
{
$d1 = $this->from;
$d2 = $this->to;
return abs($d2->format('m') - $d1->format('m') + (($d2->format('Y') - $d1->format('Y')) * 12)) + 1;
}
public function countYears()
{
$d1 = $this->from;
$d2 = $this->to;
return $d1->diff($d2)->y + 1;
}
public function getDaysData($DBData)
{
$startDay = $this->from->format('j');
$startMonth = $this->from->format('n');
$startYear = $this->from->format('Y');
$endDay = $this->from->format('j');
$endMonth = $this->to->format('n');
$endYear = $this->to->format('Y');
$countMonths = $this->countMonths();
$countDays = $this->countDays();
$x = [];
$data = [];
for ($year = 0; $year <= ($endYear - $startYear); $year++) {
for ($month = $startMonth; $month <= 12; $month++) {
$days = cal_days_in_month(CAL_GREGORIAN, $month, $startYear + $year);
for ($day = $startDay; $day <= $days; $day++) {
if ($countDays == 0) {
break;
} else {
$countDays--;
}
if ($this->tabInstance && method_exists($this->tabInstance, 'getDaysData')) {
$this->tabInstance->{'getDaysData'}($data, $DBData, $year, $month, $day);
} else {
$data[0]['data'][] = (!empty($DBData[$year][$month][$day])) ? round($DBData[$year][$month][$day], 2) : 0;
}
$x[] = $day.'.';
}
$startDay = 1;
if ($countMonths == 0) {
break;
} else {
$countMonths--;
}
}
$startMonth = 1;
}
return [$x, $data];
}
public function getMonthsData($DBData)
{
$startMonth = $this->from->format('n');
$startYear = $this->from->format('Y');
$endMonth = $this->to->format('n');
$endYear = $this->to->format('Y');
$countMonths = $this->countMonths();
$x = [];
$data = [];
for ($year = 0; $year <= ($endYear - $startYear); $year++) {
for ($month = $startMonth; $month <= 12; $month++) {
if ($countMonths == 0) {
break;
} else {
$countMonths--;
}
if ($this->tabInstance && method_exists($this->tabInstance, 'getMonthsData')) {
$this->tabInstance->{'getMonthsData'}($data, $DBData, $year, $month);
} else {
$data[0]['data'][] = (!empty($DBData[$year][$month])) ? round($DBData[$year][$month], 2) : 0;
}
$x[] = translate($this->months[$month], 'timedate');
}
$startMonth = 1;
}
return [$x, $data];
}
public function getYearsData($DBData)
{
$startYear = $this->from->format('Y');
$endYear = $this->to->format('Y');
$x = [];
$data = [];
for ($year = 0; $year <= $endYear - $startYear; $year++) {
if ($this->tabInstance && method_exists($this->tabInstance, 'getYearsData')) {
$this->tabInstance->{'getYearsData'}($data, $DBData, $year);
} else {
$data[0]['data'][] = (!empty($DBData[$year])) ? round($DBData[$year], 2) : 0;
}
$x[] = $startYear + $year;
}
return [$x, $data];
}
public function formatData($SQL)
{
$data = [];
if ($this->interval == 'year') {
foreach ($SQL as $row) {
$data[$row['year']] = $row['sum'];
}
} elseif ($this->interval == 'month') {
foreach ($SQL as $row) {
$data[$row['year']][$row['month']] = $row['sum'];
}
} elseif ($this->interval == 'day') {
foreach ($SQL as $row) {
$data[$row['year']][$row['month']][$row['day']] = $row['sum'];
}
}
return $data;
}
}