70 lines
2.0 KiB
PHP
70 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Widgets;
|
|
|
|
use App\Models\Report;
|
|
use Carbon\Carbon;
|
|
use Filament\Widgets\ChartWidget;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class TaskRealizationChart extends ChartWidget
|
|
{
|
|
protected ?string $heading = 'Realisasi Penugasan (Laporan)';
|
|
|
|
protected static ?int $sort = 10;
|
|
|
|
protected int|string|array $columnSpan = 1;
|
|
|
|
protected function getData(): array
|
|
{
|
|
$data = Report::query()
|
|
->select(
|
|
DB::raw('COUNT(*) as count'),
|
|
DB::raw('status'),
|
|
DB::raw("DATE_FORMAT(created_at, '%Y-%m') as month")
|
|
)
|
|
->where('created_at', '>=', now()->subMonths(6))
|
|
->groupBy('month', 'status')
|
|
->orderBy('month')
|
|
->get();
|
|
|
|
$months = $data->pluck('month')->unique()->sort()->values();
|
|
|
|
$acceptedData = [];
|
|
$pendingData = [];
|
|
$rejectedData = [];
|
|
|
|
foreach ($months as $month) {
|
|
$acceptedData[] = $data->where('month', $month)->accepted()->first()?->count ?? 0;
|
|
$pendingData[] = $data->where('month', $month)->pending()->first()?->count ?? 0;
|
|
$rejectedData[] = $data->where('month', $month)->rejected()->first()?->count ?? 0;
|
|
}
|
|
|
|
return [
|
|
'datasets' => [
|
|
[
|
|
'label' => 'Disetujui',
|
|
'data' => $acceptedData,
|
|
'backgroundColor' => '#10B981',
|
|
],
|
|
[
|
|
'label' => 'Menunggu',
|
|
'data' => $pendingData,
|
|
'backgroundColor' => '#F59E0B',
|
|
],
|
|
[
|
|
'label' => 'Ditolak',
|
|
'data' => $rejectedData,
|
|
'backgroundColor' => '#EF4444',
|
|
],
|
|
],
|
|
'labels' => $months->map(fn ($m) => Carbon::createFromFormat('Y-m', $m)->translatedFormat('M Y'))->toArray(),
|
|
];
|
|
}
|
|
|
|
protected function getType(): string
|
|
{
|
|
return 'bar';
|
|
}
|
|
}
|