abstract-agent-ability.php
3 months ago
create-agent.php
3 months ago
delete-agent.php
3 months ago
disable-agent.php
3 months ago
enable-agent.php
3 months ago
get-agent-bookings.php
3 months ago
get-agent-revenue.php
3 months ago
get-agent-services.php
3 months ago
get-agent.php
3 months ago
get-agents.php
3 months ago
update-agent.php
3 months ago
get-agent-bookings.php
93 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | class LatePointAbilityGetAgentBookings extends LatePointAbstractAgentAbility { |
| 7 | |
| 8 | protected function configure(): void { |
| 9 | $this->id = 'latepoint/get-agent-bookings'; |
| 10 | $this->label = __( 'Get agent bookings', 'latepoint' ); |
| 11 | $this->description = __( 'Returns bookings assigned to a specific agent.', 'latepoint' ); |
| 12 | $this->permission = 'agent__view'; |
| 13 | $this->read_only = true; |
| 14 | } |
| 15 | |
| 16 | public function get_input_schema(): array { |
| 17 | return [ |
| 18 | 'type' => 'object', |
| 19 | 'properties' => array_merge( |
| 20 | self::pagination(), |
| 21 | [ |
| 22 | 'agent_id' => [ |
| 23 | 'type' => 'integer', |
| 24 | 'description' => __( 'Agent ID.', 'latepoint' ), |
| 25 | ], |
| 26 | 'date_from' => [ |
| 27 | 'type' => 'string', |
| 28 | 'format' => 'date', |
| 29 | ], |
| 30 | 'date_to' => [ |
| 31 | 'type' => 'string', |
| 32 | 'format' => 'date', |
| 33 | ], |
| 34 | 'status' => [ 'type' => 'string' ], |
| 35 | ] |
| 36 | ), |
| 37 | 'required' => [ 'agent_id' ], |
| 38 | ]; |
| 39 | } |
| 40 | |
| 41 | public function get_output_schema(): array { |
| 42 | return [ |
| 43 | 'type' => 'object', |
| 44 | 'properties' => [ |
| 45 | 'bookings' => [ |
| 46 | 'type' => 'array', |
| 47 | 'items' => [ 'type' => 'object' ], |
| 48 | ], |
| 49 | 'total' => [ 'type' => 'integer' ], |
| 50 | 'page' => [ 'type' => 'integer' ], |
| 51 | 'per_page' => [ 'type' => 'integer' ], |
| 52 | ], |
| 53 | ]; |
| 54 | } |
| 55 | |
| 56 | public function execute( array $args ) { |
| 57 | $agent = new OsAgentModel( (int) $args['agent_id'] ); |
| 58 | if ( $agent->is_new_record() ) { |
| 59 | return new WP_Error( 'not_found', __( 'Agent not found.', 'latepoint' ), [ 'status' => 404 ] ); |
| 60 | } |
| 61 | |
| 62 | $page = max( 1, (int) ( $args['page'] ?? 1 ) ); |
| 63 | $per_page = min( 100, max( 1, (int) ( $args['per_page'] ?? 20 ) ) ); |
| 64 | $offset = ( $page - 1 ) * $per_page; |
| 65 | |
| 66 | $query = ( new OsBookingModel() )->where( [ 'agent_id' => $agent->id ] ); |
| 67 | if ( ! empty( $args['status'] ) ) { |
| 68 | $query->where( [ 'status' => sanitize_text_field( $args['status'] ) ] ); |
| 69 | } |
| 70 | if ( ! empty( $args['date_from'] ) ) { |
| 71 | $query->where( [ 'start_date >=' => sanitize_text_field( $args['date_from'] ) ] ); |
| 72 | } |
| 73 | if ( ! empty( $args['date_to'] ) ) { |
| 74 | $query->where( [ 'start_date <=' => sanitize_text_field( $args['date_to'] ) ] ); |
| 75 | } |
| 76 | |
| 77 | $bookings = ( clone $query ) |
| 78 | ->order_by( 'start_date ASC, start_time ASC' ) |
| 79 | ->set_limit( $per_page ) |
| 80 | ->set_offset( $offset ) |
| 81 | ->get_results_as_models(); |
| 82 | $total = $query->count(); |
| 83 | |
| 84 | $booking_serializer = new LatePointAbilityListBookings(); |
| 85 | return [ |
| 86 | 'bookings' => array_map( [ $booking_serializer, 'serialize_booking' ], $bookings ), |
| 87 | 'total' => (int) $total, |
| 88 | 'page' => $page, |
| 89 | 'per_page' => $per_page, |
| 90 | ]; |
| 91 | } |
| 92 | } |
| 93 |