validasi kelola sampah

This commit is contained in:
rahmagustin 2026-03-30 22:29:48 +07:00
parent b39123b6d8
commit 00b26280c2
2 changed files with 99 additions and 46 deletions

View File

@ -5,7 +5,6 @@
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Sampah;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Carbon\Carbon;
@ -17,12 +16,10 @@ public function index(Request $request)
Carbon::setLocale('id');
$title = 'Data Sampah';
$tahun = $request->tahun;
$query = Sampah::with('user');
// Filter tahun jika dipilih
if ($tahun) {
$query->where('tahun', $tahun);
}
@ -31,7 +28,6 @@ public function index(Request $request)
->orderBy('bulan', 'desc')
->get();
// ambil daftar tahun untuk dropdown filter
$listTahun = Sampah::select('tahun')
->distinct()
->orderBy('tahun', 'desc')
@ -43,46 +39,90 @@ public function index(Request $request)
public function create()
{
$title = 'Tambah Data Sampah';
$users = User::all();
return view('admin.sampah.create', compact('title'));
}
return view('admin.sampah.create', compact('title', 'users'));
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)
{
$request->merge([
'total_sampah' => $this->convertToDecimal($request->total_sampah),
'total_kelola' => $this->convertToDecimal($request->total_kelola),
'total_daur_ulang' => $this->convertToDecimal($request->total_daur_ulang),
]);
$validator = Validator::make($request->all(), [
'tahun' => 'required|digits:4',
'bulan' => 'required|integer|min:1|max:12',
'total_sampah' => 'required|numeric|min:0',
'total_kelola' => 'required|numeric|min:0',
'total_daur_ulang' => 'required|numeric|min:0',
]);
// ✅ VALIDASI DULU (tanpa convert)
$validator = Validator::make(
$request->all(),
$this->rules(),
$this->messages(),
$this->attributes()
);
if ($validator->fails()) {
return back()->withErrors($validator)->withInput();
}
if (($request->total_kelola + $request->total_daur_ulang) > $request->total_sampah) {
// ✅ 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 = $request->total_sampah - ($request->total_kelola + $request->total_daur_ulang);
$sisa_sampah = $total_sampah - ($total_kelola + $total_daur_ulang);
Sampah::create([
'user_id' => Auth::id(),
'tahun' => $request->tahun,
'bulan' => $request->bulan,
'total_sampah' => $request->total_sampah,
'total_kelola' => $request->total_kelola,
'total_daur_ulang' => $request->total_daur_ulang,
'total_sampah' => $total_sampah,
'total_kelola' => $total_kelola,
'total_daur_ulang' => $total_daur_ulang,
'sisa_sampah' => $sisa_sampah,
]);
@ -102,39 +142,52 @@ public function update(Request $request, $id)
{
$sampah = Sampah::findOrFail($id);
$request->merge([
'total_sampah' => $this->convertToDecimal($request->total_sampah),
'total_kelola' => $this->convertToDecimal($request->total_kelola),
'total_daur_ulang' => $this->convertToDecimal($request->total_daur_ulang),
]);
$validator = Validator::make($request->all(), [
'tahun' => 'required|digits:4',
'bulan' => 'required|integer|min:1|max:12',
'total_sampah' => 'required|numeric|min:0',
'total_kelola' => 'required|numeric|min:0',
'total_daur_ulang' => 'required|numeric|min:0',
]);
// VALIDASI DULU
$validator = Validator::make(
$request->all(),
$this->rules(),
$this->messages(),
$this->attributes()
);
if ($validator->fails()) {
return back()->withErrors($validator)->withInput();
}
if (($request->total_kelola + $request->total_daur_ulang) > $request->total_sampah) {
// 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 = $request->total_sampah - ($request->total_kelola + $request->total_daur_ulang);
$sisa_sampah = $total_sampah - ($total_kelola + $total_daur_ulang);
$sampah->update([
'user_id' => Auth::id(),
'tahun' => $request->tahun,
'bulan' => $request->bulan,
'total_sampah' => $request->total_sampah,
'total_kelola' => $request->total_kelola,
'total_daur_ulang' => $request->total_daur_ulang,
'total_sampah' => $total_sampah,
'total_kelola' => $total_kelola,
'total_daur_ulang' => $total_daur_ulang,
'sisa_sampah' => $sisa_sampah,
]);
@ -153,10 +206,10 @@ public function destroy($id)
private function convertToDecimal($value)
{
if (!$value) {
return 0;
if ($value === null || $value === '') {
return null; // ✅ biar required bisa jalan
}
return str_replace(',', '.', str_replace('.', '', $value));
return floatval(str_replace(',', '.', str_replace('.', '', $value)));
}
}

View File

@ -104,7 +104,7 @@
<li class="nav-item {{ request()->routeIs('admin.kategori.index') ? 'active' : '' }}">
<a class="nav-link" href="{{ route('admin.kategori.index') }}">
<i class="icon-menu menu-icon"></i>
<span class="menu-title">Kategori TPS</span>
<span class="menu-title">Kelola Kategori TPS</span>
</a>
</li>
<li class="nav-item {{ request()->routeIs('admin.sampah.index') ? 'active' : '' }}">