Merge branch 'main' of https://git.probindo.com/Probindo/database-pertani-web
This commit is contained in:
@@ -12,19 +12,24 @@ use App\Models\Map\Panen;
|
||||
|
||||
class PanenController extends Controller
|
||||
{
|
||||
public function index(): JsonResponse
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
Gate::authorize('petani');
|
||||
if (!Gate::any(['petani', 'fasilitator'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$user = Auth::user();
|
||||
$listPanen = Panen::where('farmer_id', $user->profile->id)->get();
|
||||
$profile = $user->profile;
|
||||
|
||||
$profileId = $user->role === 'petani' ? $profile->id : $request->id;
|
||||
$listPanen = Panen::when($profileId, fn($q,$v) => $q->where('farmer_id',$user->id))->get();
|
||||
|
||||
return response()->json($listPanen);
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
Gate::authorize('petani');
|
||||
Gate::authorize(['petani', 'fasilitator']);
|
||||
|
||||
$validated = $request->validate([
|
||||
'land_id' => ['required', 'integer'],
|
||||
@@ -51,14 +56,14 @@ class PanenController extends Controller
|
||||
|
||||
public function show(string $id): JsonResponse
|
||||
{
|
||||
Gate::authorize('petani');
|
||||
Gate::authorize(['petani', 'fasilitator']);
|
||||
$panen = Panen::findOrFail($id);
|
||||
return response()->json($panen);
|
||||
}
|
||||
|
||||
public function update(Request $request, string $id): JsonResponse
|
||||
{
|
||||
Gate::authorize('petani');
|
||||
Gate::authorize(['petani', 'fasilitator']);
|
||||
$panen = Panen::findOrFail($id);
|
||||
|
||||
$validated = $request->validate([
|
||||
@@ -74,7 +79,7 @@ class PanenController extends Controller
|
||||
|
||||
public function destroy(string $id): JsonResponse
|
||||
{
|
||||
Gate::authorize('petani');
|
||||
Gate::authorize(['petani', 'fasilitator']);
|
||||
Panen::findOrFail($id)->delete();
|
||||
return response()->json(null, 204);
|
||||
}
|
||||
@@ -84,7 +89,7 @@ class PanenController extends Controller
|
||||
*/
|
||||
public function batchUpsert(Request $request): JsonResponse
|
||||
{
|
||||
Gate::authorize('petani');
|
||||
Gate::authorize(['petani', 'fasilitator']);
|
||||
$profileId = Auth::user()->profile->id;
|
||||
|
||||
$validated = $request->validate([
|
||||
|
||||
@@ -240,7 +240,7 @@ class ProfileController extends Controller
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'email_verified_at' => ['nullable', 'datetime'],
|
||||
'email_verified_at' => ['nullable', 'date'],
|
||||
]);
|
||||
|
||||
$profile = Profile::findOrFail($id);
|
||||
|
||||
@@ -6,6 +6,7 @@ use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Pertanian\Lahan;
|
||||
use App\Models\Profile;
|
||||
use App\Models\Pertanian\Adat;
|
||||
use App\Models\Wilayah\Provinsi;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
@@ -14,24 +15,73 @@ class LahanController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$query = Lahan::with(['profile', 'desakelurahan'])->orderBy('created_at', 'desc');
|
||||
$query = Lahan::with(['profile', 'desakelurahan.kecamatan.kabupatenkota.provinsi', 'adat'])
|
||||
->orderBy('created_at', 'desc');
|
||||
|
||||
if ($request->search) {
|
||||
$query->where('nama', 'like', '%' . $request->search . '%')
|
||||
->orWhereHas('profile', function($q) use ($request) {
|
||||
$q->where('nama', 'like', '%' . $request->search . '%');
|
||||
$query->where(function($q) use ($request) {
|
||||
$q->where('nama', 'like', '%' . $request->search . '%')
|
||||
->orWhereHas('profile', function($qProfile) use ($request) {
|
||||
$qProfile->where('nama', 'like', '%' . $request->search . '%');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if ($request->filled('profile_id')) {
|
||||
$query->where('profile_id', $request->profile_id);
|
||||
}
|
||||
if ($request->filled('status_kepemilikan')) {
|
||||
$query->where('status_kepemilikan', $request->status_kepemilikan);
|
||||
}
|
||||
if ($request->filled('adat_pemilik_id')) {
|
||||
$query->where('adat_pemilik_id', $request->adat_pemilik_id);
|
||||
}
|
||||
|
||||
if ($request->filled('desa_kelurahan_id')) {
|
||||
$query->where('desa_kelurahan_id', $request->desa_kelurahan_id);
|
||||
} elseif ($request->filled('kecamatan_id')) {
|
||||
$query->whereHas('desakelurahan', function($q) use ($request) {
|
||||
$q->where('kecamatan_id', $request->kecamatan_id);
|
||||
});
|
||||
} elseif ($request->filled('kabupatenkota_id')) {
|
||||
$query->whereHas('desakelurahan.kecamatan', function($q) use ($request) {
|
||||
$q->where('kabupatenkota_id', $request->kabupatenkota_id);
|
||||
});
|
||||
} elseif ($request->filled('provinsi_id')) {
|
||||
$query->whereHas('desakelurahan.kecamatan.kabupatenkota', function($q) use ($request) {
|
||||
$q->where('provinsi_id', $request->provinsi_id);
|
||||
});
|
||||
}
|
||||
|
||||
$lahan = $query->paginate($request->per_page ?? 10);
|
||||
return view('master.lahan.index', compact('lahan'));
|
||||
|
||||
$provinsi = Provinsi::orderBy('nama', 'asc')->get();
|
||||
$profiles = Profile::orderBy('nama', 'asc')->get();
|
||||
$adat = Adat::orderBy('nama', 'asc')->get();
|
||||
|
||||
$kabupaten = [];
|
||||
$kecamatan = [];
|
||||
$desa = [];
|
||||
|
||||
if ($request->filled('provinsi_id')) {
|
||||
$kabupaten = KabupatenKota::where('provinsi_id', $request->provinsi_id)->orderBy('nama', 'asc')->get();
|
||||
}
|
||||
if ($request->filled('kabupatenkota_id')) {
|
||||
$kecamatan = Kecamatan::where('kabupatenkota_id', $request->kabupatenkota_id)->orderBy('nama', 'asc')->get();
|
||||
}
|
||||
if ($request->filled('kecamatan_id')) {
|
||||
$desa = DesaKelurahan::where('kecamatan_id', $request->kecamatan_id)->orderBy('nama', 'asc')->get();
|
||||
}
|
||||
|
||||
return view('produktivitas.lahan.index', compact('lahan', 'provinsi', 'profiles', 'adat', 'kabupaten', 'kecamatan', 'desa'));
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$provinsi = Provinsi::orderBy('nama', 'asc')->get();
|
||||
$profiles = Profile::orderBy('nama', 'asc')->get();
|
||||
return view('master.lahan.add', compact('provinsi', 'profiles'));
|
||||
$adat = Adat::orderBy('nama', 'asc')->get();
|
||||
return view('produktivitas.lahan.add', compact('provinsi', 'profiles', 'adat'));
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
@@ -51,22 +101,29 @@ class LahanController extends Controller
|
||||
return response()->json(['status' => 'error', 'errors' => $validator->errors()], 422);
|
||||
}
|
||||
|
||||
$pathData = null;
|
||||
|
||||
$keluargaPengelolaId = Profile::where('id', $request->profile_id)->value('keluarga_id');
|
||||
|
||||
if ($request->filled('path')) {
|
||||
json_decode($request->path);
|
||||
$pathData = json_decode($request->path, true);
|
||||
|
||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||
return response()->json(['status' => 'error', 'message' => 'Format koordinat (path) bukan JSON array yang valid.'], 422);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Lahan::create([
|
||||
'nama' => $request->nama,
|
||||
'status_kepemilikan' => $request->status_kepemilikan,
|
||||
'profile_id' => $request->profile_id,
|
||||
'desa_kelurahan_id' => $request->desa_kelurahan_id,
|
||||
'luas_lahan' => $request->luas_lahan,
|
||||
'path' => $request->path,
|
||||
'created_by' => auth()->id(),
|
||||
'updated_by' => auth()->id(),
|
||||
'nama' => $request->nama,
|
||||
'status_kepemilikan' => $request->status_kepemilikan,
|
||||
'profile_id' => $request->profile_id,
|
||||
'keluarga_pengelola_id' => $keluargaPengelolaId ?? null,
|
||||
'desa_kelurahan_id' => $request->desa_kelurahan_id,
|
||||
'adat_pemilik_id' => $request->adat_pemilik_id,
|
||||
'luas_lahan' => $request->luas_lahan,
|
||||
'path' => $pathData,
|
||||
'created_by' => auth()->id(),
|
||||
'updated_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
return response()->json(['status' => 'success', 'message' => 'Lahan berhasil ditambahkan.']);
|
||||
@@ -74,9 +131,89 @@ class LahanController extends Controller
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$lahan = Lahan::with(['profile', 'desakelurahan.kecamatan.kabupatenkota.provinsi', 'tanaman.komoditas'])->findOrFail($id);
|
||||
$lahan = Lahan::with(['profile', 'desakelurahan.kecamatan.kabupatenkota.provinsi', 'tanaman.komoditas', 'adat'])->findOrFail($id);
|
||||
|
||||
return view('master.lahan.view', compact('lahan'));
|
||||
return view('produktivitas.lahan.view', compact('lahan'));
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
$lahan = Lahan::with('desakelurahan.kecamatan.kabupatenkota.provinsi')->findOrFail($id);
|
||||
|
||||
$provinsi = Provinsi::orderBy('nama', 'asc')->get();
|
||||
$profiles = Profile::orderBy('nama', 'asc')->get();
|
||||
$adat = Adat::orderBy('nama', 'asc')->get();
|
||||
|
||||
return view('produktivitas.lahan.edit', compact('lahan', 'provinsi', 'profiles', 'adat'));
|
||||
}
|
||||
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'nama' => ['required', 'string', 'max:255'],
|
||||
'status_kepemilikan' => ['required', 'string', 'max:255'],
|
||||
'profile_id' => ['required'],
|
||||
'keluarga_pengelola_id' => ['nullable'],
|
||||
'adat_pemilik_id' => ['nullable'],
|
||||
'desa_kelurahan_id' => ['nullable'],
|
||||
'luas_lahan' => ['nullable', 'numeric'],
|
||||
'path' => ['nullable', 'string'],
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['status' => 'error', 'errors' => $validator->errors()], 422);
|
||||
}
|
||||
|
||||
$lahan = Lahan::findOrFail($id);
|
||||
|
||||
$pathData = null;
|
||||
|
||||
$keluargaPengelolaId = Profile::where('id', $request->profile_id)->value('keluarga_id');
|
||||
|
||||
if ($request->filled('path')) {
|
||||
$pathData = json_decode($request->path, true);
|
||||
|
||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||
return response()->json(['status' => 'error', 'message' => 'Format koordinat (path) bukan JSON array yang valid.'], 422);
|
||||
}
|
||||
}
|
||||
|
||||
$lahan->update([
|
||||
'nama' => $request->nama,
|
||||
'status_kepemilikan' => $request->status_kepemilikan,
|
||||
'profile_id' => $request->profile_id,
|
||||
'keluarga_pengelola_id' => $keluargaPengelolaId ?? null,
|
||||
'desa_kelurahan_id' => $request->desa_kelurahan_id,
|
||||
'adat_pemilik_id' => $request->status_kepemilikan === 'adat' ? $request->adat_pemilik_id : null,
|
||||
'luas_lahan' => $request->luas_lahan,
|
||||
'path' => $pathData,
|
||||
'updated_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
return response()->json(['status' => 'success', 'message' => 'Data Lahan berhasil diperbarui.']);
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
try {
|
||||
$lahan = Lahan::findOrFail($id);
|
||||
|
||||
$lahan->deleted_by = auth()->id(); // atau auth()->user()->id
|
||||
$lahan->save();
|
||||
|
||||
$lahan->delete();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Data lahan berhasil dihapus.'
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Gagal menghapus data Lahan. Silakan coba lagi nanti.'
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user