Admin Direct Booking
This commit is contained in:
parent
3a02bf8c96
commit
171c04b248
|
@ -22,6 +22,84 @@ public function __construct(MidtransService $midtransService)
|
||||||
$this->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) {
|
public function createPaymentIntent(Request $request) {
|
||||||
try {
|
try {
|
||||||
$request->validate([
|
$request->validate([
|
||||||
|
@ -30,6 +108,13 @@ public function createPaymentIntent(Request $request) {
|
||||||
'end_time' => 'required|date|after:start_time',
|
'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)
|
// Cek apakah meja sedang dibooking pada waktu tersebut (hanya yang sudah paid)
|
||||||
$conflict = Booking::where('table_id', $request->table_id)
|
$conflict = Booking::where('table_id', $request->table_id)
|
||||||
->where(function($query) use ($request) {
|
->where(function($query) use ($request) {
|
||||||
|
|
|
@ -45,6 +45,9 @@
|
||||||
|
|
||||||
// Routes that require both authentication and email verification
|
// Routes that require both authentication and email verification
|
||||||
Route::middleware(['auth', 'verified'])->group(function () {
|
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
|
// Booking history routes
|
||||||
Route::get('/booking/history', [BookingHistoryController::class, 'index'])->name('booking.history');
|
Route::get('/booking/history', [BookingHistoryController::class, 'index'])->name('booking.history');
|
||||||
|
|
||||||
|
@ -57,6 +60,7 @@
|
||||||
Route::middleware(['password.confirm'])->group(function () {
|
Route::middleware(['password.confirm'])->group(function () {
|
||||||
// Any sensitive operations that should still require password confirmation can go here
|
// Any sensitive operations that should still require password confirmation can go here
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Admin routes (admin tetap perlu verified untuk keamanan)
|
// Admin routes (admin tetap perlu verified untuk keamanan)
|
||||||
|
|
Loading…
Reference in New Issue