From 171c04b248397c8291e9b5d0067a10969a6476b0 Mon Sep 17 00:00:00 2001 From: Stephen Gesityan Date: Mon, 12 May 2025 04:12:05 +0700 Subject: [PATCH] Admin Direct Booking --- .../Controllers/pages/BookingController.php | 85 +++++++++++++++++++ routes/web.php | 4 + 2 files changed, 89 insertions(+) diff --git a/app/Http/Controllers/pages/BookingController.php b/app/Http/Controllers/pages/BookingController.php index 91808a2..cff93b9 100644 --- a/app/Http/Controllers/pages/BookingController.php +++ b/app/Http/Controllers/pages/BookingController.php @@ -22,6 +22,84 @@ public function __construct(MidtransService $midtransService) $this->midtransService = $midtransService; } + // Tambahkan method baru untuk booking langsung oleh admin + public function adminDirectBooking(Request $request) { + try { + $request->validate([ + 'table_id' => 'required|exists:tables,id', + 'start_time' => 'required|date', + 'end_time' => 'required|date|after:start_time', + ]); + + $user = Auth::user(); + + // Validasi bahwa user adalah admin dan mengelola venue dari meja tersebut + $table = Table::findOrFail($request->table_id); + if ($user->role !== 'admin' || $user->venue_id !== $table->venue_id) { + return response()->json([ + 'message' => 'Unauthorized action' + ], 403); + } + + // Cek konflik booking + $conflict = Booking::where('table_id', $request->table_id) + ->where(function($query) use ($request) { + $query->whereBetween('start_time', [$request->start_time, $request->end_time]) + ->orWhere(function($query) use ($request) { + $query->where('start_time', '<', $request->start_time) + ->where('end_time', '>', $request->start_time); + }); + }) + ->where('status', 'paid') + ->exists(); + + if ($conflict) { + return response()->json(['message' => 'Meja sudah dibooking di jam tersebut'], 409); + } + + // Hitung total biaya (meskipun admin tidak membayar, kita tetap catat nilainya) + $startTime = Carbon::parse($request->start_time); + $endTime = Carbon::parse($request->end_time); + $duration = $endTime->diffInHours($startTime); + $totalAmount = $duration * $table->price_per_hour; + + // Generate order ID unik untuk admin + $adminOrderId = 'ADMIN-' . $user->id . '-' . time(); + + // Buat booking langsung dengan status paid + $booking = Booking::create([ + 'table_id' => $request->table_id, + 'user_id' => $user->id, + 'start_time' => $request->start_time, + 'end_time' => $request->end_time, + 'status' => 'paid', // langsung set sebagai paid + 'total_amount' => $totalAmount, + 'payment_id' => null, // Admin tidak perlu payment_id + 'payment_method' => 'admin_direct', // Tandai sebagai booking langsung admin + 'order_id' => $adminOrderId, + ]); + + // Update table status menjadi Booked + $table->update(['status' => 'Booked']); + + return response()->json([ + 'message' => 'Booking created successfully', + 'booking_id' => $booking->id + ]); + + } catch (\Exception $e) { + \Log::error('Admin direct booking error:', [ + 'message' => $e->getMessage(), + 'trace' => $e->getTraceAsString() + ]); + + return response()->json([ + 'message' => 'Failed to create booking: ' . $e->getMessage() + ], 500); + } + } + + public function createPaymentIntent(Request $request) { try { $request->validate([ @@ -30,6 +108,13 @@ public function createPaymentIntent(Request $request) { 'end_time' => 'required|date|after:start_time', ]); + $user = Auth::user(); + $table = Table::findOrFail($request->table_id); + + if ($user->role === 'admin' && $user->venue_id === $table->venue_id) { + return $this->adminDirectBooking($request); + } + // Cek apakah meja sedang dibooking pada waktu tersebut (hanya yang sudah paid) $conflict = Booking::where('table_id', $request->table_id) ->where(function($query) use ($request) { diff --git a/routes/web.php b/routes/web.php index 4b9744c..5fd3768 100644 --- a/routes/web.php +++ b/routes/web.php @@ -45,6 +45,9 @@ // Routes that require both authentication and email verification Route::middleware(['auth', 'verified'])->group(function () { + // Admin direct booking route (hanya akan berfungsi untuk admin dari controller) + Route::post('/booking/admin-direct', [BookingController::class, 'adminDirectBooking'])->name('booking.admin-direct'); + // Booking history routes Route::get('/booking/history', [BookingHistoryController::class, 'index'])->name('booking.history'); @@ -57,6 +60,7 @@ Route::middleware(['password.confirm'])->group(function () { // Any sensitive operations that should still require password confirmation can go here }); + }); // Admin routes (admin tetap perlu verified untuk keamanan)