filter profil petani
This commit is contained in:
@@ -12,6 +12,8 @@ use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
use App\Models\Wilayah\Provinsi;
|
||||
use App\Models\Wilayah\KabupatenKota;
|
||||
use App\Models\Wilayah\Kecamatan;
|
||||
use App\Models\Pertanian\Petani;
|
||||
use App\Models\Wilayah\DesaKelurahan;
|
||||
use App\Models\Pertanian\Komoditas;
|
||||
@@ -35,6 +37,20 @@ class PetaniController extends Controller
|
||||
}
|
||||
|
||||
$provinsi = Provinsi::select('id', 'kode', 'nama')->orderBy('kode', 'asc')->get();
|
||||
$userRole = auth()->user()->role->value;
|
||||
$showFasilitatorColumn = in_array($userRole, ['super_admin', 'admin'], true);
|
||||
$defaultKabupaten = KabupatenKota::select('id', 'nama')
|
||||
->whereRaw('lower(nama) like ?', ['%ngada%'])
|
||||
->orderBy('nama')
|
||||
->first();
|
||||
$selectedKabupatenId = $request->query('kabupaten_kota_id', $defaultKabupaten?->id);
|
||||
$filters = [
|
||||
'search' => $request->query('search'),
|
||||
'komoditas_id' => $request->query('komoditas_id'),
|
||||
'kabupaten_kota_id' => $selectedKabupatenId,
|
||||
'kecamatan_id' => $request->query('kecamatan_id'),
|
||||
'desa_kelurahan_id' => $request->query('desa_kelurahan_id'),
|
||||
];
|
||||
|
||||
$query = Petani::with([
|
||||
'desaKelurahan:id,nama,kecamatan_id',
|
||||
@@ -43,6 +59,7 @@ class PetaniController extends Controller
|
||||
'desaKelurahan.kecamatan.kabupatenKota.provinsi:id,nama',
|
||||
'komoditas:id,nama',
|
||||
'skor:id,petani_id,skor_kesiapan_adopsi',
|
||||
'creator:id,name,role',
|
||||
])
|
||||
->orderBy('created_at', 'desc');
|
||||
|
||||
@@ -53,7 +70,27 @@ class PetaniController extends Controller
|
||||
});
|
||||
}
|
||||
|
||||
if (auth()->user()->role->value === 'fasilitator') {
|
||||
if ($request->filled('komoditas_id')) {
|
||||
$query->where('komoditas_id', $request->komoditas_id);
|
||||
}
|
||||
|
||||
if ($filters['kabupaten_kota_id']) {
|
||||
$query->whereHas('desaKelurahan.kecamatan', function ($query) use ($filters) {
|
||||
$query->where('kabupaten_kota_id', $filters['kabupaten_kota_id']);
|
||||
});
|
||||
}
|
||||
|
||||
if ($request->filled('kecamatan_id')) {
|
||||
$query->whereHas('desaKelurahan', function ($query) use ($request) {
|
||||
$query->where('kecamatan_id', $request->kecamatan_id);
|
||||
});
|
||||
}
|
||||
|
||||
if ($request->filled('desa_kelurahan_id')) {
|
||||
$query->where('desa_kelurahan_id', $request->desa_kelurahan_id);
|
||||
}
|
||||
|
||||
if ($userRole === 'fasilitator') {
|
||||
$query->where('created_by', auth()->user()->id);
|
||||
}
|
||||
|
||||
@@ -61,8 +98,23 @@ class PetaniController extends Controller
|
||||
$petani = $query->paginate($request->per_page ?? 10);
|
||||
|
||||
$komoditas = Komoditas::select('id', 'nama')->where('is_utama', true)->get();
|
||||
$komoditasOptions = Komoditas::select('id', 'nama')->orderBy('nama')->get();
|
||||
$kabupatenOptions = KabupatenKota::select('id', 'nama')->orderBy('nama')->get();
|
||||
$selectedKabupaten = $filters['kabupaten_kota_id']
|
||||
? KabupatenKota::select('id', 'nama')->find($filters['kabupaten_kota_id'])
|
||||
: null;
|
||||
$kecamatanOptions = $filters['kabupaten_kota_id']
|
||||
? Kecamatan::select('id', 'nama')->where('kabupaten_kota_id', $filters['kabupaten_kota_id'])->orderBy('nama')->get()
|
||||
: collect();
|
||||
$desaOptions = $request->filled('kecamatan_id')
|
||||
? DesaKelurahan::select('id', 'nama')->where('kecamatan_id', $request->kecamatan_id)->orderBy('nama')->get()
|
||||
: collect();
|
||||
$lahanQuery = Lahan::whereIn('petani_id', $summaryPetaniIds)
|
||||
->whereIn('komoditas_id', $komoditas->pluck('id'));
|
||||
->when(
|
||||
$request->filled('komoditas_id'),
|
||||
fn ($query) => $query->where('komoditas_id', $request->komoditas_id),
|
||||
fn ($query) => $query->whereIn('komoditas_id', $komoditas->pluck('id'))
|
||||
);
|
||||
$lahanIds = (clone $lahanQuery)->pluck('id');
|
||||
$totalLuasLahan = (clone $lahanQuery)->sum('luas_lahan');
|
||||
$totalTanaman = Tanaman::whereIn('lahan_id', $lahanIds)->count();
|
||||
@@ -81,7 +133,21 @@ class PetaniController extends Controller
|
||||
->groupByRaw($case)
|
||||
->get();
|
||||
|
||||
return view('produktivitas.petani.index', compact('petani', 'provinsi', 'komoditas', 'skor', 'totalLuasLahan', 'totalTanaman'));
|
||||
return view('produktivitas.petani.index', compact(
|
||||
'petani',
|
||||
'provinsi',
|
||||
'komoditas',
|
||||
'komoditasOptions',
|
||||
'kabupatenOptions',
|
||||
'selectedKabupaten',
|
||||
'kecamatanOptions',
|
||||
'desaOptions',
|
||||
'filters',
|
||||
'skor',
|
||||
'totalLuasLahan',
|
||||
'totalTanaman',
|
||||
'showFasilitatorColumn'
|
||||
));
|
||||
}
|
||||
|
||||
public function create()
|
||||
|
||||
Reference in New Issue
Block a user