|
| 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 Carbon\CarbonPeriod; |
| 11 | +use Filament\Widgets\BarChartWidget; |
| 12 | +use Illuminate\Database\Eloquent\Collection; |
| 13 | +use Illuminate\Support\Facades\DB; |
| 14 | + |
| 15 | +class WeeklyReport extends BarChartWidget |
| 16 | +{ |
| 17 | + protected int|string|array $columnSpan = [ |
| 18 | + 'sm' => 1, |
| 19 | + 'md' => 6, |
| 20 | + 'lg' => 3 |
| 21 | + ]; |
| 22 | + |
| 23 | + protected function getData(): array |
| 24 | + { |
| 25 | + $now = Carbon::now(); |
| 26 | + |
| 27 | + $weekStartDate = $now->startOfWeek()->format('Y-m-d'); |
| 28 | + $weekEndDate = $now->endOfWeek()->format('Y-m-d'); |
| 29 | + |
| 30 | + $collection = $this->filter(auth()->user(), [ |
| 31 | + 'year' => null, |
| 32 | + 'weekStartDate' => $weekStartDate, |
| 33 | + 'weekEndDate' => $weekEndDate |
| 34 | + ]); |
| 35 | + |
| 36 | + $dates = $this->buildDatesRange($weekStartDate, $weekEndDate); |
| 37 | + |
| 38 | + $datasets = $this->buildRapport($collection, $dates); |
| 39 | + |
| 40 | + return [ |
| 41 | + 'datasets' => [ |
| 42 | + [ |
| 43 | + 'label' => __('Weekly time logged'), |
| 44 | + 'data' => $datasets, |
| 45 | + 'backgroundColor' => [ |
| 46 | + 'rgba(54, 162, 235, .6)' |
| 47 | + ], |
| 48 | + 'borderColor' => [ |
| 49 | + 'rgba(54, 162, 235, .8)' |
| 50 | + ], |
| 51 | + ], |
| 52 | + ], |
| 53 | + 'labels' => $dates, |
| 54 | + ]; |
| 55 | + } |
| 56 | + |
| 57 | + protected function buildRapport(Collection $collection, array $dates): array |
| 58 | + { |
| 59 | + $template = $this->createReportTemplate($dates); |
| 60 | + foreach ($collection as $item) { |
| 61 | + $template[$item->day]['value'] = $item->value; |
| 62 | + } |
| 63 | + return collect($template)->pluck('value')->toArray(); |
| 64 | + } |
| 65 | + |
| 66 | + protected function filter(User $user, array $params) |
| 67 | + { |
| 68 | + return TicketHour::select([ |
| 69 | + DB::raw("DATE_FORMAT(created_at,'%Y-%m-%d') as day"), |
| 70 | + DB::raw('SUM(value) as value'), |
| 71 | + ]) |
| 72 | + ->whereBetween('created_at', [$params['weekStartDate'], $params['weekEndDate']]) |
| 73 | + ->whereRaw( |
| 74 | + DB::raw("YEAR(created_at)=" . (is_null($params['year']) ? Carbon::now()->format('Y') : $params['year'])) |
| 75 | + ) |
| 76 | + ->where('user_id', $user->id) |
| 77 | + ->groupBy(DB::raw("DATE_FORMAT(created_at,'%Y-%m-%d')")) |
| 78 | + ->get(); |
| 79 | + } |
| 80 | + |
| 81 | + protected function buildDatesRange($weekStartDate, $weekEndDate): array |
| 82 | + { |
| 83 | + $period = CarbonPeriod::create($weekStartDate, $weekEndDate); |
| 84 | + |
| 85 | + $dates = []; |
| 86 | + foreach ($period as $item) { |
| 87 | + $dates[] = $item->format('Y-m-d'); |
| 88 | + } |
| 89 | + |
| 90 | + return $dates; |
| 91 | + } |
| 92 | + |
| 93 | + protected function createReportTemplate(array $dates): array |
| 94 | + { |
| 95 | + $template = []; |
| 96 | + foreach ($dates as $date) { |
| 97 | + $template[$date]['value'] = 0; |
| 98 | + } |
| 99 | + return $template; |
| 100 | + } |
| 101 | +} |
0 commit comments