184 lines
6.3 KiB
PHP
184 lines
6.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Kriteria;
|
|
use App\Models\SubKriteria;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class SubkriteriaController extends Controller
|
|
{
|
|
public function index(\Illuminate\Http\Request $request)
|
|
{
|
|
// Tangkap request 'per_page', default-nya 10
|
|
$perPage = $request->input('per_page', 10);
|
|
|
|
// Gunakan paginate pada Kriteria (Induk)
|
|
$kriterias = Kriteria::query()
|
|
->with(['subKriterias' => function ($query) {
|
|
$query->orderBy('nilai_acuan', 'asc');
|
|
}])
|
|
->orderBy('kode', 'asc')
|
|
->paginate($perPage)
|
|
->appends($request->query());
|
|
|
|
return view('subkriteria.index', compact('kriterias', 'perPage'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$kriterias = Kriteria::all();
|
|
return view('subkriteria.create', compact('kriterias'));
|
|
}
|
|
|
|
public function getByKriteria($kriteria_id)
|
|
{
|
|
$subs = SubKriteria::where('kriteria_id', $kriteria_id)
|
|
->orderBy('nilai_acuan', 'asc')
|
|
->get();
|
|
|
|
return response()->json($subs);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'kriteria_id' => 'required|exists:kriterias,id',
|
|
'nama_sub' => 'required|array',
|
|
'nama_sub.*' => 'required|string|max:255',
|
|
'nilai_acuan' => 'required|array',
|
|
'nilai_acuan.*' => 'required|numeric',
|
|
]);
|
|
|
|
DB::transaction(function () use ($request) {
|
|
|
|
SubKriteria::where('kriteria_id', $request->kriteria_id)->update(['bobot_lokal' => 0]);
|
|
|
|
foreach ($request->nama_sub as $index => $nama) {
|
|
SubKriteria::create([
|
|
'kriteria_id' => $request->kriteria_id,
|
|
'nama_sub' => $nama,
|
|
'nilai_acuan' => $request->nilai_acuan[$index],
|
|
'kode' => 'TEMP-'.uniqid(),
|
|
'bobot_lokal' => 0,
|
|
]);
|
|
}
|
|
$this->refreshSubKriteriaCodes($request->kriteria_id);
|
|
});
|
|
|
|
return redirect()->route('subkriteria.index')->with('success', 'Data berhasil ditambah & Bobot lokal direset. Silakan lakukan perbandingan ulang pakar.');
|
|
}
|
|
|
|
// --- METHOD EDIT ---
|
|
public function edit($id)
|
|
{
|
|
$subkriteria = SubKriteria::findOrFail($id);
|
|
// Kita ambil data kriteria induknya saja untuk label di view
|
|
$kriteria = Kriteria::findOrFail($subkriteria->kriteria_id);
|
|
|
|
return view('subkriteria.edit', compact('subkriteria', 'kriteria'));
|
|
}
|
|
|
|
// --- METHOD UPDATE ---
|
|
public function update(Request $request, $id)
|
|
{
|
|
$request->validate([
|
|
'nama_sub' => 'required|string|max:255',
|
|
'nilai_acuan' => 'required|numeric',
|
|
]);
|
|
|
|
$sub = SubKriteria::findOrFail($id);
|
|
|
|
DB::transaction(function () use ($request, $sub) {
|
|
// Update data sub kriteria
|
|
$sub->update([
|
|
'nama_sub' => $request->nama_sub,
|
|
'nilai_acuan' => $request->nilai_acuan,
|
|
]);
|
|
|
|
// Refresh kode agar jika urutan berubah, S.C1.1 dst tetap rapi
|
|
$this->refreshSubKriteriaCodes($sub->kriteria_id);
|
|
});
|
|
|
|
return redirect()->route('subkriteria.index')->with('success', 'Sub Kriteria berhasil diperbarui!');
|
|
}
|
|
|
|
private function refreshSubKriteriaCodes($kriteria_id)
|
|
{
|
|
$kriteria = Kriteria::find($kriteria_id);
|
|
$subs = SubKriteria::where('kriteria_id', $kriteria_id)
|
|
->orderBy('nilai_acuan', 'asc')
|
|
->get();
|
|
|
|
foreach ($subs as $sub) {
|
|
$sub->kode = 'TEMP-'.$sub->id.'-'.uniqid();
|
|
$sub->save();
|
|
}
|
|
|
|
foreach ($subs as $index => $sub) {
|
|
$sub->kode = 'S.'.$kriteria->kode.'.'.($index + 1);
|
|
$sub->save();
|
|
}
|
|
}
|
|
|
|
// public function destroy($id)
|
|
// {
|
|
// $sub = SubKriteria::findOrFail($id);
|
|
// $k_id = $sub->kriteria_id;
|
|
// $sub->delete();
|
|
|
|
// $this->refreshSubKriteriaCodes($k_id);
|
|
|
|
// return redirect()->route('subkriteria.index')->with('success', 'Sub Kriteria dihapus!');
|
|
// }
|
|
public function destroy($id)
|
|
{
|
|
// ===================================================================
|
|
// 1. PAGAR PELINDUNG ARSIP
|
|
// Cek apakah sistem sudah pernah menghasilkan riwayat Hasil Seleksi
|
|
// ===================================================================
|
|
$adaHasilSeleksi = DB::table('hasil_seleksis')->count();
|
|
|
|
if ($adaHasilSeleksi > 0) {
|
|
return redirect()->back()->with('error', 'Gagal! Sub-Kriteria tidak bisa dihapus karena kriteria induknya sudah menjadi arsip riwayat Hasil Seleksi. Silakan Edit (Update) saja namanya jika ada perubahan.');
|
|
}
|
|
|
|
// jika lolos gass lanjut hapus heheee
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
$sub = SubKriteria::findOrFail($id);
|
|
$k_id = $sub->kriteria_id;
|
|
|
|
// 1. Validasi: Jangan biarkan kriteria tidak punya subkriteria sama sekali
|
|
$count = SubKriteria::where('kriteria_id', $k_id)->count();
|
|
if ($count <= 1) {
|
|
return redirect()->back()->with('error', 'Gagal! Kriteria wajib memiliki minimal satu sub-kriteria.');
|
|
}
|
|
|
|
// 2. Antisipasi data warga: Pindahkan warga yang memilih sub ini ke sub lain yang tersisa
|
|
$subLain = SubKriteria::where('kriteria_id', $k_id)->where('id', '!=', $id)->first();
|
|
DB::table('warga_details')
|
|
->where('sub_kriteria_id', $id)
|
|
->update(['sub_kriteria_id' => $subLain->id]);
|
|
|
|
// 3. Hapus Subkriteria
|
|
$sub->delete();
|
|
|
|
// 4. Reset bobot lokal semua subkriteria yang tersisa di kriteria ini
|
|
SubKriteria::where('kriteria_id', $k_id)->update(['bobot_lokal' => 0]);
|
|
|
|
// 5. Rapikan kode S.C1.1 dst
|
|
$this->refreshSubKriteriaCodes($k_id);
|
|
|
|
DB::commit();
|
|
return redirect()->route('subkriteria.index')->with('success', 'Sub-Kriteria dihapus & Bobot lokal direset!');
|
|
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return redirect()->back()->with('error', 'Terjadi kesalahan: ' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|