<?php
 namespace App\Dashboard\Widget; class Charts_CancelledServicesCountDaily extends Charts_GeneralAbstract { use Traits\FinancialWidget; public function getTemplatePath() { return APP_REAL_PATH."/_templates/application/dashboard/widgets/D3Charts/column.tpl"; } protected function defineChart() { $chart = new \App\QuickReports\Charts\Types\Column(); $_LANG = \App\Loader_Lang::getInstance()->getAll(); $chart->setTitle($_LANG['dashboard']['widgetsList']['App\Dashboard\Widget\Charts_CancelledServicesCountDaily']['chartTitle']); $chart->setDescription($_LANG['dashboard']['widgetsList']['App\Dashboard\Widget\Charts_CancelledServicesCountDaily']['chartDescription']); $chart->setDataSetsDisplayGrouping('clusteredAndStacked'); $records = $this->getRecords(); $dataSets = $this->getDataSets($records); $chart->setDataSetsManually( $dataSets ); return $chart; } private function getRecords() { global $DBTPivotTablesData_orderServices; $startTransDate = \Engine\DateTime::create('-30 days')->formatDateIso(); $recordsList1SQL = "SELECT addDate AS transDate, COUNT(*) AS cnt FROM `$DBTPivotTablesData_orderServices` WHERE addDate>=? GROUP BY addDate"; $recordsList1 = $this->executeAndCacheSql($recordsList1SQL, [ $startTransDate ]); $recordsList2SQL = "SELECT cancelDate AS transDate, COUNT(*) AS cnt FROM `$DBTPivotTablesData_orderServices` WHERE cancelDate IS NOT NULL AND cancelDate>=? GROUP BY cancelDate"; $recordsList2 = $this->executeAndCacheSql($recordsList2SQL, [ $startTransDate ]); $recordsList3SQL = "SELECT refundDate AS transDate, COUNT(*) AS cnt FROM `$DBTPivotTablesData_orderServices` WHERE refundDate IS NOT NULL AND refundDate>=? GROUP BY refundDate"; $recordsList3 = $this->executeAndCacheSql($recordsList3SQL, [ $startTransDate ]); $records = []; $date = $startTransDate; while ($date<=TODAY_ISO) { $records['add'][$date] = 0; $records['cancel'][$date] = 0; $records['refund'][$date] = 0; $dateObj = \Engine\DateTime::createFromIsoDate($date); $dateObj->add(\DateInterval::createFromDateString('1 day')); $date = $dateObj->formatDateIso(); } foreach($recordsList1 as $row) { $records['add'][$row['transDate']] = (Int)$row['cnt']; } foreach($recordsList2 as $row) { $records['cancel'][$row['transDate']] = (Int)$row['cnt']; } foreach($recordsList3 as $row) { $records['refund'][$row['transDate']] = (Int)$row['cnt']; } return $records; } private function getDataSets($records) { $_LANG = \App\Loader_Lang::getInstance()->getGlobal(); $data = []; foreach ($records as $status=>$statusRecords) { $thisData = [ 'attributes' => [ 'label' => $_LANG['dashboard']['widgetsList']['App\Dashboard\Widget\Charts_CancelledServicesCountDaily']['status'][$status] ], 'data'=>[] ]; foreach ($statusRecords as $date => $value) { $thisData['data'][] = ['n'=>\Engine\DateTime::createFromIsoDate($date)->format('d M'), 'v'=>$value]; } $data[] = $thisData; } return $data; } }