update dashboard produksi

This commit is contained in:
2026-10-16 10:04:56 +07:00
parent 4370d3c3d0
commit 60d32a88e9
4 changed files with 318 additions and 389 deletions
@@ -0,0 +1,117 @@
<?php
use App\Enums\AksesPasar\PraktikFermentasi;
use App\Http\Controllers\Web\Dashboard\SummaryController;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
beforeEach(function () {
Schema::create('master_komoditas', function (Blueprint $table) {
$table->id();
$table->string('nama');
});
foreach ([
'master_kabupaten_kota' => 'provinsi_id',
'master_kecamatan' => 'kabupaten_kota_id',
'master_desa_kelurahan' => 'kecamatan_id',
] as $name => $parent) {
Schema::create($name, function (Blueprint $table) use ($parent) {
$table->id();
$table->integer($parent);
});
}
Schema::create('petani', function (Blueprint $table) {
$table->id();
$table->integer('komoditas_id')->nullable();
$table->integer('desa_kelurahan_id');
$table->softDeletes();
});
Schema::create('akses_pasar', function (Blueprint $table) {
$table->id();
$table->integer('petani_id');
$table->string('praktik_fermentasi')->nullable();
$table->softDeletes();
});
Schema::create('petani_skor', function (Blueprint $table) {
$table->id();
$table->integer('petani_id');
$table->float('skor_kesiapan_adopsi');
$table->softDeletes();
});
DB::table('master_komoditas')->insert([
['id' => 1, 'nama' => 'Kakao'], ['id' => 2, 'nama' => 'Kopi'], ['id' => 3, 'nama' => 'Vanili'],
]);
DB::table('master_kabupaten_kota')->insert(['id' => 1, 'provinsi_id' => 1]);
DB::table('master_kecamatan')->insert(['id' => 1, 'kabupaten_kota_id' => 1]);
DB::table('master_desa_kelurahan')->insert([
['id' => 1, 'kecamatan_id' => 1], ['id' => 2, 'kecamatan_id' => 1],
]);
foreach ([
[1, 1, 1, PraktikFermentasi::RUTIN_SENDIRI->value],
[2, 1, 1, PraktikFermentasi::RUTIN_KELOMPOK->value],
[3, 2, 2, PraktikFermentasi::PERNAH_SENDIRI->value],
[4, 2, 2, PraktikFermentasi::PERNAH_KELOMPOK->value],
[5, 3, 2, PraktikFermentasi::BELUM_PERNAH->value],
[6, 3, 2, null],
[7, 1, 1, ''],
[8, 2, 2, PraktikFermentasi::RUTIN_SENDIRI->value],
[9, null, 2, PraktikFermentasi::BELUM_PERNAH->value],
[10, 1, 1, PraktikFermentasi::RUTIN_SENDIRI->value],
[11, 1, 1, 'Tidak dikenal'],
] as [$id, $commodity, $village, $practice]) {
DB::table('petani')->insert([
'id' => $id, 'komoditas_id' => $commodity, 'desa_kelurahan_id' => $village,
'deleted_at' => $id === 10 ? '2026-01-01' : null,
]);
DB::table('akses_pasar')->insert([
'petani_id' => $id, 'praktik_fermentasi' => $practice,
'deleted_at' => $id === 8 ? '2026-01-01' : null,
]);
}
DB::table('petani_skor')->insert([
['petani_id' => 1, 'skor_kesiapan_adopsi' => 4.1, 'deleted_at' => null],
['petani_id' => 2, 'skor_kesiapan_adopsi' => 2.6, 'deleted_at' => null],
['petani_id' => 3, 'skor_kesiapan_adopsi' => 4.4, 'deleted_at' => '2026-01-01'],
]);
$controller = app(SummaryController::class);
$query = new ReflectionMethod($controller, 'filteredAksesPasar');
$chart = new ReflectionMethod($controller, 'fermentationPracticeByCommodityChart');
$this->fermentationChart = fn (array $filters = []) => $chart->invoke(
$controller, $query->invoke($controller, new Request($filters)),
);
});
test('fermentation groups individual and collective responses by commodity', function () {
expect(($this->fermentationChart)())->toBe([
'labels' => ['Belum ditentukan', 'Kakao', 'Kopi', 'Vanili'],
'series' => [
['name' => 'Ya, rutin', 'data' => [0, 2, 0, 0]],
['name' => 'Pernah mencoba', 'data' => [0, 0, 2, 0]],
['name' => 'Belum pernah', 'data' => [1, 0, 0, 1]],
],
]);
});
test('fermentation respects region and commodity filters', function () {
$chart = ($this->fermentationChart)(['desaKelurahan_id' => 2, 'komoditas_id' => 2]);
expect($chart['labels'])->toBe(['Kopi']);
expect(array_column($chart['series'], 'data'))->toBe([[0], [2], [0]]);
expect(($this->fermentationChart)(['desaKelurahan_id' => 1])['labels'])->toBe(['Kakao']);
});
test('fermentation respects segment filters and excludes deleted scores', function () {
$chart = ($this->fermentationChart)(['segmen' => 'Pemerhati']);
expect($chart['labels'])->toBe(['Kakao']);
expect(array_column($chart['series'], 'data'))->toBe([[1], [0], [0]]);
expect(($this->fermentationChart)(['segmen' => 'Penggerak'])['labels'])->toBe(['Kakao']);
});
test('fermentation returns empty grouped series when no matching responses exist', function () {
$chart = ($this->fermentationChart)(['provinsi_id' => 99]);
expect($chart['labels'])->toBe([]);
expect(array_column($chart['series'], 'data'))->toBe([[], [], []]);
});
+129
View File
@@ -0,0 +1,129 @@
<?php
use App\Enums\PraktikBudidaya\FrekuensiPemangkasanUtama;
use App\Enums\PraktikBudidaya\PemberianPupuk;
use App\Http\Controllers\Web\Dashboard\SummaryController;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
beforeEach(function () {
foreach ([
'master_provinsi' => null,
'master_kabupaten_kota' => 'provinsi_id',
'master_kecamatan' => 'kabupaten_kota_id',
'master_desa_kelurahan' => 'kecamatan_id',
] as $name => $parent) {
Schema::create($name, function (Blueprint $table) use ($parent) {
$table->id();
$table->string('nama');
$table->text('path')->nullable();
if ($parent) {
$table->integer($parent);
}
$table->softDeletes();
});
DB::table($name)->insert(['id' => 1, 'nama' => 'Wilayah'] + ($parent ? [$parent => 1] : []));
}
Schema::create('master_komoditas', function (Blueprint $table) {
$table->id();
$table->string('nama');
});
foreach (['petani', 'profiles'] as $name) {
Schema::create($name, function (Blueprint $table) {
$table->id();
$table->string('nama');
$table->softDeletes();
});
}
foreach (['map_lahan', 'map_tanaman'] as $name) {
Schema::create($name, function (Blueprint $table) use ($name) {
$table->id();
$table->integer('komoditas_id');
$table->integer('umur_tanaman')->nullable();
foreach (['serangan_hama', 'penyakit_tanaman', 'jenis_hama', 'jenis_penyakit'] as $column) {
$table->text($column)->nullable();
}
if ($name === 'map_lahan') {
$table->integer('petani_id')->nullable();
$table->integer('profile_id')->nullable();
$table->integer('desa_kelurahan_id');
$table->text('path');
$table->float('luas_lahan');
$table->integer('jumlah_tanaman');
} else {
$table->integer('lahan_id');
}
$table->timestamps();
$table->softDeletes();
});
}
Schema::create('praktik_budidaya', function (Blueprint $table) {
$table->id();
$table->integer('petani_id');
$table->integer('frekuensi_pemangkasan_utama');
$table->integer('pemberian_pupuk');
$table->text('kebiasaan_konservasi_tanah');
$table->timestamps();
$table->softDeletes();
});
DB::table('master_komoditas')->insert(['id' => 1, 'nama' => 'Kakao']);
DB::table('petani')->insert(['id' => 1, 'nama' => 'Petani A']);
DB::table('profiles')->insert(['id' => 1, 'nama' => 'Pemilik B']);
$land = [
'komoditas_id' => 1, 'desa_kelurahan_id' => 1, 'luas_lahan' => 1.5,
'jumlah_tanaman' => 50, 'umur_tanaman' => 8,
'path' => '[[[-8.1,121.1],[-8.2,121.1],[-8.1,121.2]]]',
'jenis_hama' => '["Penggerek buah"]',
];
DB::table('map_lahan')->insert($land + ['id' => 1, 'petani_id' => 1]);
DB::table('map_lahan')->insert($land + ['id' => 2, 'profile_id' => 1]);
DB::table('map_lahan')->insert($land + ['id' => 3, 'petani_id' => 1, 'deleted_at' => '2026-01-01']);
DB::table('map_tanaman')->insert([
'lahan_id' => 1, 'komoditas_id' => 1, 'umur_tanaman' => 4,
'jenis_penyakit' => '["Busuk buah"]',
]);
foreach ([
['2026-01-01', 1, null],
['2026-02-01', 4, null],
['2026-03-01', 5, '2026-03-02'],
] as [$updatedAt, $value, $deletedAt]) {
DB::table('praktik_budidaya')->insert([
'petani_id' => 1, 'frekuensi_pemangkasan_utama' => $value, 'pemberian_pupuk' => $value,
'kebiasaan_konservasi_tanah' => '["Menumpuk daun kering/sisa tanaman di sekitar pohon"]',
'updated_at' => $updatedAt, 'deleted_at' => $deletedAt,
]);
}
$controller = app(SummaryController::class);
$method = new ReflectionMethod($controller, 'productionMapLevelData');
$this->mapLevels = fn (array $filters = []) => $method->invoke($controller, new Request($filters));
});
test('land map combines plants and OPT with the latest active cultivation practice', function () {
$lands = collect(($this->mapLevels)()['lahan'])->keyBy('id');
expect($lands->keys()->all())->toBe([1, 2]);
expect($lands[1])->toMatchArray([
'nama_petani' => 'Petani A',
'luas' => 1.5,
'komoditas' => [['nama' => 'Kakao', 'jumlah' => 1, 'umur' => 4.0]],
'opt' => ['Busuk Buah', 'Penggerek Buah'],
'pemangkasan' => FrekuensiPemangkasanUtama::DUA_KALI_TAHUN->label(),
'pemupukan' => PemberianPupuk::JENIS_DOSIS_SESUAI_WAKTU_CUKUP_TEPAT->label(),
'praktik_baik' => ['Menumpuk daun kering/sisa tanaman di sekitar pohon'],
]);
expect($lands[2])->toMatchArray([
'nama_petani' => 'Pemilik B',
'komoditas' => [['nama' => 'Kakao', 'jumlah' => 50, 'umur' => 8.0]],
'pemangkasan' => 'Belum ada data',
'pemupukan' => 'Belum ada data',
'praktik_baik' => [],
]);
});
test('combined map respects region and commodity filters', function () {
expect(($this->mapLevels)(['desaKelurahan_id' => 1, 'komoditas_id' => 1])['lahan'])->toHaveCount(2);
expect(($this->mapLevels)(['desaKelurahan_id' => 99])['lahan'])->toBe([]);
expect(($this->mapLevels)(['komoditas_id' => 99])['lahan'])->toBe([]);
});