abstract-service-ability.php
3 months ago
create-service.php
3 months ago
delete-service.php
3 months ago
disable-service.php
3 months ago
duplicate-service.php
3 months ago
enable-service.php
3 months ago
get-service-agents.php
3 months ago
get-service-bookings.php
3 months ago
get-service.php
3 months ago
get-services.php
3 months ago
list-service-categories.php
3 months ago
update-service.php
3 months ago
get-service-bookings.php
109 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | class LatePointAbilityGetServiceBookings extends LatePointAbstractServiceAbility { |
| 7 | |
| 8 | protected function configure(): void { |
| 9 | $this->id = 'latepoint/get-service-bookings'; |
| 10 | $this->label = __( 'Get service bookings', 'latepoint' ); |
| 11 | $this->description = __( 'Returns bookings for a specific service.', 'latepoint' ); |
| 12 | $this->permission = 'service__view'; |
| 13 | $this->read_only = true; |
| 14 | } |
| 15 | |
| 16 | public function get_input_schema(): array { |
| 17 | return [ |
| 18 | 'type' => 'object', |
| 19 | 'properties' => [ |
| 20 | 'service_id' => [ |
| 21 | 'type' => 'integer', |
| 22 | 'description' => __( 'Service ID.', 'latepoint' ), |
| 23 | ], |
| 24 | 'date_from' => [ |
| 25 | 'type' => 'string', |
| 26 | 'format' => 'date', |
| 27 | ], |
| 28 | 'date_to' => [ |
| 29 | 'type' => 'string', |
| 30 | 'format' => 'date', |
| 31 | ], |
| 32 | 'page' => [ |
| 33 | 'type' => 'integer', |
| 34 | 'default' => 1, |
| 35 | ], |
| 36 | 'per_page' => [ |
| 37 | 'type' => 'integer', |
| 38 | 'default' => 20, |
| 39 | ], |
| 40 | ], |
| 41 | 'required' => [ 'service_id' ], |
| 42 | ]; |
| 43 | } |
| 44 | |
| 45 | public function get_output_schema(): array { |
| 46 | return [ |
| 47 | 'type' => 'object', |
| 48 | 'properties' => [ |
| 49 | 'bookings' => [ 'type' => 'array' ], |
| 50 | 'total' => [ 'type' => 'integer' ], |
| 51 | 'page' => [ 'type' => 'integer' ], |
| 52 | 'per_page' => [ 'type' => 'integer' ], |
| 53 | ], |
| 54 | ]; |
| 55 | } |
| 56 | |
| 57 | public function execute( array $args ) { |
| 58 | $service = new OsServiceModel( (int) $args['service_id'] ); |
| 59 | if ( $service->is_new_record() ) { |
| 60 | return new WP_Error( 'not_found', __( 'Service not found.', 'latepoint' ), [ 'status' => 404 ] ); |
| 61 | } |
| 62 | |
| 63 | $page = max( 1, (int) ( $args['page'] ?? 1 ) ); |
| 64 | $per_page = min( 100, max( 1, (int) ( $args['per_page'] ?? 20 ) ) ); |
| 65 | $offset = ( $page - 1 ) * $per_page; |
| 66 | |
| 67 | $query = ( new OsBookingModel() )->where( [ 'service_id' => $service->id ] ); |
| 68 | if ( ! empty( $args['date_from'] ) ) { |
| 69 | $query->where( [ 'start_date >=' => sanitize_text_field( $args['date_from'] ) ] ); |
| 70 | } |
| 71 | if ( ! empty( $args['date_to'] ) ) { |
| 72 | $query->where( [ 'start_date <=' => sanitize_text_field( $args['date_to'] ) ] ); |
| 73 | } |
| 74 | |
| 75 | $bookings = ( clone $query ) |
| 76 | ->order_by( 'start_date ASC, start_time ASC' ) |
| 77 | ->set_limit( $per_page ) |
| 78 | ->set_offset( $offset ) |
| 79 | ->get_results_as_models(); |
| 80 | $total = $query->count(); |
| 81 | |
| 82 | return [ |
| 83 | 'bookings' => array_map( [ $this, 'serialize_booking' ], $bookings ), |
| 84 | 'total' => (int) $total, |
| 85 | 'page' => $page, |
| 86 | 'per_page' => $per_page, |
| 87 | ]; |
| 88 | } |
| 89 | |
| 90 | private function serialize_booking( OsBookingModel $b ): array { |
| 91 | return [ |
| 92 | 'id' => (int) $b->id, |
| 93 | 'status' => (string) $b->status, |
| 94 | 'customer_id' => (int) $b->customer_id, |
| 95 | 'agent_id' => (int) $b->agent_id, |
| 96 | 'service_id' => (int) $b->service_id, |
| 97 | 'location_id' => (int) $b->location_id, |
| 98 | 'start_date' => (string) $b->start_date, |
| 99 | 'start_time' => (int) $b->start_time, |
| 100 | 'end_time' => (int) $b->end_time, |
| 101 | 'duration' => (int) $b->duration, |
| 102 | 'customer_name' => $b->customer ? trim( $b->customer->first_name . ' ' . $b->customer->last_name ) : '', |
| 103 | 'service_name' => $b->service ? (string) $b->service->name : '', |
| 104 | 'agent_name' => $b->agent ? trim( $b->agent->first_name . ' ' . $b->agent->last_name ) : '', |
| 105 | 'notes' => $b->order ? (string) ( $b->order->customer_comment ?? '' ) : '', |
| 106 | ]; |
| 107 | } |
| 108 | } |
| 109 |