fix(assessment-form): store function

This commit is contained in:
arieeefajar 2025-06-02 20:36:28 +07:00
parent 37e50bc841
commit 24762fa3aa
6 changed files with 110 additions and 37 deletions

View File

@ -7,6 +7,7 @@
use App\Models\Indicator;
use App\Models\Land;
use App\Models\Rule;
use App\Models\RuleExpert;
use Dotenv\Parser\Value;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
@ -56,44 +57,42 @@ public function store(Request $request)
return redirect()->back();
}
$cf_h = collect(Rule::whereIn('indicator_id', array_keys($cf_e))
->orderByDesc('cf')
->get()
->unique('indicator_id')
->pluck('cf', 'indicator_id')
->sortKeys());
// $cf_h = collect(Rule::whereIn('indicator_id', array_keys($cf_e))
// ->orderByDesc('cf')
// ->get()
// ->unique('indicator_id')
// ->pluck('cf', 'indicator_id')
// ->sortKeys());
$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();
})->collapse()->toArray();
// $cf_he_array = $cf_he->toArray();
$cf_he_array = collect($cf_he)->sortKeys()->values()->toArray();
// $cf_he_array = collect($cf_he)->sortKeys()->values()->toArray();
$cf_combined = array_shift($cf_he_array);
$cf_combined = array_shift($cf_he);
foreach ($cf_he_array as $cf) {
foreach ($cf_he as $cf) {
$cf_combined = $this->calculateCFc($cf_combined, $cf);
}
$cf_pakar = Rule::pluck('cf')->toArray();
$presentase = round($cf_combined * 100, 2);
$mean_cf = collect($cf_pakar)->avg();
$std_dev_cf = sqrt(collect($cf_pakar)->map(function ($cf) use ($mean_cf) {
return pow($cf - $mean_cf, 2);
})->avg());
if ($presentase < 0) {
$presentase = "minus";
}
$threshold_cocok = $mean_cf + $std_dev_cf;
$threshold_cocok_bersyarat = $mean_cf - $std_dev_cf;
if ($cf_combined > $threshold_cocok) {
if ($presentase >= 80 && $presentase <= 98) {
$hasil = 'cocok';
} elseif ($cf_combined > $threshold_cocok_bersyarat) {
} elseif ($presentase >= 62 && $presentase <= 79) {
$hasil = 'cocok bersyarat';
} else {
} elseif ($presentase >= 42 && $presentase <= 61) {
$hasil = 'tidak cocok';
}
@ -116,11 +115,6 @@ public function store(Request $request)
$evaluationDetail->save();
}
DB::commit();
$presentase = round($cf_combined * 100, 2);
if ($presentase < 0) {
$presentase = "minus";
}
toast("Data berhasil disimpan.", "success")->position('top-right')->autoclose(3000);
return redirect()->back()->with(['land' => $landName, 'presentase' => $presentase, 'hasil' => $hasil]);

14
app/Models/RuleExpert.php Normal file
View File

@ -0,0 +1,14 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class RuleExpert extends Model
{
use HasFactory;
protected $guarded = [];
protected $table = 'rule_expert_cf';
}

View File

@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('rule_expert_cf', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('indicator_id');
$table->string('parameter_type');
$table->string('description');
$table->float('cf');
$table->timestamps();
});
Schema::table('rule_expert_cf', function (Blueprint $table) {
$table->foreign('indicator_id')->references('id')->on('indicators')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('rule_expert_cf');
}
};

View File

@ -3,6 +3,7 @@
namespace Database\Seeders;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
@ -25,7 +26,8 @@ public function run(): void
RegenciesSeeder::class,
// LandSeeder::class,
IndicatorSeeder::class,
RuleSeeder::class
RuleSeeder::class,
RuleExpertSeeder::class
]);
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace Database\Seeders;
use App\Models\RuleExpert;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class RuleExpertSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$rules = [
['indicator_id' => 1, 'parameter_type' => "Ideal", 'description' => "Tingkat keasaman pH tanah antara 6.0 - 7.0", 'cf' => 0.2],
['indicator_id' => 2, 'parameter_type' => "Sedang", 'description' => "Lahan berada di ketinggian sedang (800 - 1200 mdpl)", 'cf' => 0.4],
['indicator_id' => 3, 'parameter_type' => "Berlimpah", 'description' => "Irigasi teknis, air selalu tersedia sepanjang tahun", 'cf' => 0.6],
['indicator_id' => 4, 'parameter_type' => "Sedang", 'description' => "Curah hujan antara 1000 - 1500 mm/tahun", 'cf' => 0.6],
['indicator_id' => 5, 'parameter_type' => "Jauh", 'description' => "Jarak antar lahan jauh > 400 meter", 'cf' => 0.8],
];
foreach ($rules as $rule) {
RuleExpert::create($rule);
}
}
}

