abstract-calendar-ability.php
4 months ago
add-off-period.php
4 months ago
check-slot-availability.php
4 months ago
get-available-slots.php
4 months ago
get-work-schedule.php
4 months ago
list-off-periods.php
4 months ago
remove-off-period.php
4 months ago
check-slot-availability.php
89 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | class LatePointAbilityCheckSlotAvailability extends LatePointAbstractCalendarAbility { |
| 7 | |
| 8 | protected function configure(): void { |
| 9 | $this->id = 'latepoint/check-slot-availability'; |
| 10 | $this->label = __( 'Check slot availability', 'latepoint' ); |
| 11 | $this->description = __( 'Checks whether a specific time slot has no conflicting bookings.', 'latepoint' ); |
| 12 | $this->permission = ''; |
| 13 | $this->read_only = true; |
| 14 | } |
| 15 | |
| 16 | public function check_permission(): bool { |
| 17 | return true; |
| 18 | } |
| 19 | |
| 20 | public function get_input_schema(): array { |
| 21 | return [ |
| 22 | 'type' => 'object', |
| 23 | 'properties' => [ |
| 24 | 'service_id' => [ 'type' => 'integer' ], |
| 25 | 'date' => [ |
| 26 | 'type' => 'string', |
| 27 | 'format' => 'date', |
| 28 | ], |
| 29 | 'start_time' => [ |
| 30 | 'type' => 'integer', |
| 31 | 'description' => __( 'Start time (minutes from midnight).', 'latepoint' ), |
| 32 | ], |
| 33 | 'agent_id' => [ 'type' => 'integer' ], |
| 34 | 'location_id' => [ 'type' => 'integer' ], |
| 35 | ], |
| 36 | 'required' => [ 'service_id', 'date', 'start_time' ], |
| 37 | ]; |
| 38 | } |
| 39 | |
| 40 | public function get_output_schema(): array { |
| 41 | return [ |
| 42 | 'type' => 'object', |
| 43 | 'properties' => [ |
| 44 | 'available' => [ 'type' => 'boolean' ], |
| 45 | 'conflicts' => [ |
| 46 | 'type' => 'array', |
| 47 | 'items' => [ 'type' => 'object' ], |
| 48 | ], |
| 49 | ], |
| 50 | ]; |
| 51 | } |
| 52 | |
| 53 | public function execute( array $args ) { |
| 54 | $service_id = (int) $args['service_id']; |
| 55 | $date = sanitize_text_field( $args['date'] ); |
| 56 | $start_time = (int) $args['start_time']; |
| 57 | $agent_id = ! empty( $args['agent_id'] ) ? (int) $args['agent_id'] : 0; |
| 58 | |
| 59 | if ( $service_id ) { |
| 60 | $service = new OsServiceModel( $service_id ); |
| 61 | $duration = ( ! $service->is_new_record() && $service->duration ) ? (int) $service->duration : 60; |
| 62 | } else { |
| 63 | $duration = 60; |
| 64 | } |
| 65 | $end_time = $start_time + $duration; |
| 66 | |
| 67 | $query = new OsBookingModel(); |
| 68 | $query->where( |
| 69 | [ |
| 70 | 'start_date' => $date, |
| 71 | 'status' => LATEPOINT_BOOKING_STATUS_APPROVED, |
| 72 | ] |
| 73 | ); |
| 74 | if ( $agent_id ) { |
| 75 | $query->where( [ 'agent_id' => $agent_id ] ); |
| 76 | } |
| 77 | $query->where( [ 'start_time <' => $end_time ] ); |
| 78 | $query->where( [ 'end_time >' => $start_time ] ); |
| 79 | |
| 80 | $count = $query->count(); |
| 81 | $available = ( $count === 0 ); |
| 82 | |
| 83 | return [ |
| 84 | 'available' => $available, |
| 85 | 'conflicts' => [], |
| 86 | ]; |
| 87 | } |
| 88 | } |
| 89 |