147 lines
5.9 KiB
PHP
147 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Web\Dashboard;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\Profile;
|
|
use App\Models\Wilayah\Provinsi;
|
|
use App\Models\wilayah\KabupatenKota;
|
|
use App\Models\Wilayah\Kecamatan;
|
|
use App\Models\Wilayah\DesaKelurahan;
|
|
use App\Models\Pertanian\Lahan;
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
class SummaryController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
if (!Gate::allows('super_admin') && !Gate::allows('admin')) {
|
|
abort(403);
|
|
}
|
|
$title = 'Summary Dashboard';
|
|
return view('dashboard.summary', compact('title'));
|
|
}
|
|
|
|
public function produktivitas(Request $request)
|
|
{
|
|
if (!Gate::allows('super_admin') && !Gate::allows('admin') && !Gate::allows('fasilitator')) {
|
|
abort(403);
|
|
}
|
|
|
|
$title = 'Produktivitas Dashboard';
|
|
$luasTotal = Lahan::whereNotNull('path')->sum('luas_lahan');
|
|
$totalpetani = Profile::where('role', 'petani')->count();
|
|
$totallahan = Lahan::whereNotNull('path')->count();
|
|
|
|
$petaniList = Profile::select('id', 'nama', 'role', 'desa_kelurahan_id')
|
|
->where('role', 'petani')
|
|
->with([
|
|
'lahan:id,profile_id,luas_lahan',
|
|
'desaKelurahan:id,nama,kecamatan_id',
|
|
])
|
|
->paginate(20);
|
|
|
|
$lahanSummaryByDesa = Lahan::whereNotNull('path')
|
|
->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();
|
|
|
|
$geoProvinsi = [];
|
|
$geoKabupaten = [];
|
|
$geoKecamatan = [];
|
|
$geoDesa = [];
|
|
$geoLahan = [];
|
|
|
|
foreach ($lahanSummaryByDesa as $summary) {
|
|
$desa = $desaList->get($summary->desa_kelurahan_id);
|
|
$kec = $desa ? $desa->kecamatan : null;
|
|
$kab = $kec ? $kec->kabupatenKota : null;
|
|
$prov = $kab ? $kab->provinsi : null;
|
|
$totalLahan = (int) $summary->total_lahan;
|
|
$totalLuas = (float) $summary->total_luas;
|
|
|
|
if ($prov && $prov->path) {
|
|
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]['total_lahan'] += $totalLahan;
|
|
$geoProvinsi[$prov->id]['total_luas'] += $totalLuas;
|
|
}
|
|
|
|
if ($kab && $kab->path) {
|
|
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]['total_lahan'] += $totalLahan;
|
|
$geoKabupaten[$kab->id]['total_luas'] += $totalLuas;
|
|
}
|
|
|
|
if ($kec && $kec->path) {
|
|
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]['total_lahan'] += $totalLahan;
|
|
$geoKecamatan[$kec->id]['total_luas'] += $totalLuas;
|
|
}
|
|
|
|
if ($desa && $desa->path) {
|
|
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]['total_lahan'] += $totalLahan;
|
|
$geoDesa[$desa->id]['total_luas'] += $totalLuas;
|
|
}
|
|
}
|
|
|
|
foreach ($mapLahan as $lahan) {
|
|
$geoLahan[] = [
|
|
'id' => $lahan->id,
|
|
'parent_id' => $lahan->desa_kelurahan_id,
|
|
'polygon' => $lahan->path,
|
|
'nama_petani' => $lahan->profile->nama ?? 'N/A',
|
|
'luas' => $lahan->luas_lahan,
|
|
'tanaman' => $lahan->tanaman->map(fn($t) => $t->komoditas->nama ?? null)->filter()->unique()->values()
|
|
];
|
|
}
|
|
|
|
return view('dashboard.produktivitas', [
|
|
'title' => $title,
|
|
'petaniList' => $petaniList,
|
|
'totalpetani' => $totalpetani,
|
|
'totallahan' => $totallahan,
|
|
'luasTotal' => $luasTotal,
|
|
'geoProvinsi' => array_values($geoProvinsi),
|
|
'geoKabupaten' => array_values($geoKabupaten),
|
|
'geoKecamatan' => array_values($geoKecamatan),
|
|
'geoDesa' => array_values($geoDesa),
|
|
'geoLahan' => $geoLahan
|
|
]);
|
|
}
|
|
}
|