From b1b98285144bc8be6cbba512533eb66434bc76a3 Mon Sep 17 00:00:00 2001 From: SyaharaniSalsabilla Date: Wed, 22 Jul 2026 16:54:57 +0700 Subject: [PATCH] update jumlah tanaman --- .../Web/Dashboard/SurveyPetaniController.php | 18 ++++++++++++------ .../Web/Produktivitas/PetaniController.php | 6 +++--- .../produktivitas/petani/header.blade.php | 7 ++++++- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/Web/Dashboard/SurveyPetaniController.php b/app/Http/Controllers/Web/Dashboard/SurveyPetaniController.php index 577fb8b..00886c5 100644 --- a/app/Http/Controllers/Web/Dashboard/SurveyPetaniController.php +++ b/app/Http/Controllers/Web/Dashboard/SurveyPetaniController.php @@ -11,6 +11,7 @@ use App\Models\User; use App\Models\Pertanian\Petani; use App\Models\Pertanian\Komoditas; use App\Models\Pertanian\Lahan; +use App\Models\Pertanian\Tanaman; use App\Models\Pertanian\SurveyPelatihan; use App\Models\Pertanian\SurveySertifikasi; @@ -146,21 +147,26 @@ class SurveyPetaniController extends Controller $totalLuasLahan = ((float) Lahan::query() ->sum('luas_lahan')) / 10000; - $totalJumlahTanaman = (int) Lahan::query() - ->sum('jumlah_tanaman'); - // Jumlah tanaman berdasarkan komoditas petani - $tanamanPerKomoditasCollection = DB::table("{$lahanTable} as l") + $tanamanTable = (new Tanaman())->getTable(); + + $tanamanPerKomoditasCollection = DB::table("{$tanamanTable} as t") + ->join("{$lahanTable} as l", 'l.id', '=', 't.lahan_id') ->join("{$petaniTable} as p", 'p.id', '=', 'l.petani_id') - ->leftJoin("{$komoditasTable} as k", 'k.id', '=', 'p.komoditas_id') + ->leftJoin("{$komoditasTable} as k", 'k.id', '=', 't.komoditas_id') ->selectRaw("COALESCE(k.nama, 'Belum ditentukan') as label") - ->selectRaw('COALESCE(SUM(l.jumlah_tanaman), 0) as total') + ->selectRaw('COUNT(t.id) as total') ->where('k.is_utama', true) + ->whereNull('t.deleted_at') + ->whereNull('l.deleted_at') + ->whereNull('p.deleted_at') ->groupBy('k.id', 'k.nama') ->orderByDesc('total') ->get(); + $totalJumlahTanaman = (int) $tanamanPerKomoditasCollection->sum('total'); + $tanamanPerKomoditas = $tanamanPerKomoditasCollection ->map(function ($item, $index) use ($colors, $totalJumlahTanaman) { $color = $colors[$index % count($colors)]; diff --git a/app/Http/Controllers/Web/Produktivitas/PetaniController.php b/app/Http/Controllers/Web/Produktivitas/PetaniController.php index 30c5cd8..04b08a7 100644 --- a/app/Http/Controllers/Web/Produktivitas/PetaniController.php +++ b/app/Http/Controllers/Web/Produktivitas/PetaniController.php @@ -50,14 +50,14 @@ class PetaniController extends Controller $query->where('created_by', auth()->user()->id); } + $summaryPetaniIds = (clone $query)->pluck('id'); $petani = $query->paginate($request->per_page ?? 10); $komoditas = Komoditas::where('is_utama', true)->get(); - $lahan = Lahan::whereIn('petani_id', $petani->pluck('id')) + $lahan = Lahan::whereIn('petani_id', $summaryPetaniIds) ->whereIn('komoditas_id', $komoditas->pluck('id')) ->get(); $tanaman = Tanaman::whereIn('lahan_id', $lahan->pluck('id')) - ->whereIn('lahan_id', $lahan->pluck('id')) ->get(); $case = " @@ -70,7 +70,7 @@ class PetaniController extends Controller END "; $skor = SkorPetani::selectRaw("$case AS segmen, COUNT(*) AS total") - ->whereIn('petani_id', $petani->pluck('id')) + ->whereIn('petani_id', $summaryPetaniIds) ->groupByRaw($case) ->get(); diff --git a/resources/views/produktivitas/petani/header.blade.php b/resources/views/produktivitas/petani/header.blade.php index 6914889..e3ed814 100644 --- a/resources/views/produktivitas/petani/header.blade.php +++ b/resources/views/produktivitas/petani/header.blade.php @@ -93,7 +93,12 @@ : collect(); $totalLuasLahan = $petani ? $lahanPetani->where('komoditas_id', $petani->komoditas_id)->sum('luas_lahan') : 0; $totalLuasLahanHa = $totalLuasLahan / 10000; - $totalTanaman = $petani ? $lahanPetani->where('komoditas_id', $petani->komoditas_id)->sum('jumlah_tanaman') : 0; + $lahanKomoditasUtamaIds = $petani + ? $lahanPetani->where('komoditas_id', $petani->komoditas_id)->pluck('id') + : collect(); + $totalTanaman = $petani && \Illuminate\Support\Facades\Schema::hasTable('map_tanaman') + ? \App\Models\Pertanian\Tanaman::whereIn('lahan_id', $lahanKomoditasUtamaIds)->count() + : 0; $pendidikanLabel = isset($petani) ? $petani->pendidikan_terakhir?->label() : null; @endphp