INI DIAAAAAAAAAAAAA, RESCHEDULE

This commit is contained in:
Stephen Gesityan 2025-05-15 07:28:17 +07:00
parent 39f984a3e5
commit 19423cac45
2 changed files with 205 additions and 194 deletions

View File

@ -490,16 +490,11 @@ public function showReschedule($id)
return redirect()->route('booking.history')->with('error', 'Anda tidak memiliki akses ke booking ini.');
}
// Check if booking is upcoming and paid
// Check if booking is upcoming
if ($booking->start_time <= now() || $booking->status !== 'paid') {
return redirect()->route('booking.history')->with('error', 'Booking ini tidak dapat di-reschedule.');
}
// Check if already rescheduled
if ($booking->has_rescheduled) {
return redirect()->route('booking.history')->with('error', 'Booking ini sudah pernah di-reschedule sebelumnya.');
}
// Check if it's within the time limit (at least 1 hour before start)
$rescheduleDeadline = Carbon::parse($booking->start_time)->subHour();
if (now() > $rescheduleDeadline) {
@ -528,11 +523,10 @@ public function processReschedule(Request $request, $id)
$booking = Booking::findOrFail($id);
// Perform the same validation as in showReschedule
// Perform validation
if ($booking->user_id !== auth()->id() ||
$booking->start_time <= now() ||
$booking->status !== 'paid' ||
$booking->has_rescheduled ||
now() > Carbon::parse($booking->start_time)->subHour()) {
return response()->json([
'success' => false,
@ -540,7 +534,7 @@ public function processReschedule(Request $request, $id)
], 422);
}
// Check if the selected time is available (except for this booking)
// Check if the selected time is available (exclude current booking when checking conflicts)
$existingBookings = Booking::where('table_id', $request->table_id)
->where('id', '!=', $booking->id)
->where('status', 'paid')
@ -558,19 +552,10 @@ public function processReschedule(Request $request, $id)
], 422);
}
// Store original booking details
$originalStartTime = $booking->start_time;
$originalEndTime = $booking->end_time;
$originalTableId = $booking->table_id;
// Update the booking
$booking->original_start_time = $originalStartTime;
$booking->original_end_time = $originalEndTime;
$booking->original_table_id = $originalTableId;
// Update the booking with new schedule
$booking->start_time = $request->start_time;
$booking->end_time = $request->end_time;
$booking->table_id = $request->table_id;
$booking->has_rescheduled = true;
$booking->save();
return response()->json([

View File

@ -26,7 +26,6 @@
<p class="font-medium">{{ $duration }} Jam</p>
</div>
</div>
<div class="bg-yellow-50 border border-yellow-200 rounded-lg p-4 mb-6">
<div class="flex items-start">
<div class="mr-3 text-yellow-500">
@ -35,9 +34,9 @@
<div>
<h3 class="font-semibold text-yellow-700">Perhatian</h3>
<p class="text-yellow-700 text-sm">
Reschedule hanya dapat dilakukan 1x untuk setiap booking<br>
Batas waktu reschedule adalah 1 jam sebelum jadwal booking<br>
Durasi booking akan tetap sama ({{ $duration }} jam)
Reschedule dapat dilakukan selama minimal 1 jam sebelum jadwal booking<br>
Durasi booking akan tetap sama ({{ $duration }} jam)<br>
Setelah reschedule, jadwal lama akan digantikan dengan jadwal baru
</p>
</div>
</div>
@ -102,7 +101,6 @@ class="text-white px-4 py-2 rounded-lg">
</div>
</div>
</div>
<script>
document.addEventListener('alpine:init', () => {
Alpine.data('rescheduleForm', () => ({
@ -110,6 +108,8 @@ class="text-white px-4 py-2 rounded-lg">
bookingId: {{ $booking->id }},
bookingDuration: {{ $duration }},
originalTableId: {{ $booking->table_id }},
originalStartTime: "{{ \Carbon\Carbon::parse($booking->start_time)->format('H:i') }}",
originalDate: "{{ \Carbon\Carbon::parse($booking->start_time)->format('Y-m-d') }}",
selectedDate: '',
selectedTableId: '',
selectedStartHour: null,
@ -124,9 +124,9 @@ class="text-white px-4 py-2 rounded-lg">
const month = String(today.getMonth() + 1).padStart(2, '0');
const day = String(today.getDate()).padStart(2, '0');
this.today = `${year}-${month}-${day}`;
this.selectedDate = this.today;
// Set original table as default
// Set original date and table as default
this.selectedDate = this.originalDate;
this.selectedTableId = this.originalTableId;
// Load schedules for today and selected table
@ -134,7 +134,13 @@ class="text-white px-4 py-2 rounded-lg">
},
get canSubmit() {
return this.selectedDate && this.selectedTableId && this.selectedStartHour !== null;
return this.selectedDate &&
this.selectedTableId &&
this.selectedStartHour !== null &&
// Prevent submitting if nothing changed
(this.selectedDate !== this.originalDate ||
this.selectedTableId != this.originalTableId ||
this.selectedStartHour !== this.originalStartTime.split(':')[0]);
},
get formattedSchedule() {
@ -161,9 +167,20 @@ class="text-white px-4 py-2 rounded-lg">
try {
const response = await fetch(`/booking/reschedule/check-availability?table_id=${this.selectedTableId}&date=${this.selectedDate}&booking_id=${this.bookingId}`);
if (!response.ok) {
throw new Error('Network response was not ok');
}
this.bookedSchedules = await response.json();
// If today is the original booking date and table is the original table,
// pre-select the original start hour
if (this.selectedDate === this.originalDate &&
parseInt(this.selectedTableId) === parseInt(this.originalTableId)) {
this.selectedStartHour = this.originalStartTime.split(':')[0];
}
} catch (error) {
console.error('Error checking booked schedules:', error);
alert('Terjadi kesalahan saat memeriksa jadwal. Silakan coba lagi.');
}
},
@ -174,6 +191,15 @@ class="text-white px-4 py-2 rounded-lg">
// Check if slot end time exceeds midnight
if (endHourInt > 24) return false;
// Check if this is the original booking's time slot (should be allowed)
const isOriginalTimeSlot = this.selectedDate === this.originalDate &&
parseInt(this.selectedTableId) === parseInt(this.originalTableId) &&
hour === this.originalStartTime.split(':')[0];
if (isOriginalTimeSlot) {
return true;
}
// Check if any existing booking overlaps with this slot
return !this.bookedSchedules.some(schedule => {
const scheduleStart = parseInt(schedule.start.split(':')[0]);