first commit
This commit is contained in:
134
app/Http/Controllers/DataCentreController.php
Normal file
134
app/Http/Controllers/DataCentreController.php
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\DataCentreRequest;
|
||||
use App\Models\Consultant;
|
||||
use App\Models\DataCentre;
|
||||
use App\Services\AssignmentService;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class DataCentreController extends Controller
|
||||
{
|
||||
public function __construct(protected AssignmentService $assignmentService) {}
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
$this->authorize('viewAny', DataCentre::class);
|
||||
|
||||
$user = auth()->user();
|
||||
$query = DataCentre::query()->with('currentConsultant');
|
||||
|
||||
// Perunding hanya lihat pusat data yang ditugaskan kepadanya.
|
||||
if ($user->isPerunding() && ! $user->can('pusat_data.urus')) {
|
||||
$query->where('current_consultant_id', $user->consultant?->id ?? 0);
|
||||
}
|
||||
|
||||
if ($search = $request->string('cari')->toString()) {
|
||||
$query->where('nama_pusat_data', 'like', "%$search%")
|
||||
->orWhere('tajuk_permohonan', 'like', "%$search%");
|
||||
}
|
||||
|
||||
if ($request->filled('status')) {
|
||||
$query->where('status', $request->status);
|
||||
}
|
||||
|
||||
$dataCentres = $query->orderBy('nama_pusat_data')->paginate(15)->withQueryString();
|
||||
|
||||
return view('data_centres.index', compact('dataCentres'));
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$this->authorize('create', DataCentre::class);
|
||||
|
||||
return view('data_centres.create', ['dataCentre' => new DataCentre]);
|
||||
}
|
||||
|
||||
public function store(DataCentreRequest $request)
|
||||
{
|
||||
$dataCentre = DataCentre::create($request->validated());
|
||||
|
||||
return redirect()->route('data-centres.show', $dataCentre)
|
||||
->with('success', 'Pusat data berjaya didaftarkan.');
|
||||
}
|
||||
|
||||
public function show(DataCentre $dataCentre)
|
||||
{
|
||||
$this->authorize('view', $dataCentre);
|
||||
|
||||
$dataCentre->load([
|
||||
'currentConsultant',
|
||||
'assignments.consultant',
|
||||
'assignments.assignedBy',
|
||||
'submissions.reportingCycle',
|
||||
]);
|
||||
$consultants = Consultant::aktif()->orderBy('nama_perunding')->get();
|
||||
|
||||
return view('data_centres.show', compact('dataCentre', 'consultants'));
|
||||
}
|
||||
|
||||
public function edit(DataCentre $dataCentre)
|
||||
{
|
||||
$this->authorize('update', $dataCentre);
|
||||
|
||||
return view('data_centres.edit', compact('dataCentre'));
|
||||
}
|
||||
|
||||
public function update(DataCentreRequest $request, DataCentre $dataCentre)
|
||||
{
|
||||
$this->authorize('update', $dataCentre);
|
||||
|
||||
$dataCentre->update($request->validated());
|
||||
|
||||
return redirect()->route('data-centres.show', $dataCentre)
|
||||
->with('success', 'Maklumat pusat data dikemas kini.');
|
||||
}
|
||||
|
||||
public function destroy(DataCentre $dataCentre)
|
||||
{
|
||||
$this->authorize('delete', $dataCentre);
|
||||
|
||||
$dataCentre->delete();
|
||||
|
||||
return redirect()->route('data-centres.index')
|
||||
->with('success', 'Pusat data telah dipadam.');
|
||||
}
|
||||
|
||||
/** Tugaskan / tukar perunding aktif untuk pusat data. */
|
||||
public function assign(Request $request, DataCentre $dataCentre)
|
||||
{
|
||||
$this->authorize('assignConsultant', $dataCentre);
|
||||
|
||||
$data = $request->validate([
|
||||
'consultant_id' => ['required', 'exists:consultants,id'],
|
||||
'start_date' => ['nullable', 'date'],
|
||||
'reason' => ['nullable', 'string', 'max:500'],
|
||||
], [], ['consultant_id' => 'perunding', 'reason' => 'sebab']);
|
||||
|
||||
$consultant = Consultant::findOrFail($data['consultant_id']);
|
||||
|
||||
$this->assignmentService->assign(
|
||||
$dataCentre,
|
||||
$consultant,
|
||||
auth()->user(),
|
||||
$data['start_date'] ?? null,
|
||||
$data['reason'] ?? null
|
||||
);
|
||||
|
||||
return redirect()->route('data-centres.show', $dataCentre)
|
||||
->with('success', 'Perunding berjaya ditugaskan. Penugasan terdahulu (jika ada) telah ditamatkan.');
|
||||
}
|
||||
|
||||
public function endAssignment(Request $request, DataCentre $dataCentre)
|
||||
{
|
||||
$this->authorize('assignConsultant', $dataCentre);
|
||||
|
||||
$data = $request->validate(['reason' => ['nullable', 'string', 'max:500']]);
|
||||
|
||||
$this->assignmentService->endActive($dataCentre, auth()->user(), $data['reason'] ?? null);
|
||||
|
||||
return redirect()->route('data-centres.show', $dataCentre)
|
||||
->with('success', 'Penugasan perunding aktif telah ditamatkan.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user