peta sebaran dan praktek budaya
This commit is contained in:
@@ -262,6 +262,116 @@ class SummaryController extends Controller
|
||||
],
|
||||
'levels' => $levels,
|
||||
'cultivationLevels' => $this->productionCultivationMapLevelData($request, $levels),
|
||||
'information' => $this->productionMapInformationData($request),
|
||||
];
|
||||
}
|
||||
|
||||
private function productionMapInformationData(Request $request): array
|
||||
{
|
||||
$landMetrics = $this->karakteristikLandMetrics($request);
|
||||
$filteredPetaniIds = $this->filteredPetaniIdsQuery($request);
|
||||
$commodityRows = DB::table('petani')
|
||||
->whereNull('petani.deleted_at')
|
||||
->whereIn('petani.id', clone $filteredPetaniIds)
|
||||
->leftJoin('master_komoditas', 'master_komoditas.id', '=', 'petani.komoditas_id')
|
||||
->selectRaw("COALESCE(master_komoditas.nama, 'Belum ditentukan') as nama")
|
||||
->selectRaw('COUNT(DISTINCT petani.id) as jumlah')
|
||||
->groupBy('master_komoditas.nama')
|
||||
->orderByDesc('jumlah')
|
||||
->get()
|
||||
->map(fn ($row) => ['nama' => $row->nama, 'jumlah' => (int) $row->jumlah])
|
||||
->all();
|
||||
|
||||
$lahan = (clone $this->filteredLahanQuery($request))
|
||||
->leftJoin('profiles', 'profiles.id', '=', 'map_lahan.profile_id')
|
||||
->leftJoin('petani', 'petani.id', '=', 'map_lahan.petani_id')
|
||||
->tap(fn ($query) => $this->whereActiveMapOwner($query))
|
||||
->get([
|
||||
'map_lahan.id',
|
||||
'map_lahan.serangan_hama',
|
||||
'map_lahan.penyakit_tanaman',
|
||||
'map_lahan.jenis_hama',
|
||||
'map_lahan.jenis_penyakit',
|
||||
]);
|
||||
$tanamanByLahan = DB::table('map_tanaman')
|
||||
->whereNull('map_tanaman.deleted_at')
|
||||
->whereIn('map_tanaman.lahan_id', $lahan->pluck('id'))
|
||||
->get([
|
||||
'map_tanaman.lahan_id',
|
||||
'map_tanaman.serangan_hama',
|
||||
'map_tanaman.penyakit_tanaman',
|
||||
'map_tanaman.jenis_hama',
|
||||
'map_tanaman.jenis_penyakit',
|
||||
])
|
||||
->groupBy('lahan_id');
|
||||
$optCounts = [];
|
||||
|
||||
foreach ($lahan as $land) {
|
||||
foreach ($this->productionOptDetails($land, $tanamanByLahan->get($land->id, collect())) as $opt) {
|
||||
$optCounts[$opt] = ($optCounts[$opt] ?? 0) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
arsort($optCounts);
|
||||
|
||||
$mainCommodityIds = DB::table('master_komoditas')
|
||||
->where('is_utama', true)
|
||||
->whereNull('deleted_at')
|
||||
->select('id');
|
||||
$scoreQuery = DB::table('petani')
|
||||
->leftJoin('petani_skor', function ($join) {
|
||||
$join->on('petani_skor.petani_id', '=', 'petani.id')
|
||||
->whereNull('petani_skor.deleted_at');
|
||||
})
|
||||
->whereNull('petani.deleted_at')
|
||||
->whereIn('petani.id', clone $filteredPetaniIds)
|
||||
->whereIn('petani.komoditas_id', $mainCommodityIds);
|
||||
$averageIndex = (clone $scoreQuery)->avg('petani_skor.skor_kesiapan_adopsi');
|
||||
$segmentCase = "CASE
|
||||
WHEN petani_skor.skor_kesiapan_adopsi >= 4 THEN 'Penggerak'
|
||||
WHEN petani_skor.skor_kesiapan_adopsi >= 3 THEN 'Perintis'
|
||||
WHEN petani_skor.skor_kesiapan_adopsi >= 2 THEN 'Pemerhati'
|
||||
WHEN petani_skor.skor_kesiapan_adopsi >= 1 THEN 'Pemula'
|
||||
ELSE 'Belum Lengkap'
|
||||
END";
|
||||
$segmentCounts = (clone $scoreQuery)
|
||||
->selectRaw("{$segmentCase} as segmen")
|
||||
->selectRaw('COUNT(DISTINCT petani.id) as jumlah')
|
||||
->groupByRaw($segmentCase)
|
||||
->get();
|
||||
$segmentPriority = [
|
||||
'Penggerak' => 0,
|
||||
'Perintis' => 1,
|
||||
'Pemerhati' => 2,
|
||||
'Pemula' => 3,
|
||||
'Belum Lengkap' => 4,
|
||||
];
|
||||
$dominantSegment = $segmentCounts
|
||||
->sort(function ($left, $right) use ($segmentPriority) {
|
||||
return ((int) $right->jumlah <=> (int) $left->jumlah)
|
||||
?: (($segmentPriority[$left->segmen] ?? 99) <=> ($segmentPriority[$right->segmen] ?? 99));
|
||||
})
|
||||
->first();
|
||||
$averageSegment = match (true) {
|
||||
$averageIndex === null => 'Belum Lengkap',
|
||||
$averageIndex >= 4 => 'Penggerak',
|
||||
$averageIndex >= 3 => 'Perintis',
|
||||
$averageIndex >= 2 => 'Pemerhati',
|
||||
$averageIndex >= 1 => 'Pemula',
|
||||
default => 'Belum Lengkap',
|
||||
};
|
||||
|
||||
return [
|
||||
'jumlah_petani' => $landMetrics['total_petani'],
|
||||
'total_luas_lahan' => $landMetrics['total_luas_lahan'],
|
||||
'petani_per_komoditas' => $commodityRows,
|
||||
'opt' => collect($optCounts)
|
||||
->map(fn ($jumlah, $nama) => ['nama' => $nama, 'jumlah' => $jumlah])
|
||||
->values()
|
||||
->all(),
|
||||
'indeks' => $averageIndex === null ? null : round((float) $averageIndex, 2),
|
||||
'segmen' => $averageSegment,
|
||||
'segmen_dominan' => $dominantSegment->segmen ?? 'Belum Lengkap',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user