PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 2.2.0
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v2.2.0
2.5.0 2.4.0 2.3.0 2.2.5 2.2.0 2.1.2 2.1.1 trunk 1.10.0 1.10.01 1.10.02 1.5.0 1.5.01 1.5.02 1.5.1 1.5.10 1.5.20 1.5.21 1.5.22 1.5.23 1.5.24 1.5.25 1.6.0 1.7.0 1.7.1 All 34 releases
fluent-booking / app / Hooks / Scheduler / FiveMinuteScheduler.php

FiveMinuteScheduler.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 2.2.0, at app/Hooks/Scheduler/FiveMinuteScheduler.php

156 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 Calendar::query()
114 ->where('type', 'event')
115 ->where('status', 'active')
116 ->whereDoesntHave('events', function ($query) {
117 $query->whereIn('status', ['active', 'draft']);
118 })
119 ->update(['status' => 'expired']);
120
121 return true;
122 }
123
124 private function maybeAutoExpireCalendarEvents()
125 {
126 Meta::query()
127 ->where('object_type', 'calendar_event')
128 ->where('key', 'expire_time')
129 ->where('value', '<=', gmdate('Y-m-d H:i:s', time())) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
130 ->whereHas('calendar_event', function ($query) {
131 $query->where('status', 'active');
132 })
133 ->with(['calendar_event' => function ($query) {
134 $query->where('status', 'active');
135 }])
136 ->limit(500)
137 ->get()
138 ->each(function ($meta) {
139 $meta->calendar_event->update(['status' => 'expired']);
140 });
141
142 return true;
143 }
144
145 private function maybeAutoDeleteReservations()
146 {
147 Booking::query()
148 ->whereIn('event_type', ['single_event', 'group_event'])
149 ->where('status', 'reserved')
150 ->where('start_time', '<=', gmdate('Y-m-d H:i:s', time())) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
151 ->delete();
152
153 return true;
154 }
155 }
156