Skip to content

Commit

Permalink
Merge pull request #106 from sylfel:add-table-statistic
Browse files Browse the repository at this point in the history
feat: add basic table statistic
  • Loading branch information
sylfel authored Dec 3, 2024
2 parents e91797f + adc71b5 commit 1614eab
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
13 changes: 12 additions & 1 deletion app/Livewire/Charts.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,30 @@ 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 = [
$this->getChartLastMonths(),
$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'));
}
}
32 changes: 32 additions & 0 deletions app/Service/ChartDataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
43 changes: 43 additions & 0 deletions resources/views/livewire/charts.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])
<livewire:chart :key="$name" :title="$title" :labels="$labels" :datasets="$datasets" :name="$name" />
@endforeach

@foreach ($tables as $table)
<div class="col-span-2 grid">
<table class="border-collapse border border-slate-400 text-right">
<caption class="caption-top">
{{ $table['name'] }}
</caption>
<thead>
<th class="bg-slate-400"></th>
@foreach ($table['labels'] as $label)
<th scope="col" class="text-lg py-2 bg-slate-400">{{ $label }}</th>
@endforeach
</thead>
<tbody>
@foreach ($table['datasets'] as $key => $dataset)
@php
$lastValue = null;
@endphp
<tr class="odd:bg-white even:bg-slate-200">
<th scope="row" class="pt-2 pb-4 text-lg">
{{ $key }}</th>
@foreach ($dataset as $data)
@php
$diff = $lastValue ? $data - $lastValue : null;
@endphp
<td class="align-top">
<div>{{ number_format($data, 2, ',', ' ') }}</div>
@if ($diff)
<div
class="text-sm italic {{ $diff > 0 ? 'text-red-600' : 'text-green-600' }}">
{{ number_format($diff, 2, ',', ' ') }}</div>
@endif
</td>
@php
$lastValue = $data;
@endphp
@endforeach
</tr>
@endforeach
</tbody>
</table>
</div>
@endforeach
</div>
</div>

0 comments on commit 1614eab

Please sign in to comment.