blocked_period.php
1 year ago
booked_period.php
1 year ago
booking_request.php
1 year ago
booking_resource.php
1 year ago
booking_slot.php
1 year ago
filter.php
1 year ago
process_action.php
9 months ago
process_event.php
1 year ago
role.php
1 year ago
router.php
1 year ago
step.php
1 year ago
stripe_connect_customer.php
1 year ago
time_period.php
1 year ago
user.php
1 year ago
work_period.php
1 year ago
booking_request.php
98 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Copyright (c) 2022 LatePoint LLC. All rights reserved. |
| 4 | * |
| 5 | * This class is used to create booking requests, they are useful for getting availability information on the calendar |
| 6 | * and checking if a timeslot is available. |
| 7 | */ |
| 8 | |
| 9 | namespace LatePoint\Misc; |
| 10 | |
| 11 | class BookingRequest{ |
| 12 | public ?string $start_date; |
| 13 | public ?string $end_date; |
| 14 | public ?int $start_time = 0; |
| 15 | public ?int $end_time = 0; |
| 16 | public ?int $duration = 0; |
| 17 | public ?int $buffer_before = 0; |
| 18 | public ?int $buffer_after = 0; |
| 19 | public ?int $total_attendees = 1; |
| 20 | public $service_id = 0; |
| 21 | public $agent_id = 0; |
| 22 | public $location_id = 0; |
| 23 | |
| 24 | function __construct($args = []){ |
| 25 | $allowed_props = self::allowed_props(); |
| 26 | foreach($args as $key => $arg){ |
| 27 | if(in_array($key, $allowed_props)) $this->$key = $arg; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | |
| 32 | public static function create_from_booking_model(\OsBookingModel $booking): BookingRequest{ |
| 33 | $booking_request = new BookingRequest([ 'start_date' => $booking->start_date, |
| 34 | 'end_date' => $booking->end_date ? $booking->end_date : $booking->start_date, |
| 35 | 'start_time' => (int) $booking->start_time, |
| 36 | 'end_time' => (int) $booking->end_time, |
| 37 | 'duration' => (int) $booking->get_total_duration(), |
| 38 | 'buffer_before' => (int) $booking->buffer_before, |
| 39 | 'buffer_after' => (int) $booking->buffer_after, |
| 40 | 'total_attendees' => (int) $booking->total_attendees, |
| 41 | 'agent_id' => ($booking->agent_id == LATEPOINT_ANY_AGENT) ? 0 : $booking->agent_id, |
| 42 | 'service_id' => $booking->service_id, |
| 43 | 'location_id' => ($booking->location_id == LATEPOINT_ANY_LOCATION) ? 0 : $booking->location_id]); |
| 44 | return apply_filters('latepoint_create_booking_request_from_booking_model', $booking_request, $booking); |
| 45 | } |
| 46 | |
| 47 | public function get_start_time_with_buffer(): int{ |
| 48 | return $this->start_time - $this->buffer_before; |
| 49 | } |
| 50 | |
| 51 | public function get_end_time_with_buffer(): int{ |
| 52 | return $this->end_time + $this->buffer_after; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | public function get_start_datetime( string $set_timezone = '') : \OsWpDateTime{ |
| 57 | try{ |
| 58 | // start_time and start_date is legacy stored in wordpress timezone |
| 59 | $dateTime = new \OsWpDateTime( $this->start_date . ' 00:00:00', \OsTimeHelper::get_wp_timezone() ); |
| 60 | if($this->start_time > 0){ |
| 61 | $dateTime->modify( '+' . $this->start_time . ' minutes' ); |
| 62 | } |
| 63 | if(!empty($set_timezone)) $dateTime->setTimezone( new \DateTimeZone( $set_timezone ) ); |
| 64 | return $dateTime; |
| 65 | }catch(\Exception $e){ |
| 66 | return new \OsWpDateTime('now'); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | public function get_end_datetime( string $set_timezone = '') : \OsWpDateTime{ |
| 71 | try{ |
| 72 | // start_time and start_date is legacy stored in wordpress timezone |
| 73 | $dateTime = new \OsWpDateTime( $this->end_date . ' 00:00:00', \OsTimeHelper::get_wp_timezone() ); |
| 74 | if($this->end_time > 0){ |
| 75 | $dateTime->modify( '+' . $this->end_time . ' minutes' ); |
| 76 | } |
| 77 | if(!empty($set_timezone)) $dateTime->setTimezone( new \DateTimeZone( $set_timezone ) ); |
| 78 | return $dateTime; |
| 79 | }catch(\Exception $e){ |
| 80 | return new \OsWpDateTime('now'); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | |
| 85 | public static function allowed_props(): array{ |
| 86 | return ['start_date', |
| 87 | 'end_date', |
| 88 | 'start_time', |
| 89 | 'end_time', |
| 90 | 'duration', |
| 91 | 'buffer_before', |
| 92 | 'buffer_after', |
| 93 | 'total_attendees', |
| 94 | 'agent_id', |
| 95 | 'service_id', |
| 96 | 'location_id']; |
| 97 | } |
| 98 | } |