diff --git a/app/Http/Controllers/Web/Dashboard/SummaryController.php b/app/Http/Controllers/Web/Dashboard/SummaryController.php index b3ffa01..7ff615d 100644 --- a/app/Http/Controllers/Web/Dashboard/SummaryController.php +++ b/app/Http/Controllers/Web/Dashboard/SummaryController.php @@ -55,6 +55,48 @@ class SummaryController extends Controller ] + $filterData + $this->productionDashboardData($request)); } + public function mapInformation(Request $request) + { + if (!Gate::allows('super_admin') && !Gate::allows('admin') && !Gate::allows('fasilitator')) { + abort(403); + } + + $validated = $request->validate([ + 'level' => ['nullable', 'in:provinsi,kabupaten,kecamatan,desa'], + 'id' => ['required_with:level', 'integer'], + 'provinsi_id' => ['nullable', 'integer'], + 'kabupatenKota_id' => ['nullable', 'integer'], + 'kecamatan_id' => ['nullable', 'integer'], + 'desaKelurahan_id' => ['nullable', 'integer'], + 'komoditas_id' => ['nullable', 'integer'], + 'segmen' => ['nullable', 'string'], + ]); + + $filters = [ + 'provinsi_id' => null, + 'kabupatenKota_id' => null, + 'kecamatan_id' => null, + 'desaKelurahan_id' => null, + 'komoditas_id' => $validated['komoditas_id'] ?? null, + 'segmen' => $validated['segmen'] ?? null, + ]; + $regionFilter = match ($validated['level'] ?? null) { + 'provinsi' => 'provinsi_id', + 'kabupaten' => 'kabupatenKota_id', + 'kecamatan' => 'kecamatan_id', + 'desa' => 'desaKelurahan_id', + default => null, + }; + + if ($regionFilter) { + $filters[$regionFilter] = $validated['id']; + } + + $scopedRequest = Request::create('/dashboard/produktivitas/map-information', 'GET', $filters); + + return response()->json($this->productionMapInformationData($scopedRequest)); + } + public function pasar(Request $request) { if (!Gate::allows('super_admin') && !Gate::allows('admin') && !Gate::allows('fasilitator')) { @@ -181,6 +223,7 @@ class SummaryController extends Controller $healthSourceName = $tanaman->isNotEmpty() ? 'tanaman' : 'lahan'; $landMetrics = $this->karakteristikLandMetrics($request); + $totalPetani = $landMetrics['total_petani']; $totalLuasLahan = $landMetrics['total_luas_lahan']; $avgLuasPerPetani = $landMetrics['ratarata_luas_lahan']; $actualProductivity = $this->actualProductivityByCommodityChart($request); @@ -190,6 +233,14 @@ class SummaryController extends Controller $aksesPasar = $this->filteredAksesPasar($request); $kpiCards = [ + [ + 'value' => number_format($totalPetani, 0, ',', '.'), + 'unit' => 'orang', + 'label' => 'Jumlah Petani', + 'description' => 'Petani sesuai filter yang dipilih', + 'icon' => 'tabler-users', + 'variant' => 'primary', + ], [ 'value' => number_format($totalLuasLahan, 2, ',', '.'), 'unit' => 'ha', @@ -268,34 +319,34 @@ class SummaryController extends Controller 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') + $filteredMapLahan = (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)); + $mapLahanRows = (clone $filteredMapLahan)->get([ + 'map_lahan.id', + 'map_lahan.petani_id', + 'map_lahan.luas_lahan', + 'map_lahan.serangan_hama', + 'map_lahan.penyakit_tanaman', + 'map_lahan.jenis_hama', + 'map_lahan.jenis_penyakit', + ]); + $petaniIds = $mapLahanRows->pluck('petani_id')->filter()->unique()->values(); + $commodityRows = (clone $filteredMapLahan) + ->leftJoin('master_komoditas as map_komoditas', 'map_komoditas.id', '=', 'map_lahan.komoditas_id') + ->whereNotNull('map_lahan.petani_id') + ->selectRaw("COALESCE(map_komoditas.nama, 'Belum ditentukan') as nama") + ->selectRaw('COUNT(DISTINCT map_lahan.petani_id) as jumlah') + ->groupBy('map_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')) + ->whereIn('map_tanaman.lahan_id', $mapLahanRows->pluck('id')) ->get([ 'map_tanaman.lahan_id', 'map_tanaman.serangan_hama', @@ -306,7 +357,7 @@ class SummaryController extends Controller ->groupBy('lahan_id'); $optCounts = []; - foreach ($lahan as $land) { + foreach ($mapLahanRows as $land) { foreach ($this->productionOptDetails($land, $tanamanByLahan->get($land->id, collect())) as $opt) { $optCounts[$opt] = ($optCounts[$opt] ?? 0) + 1; } @@ -318,14 +369,18 @@ class SummaryController extends Controller ->where('is_utama', true) ->whereNull('deleted_at') ->select('id'); + $scorePetaniIds = DB::table('petani') + ->whereIn('petani.id', $petaniIds) + ->whereIn('petani.komoditas_id', $mainCommodityIds) + ->select('petani.id') + ->pluck('petani.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); + ->whereIn('petani.id', $scorePetaniIds); $averageIndex = (clone $scoreQuery)->avg('petani_skor.skor_kesiapan_adopsi'); $segmentCase = "CASE WHEN petani_skor.skor_kesiapan_adopsi >= 4 THEN 'Penggerak' @@ -362,8 +417,8 @@ class SummaryController extends Controller }; return [ - 'jumlah_petani' => $landMetrics['total_petani'], - 'total_luas_lahan' => $landMetrics['total_luas_lahan'], + 'jumlah_petani' => $petaniIds->count(), + 'total_luas_lahan' => (float) $mapLahanRows->sum('luas_lahan'), 'petani_per_komoditas' => $commodityRows, 'opt' => collect($optCounts) ->map(fn ($jumlah, $nama) => ['nama' => $nama, 'jumlah' => $jumlah]) diff --git a/resources/views/dashboard/produktivitas.blade.php b/resources/views/dashboard/produktivitas.blade.php index d46008e..2833828 100644 --- a/resources/views/dashboard/produktivitas.blade.php +++ b/resources/views/dashboard/produktivitas.blade.php @@ -717,7 +717,7 @@