| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Services; |
| 6 |
|
| 7 |
use Yatra\Repositories\AvailabilityRepository; |
| 8 |
use Yatra\Repositories\BookingRepository; |
| 9 |
|
| 10 |
/** |
| 11 |
* Promotes waitlist bookings to pending/confirmed when seats become available. |
| 12 |
*/ |
| 13 |
final class WaitlistPromotionService |
| 14 |
{ |
| 15 |
/** @var int */ |
| 16 |
private static $promotionDepth = 0; |
| 17 |
|
| 18 |
private const MAX_PROMOTION_CHAIN = 25; |
| 19 |
|
| 20 |
/** |
| 21 |
* Called after availability inventory sync. Promotes one waiter per invocation; |
| 22 |
* nested inventory sync continues the chain until seats or waiters run out. |
| 23 |
*/ |
| 24 |
public static function processAvailableSlots(int $tripId, int $availabilityId): void |
| 25 |
{ |
| 26 |
if (self::$promotionDepth >= self::MAX_PROMOTION_CHAIN) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
if ($tripId <= 0 || $availabilityId <= 0) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
self::$promotionDepth++; |
| 35 |
try { |
| 36 |
$availRepo = new AvailabilityRepository(); |
| 37 |
$availability = $availRepo->find($availabilityId); |
| 38 |
if (!$availability) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
$seatsFree = (int) ($availability->seats_available ?? 0); |
| 43 |
if ($seatsFree < 1) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
$bookingRepo = new BookingRepository(); |
| 48 |
$waiters = $bookingRepo->findWaitlistBookingsForAvailability($availabilityId, 1); |
| 49 |
if ($waiters === []) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
$booking = $waiters[0]; |
| 54 |
$bookingId = (int) ($booking->id ?? 0); |
| 55 |
$need = max(1, (int) ($booking->travelers_count ?? 1)); |
| 56 |
|
| 57 |
if ($need > $seatsFree) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
if (!apply_filters('yatra_waitlist_before_promote_booking', true, $bookingId, $tripId, $availabilityId)) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
$auto = SettingsService::isEnabled('waitlist_auto_confirm'); |
| 66 |
$newStatus = $auto ? 'confirmed' : 'pending'; |
| 67 |
|
| 68 |
$availRepo->incrementSeatsWaitlist($availabilityId, -$need); |
| 69 |
|
| 70 |
$update = [ |
| 71 |
'status' => $newStatus, |
| 72 |
]; |
| 73 |
if ($newStatus === 'confirmed') { |
| 74 |
$update['confirmed_at'] = current_time('mysql'); |
| 75 |
} |
| 76 |
|
| 77 |
$bookingRepo->update($bookingId, $update); |
| 78 |
|
| 79 |
do_action('yatra_booking_status_changed', $bookingId, 'waitlist', $newStatus); |
| 80 |
do_action('yatra_waitlist_booking_promoted', $bookingId, $tripId, $availabilityId, $newStatus); |
| 81 |
|
| 82 |
if ($newStatus === 'confirmed') { |
| 83 |
\yatra_trigger_booking_confirmed($bookingId, 'waitlist'); |
| 84 |
} |
| 85 |
|
| 86 |
if (SettingsService::isEnabled('booking_confirmation')) { |
| 87 |
$bs = new BookingService(); |
| 88 |
$bs->sendNewBookingTransactionalConfirmation($bookingId); |
| 89 |
} |
| 90 |
} finally { |
| 91 |
self::$promotionDepth--; |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|