68 lines
2.5 KiB
PHP
68 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\DataCentre;
|
|
use App\Models\ReportingCycle;
|
|
use App\Models\Submission;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ReminderController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
abort_unless(auth()->user()->can('peringatan.lihat'), 403);
|
|
|
|
$cycles = ReportingCycle::orderByDesc('renewal_year')->get();
|
|
$cycleId = $request->input('cycle') ?: ReportingCycle::aktif()->orderByDesc('renewal_year')->value('id');
|
|
$cycle = $cycleId ? ReportingCycle::find($cycleId) : null;
|
|
|
|
$rows = collect();
|
|
|
|
if ($cycle) {
|
|
// Pusat data aktif yang mempunyai perunding aktif.
|
|
$query = DataCentre::query()->aktif()
|
|
->whereNotNull('current_consultant_id')
|
|
->with('currentConsultant');
|
|
|
|
// ID pusat data yang TELAH menghantar (bukan draf) untuk kitaran ini.
|
|
$submittedDcIds = Submission::where('reporting_cycle_id', $cycle->id)
|
|
->where('status', '!=', 'draf')
|
|
->pluck('data_centre_id')->all();
|
|
|
|
// Buang yang telah hantar — peraturan: keluar dari senarai peringatan selepas hantar.
|
|
$query->whereNotIn('id', $submittedDcIds);
|
|
|
|
if ($request->filled('consultant')) {
|
|
$query->where('current_consultant_id', $request->consultant);
|
|
}
|
|
if ($request->filled('data_centre')) {
|
|
$query->where('id', $request->data_centre);
|
|
}
|
|
|
|
$rows = $query->orderBy('nama_pusat_data')->get()->map(function ($dc) use ($cycle) {
|
|
// Adakah ada draf separa lengkap?
|
|
$draft = Submission::where('reporting_cycle_id', $cycle->id)
|
|
->where('data_centre_id', $dc->id)->first();
|
|
|
|
return (object) [
|
|
'data_centre' => $dc,
|
|
'consultant' => $dc->currentConsultant,
|
|
'status' => $draft ? $draft->status : 'belum_mula',
|
|
'is_overdue' => $cycle->isOverdue(),
|
|
'submission' => $draft,
|
|
];
|
|
});
|
|
|
|
// Penapis lampau tempoh sahaja.
|
|
if ($request->boolean('overdue')) {
|
|
$rows = $rows->filter(fn ($r) => $r->is_overdue)->values();
|
|
}
|
|
}
|
|
|
|
$consultants = \App\Models\Consultant::aktif()->orderBy('nama_perunding')->get();
|
|
|
|
return view('reminders.index', compact('rows', 'cycles', 'cycle', 'consultants'));
|
|
}
|
|
}
|