83 lines
2.6 KiB
PHP
83 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\BukuTamu;
|
|
use App\Models\Anggota;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class BukuTamuController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
return view('buku_tamu.index');
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
if ($request->tipe === 'member') {
|
|
// Jalur member: lookup dari anggotas
|
|
$request->validate([
|
|
'no_anggota' => 'required',
|
|
'keperluan' => 'required',
|
|
]);
|
|
|
|
$anggota = Anggota::where('no_ktp', $request->no_anggota)->orWhere('no_identitas', $request->no_anggota)->first();
|
|
|
|
if (!$anggota) {
|
|
return back()->withErrors(['no_anggota' => 'Nomor KTP / NIK tidak ditemukan dalam sistem kami.'])->withInput();
|
|
}
|
|
|
|
// Cari user terkait untuk id_user
|
|
$user = DB::table('users')->where('name', $anggota->nama)->first();
|
|
|
|
BukuTamu::create([
|
|
'id_user' => $user?->id,
|
|
'tujuan_kunjungan' => $request->keperluan,
|
|
'tanggal_kunjungan' => now()->toDateString(),
|
|
]);
|
|
|
|
return redirect()->route('home')->with([
|
|
'status' => 'Halo ' . $anggota->nama . ', berhasil check-in!',
|
|
'show_popup' => true,
|
|
]);
|
|
|
|
} else {
|
|
// Jalur tamu: isi manual
|
|
$request->validate([
|
|
'nama_tamu' => 'required|string|max:255',
|
|
'email' => 'required|email|max:255',
|
|
'no_hp' => 'required|digits_between:10,13',
|
|
'asal_instansi' => 'required|string|max:255',
|
|
'status' => 'required|string|max:255',
|
|
'keperluan' => 'required',
|
|
]);
|
|
|
|
BukuTamu::create([
|
|
'nama_tamu' => $request->nama_tamu,
|
|
'email' => $request->email,
|
|
'no_hp' => $request->no_hp,
|
|
'status' => $request->status,
|
|
'asal_instansi' => $request->asal_instansi,
|
|
'tujuan_kunjungan' => $request->keperluan,
|
|
'tanggal_kunjungan' => now()->toDateString(),
|
|
]);
|
|
|
|
return redirect()->route('home')->with([
|
|
'status' => 'Halo ' . $request->nama_tamu . ', berhasil check-in!',
|
|
'show_popup' => true,
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function adminList()
|
|
{
|
|
$bukuTamu = BukuTamu::with('user')
|
|
->orderBy('tanggal_kunjungan', 'desc')
|
|
->paginate(10);
|
|
|
|
return view('buku_tamu.admin', compact('bukuTamu'));
|
|
}
|
|
}
|