Add controllers for Anggota, Buku, Kategori, Peminjaman, and Dashboard with CRUD operations
This commit is contained in:
88
app/Http/Controllers/PeminjamanController.php
Normal file
88
app/Http/Controllers/PeminjamanController.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Peminjaman;
|
||||
use App\Models\Anggota;
|
||||
use App\Models\Buku;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
|
||||
class PeminjamanController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$peminjamans = Peminjaman::with(['anggota', 'buku'])
|
||||
->latest()
|
||||
->paginate(10);
|
||||
|
||||
return Inertia::render('Peminjaman/Index', [
|
||||
'peminjamans' => $peminjamans,
|
||||
]);
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
return Inertia::render('Peminjaman/Create', [
|
||||
'anggotas' => Anggota::where('status', 'aktif')->get(),
|
||||
'bukus' => Buku::where('stok_tersedia', '>', 0)->get(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'anggota_id' => 'required|exists:anggotas,id',
|
||||
'buku_id' => 'required|exists:bukus,id',
|
||||
'tanggal_pinjam' => 'required|date',
|
||||
'tanggal_kembali_rencana' => 'required|date|after:tanggal_pinjam',
|
||||
'catatan' => 'nullable|string',
|
||||
]);
|
||||
|
||||
$buku = Buku::findOrFail($validated['buku_id']);
|
||||
|
||||
if ($buku->stok_tersedia <= 0) {
|
||||
return back()->withErrors(['buku_id' => 'Stok buku tidak tersedia.']);
|
||||
}
|
||||
|
||||
$buku->decrement('stok_tersedia');
|
||||
Peminjaman::create($validated);
|
||||
|
||||
return redirect()->route('peminjaman.index')->with('success', 'Peminjaman berhasil dicatat!');
|
||||
}
|
||||
|
||||
public function show(Peminjaman $peminjaman)
|
||||
{
|
||||
return Inertia::render('Peminjaman/Show', [
|
||||
'peminjaman' => $peminjaman->load('anggota', 'buku'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function kembalikan(Request $request, Peminjaman $peminjaman)
|
||||
{
|
||||
$tanggalKembali = $request->tanggal_kembali_aktual ?? now()->toDateString();
|
||||
$denda = 0;
|
||||
|
||||
if ($tanggalKembali > $peminjaman->tanggal_kembali_rencana->toDateString()) {
|
||||
$selisih = \Carbon\Carbon::parse($tanggalKembali)
|
||||
->diffInDays($peminjaman->tanggal_kembali_rencana);
|
||||
$denda = $selisih * 1000; // Rp 1.000 per hari
|
||||
}
|
||||
|
||||
$peminjaman->update([
|
||||
'tanggal_kembali_aktual' => $tanggalKembali,
|
||||
'status' => 'dikembalikan',
|
||||
'denda' => $denda,
|
||||
]);
|
||||
|
||||
$peminjaman->buku->increment('stok_tersedia');
|
||||
|
||||
return redirect()->route('peminjaman.index')->with('success', 'Buku berhasil dikembalikan!');
|
||||
}
|
||||
|
||||
public function destroy(Peminjaman $peminjaman)
|
||||
{
|
||||
$peminjaman->delete();
|
||||
return redirect()->route('peminjaman.index')->with('success', 'Data peminjaman dihapus!');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user