activities_helper.php
3 months ago
agent_helper.php
3 months ago
analytics_helper.php
4 months ago
auth_helper.php
3 months ago
blocks_helper.php
3 months ago
booking_helper.php
3 months ago
bricks_helper.php
3 months ago
bundles_helper.php
3 months ago
calendar_helper.php
3 months ago
carts_helper.php
3 months ago
connector_helper.php
3 months ago
csv_helper.php
3 months ago
customer_helper.php
3 months ago
customer_import_helper.php
3 months ago
database_helper.php
3 months 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 months 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 months 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
3 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
3 months ago
price_breakdown_helper.php
3 months ago
process_jobs_helper.php
3 months ago
processes_helper.php
3 months ago
replacer_helper.php
3 months ago
resource_helper.php
3 months ago
roles_helper.php
3 months ago
router_helper.php
3 months ago
service_helper.php
3 months ago
sessions_helper.php
3 months ago
settings_helper.php
3 months ago
short_links_systems_helper.php
3 months ago
shortcodes_helper.php
3 months ago
sms_helper.php
3 months ago
steps_helper.php
3 months ago
stripe_connect_helper.php
3 months ago
styles_helper.php
3 months ago
support_topics_helper.php
3 months ago
time_helper.php
3 months ago
timeline_helper.php
3 months ago
transaction_helper.php
3 months ago
transaction_intent_helper.php
3 months ago
util_helper.php
3 months ago
version_specific_updates_helper.php
3 months ago
whatsapp_helper.php
3 months ago
work_periods_helper.php
3 months ago
wp_datetime.php
3 months ago
wp_user_helper.php
3 months ago
resource_helper.php
295 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 | $weekday_periods = OsWorkPeriodsHelper::get_work_periods_grouped_by_weekday( $filter ); |
| 67 | $daily_resources = []; |
| 68 | // loop through the requested days and fill in array with work periods that are applicable to that day |
| 69 | |
| 70 | // Booked periods |
| 71 | $booked_periods_filter = new \LatePoint\Misc\Filter(); |
| 72 | $booked_periods_filter->date_from = $date_from->format( 'Y-m-d' ); |
| 73 | $booked_periods_filter->date_to = $date_to->format( 'Y-m-d' ); |
| 74 | $booked_periods_filter->statuses = OsBookingHelper::get_timeslot_blocking_statuses(); |
| 75 | if ( $settings['exclude_booking_ids'] ) { |
| 76 | $booked_periods_filter->exclude_booking_ids = $settings['exclude_booking_ids']; |
| 77 | } |
| 78 | if ( $settings['consider_cart_items'] ) { |
| 79 | $booked_periods_filter->consider_cart_items = true; |
| 80 | } |
| 81 | |
| 82 | $booked_periods = OsBookingHelper::get_booked_periods_grouped_by_day( $booked_periods_filter ); |
| 83 | $blocked_periods = OsBookingHelper::get_blocked_periods_grouped_by_day( $filter, $settings['accessed_from_backend'] ); |
| 84 | |
| 85 | |
| 86 | for ( $day_date = clone $date_from; $day_date->format( 'Y-m-d' ) <= $date_to->format( 'Y-m-d' ); $day_date->modify( '+1 day' ) ) { |
| 87 | $daily_resources[ $day_date->format( 'Y-m-d' ) ] = []; |
| 88 | // fill every day with available resources |
| 89 | foreach ( $resources as $resource ) { |
| 90 | $last_added_period = false; |
| 91 | $available_work_periods_groups = []; |
| 92 | $group_index = 0; |
| 93 | // loop through available work periods for this week day |
| 94 | foreach ( $weekday_periods[ $day_date->format( 'N' ) ] as $period ) { |
| 95 | if ( $period->custom_date && ( $period->custom_date != $day_date->format( 'Y-m-d' ) ) ) { |
| 96 | continue; |
| 97 | } // if this period has a custom date set and if it doesn't match the one we search for - skip it |
| 98 | // only add this work period if agent/location/service match or not set |
| 99 | 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 ) ) { |
| 100 | if ( $last_added_period ) { |
| 101 | // if weight of previously added period is different - break, no need to add any other periods |
| 102 | if ( $last_added_period->weight != $period->weight ) { |
| 103 | break; |
| 104 | } |
| 105 | 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 ) ) { |
| 106 | // same weight NOT same exact properties, create a new group of work periods, which will later be used to find intersections |
| 107 | $group_index++; |
| 108 | } |
| 109 | if ( ( $period->start_time == 0 ) && ( $period->end_time == 0 ) ) { |
| 110 | $available_work_periods_groups = []; |
| 111 | break; |
| 112 | } |
| 113 | $available_work_periods_groups[ $group_index ][] = \LatePoint\Misc\TimePeriod::create_from_work_period( $period ); |
| 114 | $last_added_period = $period; |
| 115 | } else { |
| 116 | if ( ( $period->start_time == 0 ) && ( $period->end_time == 0 ) ) { |
| 117 | $available_work_periods_groups = []; |
| 118 | break; |
| 119 | } |
| 120 | $available_work_periods_groups[ $group_index ][] = \LatePoint\Misc\TimePeriod::create_from_work_period( $period ); |
| 121 | $last_added_period = $period; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | $day_resource = clone $resource; |
| 127 | $day_resource->date = $day_date->format( 'Y-m-d' ); |
| 128 | $day_resource->add_available_periods( $available_work_periods_groups ); |
| 129 | |
| 130 | /// ----------------------------- |
| 131 | /// LOGIC FOR CALCULATING "BOOKED" PERIODS |
| 132 | /// ----------------------------- |
| 133 | foreach ( $booked_periods[ $day_date->format( 'Y-m-d' ) ] as $booked_period ) { |
| 134 | |
| 135 | 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 ) ) { |
| 136 | // same service, agent and location is already booked, block no matter what |
| 137 | $day_resource->add_booked_period( $booked_period ); |
| 138 | continue; |
| 139 | } |
| 140 | |
| 141 | if ( $day_resource->agent_id == $booked_period->agent_id ) { |
| 142 | // Same agent |
| 143 | if ( ( $day_resource->location_id != $booked_period->location_id ) && OsSettingsHelper::is_on( 'one_location_at_time' ) ) { |
| 144 | // different location is booked, but the same agent, block if "Agents can only be present in one location at a time" is ON |
| 145 | $day_resource->add_booked_period( $booked_period, true ); |
| 146 | continue; |
| 147 | } |
| 148 | if ( ( $day_resource->service_id != $booked_period->service_id ) && ( $day_resource->location_id == $booked_period->location_id ) && ! OsSettingsHelper::is_on( 'multiple_services_at_time' ) ) { |
| 149 | // Different service, but same location, block, if "One agent can perform different services simultaneously" is OFF |
| 150 | $day_resource->add_booked_period( $booked_period, true ); |
| 151 | } |
| 152 | } else { |
| 153 | // Different agent |
| 154 | if ( ( $day_resource->location_id == $booked_period->location_id ) && OsSettingsHelper::is_on( 'one_agent_at_location' ) ) { |
| 155 | // 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 |
| 156 | // set to max capacity, to block slot even if it still has room |
| 157 | $day_resource->add_booked_period( $booked_period, true ); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /// ----------------------------- |
| 163 | /// LOGIC FOR CALCULATING "BLOCKED" PERIODS |
| 164 | /// ----------------------------- |
| 165 | foreach ( $blocked_periods[ $day_date->format( 'Y-m-d' ) ] as $blocked_period ) { |
| 166 | if ( ! $blocked_period->agent_id || $day_resource->agent_id == $blocked_period->agent_id ) { |
| 167 | $day_resource->add_blocked_period( $blocked_period ); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | $day_resource->build_bookable_slots( $booking_request, $day_resource->get_timeblock_interval() ); |
| 172 | $daily_resources[ $day_date->format( 'Y-m-d' ) ][] = $day_resource; |
| 173 | } |
| 174 | } |
| 175 | $daily_resources = apply_filters( 'latepoint_get_resources_grouped_by_day', $daily_resources, $booking_request, $date_from, $date_to, $settings ); |
| 176 | |
| 177 | return $daily_resources; |
| 178 | } |
| 179 | |
| 180 | |
| 181 | /** |
| 182 | * @param \LatePoint\Misc\BookingResource[] $resources |
| 183 | * |
| 184 | * @return array |
| 185 | */ |
| 186 | public static function get_ordered_booking_slots_from_resources( array $resources ): array { |
| 187 | $booking_slots = []; |
| 188 | foreach ( $resources as $resource ) { |
| 189 | $booking_slots = array_merge( $booking_slots, $resource->slots ); |
| 190 | } |
| 191 | |
| 192 | usort( |
| 193 | $booking_slots, |
| 194 | function ( $first, $second ) { |
| 195 | return $first->start_time <=> $second->start_time; |
| 196 | } |
| 197 | ); |
| 198 | |
| 199 | if ( count( $resources ) > 1 ) { |
| 200 | $squashed_booking_slots = []; |
| 201 | $last_added_slot = false; |
| 202 | foreach ( $booking_slots as $booking_slot ) { |
| 203 | if ( $last_added_slot && ( $last_added_slot->start_time == $booking_slot->start_time ) ) { |
| 204 | if ( $last_added_slot->available_capacity() < $booking_slot->available_capacity() ) { |
| 205 | $squashed_booking_slots[ count( $squashed_booking_slots ) - 1 ] = $booking_slot; |
| 206 | $last_added_slot = $booking_slot; |
| 207 | } |
| 208 | } else { |
| 209 | $squashed_booking_slots[] = $booking_slot; |
| 210 | $last_added_slot = $booking_slot; |
| 211 | } |
| 212 | } |
| 213 | $booking_slots = $squashed_booking_slots; |
| 214 | } |
| 215 | |
| 216 | return $booking_slots; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * @param \LatePoint\Misc\BookingResource[] |
| 221 | * |
| 222 | * @return \LatePoint\Misc\TimePeriod |
| 223 | */ |
| 224 | public static function get_work_boundaries_for_resources( $resources ): \LatePoint\Misc\TimePeriod { |
| 225 | $times = []; |
| 226 | foreach ( $resources as $resource ) { |
| 227 | foreach ( $resource->work_time_periods as $work_time_period ) { |
| 228 | $times[] = $work_time_period->start_time; |
| 229 | $times[] = $work_time_period->end_time; |
| 230 | } |
| 231 | foreach ( $resource->booked_time_periods as $booked_time_period ) { |
| 232 | if ( $booked_time_period->start_date == $booked_time_period->end_date ) { |
| 233 | // same day event |
| 234 | $times[] = $booked_time_period->start_time; |
| 235 | $times[] = $booked_time_period->end_time; |
| 236 | } else { |
| 237 | // event spans mutiple days, expand boundaries to a full day |
| 238 | $times[] = 0; |
| 239 | $times[] = 24 * 60 - 1; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | if ( $times ) { |
| 244 | $boundary_time_period = new \LatePoint\Misc\TimePeriod( |
| 245 | [ |
| 246 | 'start_time' => min( $times ), |
| 247 | 'end_time' => max( $times ), |
| 248 | ] |
| 249 | ); |
| 250 | } else { |
| 251 | $boundary_time_period = new \LatePoint\Misc\TimePeriod( |
| 252 | [ |
| 253 | 'start_time' => 0, |
| 254 | 'end_time' => 0, |
| 255 | ] |
| 256 | ); |
| 257 | } |
| 258 | |
| 259 | return $boundary_time_period; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * @param array $groups_of_resources |
| 264 | * |
| 265 | * @return \LatePoint\Misc\TimePeriod |
| 266 | */ |
| 267 | public static function get_work_boundaries_for_groups_of_resources( array $groups_of_resources ): \LatePoint\Misc\TimePeriod { |
| 268 | $times = []; |
| 269 | foreach ( $groups_of_resources as $resources ) { |
| 270 | $time_period = self::get_work_boundaries_for_resources( $resources ); |
| 271 | if ( $time_period->start_time || $time_period->end_time ) { |
| 272 | $times[] = $time_period->start_time; |
| 273 | $times[] = $time_period->end_time; |
| 274 | } |
| 275 | } |
| 276 | if ( $times ) { |
| 277 | $boundary_time_period = new \LatePoint\Misc\TimePeriod( |
| 278 | [ |
| 279 | 'start_time' => min( $times ), |
| 280 | 'end_time' => max( $times ), |
| 281 | ] |
| 282 | ); |
| 283 | } else { |
| 284 | $boundary_time_period = new \LatePoint\Misc\TimePeriod( |
| 285 | [ |
| 286 | 'start_time' => 0, |
| 287 | 'end_time' => 0, |
| 288 | ] |
| 289 | ); |
| 290 | } |
| 291 | |
| 292 | return $boundary_time_period; |
| 293 | } |
| 294 | } |
| 295 |