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)];