PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.6.6
LatePoint – Calendar Booking Plugin for Appointments and Events v5.6.6
5.6.6 5.6.5 5.6.4 5.6.3 5.6.2 5.6.1 5.6.0 5.5.2 5.5.1 5.5.0 5.4.2 trunk 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.1.91 5.1.92 5.1.93 5.1.94 5.2.0 5.2.1 5.2.10 5.2.11 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.2 5.4.0 5.4.1
latepoint / lib / helpers / resource_helper.php
latepoint / lib / helpers Last commit date
activities_helper.php 3 months ago agent_helper.php 3 months ago analytics_helper.php 1 week ago auth_helper.php 6 days ago blocks_helper.php 3 weeks ago booking_helper.php 4 days ago bricks_helper.php 3 months ago bundles_helper.php 4 days ago calendar_helper.php 2 days ago carts_helper.php 3 months ago connector_helper.php 3 months ago csv_helper.php 3 months ago customer_helper.php 1 month ago customer_import_helper.php 1 month ago database_helper.php 1 week ago debug_helper.php 3 months ago defaults_helper.php 3 months ago elementor_helper.php 3 months ago email_helper.php 3 months ago encrypt_helper.php 3 months ago events_helper.php 3 months ago form_helper.php 3 months ago icalendar_helper.php 3 months ago image_helper.php 3 months ago invoices_helper.php 3 weeks ago license_helper.php 3 months ago location_helper.php 3 months ago marketing_systems_helper.php 3 months ago meeting_systems_helper.php 3 months ago menu_helper.php 3 weeks ago meta_helper.php 3 months ago migrations_helper.php 3 months ago money_helper.php 3 months ago notifications_helper.php 3 months ago nps_survey_helper.php 3 months ago order_intent_helper.php 2 months ago orders_helper.php 3 months ago otp_helper.php 3 months ago pages_helper.php 3 months ago params_helper.php 3 months ago payments_helper.php 2 months ago plugin_version_update_helper.php 2 months ago price_breakdown_helper.php 3 months ago process_jobs_helper.php 3 months ago processes_helper.php 3 months ago razorpay_connect_helper.php 1 week ago replacer_helper.php 3 months ago resource_helper.php 4 days ago roles_helper.php 3 weeks ago router_helper.php 3 months ago service_helper.php 3 months ago sessions_helper.php 3 months ago settings_helper.php 1 week ago short_links_systems_helper.php 3 months ago shortcodes_helper.php 3 weeks ago sms_helper.php 3 months ago steps_helper.php 1 week ago stripe_connect_helper.php 1 week ago styles_helper.php 3 months ago support_topics_helper.php 3 months ago time_helper.php 2 months ago timeline_helper.php 2 months ago transaction_helper.php 1 month ago transaction_intent_helper.php 3 months ago util_helper.php 1 month ago version_specific_updates_helper.php 3 months ago whatsapp_helper.php 1 month ago work_periods_helper.php 3 months ago wp_datetime.php 3 months ago wp_user_helper.php 3 months ago
resource_helper.php
300 lines
1 <?php
2 /*
3 * Copyright (c) 2022 LatePoint LLC. All rights reserved.
4 */
5
6 class OsResourceHelper {
7
8 /**
9 * @param \LatePoint\Misc\BookingRequest $booking_request
10 * @param DateTime $date_from
11 * @param DateTime|null $date_to
12 * @param array $settings
13 *
14 * @return array
15 *
16 *
17 * Returns an array of work periods, grouped by days that were requested in the filter.
18 * example: ['2022-02-24' => [], '2022-02-25' => [], ...]
19 *
20 * | Agent | Service | Location | Date | Hours | Weight
21 * | -----------------------------------------------------------------------------
22 * | 1 | 1 | 1 | 2022-01-15 | 7:00 - 18:00 | 7
23 * | 1 | 0 | 1 | 2022-01-15 | 8:00 - 14:00 | 6
24 * | 1 | 0 | 0 | 2022-01-15 | 8:00 - 14:00 | 5
25 * | 0 | 0 | 1 | 2022-01-15 | 11:00 - 12:00 | 5
26 * | 0 | 0 | 0 | 2022-01-15 | 11:00 - 12:00 | 4
27 * | 1 | 0 | 1 | NULL | 0:00 - 0:00 | 2
28 * | 1 | 0 | 0 | NULL | 9:00 - 12:00 | 1
29 * | 0 | 0 | 0 | NULL | 11:00 - 17:00 | 0
30 *
31 */
32 public static function get_resources_grouped_by_day( \LatePoint\Misc\BookingRequest $booking_request, DateTime $date_from, ?DateTime $date_to = null, array $settings = [] ): array {
33 $defaults = [
34 'now' => OsTimeHelper::now_datetime_object(),
35 'exclude_booking_ids' => [],
36 'accessed_from_backend' => false,
37 'consider_cart_items' => false,
38 'timezone_name' => OsTimeHelper::get_wp_timezone_name(),
39 ];
40 $settings = array_merge( $defaults, $settings );
41
42 $connections = OsConnectorHelper::get_connections_that_satisfy_booking_request( $booking_request, $settings['accessed_from_backend'] );
43 $resources = [];
44 foreach ( $connections as $connection ) {
45 $resources[] = \LatePoint\Misc\BookingResource::create_from_connection( $connection );
46 }
47 if ( empty( $date_to ) ) {
48 $date_to = clone $date_from;
49 }
50
51
52 // all resource management is done in WP timezone, if requested timezone is different - make sure to include couple days on each end to accommodate for timezone differences, which could be up to 26 hours
53 if ( $settings['timezone_name'] != OsTimeHelper::get_wp_timezone_name() ) {
54 $date_from->modify( '-2 days' );
55 $date_to->modify( '+2 days' );
56 }
57
58 $filter = new \LatePoint\Misc\Filter(
59 [
60 'service_id' => $booking_request->service_id,
61 'connections' => $connections,
62 'date_from' => $date_from->format( 'Y-m-d' ),
63 'date_to' => $date_to->format( 'Y-m-d' ),
64 ]
65 );
66 // Pass excluded bookings (e.g. the booking being rescheduled) through to blocked-period generation
67 // so daily-booking-limit checks don't count the booking against itself.
68 if ( isset( $settings['exclude_booking_ids'] ) ) {
69 $filter->exclude_booking_ids = $settings['exclude_booking_ids'];
70 }
71 $weekday_periods = OsWorkPeriodsHelper::get_work_periods_grouped_by_weekday( $filter );
72 $daily_resources = [];
73 // loop through the requested days and fill in array with work periods that are applicable to that day
74
75 // Booked periods
76 $booked_periods_filter = new \LatePoint\Misc\Filter();
77 $booked_periods_filter->date_from = $date_from->format( 'Y-m-d' );
78 $booked_periods_filter->date_to = $date_to->format( 'Y-m-d' );
79 $booked_periods_filter->statuses = OsBookingHelper::get_timeslot_blocking_statuses();
80 if ( $settings['exclude_booking_ids'] ) {
81 $booked_periods_filter->exclude_booking_ids = $settings['exclude_booking_ids'];
82 }
83 if ( $settings['consider_cart_items'] ) {
84 $booked_periods_filter->consider_cart_items = true;
85 }
86
87 $booked_periods = OsBookingHelper::get_booked_periods_grouped_by_day( $booked_periods_filter );
88 $blocked_periods = OsBookingHelper::get_blocked_periods_grouped_by_day( $filter, $settings['accessed_from_backend'] );
89
90
91 for ( $day_date = clone $date_from; $day_date->format( 'Y-m-d' ) <= $date_to->format( 'Y-m-d' ); $day_date->modify( '+1 day' ) ) {
92 $daily_resources[ $day_date->format( 'Y-m-d' ) ] = [];
93 // fill every day with available resources
94 foreach ( $resources as $resource ) {
95 $last_added_period = false;
96 $available_work_periods_groups = [];
97 $group_index = 0;
98 // loop through available work periods for this week day
99 foreach ( $weekday_periods[ $day_date->format( 'N' ) ] as $period ) {
100 if ( $period->custom_date && ( $period->custom_date != $day_date->format( 'Y-m-d' ) ) ) {
101 continue;
102 } // if this period has a custom date set and if it doesn't match the one we search for - skip it
103 // only add this work period if agent/location/service match or not set
104 if ( ( ! $period->agent_id || $period->agent_id == $resource->agent_id ) && ( ! $period->service_id || $period->service_id == $resource->service_id ) && ( ! $period->location_id || $period->location_id == $resource->location_id ) ) {
105 if ( $last_added_period ) {
106 // if weight of previously added period is different - break, no need to add any other periods
107 if ( $last_added_period->weight != $period->weight ) {
108 break;
109 }
110 if ( ( $last_added_period->service_id != $period->service_id ) || ( $last_added_period->agent_id != $period->agent_id ) || ( $last_added_period->location_id != $period->location_id ) ) {
111 // same weight NOT same exact properties, create a new group of work periods, which will later be used to find intersections
112 $group_index++;
113 }
114 if ( ( $period->start_time == 0 ) && ( $period->end_time == 0 ) ) {
115 $available_work_periods_groups = [];
116 break;
117 }
118 $available_work_periods_groups[ $group_index ][] = \LatePoint\Misc\TimePeriod::create_from_work_period( $period );
119 $last_added_period = $period;
120 } else {
121 if ( ( $period->start_time == 0 ) && ( $period->end_time == 0 ) ) {
122 $available_work_periods_groups = [];
123 break;
124 }
125 $available_work_periods_groups[ $group_index ][] = \LatePoint\Misc\TimePeriod::create_from_work_period( $period );
126 $last_added_period = $period;
127 }
128 }
129 }
130
131 $day_resource = clone $resource;
132 $day_resource->date = $day_date->format( 'Y-m-d' );
133 $day_resource->add_available_periods( $available_work_periods_groups );
134
135 /// -----------------------------
136 /// LOGIC FOR CALCULATING "BOOKED" PERIODS
137 /// -----------------------------
138 foreach ( $booked_periods[ $day_date->format( 'Y-m-d' ) ] as $booked_period ) {
139
140 if ( ( $day_resource->service_id == $booked_period->service_id ) && ( $day_resource->location_id == $booked_period->location_id ) && ( $day_resource->agent_id == $booked_period->agent_id ) ) {
141 // same service, agent and location is already booked, block no matter what
142 $day_resource->add_booked_period( $booked_period );
143 continue;
144 }
145
146 if ( $day_resource->agent_id == $booked_period->agent_id ) {
147 // Same agent
148 if ( ( $day_resource->location_id != $booked_period->location_id ) && OsSettingsHelper::is_on( 'one_location_at_time' ) ) {
149 // different location is booked, but the same agent, block if "Agents can only be present in one location at a time" is ON
150 $day_resource->add_booked_period( $booked_period, true );
151 continue;
152 }
153 if ( ( $day_resource->service_id != $booked_period->service_id ) && ( $day_resource->location_id == $booked_period->location_id ) && ! OsSettingsHelper::is_on( 'multiple_services_at_time' ) ) {
154 // Different service, but same location, block, if "One agent can perform different services simultaneously" is OFF
155 $day_resource->add_booked_period( $booked_period, true );
156 }
157 } else {
158 // Different agent
159 if ( ( $day_resource->location_id == $booked_period->location_id ) && OsSettingsHelper::is_on( 'one_agent_at_location' ) ) {
160 // same location, so it doesn't matter who's the agent, block, because location can only be used by a single agent at a time
161 // set to max capacity, to block slot even if it still has room
162 $day_resource->add_booked_period( $booked_period, true );
163 }
164 }
165 }
166
167 /// -----------------------------
168 /// LOGIC FOR CALCULATING "BLOCKED" PERIODS
169 /// -----------------------------
170 foreach ( $blocked_periods[ $day_date->format( 'Y-m-d' ) ] as $blocked_period ) {
171 if ( ! $blocked_period->agent_id || $day_resource->agent_id == $blocked_period->agent_id ) {
172 $day_resource->add_blocked_period( $blocked_period );
173 }
174 }
175
176 $day_resource->build_bookable_slots( $booking_request, $day_resource->get_timeblock_interval() );
177 $daily_resources[ $day_date->format( 'Y-m-d' ) ][] = $day_resource;
178 }
179 }
180 $daily_resources = apply_filters( 'latepoint_get_resources_grouped_by_day', $daily_resources, $booking_request, $date_from, $date_to, $settings );
181
182 return $daily_resources;
183 }
184
185
186 /**
187 * @param \LatePoint\Misc\BookingResource[] $resources
188 *
189 * @return array
190 */
191 public static function get_ordered_booking_slots_from_resources( array $resources ): array {
192 $booking_slots = [];
193 foreach ( $resources as $resource ) {
194 $booking_slots = array_merge( $booking_slots, $resource->slots );
195 }
196
197 usort(
198 $booking_slots,
199 function ( $first, $second ) {
200 return $first->start_time <=> $second->start_time;
201 }
202 );
203
204 if ( count( $resources ) > 1 ) {
205 $squashed_booking_slots = [];
206 $last_added_slot = false;
207 foreach ( $booking_slots as $booking_slot ) {
208 if ( $last_added_slot && ( $last_added_slot->start_time == $booking_slot->start_time ) ) {
209 if ( $last_added_slot->available_capacity() < $booking_slot->available_capacity() ) {
210 $squashed_booking_slots[ count( $squashed_booking_slots ) - 1 ] = $booking_slot;
211 $last_added_slot = $booking_slot;
212 }
213 } else {
214 $squashed_booking_slots[] = $booking_slot;
215 $last_added_slot = $booking_slot;
216 }
217 }
218 $booking_slots = $squashed_booking_slots;
219 }
220
221 return $booking_slots;
222 }
223
224 /**
225 * @param \LatePoint\Misc\BookingResource[]
226 *
227 * @return \LatePoint\Misc\TimePeriod
228 */
229 public static function get_work_boundaries_for_resources( $resources ): \LatePoint\Misc\TimePeriod {
230 $times = [];
231 foreach ( $resources as $resource ) {
232 foreach ( $resource->work_time_periods as $work_time_period ) {
233 $times[] = $work_time_period->start_time;
234 $times[] = $work_time_period->end_time;
235 }
236 foreach ( $resource->booked_time_periods as $booked_time_period ) {
237 if ( $booked_time_period->start_date == $booked_time_period->end_date ) {
238 // same day event
239 $times[] = $booked_time_period->start_time;
240 $times[] = $booked_time_period->end_time;
241 } else {
242 // event spans mutiple days, expand boundaries to a full day
243 $times[] = 0;
244 $times[] = 24 * 60 - 1;
245 }
246 }
247 }
248 if ( $times ) {
249 $boundary_time_period = new \LatePoint\Misc\TimePeriod(
250 [
251 'start_time' => min( $times ),
252 'end_time' => max( $times ),
253 ]
254 );
255 } else {
256 $boundary_time_period = new \LatePoint\Misc\TimePeriod(
257 [
258 'start_time' => 0,
259 'end_time' => 0,
260 ]
261 );
262 }
263
264 return $boundary_time_period;
265 }
266
267 /**
268 * @param array $groups_of_resources
269 *
270 * @return \LatePoint\Misc\TimePeriod
271 */
272 public static function get_work_boundaries_for_groups_of_resources( array $groups_of_resources ): \LatePoint\Misc\TimePeriod {
273 $times = [];
274 foreach ( $groups_of_resources as $resources ) {
275 $time_period = self::get_work_boundaries_for_resources( $resources );
276 if ( $time_period->start_time || $time_period->end_time ) {
277 $times[] = $time_period->start_time;
278 $times[] = $time_period->end_time;
279 }
280 }
281 if ( $times ) {
282 $boundary_time_period = new \LatePoint\Misc\TimePeriod(
283 [
284 'start_time' => min( $times ),
285 'end_time' => max( $times ),
286 ]
287 );
288 } else {
289 $boundary_time_period = new \LatePoint\Misc\TimePeriod(
290 [
291 'start_time' => 0,
292 'end_time' => 0,
293 ]
294 );
295 }
296
297 return $boundary_time_period;
298 }
299 }
300