MIF_E31221322/app/Http/Controllers/AssesmentFormController.php

116 lines
3.8 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Evaluation;
use App\Models\EvaluationDetail;
use App\Models\Indicator;
use App\Models\Land;
use App\Models\RuleExpert;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
class AssesmentFormController extends Controller
{
public function index()
{
$lands = Land::select('id', 'owner')->whereNotIn('id', function ($query) {
$query->select('land_id')->from('evaluation');
})->orderBy('created_at', 'desc')->get();
$indicators = Indicator::select('id', 'name')->whereHas('rules')->with('rules')->get();
return view('assessment.form', compact('lands', 'indicators'));
}
public function calculateCFc($cf1, $cf2)
{
if ($cf1 >= 0 && $cf2 >= 0) {
return $cf1 + $cf2 * (1 - $cf1);
} elseif ($cf1 < 0 && $cf2 < 0) {
return $cf1 + $cf2 * (1 + $cf1);
} else {
return ($cf1 + $cf2) / (1 - min(abs($cf1), abs($cf2)));
}
}
public function store(Request $request)
{
dd($request->all());
$customMessages = [
'land.required' => 'Harap pilih lahan',
'land.exists' => 'Lahan tidak ditemukan',
];
$validator = Validator::make($request->all(), [
'land' => ['required', 'exists:land,id'],
], $customMessages);
if ($validator->fails()) {
toast($validator->messages()->all()[0], 'error')->position('top-right')->autoclose(3000);
return redirect()->back()->withInput();
}
$cf_e = $request->except('_token', 'land');
if (count($cf_e) == 0) {
toast('Harap pilih jawaban untuk nilai indikator', 'error')->position('top-right')->autoclose(3000);
return redirect()->back();
}
$cf_h = RuleExpert::get()->keyBy('indicator_id')->pluck('cf', 'indicator_id')->toArray();
$cf_he = collect($cf_e)->map(function ($value, $key) use ($cf_h) {
return [
"cf(h,e)" . $key => (float) $value * (float) ($cf_h[$key] ?? 1)
];
})->collapse()->toArray();
$cf_combined = array_shift($cf_he);
foreach ($cf_he as $cf) {
$cf_combined = $this->calculateCFc($cf_combined, $cf);
}
$presentase = round($cf_combined * 100, 2);
if ($presentase < 0) {
$presentase = "minus";
}
if ($presentase >= 80 && $presentase < 99) {
$hasil = 'cocok';
} elseif ($presentase >= 62 && $presentase < 80) {
$hasil = 'cocok bersyarat';
} elseif ($presentase >= 42 && $presentase < 62) {
$hasil = 'tidak cocok';
}
$landName = Land::find($request->land)->owner;
DB::beginTransaction();
try {
$evaluation = new Evaluation;
$evaluation->land_id = $request->land;
$evaluation->threshold_value = $hasil;
$evaluation->cf_value = $cf_combined;
$evaluation->user_id = auth()->user()->id;
$evaluation->save();
foreach ($cf_e as $id => $value) {
$evaluationDetail = new EvaluationDetail;
$evaluationDetail->evaluation_id = $evaluation->id;
$evaluationDetail->indicator_id = $id;
$evaluationDetail->cf_e = $value;
$evaluationDetail->save();
}
DB::commit();
toast("Data berhasil disimpan.", "success")->position('top-right')->autoclose(3000);
return redirect()->back()->with(['land' => $landName, 'presentase' => $presentase, 'hasil' => $hasil]);
} catch (\Throwable $th) {
DB::rollBack();
toast($th->getMessage(), 'error')->position('top-right')->autoclose(3000);
return redirect()->back();
}
}
}