diff --git a/app/Livewire/Charts.php b/app/Livewire/Charts.php index 0730ce4..567cc64 100644 --- a/app/Livewire/Charts.php +++ b/app/Livewire/Charts.php @@ -34,6 +34,13 @@ protected function getChartLastMonthsByUser() return $this->chartDataService->getChartLastMonthsByUser($dt_base, $this->nbMonths); } + protected function getTableByCategory() + { + $dt_base = today()->day(1); + + return $this->chartDataService->getTableByCategory($dt_base, $this->nbMonths); + } + public function render() { $charts = [ @@ -41,12 +48,16 @@ public function render() $this->getChartLastMonthsByUser(), ]; + $tables = [ + $this->getTableByCategory(), + ]; + if (Livewire::isLivewireRequest()) { foreach ($charts as $chart) { $this->dispatch(sprintf('charts-%s-update', $chart['name']), ...$chart); } } - return view('livewire.charts', compact('charts')); + return view('livewire.charts', compact('charts', 'tables')); } } diff --git a/app/Service/ChartDataService.php b/app/Service/ChartDataService.php index 9f74380..e2555be 100644 --- a/app/Service/ChartDataService.php +++ b/app/Service/ChartDataService.php @@ -127,4 +127,36 @@ public function getChartLastMonthsByUser(Carbon $dt_base, int $nbMonths) return compact('labels', 'datasets', 'title', 'name'); } + + public function getTableByCategory(Carbon $dt_base, int $nbMonths) + { + // Calcul libellé + $labels = $this->getLabels($dt_base, -$nbMonths, 'YYYYMM'); + $initialPeriod = $labels->reduce(fn ($carry, $label) => $carry->put($label, 0), collect()); + $labels = $this->getLabels($dt_base, -$nbMonths); + + $name = 'evolutions'; + + $query = Category::select(DB::raw('sum(price) / 100 as price')) + ->addSelect('categories.label') + ->addSelect(DB::raw("concat(notes.year, lpad(notes.month + 1, 2,'0')) as _period")) + ->leftJoin('notes', 'categories.id', '=', 'notes.category_id') + ->whereBetween( + DB::raw("cast(concat(notes.year,'-', notes.month + 1,'-1') as date)"), + [DB::raw("add_months('{$dt_base->toDateString()}', -$nbMonths)"), "{$dt_base->toDateString()}"] + ) + ->groupBy('categories.label', '_period'); + + $queryResult = $query->get(); + + $datasets = $queryResult->reduce(function ($carry, $record) use ($initialPeriod) { + $key = $record->getAttribute('label'); + $category = $carry->get($key, collect($initialPeriod)); + $category->put($record->getAttribute('_period'), $record->getAttribute('price')); + + return $carry->put($key, $category); + }, collect()); + + return compact('labels', 'name', 'datasets'); + } } diff --git a/resources/views/livewire/charts.blade.php b/resources/views/livewire/charts.blade.php index 603d861..8b77af1 100644 --- a/resources/views/livewire/charts.blade.php +++ b/resources/views/livewire/charts.blade.php @@ -15,5 +15,48 @@ class="min-w-28 bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded- @foreach ($charts as ['title' => $title, 'labels' => $labels, 'datasets' => $datasets, 'name' => $name]) @endforeach + + @foreach ($tables as $table) +
+ + + + + @foreach ($table['labels'] as $label) + + @endforeach + + + @foreach ($table['datasets'] as $key => $dataset) + @php + $lastValue = null; + @endphp + + + @foreach ($dataset as $data) + @php + $diff = $lastValue ? $data - $lastValue : null; + @endphp + + @php + $lastValue = $data; + @endphp + @endforeach + + @endforeach + +
+ {{ $table['name'] }} +
{{ $label }}
+ {{ $key }} +
{{ number_format($data, 2, ',', ' ') }}
+ @if ($diff) +
+ {{ number_format($diff, 2, ',', ' ') }}
+ @endif +
+
+ @endforeach