| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Hooks\Scheduler; |
| 4 |
|
| 5 |
use FluentBooking\App\Models\Booking; |
| 6 |
use FluentBooking\App\Models\Calendar; |
| 7 |
use FluentBooking\App\Models\Meta; |
| 8 |
use FluentBooking\App\Services\Helper; |
| 9 |
|
| 10 |
class FiveMinuteScheduler |
| 11 |
{ |
| 12 |
public function register() |
| 13 |
{ |
| 14 |
add_action('fluent_booking_five_minutes_tasks', [$this, 'handle']); |
| 15 |
} |
| 16 |
|
| 17 |
public function handle() |
| 18 |
{ |
| 19 |
$this->maybeAutoCancelBooking(); |
| 20 |
$this->maybeAutoCompleteBookings(); |
| 21 |
$this->maybeAutoCancelPastBookings(); |
| 22 |
$this->maybeAutoDeleteReservations(); |
| 23 |
$this->maybeAutoExpireCalendarEvents(); |
| 24 |
$this->maybeAutoExpireCalendars(); |
| 25 |
} |
| 26 |
|
| 27 |
private function maybeAutoCompleteBookings() |
| 28 |
{ |
| 29 |
$autoCompleteTimeOut = (int)Helper::getGlobalAdminSetting('auto_complete_timing', 60) * 60; // 10 minutes |
| 30 |
|
| 31 |
$bookings = Booking::with('calendar_event') |
| 32 |
->where('status', 'scheduled') |
| 33 |
->where('end_time', '<', gmdate('Y-m-d H:i:s', time() - $autoCompleteTimeOut)) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 34 |
->limit(500) |
| 35 |
->get(); |
| 36 |
|
| 37 |
if ($bookings->isEmpty()) { |
| 38 |
return true; |
| 39 |
} |
| 40 |
|
| 41 |
Booking::whereIn('id', $bookings->pluck('id')) |
| 42 |
->update(['status' => 'completed']); |
| 43 |
|
| 44 |
foreach ($bookings as $booking) { |
| 45 |
$booking->status = 'completed'; |
| 46 |
do_action('fluent_booking/booking_schedule_completed', $booking, $booking->calendar_event); |
| 47 |
} |
| 48 |
|
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
private function maybeAutoCancelPastBookings() |
| 53 |
{ |
| 54 |
$autoCompleteTimeOut = (int)Helper::getGlobalAdminSetting('auto_complete_timing', 60) * 60; // 10 minutes |
| 55 |
|
| 56 |
$bookings = Booking::with('calendar_event') |
| 57 |
->where('status', 'pending') |
| 58 |
->where('end_time', '<', gmdate('Y-m-d H:i:s', time() - $autoCompleteTimeOut)) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 59 |
->limit(500) |
| 60 |
->get(); |
| 61 |
|
| 62 |
if ($bookings->isEmpty()) { |
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
Booking::whereIn('id', $bookings->pluck('id')) |
| 67 |
->update([ |
| 68 |
'status' => 'cancelled', |
| 69 |
'updated_at' => gmdate('Y-m-d H:i:s'), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 70 |
]); |
| 71 |
|
| 72 |
foreach ($bookings as $booking) { |
| 73 |
$booking->status = 'cancelled'; |
| 74 |
do_action('fluent_booking/booking_schedule_auto_cancelled', $booking, $booking->calendar_event); |
| 75 |
} |
| 76 |
|
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
private function maybeAutoCancelBooking() |
| 81 |
{ |
| 82 |
$autoCancelTimeOut = (int)Helper::getGlobalAdminSetting('auto_cancel_timing', 10) * 60; // 10 minutes |
| 83 |
|
| 84 |
$bookings = Booking::query() |
| 85 |
->with('calendar_event') |
| 86 |
->where('created_at', '<=', gmdate('Y-m-d H:i:s', time() - $autoCancelTimeOut)) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 87 |
->where('status', 'pending') |
| 88 |
->where('payment_status', 'pending') |
| 89 |
->where('payment_method', '!=', 'offline') |
| 90 |
->limit(500) |
| 91 |
->get(); |
| 92 |
|
| 93 |
if ($bookings->isEmpty()) { |
| 94 |
return true; |
| 95 |
} |
| 96 |
|
| 97 |
Booking::whereIn('id', $bookings->pluck('id')) |
| 98 |
->update([ |
| 99 |
'status' => 'cancelled', |
| 100 |
'updated_at' => gmdate('Y-m-d H:i:s'), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 101 |
]); |
| 102 |
|
| 103 |
foreach ($bookings as $booking) { |
| 104 |
$booking->status = 'cancelled'; |
| 105 |
do_action('fluent_booking/booking_schedule_auto_cancelled', $booking, $booking->calendar_event); |
| 106 |
} |
| 107 |
|
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
private function maybeAutoExpireCalendars() |
| 112 |
{ |
| 113 |
$calendarIds = Calendar::query() |
| 114 |
->where('type', 'event') |
| 115 |
->where('status', 'active') |
| 116 |
->whereDoesntHave('events', function ($query) { |
| 117 |
$query->whereIn('status', ['active', 'draft']); |
| 118 |
}) |
| 119 |
->limit(500) |
| 120 |
->pluck('id'); |
| 121 |
|
| 122 |
if ($calendarIds->isEmpty()) { |
| 123 |
return true; |
| 124 |
} |
| 125 |
|
| 126 |
Calendar::whereIn('id', $calendarIds) |
| 127 |
->where('type', 'event') |
| 128 |
->where('status', 'active') |
| 129 |
->whereDoesntHave('events', function ($query) { |
| 130 |
$query->whereIn('status', ['active', 'draft']); |
| 131 |
}) |
| 132 |
->update(['status' => 'expired']); |
| 133 |
|
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
private function maybeAutoExpireCalendarEvents() |
| 138 |
{ |
| 139 |
Meta::query() |
| 140 |
->where('object_type', 'calendar_event') |
| 141 |
->where('key', 'expire_time') |
| 142 |
->where('value', '<=', gmdate('Y-m-d H:i:s', time())) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 143 |
->whereHas('calendar_event', function ($query) { |
| 144 |
$query->where('status', 'active'); |
| 145 |
}) |
| 146 |
->with(['calendar_event' => function ($query) { |
| 147 |
$query->where('status', 'active'); |
| 148 |
}]) |
| 149 |
->limit(500) |
| 150 |
->get() |
| 151 |
->each(function ($meta) { |
| 152 |
$meta->calendar_event->update(['status' => 'expired']); |
| 153 |
}); |
| 154 |
|
| 155 |
return true; |
| 156 |
} |
| 157 |
|
| 158 |
private function maybeAutoDeleteReservations() |
| 159 |
{ |
| 160 |
$reservationIds = Booking::query() |
| 161 |
->whereIn('event_type', ['single_event', 'group_event']) |
| 162 |
->where('status', 'reserved') |
| 163 |
->where('start_time', '<=', gmdate('Y-m-d H:i:s', time())) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 164 |
->limit(500) |
| 165 |
->pluck('id'); |
| 166 |
|
| 167 |
if ($reservationIds->isEmpty()) { |
| 168 |
return true; |
| 169 |
} |
| 170 |
|
| 171 |
Booking::whereIn('id', $reservationIds) |
| 172 |
->whereIn('event_type', ['single_event', 'group_event']) |
| 173 |
->where('status', 'reserved') |
| 174 |
->where('start_time', '<=', gmdate('Y-m-d H:i:s', time())) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 175 |
->delete(); |
| 176 |
|
| 177 |
return true; |
| 178 |
} |
| 179 |
} |
| 180 |
|