Initial commit
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\AcademicYear;
|
||||
use App\Models\StudentAcademicHistory;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\View\View;
|
||||
use RealRashid\SweetAlert\Facades\Alert;
|
||||
|
||||
class AcademicYearController extends Controller
|
||||
{
|
||||
public function index(Request $request): View
|
||||
{
|
||||
$search = trim((string) $request->get('search'));
|
||||
|
||||
$academicYears = AcademicYear::withCount(['classes', 'students'])
|
||||
->when($search !== '', function ($query) use ($search) {
|
||||
$query->where('name', 'like', "%{$search}%")
|
||||
->orWhere('start_year', 'like', "%{$search}%")
|
||||
->orWhere('end_year', 'like', "%{$search}%");
|
||||
})
|
||||
->latest()
|
||||
->paginate(10)
|
||||
->withQueryString();
|
||||
|
||||
return view('academic-years.index', compact('academicYears', 'search'));
|
||||
}
|
||||
|
||||
public function create(): View
|
||||
{
|
||||
return view('academic-years.create');
|
||||
}
|
||||
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'name' => ['required', 'string', 'max:50', 'unique:academic_years,name'],
|
||||
'start_year' => ['required', 'integer', 'digits:4'],
|
||||
'end_year' => ['required', 'integer', 'digits:4', 'gte:start_year'],
|
||||
'is_active' => ['nullable', 'boolean'],
|
||||
]);
|
||||
|
||||
$data['is_active'] = $request->boolean('is_active');
|
||||
|
||||
if ($data['is_active']) {
|
||||
AcademicYear::query()->update(['is_active' => false]);
|
||||
}
|
||||
|
||||
AcademicYear::create($data);
|
||||
|
||||
return redirect()->route('academic-years.index')->with('success', 'Tahun angkatan berhasil ditambahkan.');
|
||||
}
|
||||
|
||||
public function show(AcademicYear $academicYear): RedirectResponse
|
||||
{
|
||||
return redirect()->route('academic-years.edit', $academicYear);
|
||||
}
|
||||
|
||||
public function edit(AcademicYear $academicYear): View
|
||||
{
|
||||
return view('academic-years.edit', [
|
||||
'academicYear' => $academicYear,
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request, AcademicYear $academicYear): RedirectResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'name' => ['required', 'string', 'max:50', Rule::unique('academic_years', 'name')->ignore($academicYear->id)],
|
||||
'start_year' => ['required', 'integer', 'digits:4'],
|
||||
'end_year' => ['required', 'integer', 'digits:4', 'gte:start_year'],
|
||||
'is_active' => ['nullable', 'boolean'],
|
||||
]);
|
||||
|
||||
$data['is_active'] = $request->boolean('is_active');
|
||||
|
||||
if ($data['is_active']) {
|
||||
AcademicYear::whereKeyNot($academicYear->id)->update(['is_active' => false]);
|
||||
}
|
||||
|
||||
$academicYear->update($data);
|
||||
|
||||
return redirect()->route('academic-years.index')->with('success', 'Tahun angkatan berhasil diperbarui.');
|
||||
}
|
||||
|
||||
public function destroy(AcademicYear $academicYear): RedirectResponse
|
||||
{
|
||||
$academicYear->loadCount('students');
|
||||
$classesWithStudentsCount = $academicYear->classes()->withCount('students')->get()->where('students_count', '>', 0)->count();
|
||||
$emptyClassesCount = $academicYear->classes()->withCount('students')->get()->where('students_count', 0)->count();
|
||||
|
||||
if ($academicYear->students_count > 0 || $classesWithStudentsCount > 0) {
|
||||
Alert::error('Tidak bisa dihapus', 'Tahun angkatan ini masih digunakan oleh data siswa aktif atau kelas yang masih berisi siswa.');
|
||||
|
||||
return redirect()->route('academic-years.index')->withErrors([
|
||||
'delete' => 'Tahun angkatan tidak bisa dihapus karena masih digunakan oleh data siswa aktif atau kelas yang masih berisi siswa.',
|
||||
]);
|
||||
}
|
||||
|
||||
DB::transaction(function () use ($academicYear) {
|
||||
$classIds = $academicYear->classes()->pluck('id');
|
||||
|
||||
StudentAcademicHistory::query()
|
||||
->whereIn('class_id', $classIds)
|
||||
->delete();
|
||||
|
||||
$academicYear->classes()->delete();
|
||||
$academicYear->delete();
|
||||
});
|
||||
|
||||
$message = $emptyClassesCount > 0
|
||||
? "Tahun angkatan berhasil dihapus beserta {$emptyClassesCount} kelas kosong terkait."
|
||||
: 'Tahun angkatan berhasil dihapus.';
|
||||
|
||||
Alert::success('Berhasil', $message);
|
||||
|
||||
return redirect()->route('academic-years.index')->with('success', $message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user