update jumlah tanaman

This commit is contained in:
2026-07-22 16:54:57 +07:00
parent 7af7e1bdaf
commit b1b9828514
3 changed files with 21 additions and 10 deletions
@@ -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)];
@@ -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();
@@ -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