View File

@ -15,29 +15,29 @@ public function run(): void
{
$rules = [
// pH Tanah
['indicator_id' => 1, 'parameter_type' => "Asam", 'description' => "Tingkat keasaman pH tanah kurang dari 6", 'cf' => -0.6],
['indicator_id' => 1, 'parameter_type' => "Ideal", 'description' => "Tingkat keasaman pH tanah antara 6.0 - 7.0", 'cf' => 0.8],
['indicator_id' => 1, 'parameter_type' => "Basa", 'description' => "Tingkat keasaman pH tanah lebih dari 7.0", 'cf' => -0.8],
['indicator_id' => 1, 'parameter_type' => "Ideal", 'description' => "Tingkat keasaman pH tanah antara 6.0 - 7.0", 'cf' => 1],
['indicator_id' => 1, 'parameter_type' => "Asam", 'description' => "Tingkat keasaman pH tanah kurang dari 6", 'cf' => 0.6],
['indicator_id' => 1, 'parameter_type' => "Basa", 'description' => "Tingkat keasaman pH tanah lebih dari 7.0", 'cf' => 0.2],
// Ketinggian Tempat
['indicator_id' => 2, 'parameter_type' => "Rendah", 'description' => "Lahan berada di dataran rendah (<800 mdpl)", 'cf' => 1],
['indicator_id' => 2, 'parameter_type' => "Sedang", 'description' => "Lahan berada di ketinggian sedang (800 - 1200 mdpl)", 'cf' => 0.6],
['indicator_id' => 2, 'parameter_type' => "Tinggi", 'description' => "Lahan berada di dataran tinggi (1200 - 1800 mdpl)", 'cf' => -0.6],
['indicator_id' => 2, 'parameter_type' => "Tinggi", 'description' => "Lahan berada di dataran tinggi (1200 - 1800 mdpl)", 'cf' => 0.2],
// Ketersediaan Air
['indicator_id' => 3, 'parameter_type' => "Berlimpah", 'description' => "Irigasi teknis, air selalu tersedia sepanjang tahun", 'cf' => 1.0],
['indicator_id' => 3, 'parameter_type' => "Cukup", 'description' => "Sumber air stabil, tetapi tidak selalu tersedia", 'cf' => 0.8],
['indicator_id' => 3, 'parameter_type' => "Terbatas", 'description' => "Kadang-kadang mengalami kekeringan", 'cf' => -0.6],
['indicator_id' => 3, 'parameter_type' => "Cukup", 'description' => "Sumber air stabil, tetapi tidak selalu tersedia", 'cf' => 0.6],
['indicator_id' => 3, 'parameter_type' => "Terbatas", 'description' => "Kadang-kadang mengalami kekeringan", 'cf' => 0.2],
// Curah Hujan
['indicator_id' => 4, 'parameter_type' => "Rendah", 'description' => "Curah hujan kurang dari 1000 mm/tahun", 'cf' => -0.4],
['indicator_id' => 4, 'parameter_type' => "Sedang", 'description' => "Curah hujan antara 1000 - 1500 mm/tahun", 'cf' => 1],
['indicator_id' => 4, 'parameter_type' => "Tinggi", 'description' => "Curah hujan antara 1500 - 2000 mm/tahun", 'cf' => 0.6],
['indicator_id' => 4, 'parameter_type' => "Rendah", 'description' => "Curah hujan kurang dari 1000 mm/tahun", 'cf' => 0.6],
['indicator_id' => 4, 'parameter_type' => "Tinggi", 'description' => "Curah hujan antara 1500 - 2000 mm/tahun", 'cf' => 0.2],
// Isolasi (Jarak Antar Lahan)
['indicator_id' => 5, 'parameter_type' => "Telalu Dekat", 'description' => "Jarak antar lahan sangat dekat < 250 meter", 'cf' => -0.6],
['indicator_id' => 5, 'parameter_type' => "Aman", 'description' => "Jarak antar lahan cukup jauh 250 - 400 meter", 'cf' => 0.8],
['indicator_id' => 5, 'parameter_type' => "Jauh", 'description' => "Jarak antar lahan jauh > 400 meter", 'cf' => 1.0],
['indicator_id' => 5, 'parameter_type' => "Aman", 'description' => "Jarak antar lahan cukup jauh 250 - 400 meter", 'cf' => 0.6],
['indicator_id' => 5, 'parameter_type' => "Telalu Dekat", 'description' => "Jarak antar lahan sangat dekat < 250 meter", 'cf' => 0.2],
];
foreach ($rules as $rule) {