feat: Implement local scopes for report statuses in the Report model and update the TaskRealizationChart to filter by ApprovalStatus enum.

This commit is contained in:
Yoga Pangestu 2026-02-24 11:11:18 +07:00
parent 23e19e177a
commit e4cd88136e
2 changed files with 26 additions and 4 deletions

View File

@ -2,6 +2,7 @@
namespace App\Filament\Widgets;
use App\Enums\ApprovalStatus;
use App\Models\Report;
use BezhanSalleh\FilamentShield\Traits\HasWidgetShield;
use Carbon\Carbon;
@ -27,7 +28,7 @@ protected function getData(): array
$data = Report::query()
->select(
DB::raw('COUNT(*) as count'),
DB::raw('status'),
'status',
DB::raw("DATE_FORMAT(created_at, '%Y-%m') as month")
)
->when($startDate, fn ($q) => $q->whereDate('created_at', '>=', $startDate))
@ -44,9 +45,10 @@ protected function getData(): array
$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;
$monthData = $data->where('month', $month);
$acceptedData[] = $monthData->where('status', ApprovalStatus::ACCEPTED)->first()?->count ?? 0;
$pendingData[] = $monthData->where('status', ApprovalStatus::PENDING)->first()?->count ?? 0;
$rejectedData[] = $monthData->where('status', ApprovalStatus::REJECTED)->first()?->count ?? 0;
}
return [

View File

@ -3,6 +3,8 @@
namespace App\Models;
use App\Enums\ApprovalStatus;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@ -27,6 +29,24 @@ protected function casts(): array
];
}
#[Scope]
protected function accepted(Builder $query): void
{
$query->where('status', ApprovalStatus::ACCEPTED);
}
#[Scope]
protected function pending(Builder $query): void
{
$query->where('status', ApprovalStatus::PENDING);
}
#[Scope]
protected function rejected(Builder $query): void
{
$query->where('status', ApprovalStatus::REJECTED);
}
public function mediaTaskAssignment(): BelongsTo
{
return $this->belongsTo(MediaTaskAssignment::class);