238 lines
7.8 KiB
PHP
238 lines
7.8 KiB
PHP
<?php
|
|
|
|
namespace Modules\User\Http\Controllers;
|
|
|
|
use Illuminate\Contracts\Support\Renderable;
|
|
use Illuminate\Routing\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use DataTables;
|
|
use GuzzleHttp\Client;
|
|
use Carbon\Carbon;
|
|
|
|
use App\Models\Nilai;
|
|
use App\Models\Kriteria;
|
|
use App\Models\Subkriteria;
|
|
use App\Models\Alternatif;
|
|
|
|
class NilaiController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
|
|
if ($request->ajax()) {
|
|
$data = Nilai::select('*');
|
|
// Convert the Eloquent Collection to a regular PHP array
|
|
$data->each(function ($item, $key) {
|
|
$item->rowIndex = $key + 1;
|
|
});
|
|
|
|
return Datatables::of($data)
|
|
->addIndexColumn()
|
|
->addColumn('nama', function ($row) {
|
|
if ($row->nasabah) {
|
|
return $row->alternatif_kode . ' - ' . $row->nasabah->nama_alternatif;
|
|
} else {
|
|
return 'Nasabah tidak ditemukan';
|
|
}
|
|
})
|
|
->addColumn('kriteria', function ($row) {
|
|
if ($row->kriteria) {
|
|
return $row->kriteria_kode . ' - ' . $row->kriteria->nama_kriteria;
|
|
} else {
|
|
return 'Kriteria tidak ditemukan';
|
|
}
|
|
})
|
|
->rawColumns(['nama', 'kriteria'])
|
|
->filter(function ($query) use ($request) {
|
|
if ($request->has('search')) {
|
|
$search = $request->get('search')['value'];
|
|
if (!empty($search)) {
|
|
$nasabah = Alternatif::where('nama_alternatif', 'LIKE', '%' . $search . '%')->first();
|
|
if ($nasabah) {
|
|
$query->where('alternatif_kode', $nasabah->id_alternatif);
|
|
} else {
|
|
// If no nasabah found, return an empty result
|
|
$query->where('alternatif_kode', '!=', null); // Change this to a condition that always fails
|
|
}
|
|
} else {
|
|
$query->get();
|
|
}
|
|
}
|
|
})
|
|
->make(true);
|
|
}
|
|
|
|
$data = [
|
|
'subtitle' => 'Nilai',
|
|
'button' => true,
|
|
'module' => [
|
|
'url' => route('nilai.create'),
|
|
'name' => 'Tambah Baru'
|
|
]
|
|
];
|
|
|
|
return view('user::nilai.index', compact('data'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$data = [
|
|
'subtitle' => 'Tambah baru',
|
|
];
|
|
|
|
$bobot = Nilai::all();
|
|
$alternatif = Alternatif::all();
|
|
$kriteria = Kriteria::all();
|
|
$subkriteria = Subkriteria::all();
|
|
|
|
return view('user::nilai.add', compact('data', 'bobot', 'alternatif', 'kriteria', 'subkriteria'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'alternatif_kode' => 'required',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return redirect()->back()->withInput()->with('error', 'Unexpected error, please try again. code: ' . $validator->errors()->first());
|
|
}
|
|
|
|
if (empty($request->input('kriteria_kode'))) {
|
|
return redirect()->back()->withInput()->with('error', 'Unexpected error, please try again. code: Kriteria Kode');
|
|
}
|
|
|
|
if (empty($request->input('nilaiFaktor'))) {
|
|
return redirect()->back()->withInput()->with('error', 'Unexpected error, please try again. code: Nilai Faktor');
|
|
}
|
|
|
|
$input = $request->all();
|
|
$countDataPost = count($_POST['kriteria_kode']);
|
|
$data = [];
|
|
|
|
for ($i = 0; $i < $countDataPost; $i++) {
|
|
$data[] = [
|
|
'alternatif_kode' => $_POST['alternatif_kode'],
|
|
'kriteria_kode' => $_POST['kriteria_kode'][$i],
|
|
'nilai_faktor' => $_POST['nilaiFaktor'][$i],
|
|
];
|
|
}
|
|
|
|
$check = Nilai::where('alternatif_kode', '=', $request->input('alternatif_kode'))->first();
|
|
if (!$check) {
|
|
$insert = DB::table('tbl_nilai')->insert($data);
|
|
if ($insert) {
|
|
return redirect()->route('nilai')->with('success', 'You are successfully added records');
|
|
} else {
|
|
return redirect()->back()->withInput()->with('error', 'Unexpected error, please try again');
|
|
}
|
|
} else {
|
|
return redirect()->back()->withInput()->with('error', 'Data sudah ada');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$data = [
|
|
'subtitle' => 'Edit: ' . Nilai::where('id', $id)->first()->title,
|
|
];
|
|
|
|
$nilai = Nilai::find($id);
|
|
$kriteria = Kriteria::all();
|
|
$subkriteria = Subkriteria::all();
|
|
return view('user::nilai.edit', compact('data', 'id', 'nilai', 'kriteria', 'subkriteria'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Request $request)
|
|
{
|
|
// Validasi input sebelum memperbarui data
|
|
$validator = Validator::make($request->all(), [
|
|
'nilaiId' => 'required',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return redirect()->back()->withInput()->with('error', 'Unexpected error, please try again. code: ' . $validator->errors()->first());
|
|
}
|
|
|
|
$nilaiId = $_POST['nilaiId'];
|
|
$altKode = $_POST['altKode'];
|
|
$kriKode = $_POST['kriKode'];
|
|
$nilaiFaktor = $_POST['nilaiFaktor'];
|
|
|
|
// print_r($_POST); die();
|
|
|
|
$updateBanyak = count($nilaiId);
|
|
$jml = 0;
|
|
// $error_data = [];
|
|
for ($x = 0; $x < $updateBanyak; $x++) {
|
|
$query = DB::table('tbl_nilai')->where('id', $nilaiId[$x])->update([
|
|
'alternatif_kode' => $altKode[$x],
|
|
'kriteria_kode' => $kriKode[$x],
|
|
'nilai_faktor' => $nilaiFaktor[$x],
|
|
]);
|
|
if ($query) {
|
|
$jml++;
|
|
} else {
|
|
// $error_data[] = $nilaiId[$x];
|
|
// return redirect()->back()->withInput()->with('error', 'Terjadi error pada menyimpan data');
|
|
}
|
|
}
|
|
|
|
return redirect()->route('nilai')->with('success', 'You are successfully modified records. record updated: '.$jml.' records');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
// Cari data berdasarkan ID
|
|
$post = Nilai::where('alternatif_kode', "$id");
|
|
// Jika data ditemukan
|
|
if ($post) {
|
|
// Hapus data dari database
|
|
$post->delete();
|
|
return redirect()->route('nilai')->with('success', 'You are successfully deleted records');
|
|
} else {
|
|
return redirect()->route('nilai')->with('error', 'Data not found');
|
|
}
|
|
}
|
|
|
|
public function ranking()
|
|
{
|
|
$data = [
|
|
'subtitle' => 'Ranking',
|
|
];
|
|
|
|
return view('user::nilai.ranking', compact('data'));
|
|
}
|
|
}
|