This commit is contained in:
2026-07-23 18:26:24 +07:00
parent 712b10e405
commit bed9a6f620
3 changed files with 67 additions and 26 deletions
@@ -34,12 +34,42 @@ class SummaryController extends Controller
$totalpetani = Profile::where('role', 'petani')->count(); $totalpetani = Profile::where('role', 'petani')->count();
$totallahan = Lahan::whereNotNull('path')->count(); $totallahan = Lahan::whereNotNull('path')->count();
$petaniList = Profile::where('role', 'petani') $petaniList = Profile::select('id', 'nama', 'role', 'desa_kelurahan_id')
->with(['lahan.tanaman.komoditas', 'desaKelurahan']) ->where('role', 'petani')
->with([
'lahan:id,profile_id,luas_lahan',
'desaKelurahan:id,nama,kecamatan_id',
])
->paginate(20); ->paginate(20);
$allLahan = Lahan::whereNotNull('path') $lahanSummaryByDesa = Lahan::whereNotNull('path')
->with(['profile', 'tanaman.komoditas', 'desaKelurahan.kecamatan.kabupatenKota.provinsi']) ->whereNotNull('desa_kelurahan_id')
->select('desa_kelurahan_id')
->selectRaw('COUNT(*) as total_lahan')
->selectRaw('COALESCE(SUM(luas_lahan), 0) as total_luas')
->groupBy('desa_kelurahan_id')
->get();
$desaList = DesaKelurahan::select('id', 'nama', 'path', 'kecamatan_id')
->whereIn('id', $lahanSummaryByDesa->pluck('desa_kelurahan_id')->filter()->unique()->values())
->with([
'kecamatan:id,nama,path,kabupaten_kota_id',
'kecamatan.kabupatenKota:id,nama,path,provinsi_id',
'kecamatan.kabupatenKota.provinsi:id,nama,path',
])
->get()
->keyBy('id');
$mapLahanLimit = min(max((int) $request->integer('map_lahan_limit', 500), 0), 2000);
$mapLahan = Lahan::whereNotNull('path')
->select('id', 'profile_id', 'desa_kelurahan_id', 'path', 'luas_lahan')
->with([
'profile:id,nama',
'tanaman:id,lahan_id,komoditas_id',
'tanaman.komoditas:id,nama',
])
->orderByDesc('updated_at')
->limit($mapLahanLimit)
->get(); ->get();
$geoProvinsi = []; $geoProvinsi = [];
@@ -48,47 +78,51 @@ class SummaryController extends Controller
$geoDesa = []; $geoDesa = [];
$geoLahan = []; $geoLahan = [];
foreach ($allLahan as $lahan) { foreach ($lahanSummaryByDesa as $summary) {
$desa = $lahan->desaKelurahan; $desa = $desaList->get($summary->desa_kelurahan_id);
$kec = $desa ? $desa->kecamatan : null; $kec = $desa ? $desa->kecamatan : null;
$kab = $kec ? $kec->kabupatenKota : null; $kab = $kec ? $kec->kabupatenKota : null;
$prov = $kab ? $kab->provinsi : null; $prov = $kab ? $kab->provinsi : null;
$totalLahan = (int) $summary->total_lahan;
$totalLuas = (float) $summary->total_luas;
if ($prov && $prov->path) { if ($prov && $prov->path) {
if (!isset($geoProvinsi[$prov->id])) { if (!isset($geoProvinsi[$prov->id])) {
$geoProvinsi[$prov->id] = ['id' => $prov->id, 'nama' => $prov->nama, 'polygon' => $prov->path, 'total_lahan' => 0, 'total_luas' => 0]; $geoProvinsi[$prov->id] = ['id' => $prov->id, 'nama' => $prov->nama, 'polygon' => $prov->path, 'total_lahan' => 0, 'total_luas' => 0];
} }
$geoProvinsi[$prov->id]['total_lahan']++; $geoProvinsi[$prov->id]['total_lahan'] += $totalLahan;
$geoProvinsi[$prov->id]['total_luas'] += $lahan->luas_lahan; $geoProvinsi[$prov->id]['total_luas'] += $totalLuas;
} }
if ($kab && $kab->path) { if ($kab && $kab->path) {
if (!isset($geoKabupaten[$kab->id])) { if (!isset($geoKabupaten[$kab->id])) {
$geoKabupaten[$kab->id] = ['id' => $kab->id, 'parent_id' => $prov->id ?? null, 'nama' => $kab->nama, 'polygon' => $kab->path, 'total_lahan' => 0, 'total_luas' => 0]; $geoKabupaten[$kab->id] = ['id' => $kab->id, 'parent_id' => $prov->id ?? null, 'nama' => $kab->nama, 'polygon' => $kab->path, 'total_lahan' => 0, 'total_luas' => 0];
} }
$geoKabupaten[$kab->id]['total_lahan']++; $geoKabupaten[$kab->id]['total_lahan'] += $totalLahan;
$geoKabupaten[$kab->id]['total_luas'] += $lahan->luas_lahan; $geoKabupaten[$kab->id]['total_luas'] += $totalLuas;
} }
if ($kec && $kec->path) { if ($kec && $kec->path) {
if (!isset($geoKecamatan[$kec->id])) { if (!isset($geoKecamatan[$kec->id])) {
$geoKecamatan[$kec->id] = ['id' => $kec->id, 'parent_id' => $kab->id ?? null, 'nama' => $kec->nama, 'polygon' => $kec->path, 'total_lahan' => 0, 'total_luas' => 0]; $geoKecamatan[$kec->id] = ['id' => $kec->id, 'parent_id' => $kab->id ?? null, 'nama' => $kec->nama, 'polygon' => $kec->path, 'total_lahan' => 0, 'total_luas' => 0];
} }
$geoKecamatan[$kec->id]['total_lahan']++; $geoKecamatan[$kec->id]['total_lahan'] += $totalLahan;
$geoKecamatan[$kec->id]['total_luas'] += $lahan->luas_lahan; $geoKecamatan[$kec->id]['total_luas'] += $totalLuas;
} }
if ($desa && $desa->path) { if ($desa && $desa->path) {
if (!isset($geoDesa[$desa->id])) { if (!isset($geoDesa[$desa->id])) {
$geoDesa[$desa->id] = ['id' => $desa->id, 'parent_id' => $kec->id ?? null, 'nama' => $desa->nama, 'polygon' => $desa->path, 'total_lahan' => 0, 'total_luas' => 0]; $geoDesa[$desa->id] = ['id' => $desa->id, 'parent_id' => $kec->id ?? null, 'nama' => $desa->nama, 'polygon' => $desa->path, 'total_lahan' => 0, 'total_luas' => 0];
} }
$geoDesa[$desa->id]['total_lahan']++; $geoDesa[$desa->id]['total_lahan'] += $totalLahan;
$geoDesa[$desa->id]['total_luas'] += $lahan->luas_lahan; $geoDesa[$desa->id]['total_luas'] += $totalLuas;
} }
}
foreach ($mapLahan as $lahan) {
$geoLahan[] = [ $geoLahan[] = [
'id' => $lahan->id, 'id' => $lahan->id,
'parent_id' => $desa->id ?? null, 'parent_id' => $lahan->desa_kelurahan_id,
'polygon' => $lahan->path, 'polygon' => $lahan->path,
'nama_petani' => $lahan->profile->nama ?? 'N/A', 'nama_petani' => $lahan->profile->nama ?? 'N/A',
'luas' => $lahan->luas_lahan, 'luas' => $lahan->luas_lahan,
@@ -34,9 +34,16 @@ class PetaniController extends Controller
abort(403); abort(403);
} }
$provinsi = Provinsi::orderBy('kode', 'asc')->get(); $provinsi = Provinsi::select('id', 'kode', 'nama')->orderBy('kode', 'asc')->get();
$query = Petani::with('desakelurahan') $query = Petani::with([
'desaKelurahan:id,nama,kecamatan_id',
'desaKelurahan.kecamatan:id,nama,kabupaten_kota_id',
'desaKelurahan.kecamatan.kabupatenKota:id,nama,provinsi_id',
'desaKelurahan.kecamatan.kabupatenKota.provinsi:id,nama',
'komoditas:id,nama',
'skor:id,petani_id,skor_kesiapan_adopsi',
])
->orderBy('created_at', 'desc'); ->orderBy('created_at', 'desc');
if ($request->search) { if ($request->search) {
@@ -53,12 +60,12 @@ class PetaniController extends Controller
$summaryPetaniIds = (clone $query)->pluck('id'); $summaryPetaniIds = (clone $query)->pluck('id');
$petani = $query->paginate($request->per_page ?? 10); $petani = $query->paginate($request->per_page ?? 10);
$komoditas = Komoditas::where('is_utama', true)->get(); $komoditas = Komoditas::select('id', 'nama')->where('is_utama', true)->get();
$lahan = Lahan::whereIn('petani_id', $summaryPetaniIds) $lahanQuery = Lahan::whereIn('petani_id', $summaryPetaniIds)
->whereIn('komoditas_id', $komoditas->pluck('id')) ->whereIn('komoditas_id', $komoditas->pluck('id'));
->get(); $lahanIds = (clone $lahanQuery)->pluck('id');
$tanaman = Tanaman::whereIn('lahan_id', $lahan->pluck('id')) $totalLuasLahan = (clone $lahanQuery)->sum('luas_lahan');
->get(); $totalTanaman = Tanaman::whereIn('lahan_id', $lahanIds)->count();
$case = " $case = "
CASE CASE
@@ -74,7 +81,7 @@ class PetaniController extends Controller
->groupByRaw($case) ->groupByRaw($case)
->get(); ->get();
return view('produktivitas.petani.index', compact('petani', 'provinsi', 'lahan', 'komoditas', 'tanaman', 'skor')); return view('produktivitas.petani.index', compact('petani', 'provinsi', 'komoditas', 'skor', 'totalLuasLahan', 'totalTanaman'));
} }
public function create() public function create()
@@ -15,8 +15,8 @@
<div class="w-25"> <div class="w-25">
<x-summary-card <x-summary-card
title="Lahan & Tanaman" title="Lahan & Tanaman"
count="{{ number_format(($lahan?->sum('luas_lahan') ?? 0) / 10000, 2, ',', '.') }} ha" count="{{ number_format(($totalLuasLahan ?? 0) / 10000, 2, ',', '.') }} ha"
subtitle="{{ $tanaman?->count() }} tanaman dari {{ $komoditas?->count() }} komoditas utama" subtitle="{{ $totalTanaman ?? 0 }} tanaman dari {{ $komoditas?->count() }} komoditas utama"
icon="trees" icon="trees"
color="success" color="success"
/> />