whereIn('status', ['Dipinjam', 'Terlambat']) ->get(); $groupedLoans = $loans->groupBy('user_id'); $peminjamanAktif = $groupedLoans->map(function ($userLoans, $userId) { $user = $userLoans->first()->user; $firstLoan = $userLoans->first(); return [ 'id_peminjaman' => 'PIN-ADM-'.sprintf('%03d', $userId), 'user_id' => $userId, 'peminjam' => $user->nama_lengkap ?? 'Unknown', 'email' => $user->email, 'nomor_hp' => $user->phone ?? '-', 'tanggal_pinjam' => $firstLoan->borrowed_at, 'tenggat_kembali' => $firstLoan->due_at, 'status' => $firstLoan->status, 'role' => $user->role, 'books' => $userLoans->map(fn ($l) => [ 'id' => $l->book->id, 'judul' => $l->book->judul, 'cover' => $l->book->cover, ])->toArray(), ]; })->values(); $daftarPeminjam = $peminjamanAktif->pluck('peminjam')->unique(); return view('admin.peminjaman.index', [ 'pageTitle' => 'Manajemen Peminjaman', 'peminjamanAktif' => $peminjamanAktif, 'daftarPeminjam' => $daftarPeminjam, ]); } public function create() { $users = User::whereIn('role', ['siswa', 'guru'])->get(); $groupedUsers = $users->map(function ($user) { $jumlahPinjam = Loan::where('user_id', $user->id) ->whereIn('status', ['Dipinjam', 'Terlambat']) ->count(); $user->jumlah_pinjam = $jumlahPinjam; $user->kena_limit = $jumlahPinjam >= 2; $user->disabled = $user->kena_limit || $user->is_banned; if ($user->is_banned) { $user->status_text = '(Akun Dibekukan)'; } elseif ($user->kena_limit) { $user->status_text = '(Limit Penuh: 2/2)'; } else { $user->status_text = ''; } return $user; })->groupBy('role'); $daftarBuku = Book::where('status', 'Tersedia') ->whereJsonContains('tipe_akses', 'offline') ->where('stok', '>', 0) ->get(); return view('admin.peminjaman.create', [ 'pageTitle' => 'Buat Peminjaman Manual', 'groupedUsers' => $groupedUsers, 'daftarBuku' => $daftarBuku, ]); } public function store(Request $request) { $validated = $request->validate([ 'peminjam_id' => 'required|exists:users,id', 'tanggal_pinjam' => 'required|date', 'tanggal_kembali' => 'required|date|after:tanggal_pinjam', 'buku_ids' => 'required|array|min:1|max:2', 'buku_ids.*' => 'exists:books,id', ]); \DB::beginTransaction(); try { foreach ($validated['buku_ids'] as $bookId) { $book = Book::findOrFail($bookId); // Check if book is available if ($book->status !== 'Tersedia') { throw new \Exception("Buku '{$book->judul}' tidak tersedia."); } // Check stock if ($book->stok <= 0) { throw new \Exception("Buku '{$book->judul}' stok habis."); } // Create loan record Loan::create([ 'user_id' => $validated['peminjam_id'], 'book_id' => $bookId, 'loan_code' => 'LOAN-'.date('Ymd').'-'.strtoupper(substr(md5(uniqid()), 0, 6)), 'borrowed_at' => $validated['tanggal_pinjam'], 'due_at' => $validated['tanggal_kembali'], 'status' => 'Dipinjam', ]); // Update book status and decrement stock $book->update([ 'status' => 'Dipinjam', 'stok' => $book->stok - 1, ]); } \DB::commit(); return redirect()->route('admin.peminjaman.index')->with('success', 'Peminjaman berhasil dibuat.'); } catch (\Exception $e) { \DB::rollBack(); return back()->withErrors(['error' => $e->getMessage()])->withInput(); } } public function export(Request $request) { $request->validate([ 'bulan_laporan' => 'nullable|date', ]); $query = Loan::with(['user', 'book'])->orderBy('borrowed_at', 'asc'); if ($request->filled('bulan_laporan')) { $date = Carbon::parse($request->bulan_laporan); $query->whereMonth('borrowed_at', $date->month) ->whereYear('borrowed_at', $date->year); $fileName = 'Laporan_Peminjaman_'.$date->format('Y-m').'.xls'; // Changed to .xls } else { $fileName = 'Laporan_Peminjaman_Semua.xls'; // Changed to .xls } $loans = $query->get(); $headers = [ "Content-Type" => "application/vnd.ms-excel", "Content-Disposition" => "attachment; filename=$fileName", "Pragma" => "no-cache", "Cache-Control" => "must-revalidate, post-check=0, pre-check=0", "Expires" => "0" ]; $columns = ['NO', 'ID PEMINJAMAN', 'PEMINJAM', 'ROLE', 'JUDUL BUKU', 'TGL PINJAM', 'TENGGAT KEMBALI', 'STATUS', 'DENDA KETERLAMBATAN']; $callback = function() use($loans, $columns) { $file = fopen('php://output', 'w'); // Generate HTML table for Excel echo '
| $col | "; echo '||||||||
|---|---|---|---|---|---|---|---|---|
| " . $i++ . " | "; echo "" . ($loan->loan_code ?? '-') . " | "; echo "" . ($loan->user->nama_lengkap ?? 'Unknown') . " | "; echo "" . ($loan->user->role ?? '-') . " | "; echo "" . ($loan->book->judul ?? 'Unknown') . " | "; echo "" . $loan->borrowed_at->format('d/m/Y') . " | "; echo "" . ($loan->due_at ? $loan->due_at->format('d/m/Y') : '-') . " | "; echo "" . ($loan->status ?? '-') . " | "; echo "" . ($loan->fine_overdue ?? 0) . " | "; echo '