Merge branch 'main' of https://git.probindo.com/Probindo/database-pertani-web
This commit is contained in:
@@ -4,13 +4,8 @@ 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()
|
||||
@@ -21,128 +16,4 @@ class SummaryController extends Controller
|
||||
$title = 'Summary Dashboard';
|
||||
return view('dashboard.summary', compact('title'));
|
||||
}
|
||||
|
||||
public function produktivitasBackup(Request $request)
|
||||
{
|
||||
if (!Gate::allows('super_admin') && !Gate::allows('admin')) {
|
||||
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::where('role', 'petani')
|
||||
->with(['lahan.tanaman.komoditas', 'desaKelurahan'])
|
||||
->paginate(20);
|
||||
|
||||
$allLahan = Lahan::whereNotNull('path')
|
||||
->with(['profile', 'tanaman.komoditas', 'desaKelurahan.kecamatan.kabupatenKota.provinsi'])
|
||||
->get()
|
||||
->map(function($lahan) {
|
||||
return [
|
||||
'id' => $lahan->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', compact('title', 'petaniList', 'allLahan', 'totalpetani', 'totallahan', 'luasTotal'));
|
||||
}
|
||||
|
||||
public function produktivitas(Request $request)
|
||||
{
|
||||
if (!Gate::allows('super_admin') && !Gate::allows('admin')) {
|
||||
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::where('role', 'petani')
|
||||
->with(['lahan.tanaman.komoditas', 'desaKelurahan'])
|
||||
->paginate(20);
|
||||
|
||||
$allLahan = Lahan::whereNotNull('path')
|
||||
->with(['profile', 'tanaman.komoditas', 'desaKelurahan.kecamatan.kabupatenKota.provinsi'])
|
||||
->get();
|
||||
|
||||
// Penampung data per level (menggunakan ID sebagai key agar unique)
|
||||
$geoProvinsi = [];
|
||||
$geoKabupaten = [];
|
||||
$geoKecamatan = [];
|
||||
$geoDesa = [];
|
||||
$geoLahan = [];
|
||||
|
||||
foreach ($allLahan as $lahan) {
|
||||
$desa = $lahan->desaKelurahan;
|
||||
$kec = $desa ? $desa->kecamatan : null;
|
||||
$kab = $kec ? $kec->kabupatenKota : null;
|
||||
$prov = $kab ? $kab->provinsi : null;
|
||||
|
||||
// --- Akumulasi Provinsi ---
|
||||
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']++;
|
||||
$geoProvinsi[$prov->id]['total_luas'] += $lahan->luas_lahan;
|
||||
}
|
||||
|
||||
// --- Akumulasi Kabupaten ---
|
||||
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']++;
|
||||
$geoKabupaten[$kab->id]['total_luas'] += $lahan->luas_lahan;
|
||||
}
|
||||
|
||||
// --- Akumulasi Kecamatan ---
|
||||
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']++;
|
||||
$geoKecamatan[$kec->id]['total_luas'] += $lahan->luas_lahan;
|
||||
}
|
||||
|
||||
// --- Akumulasi Desa ---
|
||||
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']++;
|
||||
$geoDesa[$desa->id]['total_luas'] += $lahan->luas_lahan;
|
||||
}
|
||||
|
||||
// Data Lahan Tetap
|
||||
$geoLahan[] = [
|
||||
'id' => $lahan->id,
|
||||
'parent_id' => $desa->id ?? null,
|
||||
'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,
|
||||
// Reset key array agar menjadi format list normal untuk JSON
|
||||
'geoProvinsi' => array_values($geoProvinsi),
|
||||
'geoKabupaten' => array_values($geoKabupaten),
|
||||
'geoKecamatan' => array_values($geoKecamatan),
|
||||
'geoDesa' => array_values($geoDesa),
|
||||
'geoLahan' => $geoLahan
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,296 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Web\Ekosistem;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\View\View;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
|
||||
use App\Models\Ekosistem\TabelBasisData;
|
||||
use App\Models\Ekosistem\FormBasisData;
|
||||
use App\Enums\LayoutForm;
|
||||
|
||||
class FormBasisDataController extends Controller
|
||||
{
|
||||
public function index(Request $request, string $tabelId): View
|
||||
{
|
||||
Gate::authorize('super_admin');
|
||||
|
||||
$tabel = TabelBasisData::find($tabelId);
|
||||
if (!$tabel) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Data tabel tidak ditemukan.'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$listEnum = LayoutForm::options();
|
||||
|
||||
$tab = $request->tab ?? 'form';
|
||||
$tabel->state = 'preview';
|
||||
|
||||
return view('ekosistem.form-basis-data.index', compact('tabel', 'listEnum', 'tab'));
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function store(Request $request, string $tabelId): JsonResponse
|
||||
{
|
||||
Gate::authorize('super_admin');
|
||||
|
||||
$tabel = TabelBasisData::find($tabelId);
|
||||
if (!$tabel) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Data tabel tidak ditemukan.'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'parent_id' => ['nullable', 'exists:ekosistem_basis_data_form,id'],
|
||||
'tipe' => ['required', Rule::enum(LayoutForm::class)],
|
||||
'column_width' => [
|
||||
Rule::requiredIf($request->tipe === LayoutForm::COLUMN->value),
|
||||
'nullable', 'integer', 'min:1', 'max:12'],
|
||||
'kolom_id' => [
|
||||
Rule::requiredIf($request->tipe === LayoutForm::VALUE->value),
|
||||
'nullable', 'exists:ekosistem_basis_data_kolom,id'],
|
||||
'section_label' => [
|
||||
Rule::requiredIf($request->tipe === LayoutForm::SECTION->value),
|
||||
'nullable', 'string'],
|
||||
]);
|
||||
|
||||
FormBasisData::create([...$validated,
|
||||
'tabel_id' => $tabelId,
|
||||
'created_by' => auth()->user()->id,
|
||||
'updated_by' => auth()->user()->id
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Form basis data berhasil ditambahkan.'
|
||||
], 201);
|
||||
}
|
||||
|
||||
public function show(string $tabelId, string $id): JsonResponse
|
||||
{
|
||||
Gate::authorize('super_admin');
|
||||
|
||||
$form = FormBasisData::where('tabel_id', $tabelId)->find($id);
|
||||
if (!$form) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Data formulir basis data tidak ditemukan.'
|
||||
], 404);
|
||||
}
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'data' => $form
|
||||
], 200);
|
||||
}
|
||||
|
||||
public function edit(string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function update(Request $request, string $tabelId, string $id)
|
||||
{
|
||||
Gate::authorize('super_admin');
|
||||
|
||||
$form = FormBasisData::where('tabel_id', $tabelId)->find($id);
|
||||
if (!$form) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Data formulir basis data tidak ditemukan.'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'parent_id' => ['nullable', 'exists:ekosistem_basis_data_form,id'],
|
||||
'tipe' => ['required', Rule::enum(LayoutForm::class)],
|
||||
'column_width' => [
|
||||
Rule::requiredIf($request->tipe === LayoutForm::COLUMN->value),
|
||||
'nullable', 'integer', 'min:1', 'max:12'],
|
||||
'kolom_id' => [
|
||||
Rule::requiredIf($request->tipe === LayoutForm::VALUE->value),
|
||||
'nullable', 'exists:ekosistem_basis_data_kolom,id'],
|
||||
'section_label' => [
|
||||
Rule::requiredIf($request->tipe === LayoutForm::SECTION->value),
|
||||
'nullable', 'string'],
|
||||
]);
|
||||
|
||||
$form->update([...$validated,
|
||||
'updated_by' => auth()->user()->id
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Form basis data berhasil diubah.'
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy(string $tabelId, string $id): JsonResponse
|
||||
{
|
||||
Gate::authorize('super_admin');
|
||||
|
||||
$form = FormBasisData::where('tabel_id', $tabelId)->find($id);
|
||||
if (!$form) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Data form basis data tidak ditemukan.'
|
||||
], 404);
|
||||
}
|
||||
if ($form->form->count() > 0) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Tidak dapat menghapus formulir basis data yang memiliki formulir.'
|
||||
], 400);
|
||||
}
|
||||
$form->update([
|
||||
'deleted_by' => auth()->user()->id
|
||||
]);
|
||||
$form->delete();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Form basis data berhasil dihapus.'
|
||||
], 200);
|
||||
}
|
||||
|
||||
public function generate(Request $request, string $tabelId): RedirectResponse
|
||||
{
|
||||
Gate::authorize('super_admin');
|
||||
|
||||
$tabel = TabelBasisData::find($tabelId);
|
||||
if (!$tabel) {
|
||||
return redirect()->route('tabel.formulir.index', $tabelId)->with('error', 'Tabel basis data tidak ditemukan.');
|
||||
}
|
||||
|
||||
DB::transaction(function () use ($tabel) {
|
||||
|
||||
FormBasisData::where('tabel_id', $tabel->id)->delete();
|
||||
|
||||
FormBasisData::create([
|
||||
'tabel_id' => $tabel->id,
|
||||
'tipe' => LayoutForm::SECTION,
|
||||
'section_label' => $tabel->label,
|
||||
'created_by' => auth()->user()->id,
|
||||
'updated_by' => auth()->user()->id
|
||||
]);
|
||||
|
||||
FormBasisData::create([
|
||||
'tabel_id' => $tabel->id,
|
||||
'tipe' => LayoutForm::HR_LINE,
|
||||
'created_by' => auth()->user()->id,
|
||||
'updated_by' => auth()->user()->id
|
||||
]);
|
||||
|
||||
$chunks = $tabel->kolom->chunk(4);
|
||||
|
||||
foreach ($chunks as $chunk) {
|
||||
|
||||
$row = FormBasisData::create([
|
||||
'tabel_id' => $tabel->id,
|
||||
'tipe' => LayoutForm::ROW,
|
||||
'created_by' => auth()->user()->id,
|
||||
'updated_by' => auth()->user()->id
|
||||
]);
|
||||
|
||||
foreach ($chunk as $kolom) {
|
||||
$column = FormBasisData::create([
|
||||
'tabel_id' => $tabel->id,
|
||||
'tipe' => LayoutForm::COLUMN,
|
||||
'column_width' => 3,
|
||||
'parent_id' => $row->id,
|
||||
'created_by' => auth()->user()->id,
|
||||
'updated_by' => auth()->user()->id
|
||||
]);
|
||||
|
||||
FormBasisData::create([
|
||||
'tabel_id' => $tabel->id,
|
||||
'tipe' => LayoutForm::VALUE,
|
||||
'parent_id' => $column->id,
|
||||
'kolom_id' => $kolom->id,
|
||||
'created_by' => auth()->user()->id,
|
||||
'updated_by' => auth()->user()->id
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
return redirect()->route('tabel.formulir.index', $tabelId)->with('success', 'Formulir berhasil dibuat.');
|
||||
}
|
||||
|
||||
public function moveUp(Request $request, string $tabelId, string $id): JsonResponse
|
||||
{
|
||||
Gate::authorize('super_admin');
|
||||
|
||||
$current = FormBasisData::where('tabel_id', $tabelId)->find($id);
|
||||
if (!$current) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Data formulir tidak ditemukan.'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$prev = FormBasisData::where('tabel_id', $tabelId)
|
||||
->where('urutan', '<', $current->urutan)
|
||||
->where('parent_id', $current->parent_id)
|
||||
->orderBy('urutan', 'desc')
|
||||
->first();
|
||||
|
||||
if ($prev) {
|
||||
// swap
|
||||
[$current->urutan, $prev->urutan] = [$prev->urutan, $current->urutan];
|
||||
$current->save();
|
||||
$prev->save();
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Formulir basis data berhasil diperbarui.'
|
||||
], 200);
|
||||
}
|
||||
|
||||
public function moveDown(Request $request, string $tabelId, string $id): JsonResponse
|
||||
{
|
||||
Gate::authorize('super_admin');
|
||||
|
||||
$current = FormBasisData::where('tabel_id', $tabelId)->find($id);
|
||||
if (!$current) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Data formulir tidak ditemukan.'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$next = FormBasisData::where('tabel_id', $tabelId)
|
||||
->where('urutan', '>', $current->urutan)
|
||||
->where('parent_id', $current->parent_id)
|
||||
->orderBy('urutan', 'asc')
|
||||
->first();
|
||||
|
||||
if ($next) {
|
||||
// swap
|
||||
[$current->urutan, $next->urutan] = [$next->urutan, $current->urutan];
|
||||
$current->save();
|
||||
$next->save();
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Formulir basis data berhasil diperbarui.'
|
||||
], 200);
|
||||
}
|
||||
}
|
||||
@@ -145,6 +145,13 @@ class KolomBasisDataController extends Controller
|
||||
'is_browse' => ['nullable', 'boolean'],
|
||||
]);
|
||||
|
||||
if (!empty($validated['enum_opsi'])) {
|
||||
$validated['enum_opsi'] = json_decode($validated['enum_opsi'], true);
|
||||
}
|
||||
if (!empty($validated['validasi'])) {
|
||||
$validated['validasi'] = json_decode($validated['validasi'], true);
|
||||
}
|
||||
|
||||
$kolom->update([...$validated,
|
||||
'updated_by' => auth()->id(),
|
||||
]);
|
||||
@@ -188,7 +195,7 @@ class KolomBasisDataController extends Controller
|
||||
], 404);
|
||||
}
|
||||
|
||||
$prev = KolomBasisData::where('tabel_id', $current->tabel_id)
|
||||
$prev = KolomBasisData::where('tabel_id', $tabelId)
|
||||
->where('urutan', '<', $current->urutan)
|
||||
->orderBy('urutan', 'desc')
|
||||
->first();
|
||||
@@ -218,7 +225,7 @@ class KolomBasisDataController extends Controller
|
||||
], 404);
|
||||
}
|
||||
|
||||
$next = KolomBasisData::where('tabel_id', $current->tabel_id)
|
||||
$next = KolomBasisData::where('tabel_id', $tabelId)
|
||||
->where('urutan', '>', $current->urutan)
|
||||
->orderBy('urutan', 'asc')
|
||||
->first();
|
||||
|
||||
@@ -8,6 +8,13 @@ use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\View\View;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
use App\Models\Ekosistem\TabelBasisData;
|
||||
use App\Models\Ekosistem\KolomBasisData;
|
||||
@@ -17,9 +24,16 @@ use App\Models\Core\Program;
|
||||
|
||||
class ValueBasisDataController extends Controller
|
||||
{
|
||||
private $tableModelClassMap = [
|
||||
'master_provinsi' => \App\Models\Wilayah\Provinsi::class,
|
||||
'master_kabupaten_kota' => \App\Models\Wilayah\KabupatenKota::class,
|
||||
'master_kecamatan' => \App\Models\Wilayah\Kecamatan::class,
|
||||
'master_desa_kelurahan' => \App\Models\Wilayah\DesaKelurahan::class,
|
||||
];
|
||||
|
||||
public function index(Request $request, string $tabelId): View
|
||||
{
|
||||
if (!Gate::any(['super_admin', 'admin'])) {
|
||||
if (!Gate::any(['super_admin', 'admin', 'fasilitator'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
@@ -40,13 +54,15 @@ class ValueBasisDataController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
$tabel = TabelBasisData::find($tabelId);
|
||||
$tabel = TabelBasisData::with('kolom')
|
||||
->whereHas('kolom', function ($query) {
|
||||
$query->where('is_active', true);
|
||||
})->find($tabelId);
|
||||
if (!$tabel) {
|
||||
abort(404, 'Data tabel tidak ditemukan.');
|
||||
}
|
||||
$kolom = KolomBasisData::where('tabel_id', $tabelId)
|
||||
$kolom = KolomBasisData::where('tabel_id', $tabel->id)
|
||||
->where('is_active', true)
|
||||
->where('is_browse', true)
|
||||
->get();
|
||||
if ($kolom->isEmpty()) {
|
||||
abort(404, 'Data kolom tidak ditemukan untuk tabel ini.');
|
||||
@@ -83,6 +99,7 @@ class ValueBasisDataController extends Controller
|
||||
MAX(
|
||||
CASE
|
||||
WHEN kolom_id = {$k->id}
|
||||
AND value_enum IS NOT NULL
|
||||
THEN (
|
||||
SELECT elem->>'label'
|
||||
FROM ekosistem_basis_data_kolom k,
|
||||
@@ -160,8 +177,11 @@ class ValueBasisDataController extends Controller
|
||||
if (!Gate::any(['super_admin', 'admin'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$tabel = TabelBasisData::find($tabelId);
|
||||
|
||||
$tabel = TabelBasisData::with('kolom')
|
||||
->whereHas('kolom', function ($query) {
|
||||
$query->where('is_active', true);
|
||||
})->find($tabelId);
|
||||
if (!$tabel) {
|
||||
abort(404, 'Data tabel tidak ditemukan.');
|
||||
}
|
||||
@@ -189,7 +209,12 @@ class ValueBasisDataController extends Controller
|
||||
if ($k->tipe == 'foreign_key') {
|
||||
$relasi['kolom_id'] = $k->id;
|
||||
try {
|
||||
$options_kolom = DB::table($k->relasi_tabel)->selectRaw($k->relasi_kolom_label . ' as label, ' . $k->relasi_kolom_id . ' as value')->whereNull('deleted_at');
|
||||
$modelClass = Arr::get($this->tableModelClassMap, $k->relasi_tabel);
|
||||
if ($modelClass) {
|
||||
$options_kolom = $modelClass::select($k->relasi_kolom_label . ' as label', $k->relasi_kolom_id . ' as value')->limit(10000);
|
||||
} else {
|
||||
$options_kolom = DB::table($k->relasi_tabel)->selectRaw($k->relasi_kolom_label . ' as label, ' . $k->relasi_kolom_id . ' as value')->whereNull('deleted_at')->limit(10000);
|
||||
}
|
||||
$relasi['options'] = $options_kolom->get();
|
||||
$relasi['sql'] = $options_kolom->toSql();
|
||||
} catch (\Throwable $th) {
|
||||
@@ -199,18 +224,84 @@ class ValueBasisDataController extends Controller
|
||||
}
|
||||
}
|
||||
$list_relasi = collect($list_relasi);
|
||||
// dd($kolom->where('tipe', 'enum')->first()?->enum_opsi);
|
||||
|
||||
|
||||
return view('ekosistem.value-basis-data.add', compact('tabel', 'kolom', 'program', 'list_relasi'));
|
||||
}
|
||||
|
||||
private function prepareValidationRules($kolom)
|
||||
{
|
||||
$rules = [];
|
||||
foreach ($kolom as $k) {
|
||||
if ($k->validasi && is_array($k->validasi)) {
|
||||
$rules[$k->kolom] = $k->validasi;
|
||||
|
||||
if (!in_array('required', $k->validasi) && !in_array('nullable', $k->validasi)) {
|
||||
$rules[$k->kolom][] = 'nullable';
|
||||
}
|
||||
|
||||
if (!in_array('string', $k->validasi) && ($k->tipe == 'varchar' || $k->tipe == 'text')) {
|
||||
$rules[$k->kolom][] = 'string';
|
||||
}
|
||||
if (!in_array('size', $k->validasi) && ($k->tipe == 'varchar')) {
|
||||
$rules[$k->kolom][] = 'size:255';
|
||||
}
|
||||
if (!in_array('integer', $k->validasi) && $k->tipe == 'integer') {
|
||||
$rules[$k->kolom][] = 'integer';
|
||||
}
|
||||
if (!in_array('boolean', $k->validasi) && $k->tipe == 'boolean') {
|
||||
$rules[$k->kolom][] = 'boolean';
|
||||
}
|
||||
if (!in_array('date', $k->validasi) && ($k->tipe == 'date' || $k->tipe == 'datetime')) {
|
||||
$rules[$k->kolom][] = 'date';
|
||||
}
|
||||
if ($k->tipe == 'foreign_key') {
|
||||
$rules[$k->kolom][] = 'exists:' . $k->relasi_tabel . ',' . $k->relasi_kolom_id;
|
||||
}
|
||||
if ($k->tipe == 'enum') {
|
||||
$enumValues = collect($k->enum_opsi)->pluck('value')->toArray();
|
||||
$rules[$k->kolom][] = Rule::in($enumValues);
|
||||
}
|
||||
|
||||
} else {
|
||||
$rules[$k->kolom] = ['nullable'];
|
||||
|
||||
if ($k->tipe == 'varchar' || $k->tipe == 'text') {
|
||||
$rules[$k->kolom][] = 'string';
|
||||
}
|
||||
if ($k->tipe == 'varchar') {
|
||||
$rules[$k->kolom][] = 'size:255';
|
||||
}
|
||||
if ($k->tipe == 'integer') {
|
||||
$rules[$k->kolom][] = 'integer';
|
||||
}
|
||||
if ($k->tipe == 'boolean') {
|
||||
$rules[$k->kolom][] = 'boolean';
|
||||
}
|
||||
if ($k->tipe == 'date' || $k->tipe == 'datetime') {
|
||||
$rules[$k->kolom][] = 'date';
|
||||
}
|
||||
if ($k->tipe == 'foreign_key') {
|
||||
$rules[$k->kolom][] = 'exists:' . $k->relasi_tabel . ',' . $k->relasi_kolom_id;
|
||||
}
|
||||
if ($k->tipe == 'enum') {
|
||||
$enumValues = collect($k->enum_opsi)->pluck('value')->toArray();
|
||||
$rules[$k->kolom][] = Rule::in($enumValues);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $rules;
|
||||
}
|
||||
|
||||
public function store(Request $request, string $tabelId): RedirectResponse
|
||||
{
|
||||
if (!Gate::any(['super_admin', 'admin'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$tabel = TabelBasisData::find($tabelId);
|
||||
$tabel = TabelBasisData::with('kolom')
|
||||
->whereHas('kolom', function ($query) {
|
||||
$query->where('is_active', true);
|
||||
})->find($tabelId);
|
||||
if (!$tabel) {
|
||||
abort(404, 'Data tabel tidak ditemukan.');
|
||||
}
|
||||
@@ -233,15 +324,7 @@ class ValueBasisDataController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
$validationRules = [];
|
||||
foreach ($kolom as $k) {
|
||||
if ($k->validasi && is_array($k->validasi)) {
|
||||
$validationRules[$k->kolom] = $k->validasi;
|
||||
} else {
|
||||
$validationRules[$k->kolom] = ['nullable'];
|
||||
}
|
||||
}
|
||||
$validated = $request->validate($validationRules);
|
||||
$validated = $request->validate($this->prepareValidationRules($kolom));
|
||||
|
||||
DB::transaction(function () use ($program, $tabelId, $validated, $kolom) {
|
||||
$maxId = ValueBasisData::where('tabel_id', $tabelId)
|
||||
@@ -270,16 +353,16 @@ class ValueBasisDataController extends Controller
|
||||
return redirect()->route('tabel.data.index', [$tabelId, 'program_id' => $program->id])->with('success', 'Data berhasil ditambahkan.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(Request $request, string $tabelId, string $id): View
|
||||
{
|
||||
if (!Gate::any(['super_admin', 'admin'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$tabel = TabelBasisData::find($tabelId);
|
||||
$tabel = TabelBasisData::with('kolom')
|
||||
->whereHas('kolom', function ($query) {
|
||||
$query->where('is_active', true);
|
||||
})->find($tabelId);
|
||||
if (!$tabel) {
|
||||
abort(404, 'Data tabel tidak ditemukan.');
|
||||
}
|
||||
@@ -315,7 +398,9 @@ class ValueBasisDataController extends Controller
|
||||
foreach ($values as $v) {
|
||||
$k = $kolom->where('id', $v->kolom_id)->first();
|
||||
if ($k->tipe == 'foreign_key') {
|
||||
$v->value_relasi_label = DB::table($k->relasi_tabel)->where($k->relasi_kolom_id, $v->value_relasi)->value($k->relasi_kolom_label);
|
||||
$v->value_relasi_label = DB::table($k->relasi_tabel)
|
||||
->where($k->relasi_kolom_id, $v->value_relasi)
|
||||
->value($k->relasi_kolom_label);
|
||||
} else if ($k->tipe == 'enum') {
|
||||
$opsi = collect($k->enum_opsi);
|
||||
$found = $opsi->first(function ($item) use ($v) {
|
||||
@@ -336,7 +421,10 @@ class ValueBasisDataController extends Controller
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$tabel = TabelBasisData::find($tabelId);
|
||||
$tabel = TabelBasisData::with('kolom')
|
||||
->whereHas('kolom', function ($query) {
|
||||
$query->where('is_active', true);
|
||||
})->find($tabelId);
|
||||
if (!$tabel) {
|
||||
abort(404, 'Data tabel tidak ditemukan.');
|
||||
}
|
||||
@@ -376,7 +464,12 @@ class ValueBasisDataController extends Controller
|
||||
if ($k->tipe == 'foreign_key') {
|
||||
$relasi['kolom_id'] = $k->id;
|
||||
try {
|
||||
$options_kolom = DB::table($k->relasi_tabel)->selectRaw($k->relasi_kolom_label . ' as label, ' . $k->relasi_kolom_id . ' as value')->whereNull('deleted_at');
|
||||
$modelClass = Arr::get($this->tableModelClassMap, 'master_desa_kelurahan');
|
||||
if ($modelClass) {
|
||||
$options_kolom = $modelClass::select($k->relasi_kolom_label . ' as label', $k->relasi_kolom_id . ' as value')->limit(10000);
|
||||
} else {
|
||||
$options_kolom = DB::table($k->relasi_tabel)->selectRaw($k->relasi_kolom_label . ' as label, ' . $k->relasi_kolom_id . ' as value')->whereNull('deleted_at')->limit(10000);
|
||||
}
|
||||
$relasi['options'] = $options_kolom->get();
|
||||
$relasi['sql'] = $options_kolom->toSql();
|
||||
} catch (\Throwable $th) {
|
||||
@@ -390,16 +483,16 @@ class ValueBasisDataController extends Controller
|
||||
return view('ekosistem.value-basis-data.edit', compact('tabel', 'kolom', 'program', 'values', 'value_id', 'list_relasi'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, string $tabelId, string $id): RedirectResponse
|
||||
{
|
||||
if (!Gate::any(['super_admin', 'admin'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$tabel = TabelBasisData::find($tabelId);
|
||||
$tabel = TabelBasisData::with('kolom')
|
||||
->whereHas('kolom', function ($query) {
|
||||
$query->where('is_active', true);
|
||||
})->find($tabelId);
|
||||
if (!$tabel) {
|
||||
abort(404, 'Data tabel tidak ditemukan.');
|
||||
}
|
||||
@@ -431,15 +524,7 @@ class ValueBasisDataController extends Controller
|
||||
abort(404, 'Data tidak ditemukan.');
|
||||
}
|
||||
|
||||
$validationRules = [];
|
||||
foreach ($kolom as $k) {
|
||||
if ($k->validasi && is_array($k->validasi)) {
|
||||
$validationRules[$k->kolom] = $k->validasi;
|
||||
} else {
|
||||
$validationRules[$k->kolom] = ['nullable'];
|
||||
}
|
||||
}
|
||||
$validated = $request->validate($validationRules);
|
||||
$validated = $request->validate($this->prepareValidationRules($kolom));
|
||||
|
||||
DB::transaction(function () use ($program, $tabelId, $validated, $kolom, $id, $values) {
|
||||
foreach ($validated as $kol => $val) {
|
||||
|
||||
@@ -15,13 +15,13 @@ class AuthController extends Controller
|
||||
public function login()
|
||||
{
|
||||
$title = 'Login';
|
||||
return view('konfigurasi.auth.login', compact('title'));
|
||||
return view('auth.login', compact('title'));
|
||||
}
|
||||
|
||||
public function register()
|
||||
{
|
||||
$title = 'Register';
|
||||
return view('konfigurasi.auth.register', compact('title'));
|
||||
return view('auth.register', compact('title'));
|
||||
}
|
||||
|
||||
public function createAccount(Request $request)
|
||||
@@ -42,7 +42,7 @@ class AuthController extends Controller
|
||||
'deleted_by' => null,
|
||||
]);
|
||||
|
||||
return redirect()->route('konfigurasi.auth.login')->with('success', 'Account created successfully. Please login.');
|
||||
return redirect()->route('auth.login')->with('success', 'Account created successfully. Please login.');
|
||||
}
|
||||
|
||||
public function verify(User $user)
|
||||
|
||||
@@ -10,6 +10,7 @@ use App\Enums\Role;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
@@ -70,6 +71,7 @@ class UserController extends Controller
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'username' => ['required', 'string', 'max:255', 'unique:users,name'],
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
|
||||
'password' => ['required', 'string', 'min:8', 'confirmed'],
|
||||
@@ -90,14 +92,17 @@ class UserController extends Controller
|
||||
|
||||
$user = User::create([
|
||||
'name' => $request->name,
|
||||
'username' => $request->username,
|
||||
'email' => $request->email,
|
||||
'password' => Hash::make($request->password),
|
||||
'role' => $request->role,
|
||||
'uid' => Str::random(10),
|
||||
'created_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
Profile::create([
|
||||
'user_id' => $user->id,
|
||||
'user_uid' => $user->uid,
|
||||
'role' => $request->role,
|
||||
'nama' => $request->name,
|
||||
'email' => $request->email,
|
||||
|
||||
@@ -0,0 +1,228 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Web\Master;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Master\JenisKomoditas;
|
||||
use App\Models\Pertanian\Komoditas;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class JenisKomoditasController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$perPage = $request->get('per_page', 10);
|
||||
$search = $request->get('search');
|
||||
|
||||
$jenisKomoditas = JenisKomoditas::with('komoditas')
|
||||
->when($search, function ($query) use ($search) {
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('nama', 'ILIKE', "%{$search}%")
|
||||
->orWhere('path', 'ILIKE', "%{$search}%")
|
||||
->orWhereHas('komoditas', function ($komoditasQuery) use ($search) {
|
||||
$komoditasQuery->where('nama', 'ILIKE', "%{$search}%");
|
||||
});
|
||||
});
|
||||
})
|
||||
->latest()
|
||||
->paginate($perPage);
|
||||
|
||||
$komoditas = Komoditas::orderBy('nama', 'asc')->get();
|
||||
|
||||
return view('master.jeniskomoditas.index', compact(
|
||||
'jenisKomoditas',
|
||||
'komoditas'
|
||||
));
|
||||
}
|
||||
|
||||
public function getByKomoditas($komoditas_id)
|
||||
{
|
||||
$jenisKomoditas = JenisKomoditas::where('komoditas_id', $komoditas_id)
|
||||
->orderBy('nama', 'asc')
|
||||
->get(['id', 'nama']);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'data' => $jenisKomoditas,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'komoditas_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
'exists:master_komoditas,id',
|
||||
],
|
||||
'nama' => [
|
||||
'required',
|
||||
'string',
|
||||
'max:255',
|
||||
Rule::unique('jenis_komoditas', 'nama')
|
||||
->where('komoditas_id', $request->komoditas_id),
|
||||
],
|
||||
], [
|
||||
'komoditas_id.required' => 'Komoditas wajib dipilih.',
|
||||
'komoditas_id.exists' => 'Komoditas yang dipilih tidak valid.',
|
||||
'nama.required' => 'Nama jenis komoditas wajib diisi.',
|
||||
'nama.unique' => 'Nama jenis komoditas sudah tersedia pada komoditas ini.',
|
||||
]);
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
$jenisKomoditas = JenisKomoditas::create([
|
||||
'komoditas_id' => $validated['komoditas_id'],
|
||||
'nama' => $validated['nama'],
|
||||
'created_by' => auth()->id(),
|
||||
'updated_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
DB::commit();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Data jenis komoditas berhasil disimpan.',
|
||||
'data' => $jenisKomoditas,
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
DB::rollBack();
|
||||
|
||||
Log::error('Gagal menyimpan jenis komoditas', [
|
||||
'error' => $e->getMessage(),
|
||||
'request' => $request->all(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Gagal menyimpan data: ' . $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(string $id)
|
||||
{
|
||||
try {
|
||||
$jenisKomoditas = JenisKomoditas::with('komoditas')->findOrFail($id);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'data' => $jenisKomoditas,
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Data jenis komoditas tidak ditemukan.',
|
||||
], 404);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, string $id)
|
||||
{
|
||||
$jenisKomoditas = JenisKomoditas::findOrFail($id);
|
||||
|
||||
$validated = $request->validate([
|
||||
'komoditas_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
'exists:master_komoditas,id',
|
||||
],
|
||||
'nama' => [
|
||||
'required',
|
||||
'string',
|
||||
'max:255',
|
||||
Rule::unique('jenis_komoditas', 'nama')
|
||||
->where('komoditas_id', $request->komoditas_id)
|
||||
->ignore($jenisKomoditas->id),
|
||||
],
|
||||
'path' => [
|
||||
'nullable',
|
||||
'string',
|
||||
],
|
||||
], [
|
||||
'komoditas_id.required' => 'Komoditas wajib dipilih.',
|
||||
'komoditas_id.exists' => 'Komoditas yang dipilih tidak valid.',
|
||||
'nama.required' => 'Nama jenis komoditas wajib diisi.',
|
||||
'nama.unique' => 'Nama jenis komoditas sudah tersedia pada komoditas ini.',
|
||||
]);
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
$jenisKomoditas->update([
|
||||
'komoditas_id' => $validated['komoditas_id'],
|
||||
'nama' => $validated['nama'],
|
||||
'updated_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
DB::commit();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Data jenis komoditas berhasil diperbarui.',
|
||||
'data' => $jenisKomoditas->fresh('komoditas'),
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
DB::rollBack();
|
||||
|
||||
Log::error('Gagal update jenis komoditas', [
|
||||
'error' => $e->getMessage(),
|
||||
'id' => $id,
|
||||
'request' => $request->all(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Gagal mengupdate data: ' . $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(string $id)
|
||||
{
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
$jenisKomoditas = JenisKomoditas::findOrFail($id);
|
||||
$jenisKomoditas->update(['deleted_by' => auth()->id()]);
|
||||
$jenisKomoditas->delete();
|
||||
|
||||
DB::commit();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Data jenis komoditas berhasil dihapus.',
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
DB::rollBack();
|
||||
|
||||
Log::error('Gagal hapus jenis komoditas', [
|
||||
'error' => $e->getMessage(),
|
||||
'id' => $id,
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Gagal menghapus data: ' . $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,9 +17,12 @@ class ProvinsiController extends Controller
|
||||
$perPage = $request->query('per_page', 10);
|
||||
$search = $request->query('search', '');
|
||||
|
||||
$provinsi = Provinsi::where('nama', 'like', "%$search%")
|
||||
->orWhere('kode', 'like', "%$search%")
|
||||
->orWhere('iso', 'like', "%$search%")
|
||||
$provinsi = Provinsi::withCount('kabupatenKota')
|
||||
->where(function($query) use ($search) {
|
||||
$query->where('nama', 'like', "%$search%")
|
||||
->orWhere('kode', 'like', "%$search%")
|
||||
->orWhere('iso', 'like', "%$search%");
|
||||
})
|
||||
->paginate($perPage);
|
||||
|
||||
return view('master.provinsi.index', compact('title', 'provinsi'));
|
||||
|
||||
@@ -0,0 +1,236 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Web\Master;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Master\Varietas;
|
||||
use App\Models\Pertanian\Komoditas;
|
||||
use App\Models\Master\JenisKomoditas;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class VarietasController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$perPage = $request->get('per_page', 10);
|
||||
$search = $request->get('search');
|
||||
|
||||
$varietas = Varietas::with(['komoditas', 'jenisKomoditas'])
|
||||
->when($search, function ($query) use ($search) {
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('nama', 'ILIKE', "%{$search}%")
|
||||
->orWhere('path', 'ILIKE', "%{$search}%")
|
||||
->orWhereHas('komoditas', function ($komoditasQuery) use ($search) {
|
||||
$komoditasQuery->where('nama', 'ILIKE', "%{$search}%");
|
||||
})
|
||||
->orWhereHas('jenisKomoditas', function ($jenisQuery) use ($search) {
|
||||
$jenisQuery->where('nama', 'ILIKE', "%{$search}%");
|
||||
});
|
||||
});
|
||||
})
|
||||
->latest()
|
||||
->paginate($perPage);
|
||||
|
||||
$komoditas = Komoditas::orderBy('nama', 'asc')->get();
|
||||
|
||||
return view('master.varietas.index', compact(
|
||||
'varietas',
|
||||
'komoditas'
|
||||
));
|
||||
}
|
||||
|
||||
public function getByJenisKomoditas($jenisKomoditasId)
|
||||
{
|
||||
$varietas = Varietas::where('jenis_komoditas_id', $jenisKomoditasId)->get();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'data' => $varietas,
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$komoditasTable = (new Komoditas)->getTable();
|
||||
$jenisKomoditasTable = (new JenisKomoditas)->getTable();
|
||||
$varietasTable = (new Varietas)->getTable();
|
||||
|
||||
$validated = $request->validate([
|
||||
'komoditas_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists($komoditasTable, 'id'),
|
||||
],
|
||||
'jenis_komoditas_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists($jenisKomoditasTable, 'id')
|
||||
->where('komoditas_id', $request->komoditas_id),
|
||||
],
|
||||
'nama' => [
|
||||
'required',
|
||||
'string',
|
||||
'max:255',
|
||||
Rule::unique($varietasTable, 'nama')
|
||||
->where('komoditas_id', $request->komoditas_id)
|
||||
->where('jenis_komoditas_id', $request->jenis_komoditas_id),
|
||||
],
|
||||
'deskripsi' => [
|
||||
'nullable',
|
||||
'string',
|
||||
],
|
||||
]);
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
$varietas = Varietas::create([
|
||||
'komoditas_id' => $validated['komoditas_id'],
|
||||
'jenis_komoditas_id' => $validated['jenis_komoditas_id'],
|
||||
'nama' => $validated['nama'],
|
||||
'deskripsi' => $validated['deskripsi'] ?? null,
|
||||
'created_by' => auth()->id(),
|
||||
'updated_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
DB::commit();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Data varietas berhasil disimpan.',
|
||||
'data' => $varietas,
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
DB::rollBack();
|
||||
|
||||
Log::error('Gagal menyimpan varietas', [
|
||||
'error' => $e->getMessage(),
|
||||
'request' => $request->all(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Gagal menyimpan data: ' . $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function show(string $id)
|
||||
{
|
||||
try {
|
||||
$varietas = Varietas::with(['komoditas', 'jenisKomoditas'])->findOrFail($id);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'data' => $varietas,
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Data varietas tidak ditemukan.',
|
||||
], 404);
|
||||
}
|
||||
}
|
||||
|
||||
public function update(Request $request, string $id)
|
||||
{
|
||||
$komoditasTable = (new Komoditas)->getTable();
|
||||
$jenisKomoditasTable = (new JenisKomoditas)->getTable();
|
||||
$varietasTable = (new Varietas)->getTable();
|
||||
|
||||
$validated = $request->validate([
|
||||
'komoditas_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists($komoditasTable, 'id'),
|
||||
],
|
||||
'jenis_komoditas_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists($jenisKomoditasTable, 'id')
|
||||
->where('komoditas_id', $request->komoditas_id),
|
||||
],
|
||||
'nama' => [
|
||||
'required',
|
||||
'string',
|
||||
'max:255',
|
||||
Rule::unique($varietasTable, 'nama')
|
||||
->where('komoditas_id', $request->komoditas_id)
|
||||
->where('jenis_komoditas_id', $request->jenis_komoditas_id),
|
||||
],
|
||||
'deskripsi' => [
|
||||
'nullable',
|
||||
'string',
|
||||
],
|
||||
]);
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
$varietas = Varietas::findOrFail($id);
|
||||
$varietas->update([
|
||||
'komoditas_id' => $validated['komoditas_id'],
|
||||
'jenis_komoditas_id' => $validated['jenis_komoditas_id'],
|
||||
'nama' => $validated['nama'],
|
||||
'deskripsi' => $validated['deskripsi'] ?? null,
|
||||
'updated_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
DB::commit();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Data varietas berhasil diperbarui.',
|
||||
'data' => $varietas->fresh(['komoditas', 'jenisKomoditas']),
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
DB::rollBack();
|
||||
|
||||
Log::error('Gagal update varietas', [
|
||||
'error' => $e->getMessage(),
|
||||
'id' => $id,
|
||||
'request' => $request->all(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Gagal mengupdate data: ' . $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function destroy(string $id)
|
||||
{
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
$varietas = Varietas::findOrFail($id);
|
||||
$varietas->update(['deleted_by' => auth()->id()]);
|
||||
$varietas->delete();
|
||||
|
||||
DB::commit();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Data varietas berhasil dihapus.',
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
DB::rollBack();
|
||||
|
||||
Log::error('Gagal hapus varietas', [
|
||||
'error' => $e->getMessage(),
|
||||
'id' => $id,
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Gagal menghapus data: ' . $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ use App\Models\Pertanian\Adat;
|
||||
use App\Models\Wilayah\Provinsi;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class LahanController extends Controller
|
||||
{
|
||||
@@ -76,6 +77,25 @@ class LahanController extends Controller
|
||||
return view('produktivitas.lahan.index', compact('lahan', 'provinsi', 'profiles', 'adat', 'kabupaten', 'kecamatan', 'desa'));
|
||||
}
|
||||
|
||||
public function getByProfile(Request $request, $profileId)
|
||||
{
|
||||
$desaKelurahanId = $request->get('desa_kelurahan_id');
|
||||
|
||||
$lahan = Lahan::with('profile')
|
||||
->where('profile_id', $profileId)
|
||||
->when($desaKelurahanId, function ($query) use ($desaKelurahanId) {
|
||||
$query->where('desa_kelurahan_id', $desaKelurahanId);
|
||||
})
|
||||
->orderBy('created_at', 'desc')
|
||||
->get(['id', 'nama', 'path', 'profile_id', 'desa_kelurahan_id']);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'data' => $lahan,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
public function create()
|
||||
{
|
||||
$provinsi = Provinsi::orderBy('nama', 'asc')->get();
|
||||
@@ -112,8 +132,12 @@ class LahanController extends Controller
|
||||
return response()->json(['status' => 'error', 'message' => 'Format koordinat (path) bukan JSON array yang valid.'], 422);
|
||||
}
|
||||
}
|
||||
|
||||
$userUID = Profile::where('id', $request->profile_id)->value('user_uid');
|
||||
$userUID = substr($userUID, 2);
|
||||
|
||||
Lahan::create([
|
||||
'uid' => 'LH' . $userUID . '-' . strtoupper(Str::random(10)),
|
||||
'nama' => $request->nama,
|
||||
'status_kepemilikan' => $request->status_kepemilikan,
|
||||
'profile_id' => $request->profile_id,
|
||||
|
||||
@@ -0,0 +1,421 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Web\Produktivitas;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Pertanian\Tanaman;
|
||||
use App\Models\Profile;
|
||||
use App\Models\Pertanian\Komoditas;
|
||||
use App\Models\Master\JenisKomoditas;
|
||||
use App\Models\Master\Varietas;
|
||||
use App\Models\Pertanian\Lahan;
|
||||
use App\Models\Wilayah\Provinsi;
|
||||
use App\Models\Wilayah\KabupatenKota;
|
||||
use App\Models\Wilayah\Kecamatan;
|
||||
use App\Models\Wilayah\DesaKelurahan;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class MapTanamanController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$perPage = $request->get('per_page', 10);
|
||||
$search = $request->get('search');
|
||||
|
||||
$tanaman = Tanaman::query()
|
||||
->select([
|
||||
DB::raw('MIN(id) as id'),
|
||||
'profile_id',
|
||||
'lahan_id',
|
||||
'komoditas_id',
|
||||
'jenis_komoditas_id',
|
||||
'varietas_id',
|
||||
DB::raw('COUNT(*) as total_tanaman'),
|
||||
DB::raw("SUM(CASE WHEN kondisi_tanaman = 'sehat' THEN 1 ELSE 0 END) as total_sehat"),
|
||||
DB::raw("SUM(CASE WHEN kondisi_tanaman = 'sakit' THEN 1 ELSE 0 END) as total_sakit"),
|
||||
DB::raw("SUM(CASE WHEN kondisi_tanaman = 'mati' THEN 1 ELSE 0 END) as total_mati"),
|
||||
DB::raw('MAX(created_at) as max_created_at'),
|
||||
])
|
||||
->with([
|
||||
'profile',
|
||||
'lahan.desaKelurahan.kecamatan.kabupatenkota.provinsi',
|
||||
'komoditas',
|
||||
'jenisKomoditas',
|
||||
'varietas',
|
||||
])
|
||||
->when($search, function ($query) use ($search) {
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->whereHas('profile', fn ($q) => $q->where('nama', 'ILIKE', "%{$search}%"))
|
||||
->orWhereHas('lahan', fn ($q) => $q->where('nama', 'ILIKE', "%{$search}%"))
|
||||
->orWhereHas('komoditas', fn ($q) => $q->where('nama', 'ILIKE', "%{$search}%"))
|
||||
->orWhereHas('jenisKomoditas', fn ($q) => $q->where('nama', 'ILIKE', "%{$search}%"))
|
||||
->orWhereHas('varietas', fn ($q) => $q->where('nama', 'ILIKE', "%{$search}%"))
|
||||
->orWhereHas('lahan.desaKelurahan', fn ($q) => $q->where('nama', 'ILIKE', "%{$search}%"))
|
||||
->orWhereHas('lahan.desaKelurahan.kecamatan', fn ($q) => $q->where('nama', 'ILIKE', "%{$search}%"))
|
||||
->orWhereHas('lahan.desaKelurahan.kecamatan.kabupatenkota', fn ($q) => $q->where('nama', 'ILIKE', "%{$search}%"))
|
||||
->orWhereHas('lahan.desaKelurahan.kecamatan.kabupatenkota.provinsi', fn ($q) => $q->where('nama', 'ILIKE', "%{$search}%"));
|
||||
});
|
||||
})
|
||||
->groupBy([
|
||||
'profile_id',
|
||||
'lahan_id',
|
||||
'komoditas_id',
|
||||
'jenis_komoditas_id',
|
||||
'varietas_id',
|
||||
])
|
||||
->orderByDesc('max_created_at')
|
||||
->paginate($perPage);
|
||||
|
||||
return view('produktivitas.mapTanaman.index', compact('tanaman'));
|
||||
}
|
||||
|
||||
private function getTanamanGroupByRepresentativeId($id)
|
||||
{
|
||||
$representative = Tanaman::findOrFail($id);
|
||||
|
||||
$points = Tanaman::with([
|
||||
'profile',
|
||||
'lahan.desaKelurahan.kecamatan.kabupatenkota.provinsi',
|
||||
'komoditas',
|
||||
'jenisKomoditas',
|
||||
'varietas',
|
||||
])
|
||||
->where('profile_id', $representative->profile_id)
|
||||
->where('lahan_id', $representative->lahan_id)
|
||||
->where('komoditas_id', $representative->komoditas_id)
|
||||
->where('jenis_komoditas_id', $representative->jenis_komoditas_id)
|
||||
->where('varietas_id', $representative->varietas_id)
|
||||
->orderBy('id')
|
||||
->get();
|
||||
|
||||
return [
|
||||
'representative' => $representative,
|
||||
'points' => $points,
|
||||
];
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$provinsi = Provinsi::select('id', 'nama', 'path')
|
||||
->orderBy('nama')
|
||||
->get();
|
||||
|
||||
$komoditas = Komoditas::orderBy('nama')->get();
|
||||
$profiles = Profile::orderBy('nama')->get();
|
||||
|
||||
return view('produktivitas.mapTanaman.add', compact(
|
||||
'provinsi',
|
||||
'komoditas',
|
||||
'profiles'
|
||||
));
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$desaKelurahanTable = (new DesaKelurahan)->getTable();
|
||||
$profileTable = (new Profile)->getTable();
|
||||
$lahanTable = (new Lahan)->getTable();
|
||||
$komoditasTable = (new Komoditas)->getTable();
|
||||
$jenisKomoditasTable = (new JenisKomoditas)->getTable();
|
||||
$varietasTable = (new Varietas)->getTable();
|
||||
|
||||
$validated = $request->validate([
|
||||
'desa_kelurahan_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists($desaKelurahanTable, 'id'),
|
||||
],
|
||||
|
||||
'profile_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists($profileTable, 'id'),
|
||||
],
|
||||
|
||||
'lahan_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists($lahanTable, 'id')
|
||||
->where('profile_id', $request->profile_id)
|
||||
->where('desa_kelurahan_id', $request->desa_kelurahan_id),
|
||||
],
|
||||
|
||||
'komoditas_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists($komoditasTable, 'id'),
|
||||
],
|
||||
|
||||
'jenis_komoditas_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists($jenisKomoditasTable, 'id')
|
||||
->where('komoditas_id', $request->komoditas_id),
|
||||
],
|
||||
|
||||
'varietas_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists($varietasTable, 'id')
|
||||
->where('jenis_komoditas_id', $request->jenis_komoditas_id),
|
||||
],
|
||||
|
||||
'points' => [
|
||||
'required',
|
||||
'array',
|
||||
'min:1',
|
||||
],
|
||||
|
||||
'points.*.latitude' => [
|
||||
'required',
|
||||
'numeric',
|
||||
],
|
||||
|
||||
'points.*.longitude' => [
|
||||
'required',
|
||||
'numeric',
|
||||
],
|
||||
|
||||
'points.*.kondisi_tanaman' => [
|
||||
'required',
|
||||
Rule::in(['sehat', 'sakit', 'mati']),
|
||||
],
|
||||
|
||||
'metode_pengumpulan' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'max:50',
|
||||
],
|
||||
|
||||
'deskripsi' => [
|
||||
'nullable',
|
||||
'string',
|
||||
],
|
||||
], [
|
||||
'points.required' => 'Minimal tambahkan satu titik tanaman pada peta.',
|
||||
'points.min' => 'Minimal tambahkan satu titik tanaman pada peta.',
|
||||
'points.*.latitude.required' => 'Latitude titik tanaman wajib ada.',
|
||||
'points.*.longitude.required' => 'Longitude titik tanaman wajib ada.',
|
||||
'points.*.kondisi_tanaman.required' => 'Kondisi tanaman wajib dipilih.',
|
||||
]);
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
$lahan = Lahan::findOrFail($validated['lahan_id']);
|
||||
$komoditas = Komoditas::findOrFail($validated['komoditas_id']);
|
||||
$profile = Profile::findOrFail($validated['profile_id']);
|
||||
|
||||
if (!$lahan->uid) {
|
||||
throw new \Exception('UID lahan tidak ditemukan.');
|
||||
}
|
||||
|
||||
$profileName = str_replace(' ', '', $profile->nama);
|
||||
$lahanUidPart = substr($lahan->uid, 2);
|
||||
|
||||
$existingCount = Tanaman::where('lahan_id', $validated['lahan_id'])
|
||||
->where('komoditas_id', $validated['komoditas_id'])
|
||||
->where('jenis_komoditas_id', $validated['jenis_komoditas_id'])
|
||||
->where('varietas_id', $validated['varietas_id'])
|
||||
->count();
|
||||
|
||||
$createdTanaman = [];
|
||||
|
||||
foreach ($validated['points'] as $index => $point) {
|
||||
$number = $existingCount + $index + 1;
|
||||
|
||||
$createdTanaman[] = Tanaman::create([
|
||||
'uid' => 'TM' . $lahanUidPart . '-' . strtoupper(Str::random(10)),
|
||||
'nama' => $komoditas->nama . ' - ' . $profileName . ' - ' . $number,
|
||||
'desa_kelurahan_id' => $validated['desa_kelurahan_id'],
|
||||
|
||||
'profile_id' => $validated['profile_id'],
|
||||
'lahan_id' => $validated['lahan_id'],
|
||||
|
||||
'komoditas_id' => $validated['komoditas_id'],
|
||||
'jenis_komoditas_id' => $validated['jenis_komoditas_id'],
|
||||
'varietas_id' => $validated['varietas_id'],
|
||||
|
||||
'latitude' => $point['latitude'],
|
||||
'longitude' => $point['longitude'],
|
||||
'kondisi_tanaman' => $point['kondisi_tanaman'],
|
||||
'metode_pengumpulan' => $validated['metode_pengumpulan'] ?? 'manual',
|
||||
'deskripsi' => $validated['deskripsi'] ?? null,
|
||||
'created_by' => auth()->id(),
|
||||
'updated_by' => auth()->id(),
|
||||
]);
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => count($createdTanaman) . ' data tanaman berhasil disimpan.',
|
||||
'redirect' => route('map-tanaman.index'),
|
||||
'data' => $createdTanaman,
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
DB::rollBack();
|
||||
|
||||
Log::error('Gagal menyimpan tanaman bulk', [
|
||||
'error' => $e->getMessage(),
|
||||
'request' => $request->all(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Gagal menyimpan data: ' . $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function show(string $id)
|
||||
{
|
||||
$group = $this->getTanamanGroupByRepresentativeId($id);
|
||||
|
||||
$tanaman = $group['representative']->load([
|
||||
'profile',
|
||||
'lahan.desaKelurahan.kecamatan.kabupatenkota.provinsi',
|
||||
'komoditas',
|
||||
'jenisKomoditas',
|
||||
'varietas',
|
||||
]);
|
||||
|
||||
$points = $group['points'];
|
||||
|
||||
return view('produktivitas.mapTanaman.view', compact(
|
||||
'tanaman',
|
||||
'points'
|
||||
));
|
||||
}
|
||||
public function edit(string $id)
|
||||
{
|
||||
$group = $this->getTanamanGroupByRepresentativeId($id);
|
||||
|
||||
$tanaman = $group['representative']->load([
|
||||
'profile',
|
||||
'lahan.desaKelurahan.kecamatan.kabupatenkota.provinsi',
|
||||
'komoditas',
|
||||
'jenisKomoditas',
|
||||
'varietas',
|
||||
]);
|
||||
|
||||
$points = $group['points'];
|
||||
|
||||
return view('produktivitas.mapTanaman.edit', compact(
|
||||
'tanaman',
|
||||
'points'
|
||||
));
|
||||
}
|
||||
|
||||
public function update(Request $request, string $id)
|
||||
{
|
||||
$group = $this->getTanamanGroupByRepresentativeId($id);
|
||||
|
||||
$representative = $group['representative'];
|
||||
|
||||
$validated = $request->validate([
|
||||
'points' => ['required', 'array', 'min:1'],
|
||||
'points.*.id' => ['nullable', 'integer', 'exists:map_tanaman,id'],
|
||||
'points.*.latitude' => ['required', 'numeric'],
|
||||
'points.*.longitude' => ['required', 'numeric'],
|
||||
'points.*.kondisi_tanaman' => [
|
||||
'required',
|
||||
Rule::in(['sehat', 'sakit', 'mati']),
|
||||
],
|
||||
'points.*._delete' => ['nullable'],
|
||||
]);
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
foreach ($validated['points'] as $point) {
|
||||
if (!empty($point['_delete']) && !empty($point['id'])) {
|
||||
$tanaman = Tanaman::where('id', $point['id'])
|
||||
->where('profile_id', $representative->profile_id)
|
||||
->where('lahan_id', $representative->lahan_id)
|
||||
->where('komoditas_id', $representative->komoditas_id)
|
||||
->where('jenis_komoditas_id', $representative->jenis_komoditas_id)
|
||||
->where('varietas_id', $representative->varietas_id)
|
||||
->first();
|
||||
|
||||
if ($tanaman) {
|
||||
$tanaman->deleted_by = auth()->id();
|
||||
$tanaman->save();
|
||||
$tanaman->delete();
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!empty($point['id'])) {
|
||||
$tanaman = Tanaman::where('id', $point['id'])
|
||||
->where('profile_id', $representative->profile_id)
|
||||
->where('lahan_id', $representative->lahan_id)
|
||||
->where('komoditas_id', $representative->komoditas_id)
|
||||
->where('jenis_komoditas_id', $representative->jenis_komoditas_id)
|
||||
->where('varietas_id', $representative->varietas_id)
|
||||
->firstOrFail();
|
||||
|
||||
$tanaman->update([
|
||||
'latitude' => $point['latitude'],
|
||||
'longitude' => $point['longitude'],
|
||||
'kondisi_tanaman' => $point['kondisi_tanaman'],
|
||||
'updated_by' => auth()->id(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Data titik tanaman berhasil diperbarui.',
|
||||
'redirect' => route('map-tanaman.index'),
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
DB::rollBack();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Gagal memperbarui data: ' . $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function destroy(string $id)
|
||||
{
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
$group = $this->getTanamanGroupByRepresentativeId($id);
|
||||
$points = $group['points'];
|
||||
|
||||
foreach ($points as $point) {
|
||||
$point->update([
|
||||
'deleted_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
$point->delete();
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => $points->count() . ' titik tanaman berhasil dihapus.',
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
DB::rollBack();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Gagal menghapus data: ' . $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,25 @@ class ProfileController extends Controller
|
||||
return view('master.profile.index', compact('profiles', 'provinsi'));
|
||||
}
|
||||
|
||||
public function getByDesaKelurahan($desaId)
|
||||
{
|
||||
try {
|
||||
$profiles = Profile::where('desa_kelurahan_id', $desaId)
|
||||
->orderBy('nama', 'asc')
|
||||
->get(['id', 'nama']);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'data' => $profiles,
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$provinsi = Provinsi::orderBy('nama', 'asc')->get();
|
||||
|
||||
@@ -54,6 +54,9 @@ class AktivitasController extends Controller
|
||||
'jenis_aktivitas' => ['required','string','max:255', Rule::in(['tujuan', 'aktivitas']),],
|
||||
'indikator' => ['required','string'],
|
||||
'deskripsi' => ['required','string'],
|
||||
'parent_id' => [
|
||||
Rule::requiredIf($request->jenis_aktivitas === 'aktivitas'),
|
||||
'nullable','exists:program_aktivitas,id',],
|
||||
]);
|
||||
|
||||
Aktivitas::create([...$validated,
|
||||
@@ -125,6 +128,9 @@ class AktivitasController extends Controller
|
||||
'jenis_aktivitas' => ['required','string','max:255', Rule::in(['tujuan', 'aktivitas']),],
|
||||
'indikator' => ['required','string'],
|
||||
'deskripsi' => ['required','string'],
|
||||
'parent_id' => [
|
||||
Rule::requiredIf($request->jenis_aktivitas === 'aktivitas'),
|
||||
'nullable','exists:program_aktivitas,id'],
|
||||
]);
|
||||
|
||||
$aktivitas->update([...$validated,
|
||||
|
||||
@@ -102,7 +102,8 @@ class FrameworkController extends Controller
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$framework = Framework::findOrFail($id);
|
||||
$framework = Framework::with('transformasi')->with('transformasi.tujuan')
|
||||
->findOrFail($id);
|
||||
$program = Program::all();
|
||||
|
||||
return view('program.framework.edit', compact('framework', 'program'));
|
||||
|
||||
@@ -0,0 +1,208 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Web\Program;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\View\View;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
use App\Models\Program\Framework;
|
||||
use App\Models\Program\Realisasi;
|
||||
|
||||
class RealisasiController extends Controller
|
||||
{
|
||||
public function index(Request $request): View
|
||||
{
|
||||
if (!Gate::any(['super_admin', 'admin'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$framework = Framework::when($request->has('frameworkid'), function ($query) use ($request) {
|
||||
$query->where('id', $request->frameworkid);
|
||||
})->latest()->first();
|
||||
if ($framework == null) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
$ringkasanIsiRealisasi = DB::select('select count(pt.id) count_target, sum(case when rl.id > 0 then 1 else 0 end) count_realisasi
|
||||
from program_target pt
|
||||
inner join program_aktivitas pa on pt.aktivitas_id = pa.id
|
||||
inner join program_transformasi ptr on pa.transformasi_id = ptr.id
|
||||
inner join program_framework pf on ptr.framework_id = pf.id
|
||||
left join
|
||||
(select target_id, max(id) id from program_realisasi pr
|
||||
group by target_id) rl on pt.id = rl.target_id
|
||||
where
|
||||
pt.deleted_at is null
|
||||
and pa.deleted_at is null
|
||||
and ptr.deleted_at is null
|
||||
and pf.id = ?
|
||||
', [$framework->id]);
|
||||
$ringkasanIsiRealisasi = collect($ringkasanIsiRealisasi)->map(function ($item) {
|
||||
$item->persentase = $item->count_target > 0 ? round(($item->count_realisasi / $item->count_target) * 100, 2) : 0;
|
||||
return $item;
|
||||
})->first();
|
||||
|
||||
$ringkasanCapaianRealisasi = DB::select("select round(avg(percent_realisasi)::numeric * 100, 2) persentase,
|
||||
sum(case when percent_realisasi >= 0.8 then 1 else 0 end) capaian_80,
|
||||
sum(case when percent_realisasi >= 0.5 and percent_realisasi < 0.8 then 1 else 0 end) capaian_50_80,
|
||||
sum(case when percent_realisasi < 0.5 then 1 else 0 end) capaian_50
|
||||
from
|
||||
(select pt.id target_id, pr.id realisasi_id,
|
||||
pt.tipe,
|
||||
case
|
||||
when pt.tipe = 'integer' then pt.target_integer
|
||||
when pt.tipe = 'float' then pt.target_float
|
||||
when pt.tipe = 'boolean' then pt.target_boolean::integer
|
||||
end target,
|
||||
case
|
||||
when pt.tipe = 'integer' then pr.realisasi_integer
|
||||
when pt.tipe = 'float' then pr.realisasi_float
|
||||
when pt.tipe = 'boolean' then pr.realisasi_boolean::integer
|
||||
end realisasi,
|
||||
case
|
||||
when pt.tipe = 'integer' then pr.realisasi_integer::float / pt.target_integer::float
|
||||
when pt.tipe = 'float' then pr.realisasi_float / pt.target_float
|
||||
when pt.tipe = 'boolean' then pr.realisasi_boolean::integer / pt.target_boolean::integer
|
||||
end percent_realisasi
|
||||
from program_target pt
|
||||
inner join program_aktivitas pa on pt.aktivitas_id = pa.id
|
||||
inner join program_transformasi ptr on pa.transformasi_id = ptr.id
|
||||
inner join program_framework pf on ptr.framework_id = pf.id
|
||||
left join
|
||||
(select target_id, max(id) id from program_realisasi pr
|
||||
group by target_id) rl on pt.id = rl.target_id
|
||||
left join program_realisasi pr on rl.id = pr.id and pr.target_id = pt.id
|
||||
where
|
||||
pt.deleted_at is null
|
||||
and pa.deleted_at is null
|
||||
and ptr.deleted_at is null
|
||||
and pf.id = ?) v",
|
||||
[$framework->id]);
|
||||
$ringkasanCapaianRealisasi = collect($ringkasanCapaianRealisasi)->first();
|
||||
|
||||
$ringkasanVerifikasi = DB::select("select count(pt.id) count_target,
|
||||
sum(case when count_verifikasi > 0 then 1 else 0 end) count_verifikasi,
|
||||
sum(case when count_verifikasi = 0 or count_verifikasi is null then 1 else 0 end) count_unverifikasi
|
||||
from program_target pt
|
||||
inner join program_aktivitas pa on pt.aktivitas_id = pa.id
|
||||
inner join program_transformasi ptr on pa.transformasi_id = ptr.id
|
||||
inner join program_framework pf on ptr.framework_id = pf.id
|
||||
left join
|
||||
(select target_id, max(id) id from program_realisasi pr
|
||||
group by target_id) rl on pt.id = rl.target_id
|
||||
left join
|
||||
(select realisasi_id, count(pv.id) count_verifikasi from program_verifikasi pv
|
||||
group by realisasi_id) rv
|
||||
on rv.realisasi_id = rl.id
|
||||
where
|
||||
pt.deleted_at is null
|
||||
and pa.deleted_at is null
|
||||
and ptr.deleted_at is null
|
||||
and pf.id = ?",
|
||||
[$framework->id]);
|
||||
$ringkasanVerifikasi = collect($ringkasanVerifikasi)->map(function ($item) {
|
||||
$item->persentase = $item->count_target > 0 ? round(($item->count_verifikasi / $item->count_target) * 100, 2) : 0;
|
||||
return $item;
|
||||
})->first();
|
||||
|
||||
return view('program.realisasi.index', compact('framework', 'ringkasanIsiRealisasi', 'ringkasanCapaianRealisasi', 'ringkasanVerifikasi'));
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
if (!Gate::any(['super_admin', 'admin'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'target_id' => ['required', 'integer', 'exists:program_target,id'],
|
||||
'realisasi_integer' => ['nullable', 'integer'],
|
||||
'realisasi_float' => ['nullable', 'numeric'],
|
||||
'realisasi_boolean' => ['nullable', 'boolean'],
|
||||
]);
|
||||
|
||||
$realisasi = Realisasi::create([...$validated,
|
||||
'created_by' => auth()->id(),
|
||||
'updated_by' => auth()->id()
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Target berhasil ditambahkan.'
|
||||
], 201);
|
||||
}
|
||||
|
||||
public function show(string $id)
|
||||
{
|
||||
if (!Gate::any(['super_admin', 'admin'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$realisasi = Realisasi::with('target', 'target.aktivitas', 'target.aktivitas.transformasi', 'verifikasi')
|
||||
->find($id);
|
||||
if (!$realisasi) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Data realisasi tidak ditemukan.'
|
||||
], 404);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'data' => $realisasi
|
||||
]);
|
||||
}
|
||||
|
||||
public function edit(string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function update(Request $request, string $id): JsonResponse
|
||||
{
|
||||
if (!Gate::any(['super_admin', 'admin'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$realisasi = Realisasi::find($id);
|
||||
if (!$realisasi) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Data realisasi tidak ditemukan.'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'target_id' => ['required', 'integer', 'exists:program_target,id'],
|
||||
'realisasi_integer' => ['nullable', 'integer'],
|
||||
'realisasi_float' => ['nullable', 'numeric'],
|
||||
'realisasi_boolean' => ['nullable', 'boolean'],
|
||||
]);
|
||||
|
||||
$realisasi->update([...$validated,
|
||||
'updated_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Target berhasil diperbarui.'
|
||||
], 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
@@ -69,6 +69,16 @@ class TargetController extends Controller
|
||||
'nullable',
|
||||
'boolean'
|
||||
],
|
||||
'tipe_realisasi' => [
|
||||
'nullable',
|
||||
'string',
|
||||
Rule::in(['manual', 'referensi'])
|
||||
],
|
||||
'value_query' => [
|
||||
Rule::requiredIf($request->tipe_realisasi === 'referensi'),
|
||||
'nullable',
|
||||
'string'
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -77,9 +87,6 @@ class TargetController extends Controller
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request, string $frameworkId, string $transformasiId, string $aktivitasId): JsonResponse
|
||||
{
|
||||
if (!Gate::any(['super_admin', 'admin'])) {
|
||||
@@ -109,12 +116,32 @@ class TargetController extends Controller
|
||||
], 201);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(string $id)
|
||||
public function show(string $frameworkId, string $transformasiId, string $aktivitasId, string $id)
|
||||
{
|
||||
//
|
||||
if (!Gate::any(['super_admin', 'admin'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$target = Target::with('realisasi', 'latestRealisasi')
|
||||
->whereHas('aktivitas.transformasi', function ($query) use ($frameworkId) {
|
||||
$query->where('framework_id', $frameworkId);
|
||||
})
|
||||
->whereHas('aktivitas', function ($query) use ($transformasiId) {
|
||||
$query->where('transformasi_id', $transformasiId);
|
||||
})
|
||||
->where('aktivitas_id', $aktivitasId)->find($id);
|
||||
|
||||
if (!$target) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Data target tidak ditemukan.'
|
||||
], 404);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'data' => $target
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -128,16 +155,67 @@ class TargetController extends Controller
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, string $id)
|
||||
public function update(Request $request, string $frameworkId, string $transformasiId, string $aktivitasId, string $id)
|
||||
{
|
||||
//
|
||||
if (!Gate::any(['super_admin', 'admin'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$target = Target::whereHas('aktivitas.transformasi', function ($query) use ($frameworkId) {
|
||||
$query->where('framework_id', $frameworkId);
|
||||
})
|
||||
->whereHas('aktivitas', function ($query) use ($transformasiId) {
|
||||
$query->where('transformasi_id', $transformasiId);
|
||||
})
|
||||
->where('aktivitas_id', $aktivitasId)->find($id);
|
||||
|
||||
if (!$target) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Data target tidak ditemukan.'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$validated = $this->validated($request);
|
||||
$target->update([...$validated,
|
||||
'updated_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Target berhasil diperbarui.'
|
||||
], 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(string $id)
|
||||
public function destroy(string $frameworkId, string $transformasiId, string $aktivitasId, string $id)
|
||||
{
|
||||
//
|
||||
if (!Gate::any(['super_admin', 'admin'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$target = Target::whereHas('aktivitas.transformasi', function ($query) use ($frameworkId) {
|
||||
$query->where('framework_id', $frameworkId);
|
||||
})
|
||||
->whereHas('aktivitas', function ($query) use ($transformasiId) {
|
||||
$query->where('transformasi_id', $transformasiId);
|
||||
})
|
||||
->where('aktivitas_id', $aktivitasId)->find($id);
|
||||
|
||||
if (!$target) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Data target tidak ditemukan.'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$target->update([
|
||||
'deleted_by' => auth()->id(),
|
||||
]);
|
||||
$target->delete();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Target berhasil dihapus.'
|
||||
], 200);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Web\Program;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\View\View;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
use App\Models\Program\Framework;
|
||||
use App\Models\Program\Realisasi;
|
||||
use App\Models\Program\Verifikasi;
|
||||
|
||||
class VerifikasiController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
if (!Gate::any(['super_admin', 'admin'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'realisasi_id' => ['required', 'integer', 'exists:program_realisasi,id'],
|
||||
'dokumen' => ['required', 'string'],
|
||||
'link_dokumen' => ['required', 'url'],
|
||||
]);
|
||||
|
||||
$verifikasi = Verifikasi::create([...$validated,
|
||||
'created_by' => auth()->id(),
|
||||
'updated_by' => auth()->id()
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Dokumen bukti berhasil ditambahkan.'
|
||||
], 201);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(string $id): JsonResponse
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(string $id): JsonResponse
|
||||
{
|
||||
if (!Gate::any(['super_admin', 'admin'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$verifikasi = Verifikasi::find($id);
|
||||
if (!$verifikasi) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Data verifikasi tidak ditemukan.'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$verifikasi->update(['updated_by' => auth()->id()]);
|
||||
$verifikasi->delete();
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Dokumen bukti berhasil dihapus.'
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -49,6 +49,7 @@ class ProgramController extends Controller
|
||||
'sub_domain' => ['required', Rule::unique(Program::class, 'sub_domain')],
|
||||
'tahun' => ['required', 'integer'],
|
||||
'instansi' => ['nullable', 'string'],
|
||||
'nama_singkat' => ['required', 'string'],
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
@@ -94,6 +95,9 @@ class ProgramController extends Controller
|
||||
$validator = Validator::make($request->all(), [
|
||||
'nama' => ['required'],
|
||||
'sub_domain' => ['required', Rule::unique(Program::class, 'sub_domain')->ignore($id)],
|
||||
'tahun' => ['required', 'integer'],
|
||||
'instansi' => ['nullable', 'string'],
|
||||
'nama_singkat' => ['required', 'string'],
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
@@ -106,9 +110,7 @@ class ProgramController extends Controller
|
||||
return redirect()->route('program.index')->with('error', 'Data program tidak ditemukan!');
|
||||
}
|
||||
|
||||
$program->update([
|
||||
'nama' => $request->nama,
|
||||
'sub_domain' => $request->sub_domain,
|
||||
$program->update([...$validator->validated(),
|
||||
'updated_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user