diff --git a/app/Ninja/Reports/AbstractReport.php b/app/Ninja/Reports/AbstractReport.php index a96f0e4ca010..a7f903da8d3a 100644 --- a/app/Ninja/Reports/AbstractReport.php +++ b/app/Ninja/Reports/AbstractReport.php @@ -3,6 +3,9 @@ namespace App\Ninja\Reports; use Auth; +use DateInterval; +use DatePeriod; +use stdClass; class AbstractReport { @@ -13,6 +16,7 @@ class AbstractReport public $totals = []; public $data = []; + public $chartData = []; public function __construct($startDate, $endDate, $isExport, $options = false) { @@ -141,4 +145,101 @@ class AbstractReport return join('', $reportParts); } + + protected function addChartData($dimension, $date, $amount) + { + if (! isset($this->chartData[$dimension])) { + $this->chartData[$dimension] = []; + } + + $date = $this->formatDate($date); + + if (! isset($this->chartData[$dimension][$date])) { + $this->chartData[$dimension][$date] = 0; + } + + $this->chartData[$dimension][$date] += $amount; + } + + public function chartGroupBy() + { + $groupBy = empty($this->options['group_dates_by']) ? 'day' : $this->options['group_dates_by']; + + if ($groupBy == 'monthyear') { + $groupBy = 'month'; + } + + return strtoupper($groupBy); + } + + protected function formatDate($date) + { + $groupBy = $this->chartGroupBy(); + $dateFormat = $groupBy == 'DAY' ? 'z' : ($groupBy == 'MONTH' ? 'm' : ''); + + return $date->format('Y' . $dateFormat); + } + + public function getChartData() + { + $startDate = date_create($this->startDate); + $endDate = date_create($this->endDate); + $groupBy = $this->chartGroupBy(); + + /* + if ($groupBy == 'DAY') { + $groupBy = 'DAYOFYEAR'; + } + */ + + $datasets = []; + $labels = []; + $totals = new stdClass(); + + foreach ($this->chartData as $dimension => $data) { + $endDate->modify('+1 '.$groupBy); + $interval = new DateInterval('P1'.substr($groupBy, 0, 1)); + $period = new DatePeriod($startDate, $interval, $endDate); + $endDate->modify('-1 '.$groupBy); + $records = []; + + foreach ($period as $date) { + $labels[] = $date->format('m/d/Y'); + /* + if ($entityType == ENTITY_INVOICE) { + $labels[] = $d->format('m/d/Y'); + } + */ + + $date = $this->formatDate($date); + $records[] = isset($data[$date]) ? $data[$date] : 0; + } + + $color = '51,122,183'; + /* + if ($entityType == ENTITY_INVOICE) { + $color = '51,122,183'; + } elseif ($entityType == ENTITY_PAYMENT) { + $color = '54,193,87'; + } elseif ($entityType == ENTITY_EXPENSE) { + $color = '128,128,128'; + } + */ + + $record = new stdClass(); + $record->data = $records; + $record->label = trans("texts.{$dimension}"); + $record->lineTension = 0; + $record->borderWidth = 4; + $record->borderColor = "rgba({$color}, 1)"; + $record->backgroundColor = "rgba({$color}, 0.1)"; + $datasets[] = $record; + } + + $data = new stdClass(); + $data->labels = $labels; + $data->datasets = $datasets; + + return $data; + } } diff --git a/app/Ninja/Reports/ActivityReport.php b/app/Ninja/Reports/ActivityReport.php index cf4e9dce9359..c3ba29583303 100644 --- a/app/Ninja/Reports/ActivityReport.php +++ b/app/Ninja/Reports/ActivityReport.php @@ -37,8 +37,10 @@ class ActivityReport extends AbstractReport $activity->present()->user, $this->isExport ? strip_tags($activity->getMessage()) : $activity->getMessage(), ]; + + $this->addChartData(ENTITY_ACTIVITY, $activity->created_at, 1); } - + //dd($this->getChartData()); } } diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index ad378395de68..0c35e9d40b64 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -17,7 +17,6 @@ @if (Auth::user()->hasPermission('view_all')) function loadChart(data) { - console.log(data); var ctx = document.getElementById('chart-canvas').getContext('2d'); if (window.myChart) { window.myChart.config.data = data; diff --git a/resources/views/reports/chart_builder.blade.php b/resources/views/reports/chart_builder.blade.php new file mode 100644 index 000000000000..9eab77745e0e --- /dev/null +++ b/resources/views/reports/chart_builder.blade.php @@ -0,0 +1,91 @@ + + +
diff --git a/resources/views/reports/emails.blade.php b/resources/views/reports/emails.blade.php index 4be7934a9890..6e0afb4a42cf 100644 --- a/resources/views/reports/emails.blade.php +++ b/resources/views/reports/emails.blade.php @@ -81,7 +81,7 @@ ticks: { beginAtZero: true, callback: function(label, index, labels) { - return label; + return roundSignificant(label); } }, }] diff --git a/resources/views/reports/report_builder.blade.php b/resources/views/reports/report_builder.blade.php index 91f0b0bf3909..3f8c89624c66 100644 --- a/resources/views/reports/report_builder.blade.php +++ b/resources/views/reports/report_builder.blade.php @@ -5,6 +5,8 @@ @include('money_script') + + @@ -264,6 +266,9 @@ @if (request()->report_type) + + @include('reports.chart_builder') +