|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Filament\Widgets\Timesheet; |
| 6 | + |
| 7 | +use App\Models\TicketHour; |
| 8 | +use App\Models\User; |
| 9 | +use Carbon\Carbon; |
| 10 | +use Filament\Widgets\BarChartWidget; |
| 11 | +use Illuminate\Support\Facades\DB; |
| 12 | + |
| 13 | +class MonthlyReport extends BarChartWidget |
| 14 | +{ |
| 15 | + protected function getHeading(): string |
| 16 | + { |
| 17 | + return __('Logged time monthly'); |
| 18 | + } |
| 19 | + |
| 20 | + protected function getData(): array |
| 21 | + { |
| 22 | + $months = [ |
| 23 | + 1 => ['January', 0], |
| 24 | + 2 => ['February', 0], |
| 25 | + 3 => ['March', 0], |
| 26 | + 4 => ['April', 0], |
| 27 | + 5 => ['May', 0], |
| 28 | + 6 => ['June', 0], |
| 29 | + 7 => ['July', 0], |
| 30 | + 8 => ['August', 0], |
| 31 | + 9 => ['September', 0], |
| 32 | + 10 => ['October', 0], |
| 33 | + 11 => ['November', 0], |
| 34 | + 12 => ['December', 0] |
| 35 | + ]; |
| 36 | + |
| 37 | + |
| 38 | + $data = $this->filter(auth()->user()); |
| 39 | + |
| 40 | + foreach ($data as $value) { |
| 41 | + if (isset($months[(int)$value->month])) { |
| 42 | + $months[(int)$value->month][1] = (float)$value->value; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + $datasets = []; |
| 47 | + $labels = []; |
| 48 | + foreach ($months as $month) { |
| 49 | + $datasets[] = $month[1]; |
| 50 | + $labels[] = $month[0]; |
| 51 | + } |
| 52 | + |
| 53 | + return [ |
| 54 | + 'datasets' => [ |
| 55 | + [ |
| 56 | + 'label' => __('Total time logged'), |
| 57 | + 'data' => $datasets, |
| 58 | + 'backgroundColor' => [ |
| 59 | + 'rgba(54, 162, 235, .6)' |
| 60 | + ], |
| 61 | + 'borderColor' => [ |
| 62 | + 'rgba(54, 162, 235, .8)' |
| 63 | + ], |
| 64 | + ], |
| 65 | + ], |
| 66 | + 'labels' => $labels, |
| 67 | + ]; |
| 68 | + } |
| 69 | + |
| 70 | + protected int|string|array $columnSpan = [ |
| 71 | + 'sm' => 1, |
| 72 | + 'md' => 6, |
| 73 | + 'lg' => 3 |
| 74 | + ]; |
| 75 | + |
| 76 | + protected function filter(User $user) |
| 77 | + { |
| 78 | + return TicketHour::select([ |
| 79 | + DB::raw("DATE_FORMAT (created_at, '%m') as month"), |
| 80 | + DB::raw('SUM(value) as value'), |
| 81 | + ]) |
| 82 | + ->where('user_id', $user->id) |
| 83 | + ->whereRaw(DB::raw("YEAR(created_at)=") . Carbon::now()->format('Y')) |
| 84 | + ->groupBy(DB::raw("DATE_FORMAT (created_at, '%m')")) |
| 85 | + ->get(); |
| 86 | + } |
| 87 | +} |
0 commit comments