174 lines
6.0 KiB
PHP
174 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
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;
|
|
|
|
class SampahController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$title = 'Data Sampah';
|
|
|
|
$sampah = Sampah::with('user')
|
|
->orderBy('tahun', 'desc')
|
|
->get();
|
|
|
|
return view('admin.sampah.index', compact('title', 'sampah'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$title = 'Tambah Data Sampah';
|
|
$users = User::all();
|
|
|
|
return view('admin.sampah.create', compact('title', 'users'));
|
|
}
|
|
|
|
/* ======================================================
|
|
STORE (TAMBAH DATA)
|
|
====================================================== */
|
|
public function store(Request $request)
|
|
{
|
|
$validator = Validator::make(
|
|
$request->all(),
|
|
[
|
|
'tahun' => 'required|digits:4',
|
|
'total_sampah' => 'required|numeric|min:0',
|
|
'total_kelola' => 'required|numeric|min:0',
|
|
'total_daur_ulang' => 'required|numeric|min:0',
|
|
],
|
|
[
|
|
'tahun.required' => 'Tahun wajib diisi.',
|
|
'tahun.digits' => 'Tahun harus 4 digit (contoh: 2023).',
|
|
|
|
'total_sampah.required' => 'Total sampah wajib diisi.',
|
|
'total_sampah.numeric' => 'Total sampah harus berupa angka.',
|
|
'total_sampah.min' => 'Total sampah tidak boleh negatif.',
|
|
|
|
'total_kelola.required' => 'Total kelola wajib diisi.',
|
|
'total_kelola.numeric' => 'Total kelola harus berupa angka.',
|
|
'total_kelola.min' => 'Total kelola tidak boleh negatif.',
|
|
|
|
'total_daur_ulang.required' => 'Total daur ulang wajib diisi.',
|
|
'total_daur_ulang.numeric' => 'Total daur ulang harus berupa angka.',
|
|
'total_daur_ulang.min' => 'Total daur ulang tidak boleh negatif.',
|
|
]
|
|
);
|
|
|
|
if ($validator->fails()) {
|
|
return back()->withErrors($validator)->withInput();
|
|
}
|
|
|
|
// VALIDASI LOGIKA DATA
|
|
if (($request->total_kelola + $request->total_daur_ulang) > $request->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);
|
|
|
|
Sampah::create([
|
|
'user_id' => Auth::id(),
|
|
'tahun' => $request->tahun,
|
|
'total_sampah' => $request->total_sampah,
|
|
'total_kelola' => $request->total_kelola,
|
|
'total_daur_ulang' => $request->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);
|
|
$users = User::all();
|
|
|
|
return view('admin.sampah.edit', compact('title', 'sampah', 'users'));
|
|
}
|
|
|
|
/* ======================================================
|
|
UPDATE (EDIT DATA)
|
|
====================================================== */
|
|
public function update(Request $request, $id)
|
|
{
|
|
$sampah = Sampah::findOrFail($id);
|
|
|
|
$validator = Validator::make(
|
|
$request->all(),
|
|
[
|
|
'user_id' => 'required|exists:users,id',
|
|
'tahun' => 'required|digits:4',
|
|
'total_sampah' => 'required|numeric|min:0',
|
|
'total_kelola' => 'required|numeric|min:0',
|
|
'total_daur_ulang' => 'required|numeric|min:0',
|
|
],
|
|
[
|
|
'user_id.required' => 'Pengguna wajib dipilih.',
|
|
'user_id.exists' => 'Pengguna tidak valid.',
|
|
|
|
'tahun.required' => 'Tahun wajib diisi.',
|
|
'tahun.digits' => 'Tahun harus 4 digit.',
|
|
|
|
'total_sampah.required' => 'Total sampah wajib diisi.',
|
|
'total_sampah.numeric' => 'Total sampah harus berupa angka.',
|
|
|
|
'total_kelola.required' => 'Total kelola wajib diisi.',
|
|
'total_kelola.numeric' => 'Total kelola harus berupa angka.',
|
|
|
|
'total_daur_ulang.required' => 'Total daur ulang wajib diisi.',
|
|
'total_daur_ulang.numeric' => 'Total daur ulang harus berupa angka.',
|
|
]
|
|
);
|
|
|
|
if ($validator->fails()) {
|
|
return back()->withErrors($validator)->withInput();
|
|
}
|
|
|
|
// VALIDASI LOGIKA DATA
|
|
if (($request->total_kelola + $request->total_daur_ulang) > $request->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);
|
|
|
|
$sampah->update([
|
|
'user_id' => $request->user_id,
|
|
'tahun' => $request->tahun,
|
|
'total_sampah' => $request->total_sampah,
|
|
'total_kelola' => $request->total_kelola,
|
|
'total_daur_ulang' => $request->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.');
|
|
}
|
|
}
|