abstract-customer-ability.php
4 months ago
connect-customer-to-wp-user.php
1 week ago
create-customer.php
4 months ago
delete-customer.php
1 week ago
get-customer-bookings.php
1 week ago
get-customer-by-email.php
1 week ago
get-customer-orders.php
1 week ago
get-customer.php
1 week ago
get-total-customers-count.php
1 week ago
list-customers.php
1 week ago
search-customers.php
1 week ago
update-customer.php
1 week ago
get-customer-bookings.php
100 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | class LatePointAbilityGetCustomerBookings extends LatePointAbstractCustomerAbility { |
| 7 | |
| 8 | protected function configure(): void { |
| 9 | $this->id = 'latepoint/get-customer-bookings'; |
| 10 | $this->label = __( 'Get customer bookings', 'latepoint' ); |
| 11 | $this->description = __( 'Returns bookings for a specific customer, with optional future/past/status filter.', 'latepoint' ); |
| 12 | $this->permission = 'customer__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 | 'customer_id' => [ |
| 23 | 'type' => 'integer', |
| 24 | 'description' => __( 'Customer ID.', 'latepoint' ), |
| 25 | ], |
| 26 | 'status' => [ |
| 27 | 'type' => 'string', |
| 28 | 'description' => __( 'Filter by booking status.', 'latepoint' ), |
| 29 | ], |
| 30 | 'time_scope' => [ |
| 31 | 'type' => 'string', |
| 32 | 'enum' => [ 'upcoming', 'past', 'all' ], |
| 33 | 'default' => 'all', |
| 34 | 'description' => __( 'Time scope filter.', 'latepoint' ), |
| 35 | ], |
| 36 | ] |
| 37 | ), |
| 38 | 'required' => [ 'customer_id' ], |
| 39 | ]; |
| 40 | } |
| 41 | |
| 42 | public function get_output_schema(): array { |
| 43 | return [ |
| 44 | 'type' => 'object', |
| 45 | 'properties' => [ |
| 46 | 'bookings' => [ |
| 47 | 'type' => 'array', |
| 48 | 'items' => [ 'type' => 'object' ], |
| 49 | ], |
| 50 | 'total' => [ 'type' => 'integer' ], |
| 51 | 'page' => [ 'type' => 'integer' ], |
| 52 | 'per_page' => [ 'type' => 'integer' ], |
| 53 | ], |
| 54 | ]; |
| 55 | } |
| 56 | |
| 57 | public function execute( array $args ) { |
| 58 | $customer = new OsCustomerModel( (int) $args['customer_id'] ); |
| 59 | if ( $customer->is_new_record() ) { |
| 60 | return new WP_Error( 'not_found', __( 'Customer not found.', 'latepoint' ), [ 'status' => 404 ] ); |
| 61 | } |
| 62 | $auth = $this->authorize_record( $customer, 'view' ); |
| 63 | if ( is_wp_error( $auth ) ) { |
| 64 | return $auth; |
| 65 | } |
| 66 | |
| 67 | $page = max( 1, (int) ( $args['page'] ?? 1 ) ); |
| 68 | $per_page = min( 100, max( 1, (int) ( $args['per_page'] ?? 20 ) ) ); |
| 69 | $offset = ( $page - 1 ) * $per_page; |
| 70 | $scope = $args['time_scope'] ?? 'all'; |
| 71 | |
| 72 | $query = ( new OsBookingModel() )->where( [ 'customer_id' => $customer->id ] ); |
| 73 | $query->filter_allowed_records(); |
| 74 | |
| 75 | if ( ! empty( $args['status'] ) ) { |
| 76 | $query->where( [ 'status' => sanitize_text_field( $args['status'] ) ] ); |
| 77 | } |
| 78 | if ( $scope === 'upcoming' ) { |
| 79 | $query->where( [ 'start_date >=' => wp_date( 'Y-m-d' ) ] ); |
| 80 | } elseif ( $scope === 'past' ) { |
| 81 | $query->where( [ 'start_date <' => wp_date( 'Y-m-d' ) ] ); |
| 82 | } |
| 83 | |
| 84 | $bookings = ( clone $query ) |
| 85 | ->order_by( 'start_date ASC, start_time ASC' ) |
| 86 | ->set_limit( $per_page ) |
| 87 | ->set_offset( $offset ) |
| 88 | ->get_results_as_models(); |
| 89 | $total = $query->count(); |
| 90 | |
| 91 | $booking_serializer = new LatePointAbilityListBookings(); |
| 92 | return [ |
| 93 | 'bookings' => array_map( [ $booking_serializer, 'serialize_booking' ], $bookings ), |
| 94 | 'total' => (int) $total, |
| 95 | 'page' => $page, |
| 96 | 'per_page' => $per_page, |
| 97 | ]; |
| 98 | } |
| 99 | } |
| 100 |