169 lines
5.5 KiB
PHP
169 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Models\Kriteria;
|
|
use App\Models\Crips;
|
|
use Barryvdh\DomPDF\Facade\Pdf;
|
|
use Carbon\Carbon;
|
|
use Validator;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class KriteriaController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth');
|
|
$this->middleware('admin');
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$kriteria = Kriteria::orderBy('id','ASC')->paginate(10);
|
|
$totalBobot = round(Kriteria::sum('bobot'), 2);
|
|
|
|
return view('admin.kriteria.index', compact('kriteria' , 'totalBobot'));
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
try {
|
|
// Validasi input
|
|
$validator = Validator::make($request->all(), [
|
|
'nama_kriteria' => 'required|string|unique:kriteria,nama_kriteria|max:255',
|
|
'attribut' => 'required|in:cost,benefit',
|
|
'bobot' => 'required|numeric|min:0|max:1'
|
|
], [
|
|
'nama_kriteria.required' => 'Nama kriteria wajib diisi',
|
|
'nama_kriteria.unique' => 'Nama kriteria sudah digunakan',
|
|
'nama_kriteria.max' => 'Nama kriteria maksimal 255 karakter',
|
|
'attribut.required' => 'Attribut wajib dipilih',
|
|
'attribut.in' => 'Attribut harus benefit atau cost',
|
|
'bobot.required' => 'Bobot wajib diisi',
|
|
'bobot.numeric' => 'Bobot harus berupa angka',
|
|
'bobot.min' => 'Bobot minimal 0',
|
|
'bobot.max' => 'Bobot maksimal 1'
|
|
]);
|
|
|
|
// Jika validasi gagal
|
|
if ($validator->fails()) {
|
|
return redirect()->back()->withErrors($validator)->withInput()
|
|
->with('error', 'Terdapat kesalahan dalam pengisian form. Data tidak disimpan.');
|
|
}
|
|
|
|
// Hitung total bobot yang sudah ada
|
|
$currentTotal = Kriteria::sum('bobot');
|
|
$newTotal = round($currentTotal + $request->bobot, 4);
|
|
|
|
// Format untuk tampilan angka
|
|
$formattedNewTotal = number_format($newTotal, 2);
|
|
|
|
// Periksa konfirmasi dari user
|
|
$isConfirmed = $request->confirmed == '1';
|
|
|
|
// Jika total bobot tidak sama dengan 1 dan belum ada konfirmasi
|
|
if (($newTotal > 1 || $newTotal < 1) && !$isConfirmed) {
|
|
return redirect()->back()
|
|
->withInput()
|
|
->with('warning', 'Total bobot akan menjadi ' . $formattedNewTotal . ' (tidak sama dengan 1). Konfirmasi terlebih dahulu.');
|
|
}
|
|
|
|
// Simpan data kriteria ke database
|
|
Kriteria::create([
|
|
'nama_kriteria' => $request->nama_kriteria,
|
|
'attribut' => $request->attribut,
|
|
'bobot' => $request->bobot
|
|
]);
|
|
|
|
return redirect()->route('kriteria.index')->with('success', 'Kriteria berhasil ditambahkan.');
|
|
|
|
} catch (\Throwable $e) {
|
|
// Tangani error tak terduga
|
|
return redirect()->back()->withInput()->with('error', 'Terjadi kesalahan saat menyimpan data: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
public function edit($id)
|
|
{
|
|
$kriteria = Kriteria::findOrFail($id);
|
|
return view('admin.kriteria.edit', compact('kriteria'));
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$kriteria = Kriteria::findOrFail($id);
|
|
$currentBobot = $kriteria->bobot;
|
|
|
|
// Validasi input
|
|
$validator = Validator::make($request->all(), [
|
|
'nama_kriteria' => [
|
|
'required',
|
|
'string',
|
|
'max:255',
|
|
Rule::unique('kriteria')->ignore($kriteria->id)
|
|
],
|
|
'attribut' => 'required|in:cost,benefit',
|
|
'bobot' => 'required|numeric|min:0|max:1'
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return back()->withErrors($validator)->withInput();
|
|
}
|
|
|
|
// Hitung total bobot setelah update
|
|
$totalBobot = round(Kriteria::sum('bobot') - $currentBobot + $request->bobot, 4);
|
|
$formattedTotal = number_format($totalBobot, 2);
|
|
|
|
// Jika melebihi 1, warning tapi tetap update
|
|
$kriteria->update([
|
|
'nama_kriteria' => $request->nama_kriteria,
|
|
'attribut' => $request->attribut,
|
|
'bobot' => $request->bobot
|
|
]);
|
|
|
|
if ($totalBobot == 1) {
|
|
return redirect()->route('kriteria.index')
|
|
->with('success', 'Kriteria berhasil diperbarui. Total bobot sekarang adalah 1.');
|
|
} elseif ($totalBobot > 1) {
|
|
return redirect()->route('kriteria.index')
|
|
->with('warning', 'Kriteria berhasil diperbarui. Total bobot sekarang adalah ' . $formattedTotal . ' (melebihi 1).');
|
|
} else {
|
|
return redirect()->route('kriteria.index')
|
|
->with('warning', 'Kriteria berhasil diperbarui. Total bobot sekarang adalah ' . $formattedTotal . ' (kurang dari 1).');
|
|
}
|
|
}
|
|
|
|
|
|
public function destroy($id)
|
|
{
|
|
try {
|
|
$kriteria = Kriteria::findOrFail($id);
|
|
$kriteria->delete();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Kriteria berhasil dihapus'
|
|
]);
|
|
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Gagal menghapus kriteria: ' . $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
public function downloadPDF()
|
|
{
|
|
setlocale(LC_ALL, 'IND');
|
|
$tanggal = Carbon::now()->formatLocalized('%A, %d %B %Y');
|
|
$kriteria = Kriteria::all();
|
|
|
|
$pdf = PDF::loadView('admin.kriteria.kriteria-pdf', compact('kriteria', 'tanggal'));
|
|
$pdf->setPaper('A3', 'portrait');
|
|
return $pdf->stream('kriteria.pdf');
|
|
}
|
|
|
|
} |