195 lines
6.9 KiB
PHP
195 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Book;
|
|
use App\Models\Loan;
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
|
|
class AdminPeminjamanController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$loans = Loan::with(['user', 'book'])
|
|
->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',
|
|
'nomor_hp' => $user->phone ?? '-',
|
|
'tanggal_pinjam' => $firstLoan->borrowed_at,
|
|
'tenggat_kembali' => $firstLoan->due_at,
|
|
'status' => $firstLoan->status,
|
|
'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 & Denda',
|
|
'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')
|
|
->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.");
|
|
}
|
|
|
|
// 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
|
|
$book->update(['status' => 'Dipinjam']);
|
|
}
|
|
|
|
\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 dendaIndex()
|
|
{
|
|
$now = Carbon::now();
|
|
|
|
// Fetch all loans that are overdue or users that are banned
|
|
$loans = Loan::with(['user', 'book'])
|
|
->where(function($query) use ($now) {
|
|
$query->where('due_at', '<', $now)
|
|
->whereIn('status', ['Dipinjam', 'Terlambat']);
|
|
})
|
|
->orWhereHas('user', function($query) {
|
|
$query->where('is_banned', true);
|
|
})
|
|
->get();
|
|
|
|
$groupedLoans = $loans->groupBy('user_id');
|
|
|
|
$siswaTelat = $groupedLoans->map(function ($userLoans, $userId) use ($now) {
|
|
$user = $userLoans->first()->user;
|
|
$firstLoan = $userLoans->first();
|
|
|
|
$tenggat = Carbon::parse($firstLoan->due_at);
|
|
$hariTelat = $now->greaterThan($tenggat) ? (int) $tenggat->diffInDays($now) : 0;
|
|
$totalDenda = $hariTelat * 1000;
|
|
|
|
// Link WA
|
|
$hp = $user->phone ?? '';
|
|
$waLink = '#';
|
|
if ($hp) {
|
|
if (substr($hp, 0, 1) == '0') $hp = '62' . substr($hp, 1);
|
|
$pesan = $hariTelat > 0
|
|
? "Halo {$user->nama_lengkap}, anda terlambat pengembalian buku. Total Denda: Rp " . number_format($totalDenda, 0, ',', '.')
|
|
: "Halo {$user->nama_lengkap}, akun anda sedang dinonaktifkan sementara. Mohon hubungi petugas.";
|
|
$waLink = "https://wa.me/{$hp}?text=" . urlencode($pesan);
|
|
}
|
|
|
|
return [
|
|
'id' => $firstLoan->id,
|
|
'peminjam' => $user->nama_lengkap,
|
|
'nomor_hp' => $user->phone ?? '-',
|
|
'kelas' => $user->kelas ?? 'Guru',
|
|
'hari_terlambat' => $hariTelat,
|
|
'total_denda' => $totalDenda,
|
|
'wa_link' => $waLink,
|
|
'is_banned' => $user->is_banned,
|
|
'tenggat_kembali' => $firstLoan->due_at,
|
|
'books' => $userLoans->map(fn($l) => [
|
|
'id' => $l->book->id,
|
|
'judul' => $l->book->judul,
|
|
])->toArray(),
|
|
];
|
|
})->values();
|
|
|
|
$listKelas = $siswaTelat->pluck('kelas')->unique()->values();
|
|
|
|
return view('admin.denda.index', [
|
|
'pageTitle' => 'Manajemen Denda & Sanksi',
|
|
'siswaTelat' => $siswaTelat,
|
|
'listKelas' => $listKelas
|
|
]);
|
|
}
|
|
|
|
public function berikanSanksi(Request $request)
|
|
{
|
|
// Actually implement banning logic here if needed
|
|
return response()->json(['status' => 'success']);
|
|
}
|
|
} |