validasi kelola sampah
This commit is contained in:
parent
b39123b6d8
commit
00b26280c2
|
|
@ -5,7 +5,6 @@
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use App\Models\Sampah;
|
use App\Models\Sampah;
|
||||||
use App\Models\User;
|
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
use Illuminate\Support\Facades\Validator;
|
use Illuminate\Support\Facades\Validator;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
|
@ -17,12 +16,10 @@ public function index(Request $request)
|
||||||
Carbon::setLocale('id');
|
Carbon::setLocale('id');
|
||||||
|
|
||||||
$title = 'Data Sampah';
|
$title = 'Data Sampah';
|
||||||
|
|
||||||
$tahun = $request->tahun;
|
$tahun = $request->tahun;
|
||||||
|
|
||||||
$query = Sampah::with('user');
|
$query = Sampah::with('user');
|
||||||
|
|
||||||
// Filter tahun jika dipilih
|
|
||||||
if ($tahun) {
|
if ($tahun) {
|
||||||
$query->where('tahun', $tahun);
|
$query->where('tahun', $tahun);
|
||||||
}
|
}
|
||||||
|
|
@ -31,7 +28,6 @@ public function index(Request $request)
|
||||||
->orderBy('bulan', 'desc')
|
->orderBy('bulan', 'desc')
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
// ambil daftar tahun untuk dropdown filter
|
|
||||||
$listTahun = Sampah::select('tahun')
|
$listTahun = Sampah::select('tahun')
|
||||||
->distinct()
|
->distinct()
|
||||||
->orderBy('tahun', 'desc')
|
->orderBy('tahun', 'desc')
|
||||||
|
|
@ -43,46 +39,90 @@ public function index(Request $request)
|
||||||
public function create()
|
public function create()
|
||||||
{
|
{
|
||||||
$title = 'Tambah Data Sampah';
|
$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)
|
public function store(Request $request)
|
||||||
{
|
{
|
||||||
$request->merge([
|
// ✅ VALIDASI DULU (tanpa convert)
|
||||||
'total_sampah' => $this->convertToDecimal($request->total_sampah),
|
$validator = Validator::make(
|
||||||
'total_kelola' => $this->convertToDecimal($request->total_kelola),
|
$request->all(),
|
||||||
'total_daur_ulang' => $this->convertToDecimal($request->total_daur_ulang),
|
$this->rules(),
|
||||||
]);
|
$this->messages(),
|
||||||
|
$this->attributes()
|
||||||
$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',
|
|
||||||
]);
|
|
||||||
|
|
||||||
if ($validator->fails()) {
|
if ($validator->fails()) {
|
||||||
return back()->withErrors($validator)->withInput();
|
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([
|
return back()->withErrors([
|
||||||
'total_kelola' => 'Jumlah kelola + daur ulang tidak boleh melebihi total sampah.'
|
'total_kelola' => 'Jumlah kelola + daur ulang tidak boleh melebihi total sampah.'
|
||||||
])->withInput();
|
])->withInput();
|
||||||
}
|
}
|
||||||
|
|
||||||
$sisa_sampah = $request->total_sampah - ($request->total_kelola + $request->total_daur_ulang);
|
$sisa_sampah = $total_sampah - ($total_kelola + $total_daur_ulang);
|
||||||
|
|
||||||
Sampah::create([
|
Sampah::create([
|
||||||
'user_id' => Auth::id(),
|
'user_id' => Auth::id(),
|
||||||
'tahun' => $request->tahun,
|
'tahun' => $request->tahun,
|
||||||
'bulan' => $request->bulan,
|
'bulan' => $request->bulan,
|
||||||
'total_sampah' => $request->total_sampah,
|
'total_sampah' => $total_sampah,
|
||||||
'total_kelola' => $request->total_kelola,
|
'total_kelola' => $total_kelola,
|
||||||
'total_daur_ulang' => $request->total_daur_ulang,
|
'total_daur_ulang' => $total_daur_ulang,
|
||||||
'sisa_sampah' => $sisa_sampah,
|
'sisa_sampah' => $sisa_sampah,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
@ -102,39 +142,52 @@ public function update(Request $request, $id)
|
||||||
{
|
{
|
||||||
$sampah = Sampah::findOrFail($id);
|
$sampah = Sampah::findOrFail($id);
|
||||||
|
|
||||||
$request->merge([
|
// VALIDASI DULU
|
||||||
'total_sampah' => $this->convertToDecimal($request->total_sampah),
|
$validator = Validator::make(
|
||||||
'total_kelola' => $this->convertToDecimal($request->total_kelola),
|
$request->all(),
|
||||||
'total_daur_ulang' => $this->convertToDecimal($request->total_daur_ulang),
|
$this->rules(),
|
||||||
]);
|
$this->messages(),
|
||||||
|
$this->attributes()
|
||||||
$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',
|
|
||||||
]);
|
|
||||||
|
|
||||||
if ($validator->fails()) {
|
if ($validator->fails()) {
|
||||||
return back()->withErrors($validator)->withInput();
|
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([
|
return back()->withErrors([
|
||||||
'total_kelola' => 'Jumlah kelola + daur ulang tidak boleh melebihi total sampah.'
|
'total_kelola' => 'Jumlah kelola + daur ulang tidak boleh melebihi total sampah.'
|
||||||
])->withInput();
|
])->withInput();
|
||||||
}
|
}
|
||||||
|
|
||||||
$sisa_sampah = $request->total_sampah - ($request->total_kelola + $request->total_daur_ulang);
|
$sisa_sampah = $total_sampah - ($total_kelola + $total_daur_ulang);
|
||||||
|
|
||||||
$sampah->update([
|
$sampah->update([
|
||||||
'user_id' => Auth::id(),
|
'user_id' => Auth::id(),
|
||||||
'tahun' => $request->tahun,
|
'tahun' => $request->tahun,
|
||||||
'bulan' => $request->bulan,
|
'bulan' => $request->bulan,
|
||||||
'total_sampah' => $request->total_sampah,
|
'total_sampah' => $total_sampah,
|
||||||
'total_kelola' => $request->total_kelola,
|
'total_kelola' => $total_kelola,
|
||||||
'total_daur_ulang' => $request->total_daur_ulang,
|
'total_daur_ulang' => $total_daur_ulang,
|
||||||
'sisa_sampah' => $sisa_sampah,
|
'sisa_sampah' => $sisa_sampah,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
@ -153,10 +206,10 @@ public function destroy($id)
|
||||||
|
|
||||||
private function convertToDecimal($value)
|
private function convertToDecimal($value)
|
||||||
{
|
{
|
||||||
if (!$value) {
|
if ($value === null || $value === '') {
|
||||||
return 0;
|
return null; // ✅ biar required bisa jalan
|
||||||
}
|
}
|
||||||
|
|
||||||
return str_replace(',', '.', str_replace('.', '', $value));
|
return floatval(str_replace(',', '.', str_replace('.', '', $value)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -104,7 +104,7 @@
|
||||||
<li class="nav-item {{ request()->routeIs('admin.kategori.index') ? 'active' : '' }}">
|
<li class="nav-item {{ request()->routeIs('admin.kategori.index') ? 'active' : '' }}">
|
||||||
<a class="nav-link" href="{{ route('admin.kategori.index') }}">
|
<a class="nav-link" href="{{ route('admin.kategori.index') }}">
|
||||||
<i class="icon-menu menu-icon"></i>
|
<i class="icon-menu menu-icon"></i>
|
||||||
<span class="menu-title">Kategori TPS</span>
|
<span class="menu-title">Kelola Kategori TPS</span>
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
<li class="nav-item {{ request()->routeIs('admin.sampah.index') ? 'active' : '' }}">
|
<li class="nav-item {{ request()->routeIs('admin.sampah.index') ? 'active' : '' }}">
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue