tahun; $query = Sampah::with('user'); if ($tahun) { $query->where('tahun', $tahun); } $sampah = $query->orderBy('tahun', 'desc') ->orderBy('bulan', 'desc') ->get(); $listTahun = Sampah::select('tahun') ->distinct() ->orderBy('tahun', 'desc') ->pluck('tahun'); return view('admin.sampah.index', compact('title', 'sampah', 'listTahun')); } public function create() { $title = 'Tambah Data Sampah'; return view('admin.sampah.create', compact('title')); } private function rules() { return [ 'tahun' => 'required|digits:4', 'bulan' => 'required|integer|min:1|max:12', 'total_sampah' => 'required', 'total_kelola' => 'required', 'total_daur_ulang' => 'required', ]; } private function messages() { return [ 'required' => ':attribute wajib diisi.', 'digits' => ':attribute harus 4 digit.', 'integer' => ':attribute harus berupa angka.', 'min' => ':attribute minimal :min.', 'max' => ':attribute maksimal :max.', ]; } private function attributes() { return [ 'tahun' => 'Tahun', 'bulan' => 'Bulan', 'total_sampah' => 'Total Sampah', 'total_kelola' => 'Total Kelola', 'total_daur_ulang' => 'Total Daur Ulang', ]; } public function store(Request $request) { // ✅ VALIDASI DULU (tanpa convert) $validator = Validator::make( $request->all(), $this->rules(), $this->messages(), $this->attributes() ); if ($validator->fails()) { return back()->withErrors($validator)->withInput(); } // ✅ BARU CONVERT SETELAH LOLOS VALIDASI $total_sampah = $this->convertToDecimal($request->total_sampah); $total_kelola = $this->convertToDecimal($request->total_kelola); $total_daur_ulang = $this->convertToDecimal($request->total_daur_ulang); // ✅ VALIDASI NUMERIC SETELAH CONVERT if (!is_numeric($total_sampah)) { return back()->withErrors(['total_sampah' => 'Total Sampah harus berupa angka.'])->withInput(); } if (!is_numeric($total_kelola)) { return back()->withErrors(['total_kelola' => 'Total Kelola harus berupa angka.'])->withInput(); } if (!is_numeric($total_daur_ulang)) { return back()->withErrors(['total_daur_ulang' => 'Total Daur Ulang harus berupa angka.'])->withInput(); } // ✅ VALIDASI LOGIKA BISNIS if (($total_kelola + $total_daur_ulang) > $total_sampah) { return back()->withErrors([ 'total_kelola' => 'Jumlah kelola + daur ulang tidak boleh melebihi total sampah.' ])->withInput(); } $sisa_sampah = $total_sampah - ($total_kelola + $total_daur_ulang); Sampah::create([ 'user_id' => Auth::id(), 'tahun' => $request->tahun, 'bulan' => $request->bulan, 'total_sampah' => $total_sampah, 'total_kelola' => $total_kelola, 'total_daur_ulang' => $total_daur_ulang, 'sisa_sampah' => $sisa_sampah, ]); return redirect()->route('admin.sampah.index') ->with('success', 'Data sampah berhasil ditambahkan.'); } public function edit($id) { $title = 'Edit Data Sampah'; $sampah = Sampah::findOrFail($id); return view('admin.sampah.edit', compact('title', 'sampah')); } public function update(Request $request, $id) { $sampah = Sampah::findOrFail($id); // VALIDASI DULU $validator = Validator::make( $request->all(), $this->rules(), $this->messages(), $this->attributes() ); if ($validator->fails()) { return back()->withErrors($validator)->withInput(); } // CONVERT $total_sampah = $this->convertToDecimal($request->total_sampah); $total_kelola = $this->convertToDecimal($request->total_kelola); $total_daur_ulang = $this->convertToDecimal($request->total_daur_ulang); // VALIDASI NUMERIC if (!is_numeric($total_sampah)) { return back()->withErrors(['total_sampah' => 'Total Sampah harus berupa angka.'])->withInput(); } if (!is_numeric($total_kelola)) { return back()->withErrors(['total_kelola' => 'Total Kelola harus berupa angka.'])->withInput(); } if (!is_numeric($total_daur_ulang)) { return back()->withErrors(['total_daur_ulang' => 'Total Daur Ulang harus berupa angka.'])->withInput(); } // VALIDASI LOGIKA if (($total_kelola + $total_daur_ulang) > $total_sampah) { return back()->withErrors([ 'total_kelola' => 'Jumlah kelola + daur ulang tidak boleh melebihi total sampah.' ])->withInput(); } $sisa_sampah = $total_sampah - ($total_kelola + $total_daur_ulang); $sampah->update([ 'user_id' => Auth::id(), 'tahun' => $request->tahun, 'bulan' => $request->bulan, 'total_sampah' => $total_sampah, 'total_kelola' => $total_kelola, 'total_daur_ulang' => $total_daur_ulang, 'sisa_sampah' => $sisa_sampah, ]); return redirect()->route('admin.sampah.index') ->with('success', 'Data sampah berhasil diperbarui.'); } public function destroy($id) { $sampah = Sampah::findOrFail($id); $sampah->delete(); return redirect()->route('admin.sampah.index') ->with('success', 'Data sampah berhasil dihapus.'); } private function convertToDecimal($value) { if ($value === null || $value === '') { return null; // ✅ biar required bisa jalan } return floatval(str_replace(',', '.', str_replace('.', '', $value))); } }