| @@ -27,16 +27,25 @@ | ||
| 27 | 27 | private function maybeAutoCompleteBookings() |
| 28 | 28 | { |
| 29 | 29 | $autoCompleteTimeOut = (int)Helper::getGlobalAdminSetting('auto_complete_timing', 60) * 60; // 10 minutes |
| 30 | 30 | |
| 31 | - $bookings = Booking::where('status', 'scheduled') | |
| 31 | + $bookings = Booking::with('calendar_event') | |
| 32 | + ->where('status', 'scheduled') | |
| 32 | 33 | ->where('end_time', '<', gmdate('Y-m-d H:i:s', time() - $autoCompleteTimeOut)) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 33 | 34 | ->limit(500) |
| 34 | 35 | ->get(); |
| 35 | 36 | |
| 37 | + if ($bookings->isEmpty()) { | |
| 38 | + return true; | |
| 39 | + } | |
| 40 | + | |
| 36 | 41 | foreach ($bookings as $booking) { |
| 42 | + // Flag each row just before its hook, so a run that dies mid-loop leaves the rest for the next run. | |
| 43 | + if (!Booking::where('id', $booking->id)->where('status', 'scheduled')->update(['status' => 'completed'])) { | |
| 44 | + continue; | |
| 45 | + } | |
| 46 | + | |
| 37 | 47 | $booking->status = 'completed'; |
| 38 | - $booking->save(); | |
| 39 | 48 | do_action('fluent_booking/booking_schedule_completed', $booking, $booking->calendar_event); |
| 40 | 49 | } |
| 41 | 50 | |
| 42 | 51 | return true; |
| @@ -45,16 +54,33 @@ | ||
| 45 | 54 | private function maybeAutoCancelPastBookings() |
| 46 | 55 | { |
| 47 | 56 | $autoCompleteTimeOut = (int)Helper::getGlobalAdminSetting('auto_complete_timing', 60) * 60; // 10 minutes |
| 48 | 57 | |
| 49 | - Booking::query() | |
| 58 | + $bookings = Booking::with('calendar_event') | |
| 50 | 59 | ->where('status', 'pending') |
| 51 | 60 | ->where('end_time', '<', gmdate('Y-m-d H:i:s', time() - $autoCompleteTimeOut)) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 52 | - ->update([ | |
| 61 | + ->limit(500) | |
| 62 | + ->get(); | |
| 63 | + | |
| 64 | + if ($bookings->isEmpty()) { | |
| 65 | + return true; | |
| 66 | + } | |
| 67 | + | |
| 68 | + foreach ($bookings as $booking) { | |
| 69 | + // Flag each row just before its hook, so a run that dies mid-loop leaves the rest for the next run. | |
| 70 | + $flagged = Booking::where('id', $booking->id)->where('status', 'pending')->update([ | |
| 53 | 71 | 'status' => 'cancelled', |
| 54 | - 'updated_at' => gmdate('Y-m-d H:i:s') // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date | |
| 72 | + 'updated_at' => gmdate('Y-m-d H:i:s'), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date | |
| 55 | 73 | ]); |
| 56 | 74 | |
| 75 | + if (!$flagged) { | |
| 76 | + continue; | |
| 77 | + } | |
| 78 | + | |
| 79 | + $booking->status = 'cancelled'; | |
| 80 | + do_action('fluent_booking/booking_schedule_auto_cancelled', $booking, $booking->calendar_event); | |
| 81 | + } | |
| 82 | + | |
| 57 | 83 | return true; |
| 58 | 84 | } |
| 59 | 85 | |
| 60 | 86 | private function maybeAutoCancelBooking() |
| @@ -60,28 +86,60 @@ | ||
| 60 | 86 | private function maybeAutoCancelBooking() |
| 61 | 87 | { |
| 62 | 88 | $autoCancelTimeOut = (int)Helper::getGlobalAdminSetting('auto_cancel_timing', 10) * 60; // 10 minutes |
| 63 | 89 | |
| 64 | - Booking::query() | |
| 90 | + $bookings = Booking::query() | |
| 91 | + ->with('calendar_event') | |
| 65 | 92 | ->where('created_at', '<=', gmdate('Y-m-d H:i:s', time() - $autoCancelTimeOut)) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 66 | 93 | ->where('status', 'pending') |
| 67 | 94 | ->where('payment_status', 'pending') |
| 68 | - ->update([ | |
| 95 | + ->where('payment_method', '!=', 'offline') | |
| 96 | + ->limit(500) | |
| 97 | + ->get(); | |
| 98 | + | |
| 99 | + if ($bookings->isEmpty()) { | |
| 100 | + return true; | |
| 101 | + } | |
| 102 | + | |
| 103 | + foreach ($bookings as $booking) { | |
| 104 | + // Flag each row just before its hook, so a run that dies mid-loop leaves the rest for the next run. | |
| 105 | + $flagged = Booking::where('id', $booking->id)->where('status', 'pending')->update([ | |
| 69 | 106 | 'status' => 'cancelled', |
| 70 | - 'updated_at' => gmdate('Y-m-d H:i:s') // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date | |
| 107 | + 'updated_at' => gmdate('Y-m-d H:i:s'), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date | |
| 71 | 108 | ]); |
| 72 | 109 | |
| 110 | + if (!$flagged) { | |
| 111 | + continue; | |
| 112 | + } | |
| 113 | + | |
| 114 | + $booking->status = 'cancelled'; | |
| 115 | + do_action('fluent_booking/booking_schedule_auto_cancelled', $booking, $booking->calendar_event); | |
| 116 | + } | |
| 117 | + | |
| 73 | 118 | return true; |
| 74 | 119 | } |
| 75 | 120 | |
| 76 | 121 | private function maybeAutoExpireCalendars() |
| 77 | 122 | { |
| 78 | - Calendar::query() | |
| 123 | + $calendarIds = Calendar::query() | |
| 79 | 124 | ->where('type', 'event') |
| 80 | 125 | ->where('status', 'active') |
| 81 | 126 | ->whereDoesntHave('events', function ($query) { |
| 82 | 127 | $query->whereIn('status', ['active', 'draft']); |
| 83 | 128 | }) |
| 129 | + ->limit(500) | |
| 130 | + ->pluck('id'); | |
| 131 | + | |
| 132 | + if ($calendarIds->isEmpty()) { | |
| 133 | + return true; | |
| 134 | + } | |
| 135 | + | |
| 136 | + Calendar::whereIn('id', $calendarIds) | |
| 137 | + ->where('type', 'event') | |
| 138 | + ->where('status', 'active') | |
| 139 | + ->whereDoesntHave('events', function ($query) { | |
| 140 | + $query->whereIn('status', ['active', 'draft']); | |
| 141 | + }) | |
| 84 | 142 | ->update(['status' => 'expired']); |
| 85 | 143 | |
| 86 | 144 | return true; |
| 87 | 145 | } |
| @@ -97,8 +155,9 @@ | ||
| 97 | 155 | }) |
| 98 | 156 | ->with(['calendar_event' => function ($query) { |
| 99 | 157 | $query->where('status', 'active'); |
| 100 | 158 | }]) |
| 159 | + ->limit(500) | |
| 101 | 160 | ->get() |
| 102 | 161 | ->each(function ($meta) { |
| 103 | 162 | $meta->calendar_event->update(['status' => 'expired']); |
| 104 | 163 | }); |
| @@ -107,9 +166,20 @@ | ||
| 107 | 166 | } |
| 108 | 167 | |
| 109 | 168 | private function maybeAutoDeleteReservations() |
| 110 | 169 | { |
| 111 | - Booking::query() | |
| 170 | + $reservationIds = Booking::query() | |
| 171 | + ->whereIn('event_type', ['single_event', 'group_event']) | |
| 172 | + ->where('status', 'reserved') | |
| 173 | + ->where('start_time', '<=', gmdate('Y-m-d H:i:s', time())) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date | |
| 174 | + ->limit(500) | |
| 175 | + ->pluck('id'); | |
| 176 | + | |
| 177 | + if ($reservationIds->isEmpty()) { | |
| 178 | + return true; | |
| 179 | + } | |
| 180 | + | |
| 181 | + Booking::whereIn('id', $reservationIds) | |
| 112 | 182 | ->whereIn('event_type', ['single_event', 'group_event']) |
| 113 | 183 | ->where('status', 'reserved') |
| 114 | 184 | ->where('start_time', '<=', gmdate('Y-m-d H:i:s', time())) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 115 | 185 | ->delete(); |