diff --git a/app/Http/Controllers/CuranmorController.php b/app/Http/Controllers/CuranmorController.php index b59297c..4fc8e06 100644 --- a/app/Http/Controllers/CuranmorController.php +++ b/app/Http/Controllers/CuranmorController.php @@ -5,6 +5,7 @@ use App\Models\Klaster; use App\Models\Curanmor; use App\Models\Kecamatan; +use App\Models\Detail_Curanmor; use Illuminate\Http\Request; use App\Services\KMeansService; use Illuminate\Validation\Rule; @@ -26,7 +27,7 @@ public function index() */ public function create() { - return view('admin.dashboardTambahCuranmor', ['kecamatans' => Kecamatan::all()], ['klasters' => Klaster::all()]); + return view('admin.dashboardTambahCuranmor', ['kecamatans' => Kecamatan::all()]); } /** @@ -35,14 +36,51 @@ public function create() public function store(Request $request) { try{ - $validateData = $request->validate([ - 'kecamatan_id' =>'required|max:255|exists:kecamatans,id|unique:curanmors,kecamatan_id', - 'jumlah_curanmor' =>'required', - 'klaster_id' =>'required|max:255|exists:klasters,id', + $request->validate([ + 'kecamatan_id' => 'required|exists:kecamatans,id', + 'jumlah_curanmor' => 'required|numeric', ]); + + $kecamatan_id = $request->kecamatan_id; + $tambahan_curanmor = $request->jumlah_curanmor; + + + $curanmor = Curanmor::where('kecamatan_id', $kecamatan_id)->first(); + + if ($curanmor) { + $curanmor->jumlah_curanmor += $tambahan_curanmor; + $curanmor->save(); + + $curanmor_id = $curanmor->id; + } else { + // Jika belum ada, bisa insert baru dulu (optional, sesuai kebutuhan) + $curanmor = Curanmor::create([ + 'kecamatan_id' => $kecamatan_id, + 'jumlah_curanmor' => $tambahan_curanmor, + ]); + $curanmor_id = $curanmor->id; + } + Detail_Curanmor::create([ + 'curanmor_id' => $curanmor_id, + 'tambahan_curanmor' => $tambahan_curanmor, + 'detailCuranmor_kecamatan_Id' => $kecamatan_id, + + ]); + + $service = new KMeansService(); + $hasil = $service->hitungKMeansCuranmor(); + file_put_contents(storage_path('app/public/hasil_kmeans_curanmor.json'), json_encode($hasil)); + + // =====CODE TAMBAH SEBELUMNYA========= + // $validateData = $request->validate([ + // 'kecamatan_id' =>'required|max:255|exists:kecamatans,id|unique:curanmors,kecamatan_id', + // 'jumlah_curanmor' =>'required', + // 'klaster_id' =>'required|max:255|exists:klasters,id', + + // ]); - Curanmor::create($validateData); + // Curanmor::create($validateData); return redirect('/dashboard/curanmor')->with('succes', 'Berhasil Menambahkan Data Curanmor Baru'); }catch (\Exception $e){ return redirect('/dashboard/curanmor')->with('error', 'Gagal Menambahkan Data Curanmor Baru'); diff --git a/app/Http/Controllers/DetailCuranmorController.php b/app/Http/Controllers/DetailCuranmorController.php index 378439b..e7e228e 100644 --- a/app/Http/Controllers/DetailCuranmorController.php +++ b/app/Http/Controllers/DetailCuranmorController.php @@ -2,9 +2,93 @@ namespace App\Http\Controllers; +use App\Models\Curanmor; use Illuminate\Http\Request; +use App\Models\Detail_Curanmor; +use App\Services\KMeansService; class DetailCuranmorController extends Controller { - // + /** + * Display a listing of the resource. + */ + public function index() + { + $detail_curanmor = Detail_Curanmor::orderBy('updated_at', 'desc')->get(); + + return view('admin.dashboardDetailCuranmor', compact('detail_curanmor')); + } + + /** + * Show the form for creating a new resource. + */ + public function create() + { + // + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + // + } + + /** + * Display the specified resource. + */ + public function show(Detail_Curanmor $detail_Curanmor) + { + // + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(Detail_Curanmor $detail_Curanmor) + { + // + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, Detail_Curanmor $detail_Curanmor) + { + // + } + + /** + * Remove the specified resource from storage. + */ + public function destroy($id) + { + try { + $detail = Detail_Curanmor::findOrFail($id); + + $curanmor = Curanmor::findOrFail($detail->curanmor_id); + + $curanmor->jumlah_curanmor -= $detail->tambahan_curanmor; + + // Pastikan tidak negatif + if ($curanmor->jumlah_curanmor < 0) { + $curanmor->jumlah_curanmor = 0; + } + + $curanmor->save(); + + $detail->delete(); + + $service = new KMeansService(); + $hasil = $service->hitungKMeansCuranmor(); + + // simpan hasil ke file json + file_put_contents(storage_path('app/public/hasil_kmeans_curanmor.json'), json_encode($hasil)); + + return redirect('/dashboard/detail-curanmor')->with('succes', 'Data berhasil dihapus dan curanmor diperbarui.'); + } catch (\Exception $e) { + return redirect('/dashboard/detail-curanmor')->with('error', 'Terjadi kesalahan Ketika Menghapus Data : ' . $e->getMessage()); + } + } } diff --git a/app/Http/Controllers/DetailCurasController.php b/app/Http/Controllers/DetailCurasController.php index fb586ab..31aced7 100644 --- a/app/Http/Controllers/DetailCurasController.php +++ b/app/Http/Controllers/DetailCurasController.php @@ -2,15 +2,98 @@ namespace App\Http\Controllers; -use Illuminate\Http\Request; +use App\Models\Curas; use App\Models\Detail_Curas; +use Illuminate\Http\Request; +use App\Services\KMeansService; class DetailCurasController extends Controller { + /** + * Display a listing of the resource. + */ public function index() -{ - $detail_curas = Detail_Curas::orderBy('created_at')->get(); + { + $detail_curas = Detail_Curas::orderBy('updated_at', 'desc')->get(); + + return view('admin.dashboardDetailCuras', compact('detail_curas')); + } + + /** + * Show the form for creating a new resource. + */ + public function create() + { + // + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + // + } + + /** + * Display the specified resource. + */ + public function show(Detail_Curas $detail_Curas) + { + // + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(Detail_Curas $detail_Curas) + { + // + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, Detail_Curas $detail_Curas) + { + // + } + + /** + * Remove the specified resource from storage. + */ + public function destroy($id) + { + try { + $detail = Detail_Curas::findOrFail($id); + + // Ambil curas terkait + $curas = Curas::findOrFail($detail->curas_id); + + // Kurangi jumlah_curas + $curas->jumlah_curas -= $detail->tambahan_curas; + + // Pastikan tidak negatif + if ($curas->jumlah_curas < 0) { + $curas->jumlah_curas = 0; + } + + $curas->save(); // Simpan perubahan curas + + // Hapus detail_curas + $detail->delete(); + + $service = new KMeansService(); + $hasil = $service->hitungKMeansCuras(); + + // simpan hasil ke file json + file_put_contents(storage_path('app/public/hasil_kmeans_curas.json'), json_encode($hasil)); + + return redirect('/dashboard/detail-curas')->with('succes', 'Data berhasil dihapus dan curas diperbarui.'); + } catch (\Exception $e) { + return redirect('/dashboard/detail-curas')->with('error', 'Terjadi kesalahan Ketika Menghapus Data : ' . $e->getMessage()); + } + + } - return view('admin.dashboardDetailCuras', compact('detail_curas')); -} } diff --git a/config/app.php b/config/app.php index f467267..6800aa1 100644 --- a/config/app.php +++ b/config/app.php @@ -65,7 +65,7 @@ | */ - 'timezone' => env('APP_TIMEZONE', 'UTC'), + 'timezone' => env('APP_TIMEZONE', 'Asia/Jakarta'), /* |-------------------------------------------------------------------------- diff --git a/resources/views/Admin/dashboardDetailCuranmor.blade.php b/resources/views/Admin/dashboardDetailCuranmor.blade.php new file mode 100644 index 0000000..2e4e243 --- /dev/null +++ b/resources/views/Admin/dashboardDetailCuranmor.blade.php @@ -0,0 +1,72 @@ + +
+
+
+
+
+
+

Detail Kasus Curanmor Per Tanggal

+

Berikut ini merupakan rincian kasus Pencurian Kendaraan Bermotor (CURANMOR) + yang dikelompokkan berdasarkan tanggal di inputkannya kasus CURANMOR yang terjadi

+
+
+ @if (session()->has('succes')) + + @endif + @if (session()->has('error')) + + @endif +
+
+
+ + + + + + + + + + + + @php + $grouped = $detail_curanmor->groupBy(function($item) { + return $item->created_at->format('Y-m-d'); + }); + @endphp + @foreach($grouped as $tanggal => $items) + @foreach($items as $index => $detail) + + @if ($index == 0) + + @endif + + + + + + @endforeach + @endforeach + +
TanggalNama KecamatanTambahan Kasus CuranmorTotal Curanmor Per KecamatanHapus Update Kasus
{{ \Carbon\Carbon::parse($tanggal)->translatedFormat('d F Y') }}{{ $detail->detailCuranmor_Kecamatan->nama_kecamatan }}{{ $detail->tambahan_curanmor }}{{ $detail->detailCuranmor_Curanmor->jumlah_curanmor }} +
+
+ @method('delete') + @csrf + + +
+
+
+
+
+
+ +
+
+
\ No newline at end of file diff --git a/resources/views/Admin/dashboardDetailCuras.blade.php b/resources/views/Admin/dashboardDetailCuras.blade.php index e06e06c..34b3b1e 100644 --- a/resources/views/Admin/dashboardDetailCuras.blade.php +++ b/resources/views/Admin/dashboardDetailCuras.blade.php @@ -6,10 +6,9 @@

Detail Kasus Curas Per Tanggal

-

Berikut ini merupakan rincian kasus Pencurian Dengan Kekerasan (CURAS)
- yang dikelompokkan berdasarkan tanggal

+

Berikut ini merupakan rincian kasus Pencurian Dengan Kekerasan (CURAS) + yang dikelompokkan berdasarkan tanggal di inputkannya kasus CURAS yang terjadi

- Tambah Kasus Curas
@if (session()->has('succes'))