abstract-booking-ability.php
1 week ago
approve-booking.php
4 months ago
cancel-booking.php
4 months ago
change-booking-status.php
1 week ago
create-booking.php
4 months ago
delete-booking.php
1 week ago
get-booking-stats.php
1 week ago
get-booking-statuses.php
4 months ago
get-booking.php
1 week ago
get-bookings-for-date.php
4 months ago
get-bookings-per-day.php
1 week ago
get-upcoming-bookings.php
4 months ago
list-bookings.php
4 months ago
reschedule-booking.php
1 week ago
update-booking.php
1 week ago
get-booking-stats.php
87 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; } |
| 4 | |
| 5 | class LatePointAbilityGetBookingStats extends LatePointAbstractBookingAbility { |
| 6 | |
| 7 | protected function configure(): void { |
| 8 | $this->id = 'latepoint/get-booking-stats'; |
| 9 | $this->label = __( 'Get booking stats', 'latepoint' ); |
| 10 | $this->description = __( 'Returns aggregate booking statistics (count, revenue, or duration) for a date range.', 'latepoint' ); |
| 11 | $this->permission = 'booking__view'; |
| 12 | $this->read_only = true; |
| 13 | } |
| 14 | |
| 15 | public function get_input_schema(): array { |
| 16 | return [ |
| 17 | 'type' => 'object', |
| 18 | 'properties' => [ |
| 19 | 'stat' => [ |
| 20 | 'type' => 'string', |
| 21 | 'enum' => [ 'bookings', 'price', 'duration' ], |
| 22 | 'description' => __( 'Metric to aggregate.', 'latepoint' ), |
| 23 | ], |
| 24 | 'date_from' => [ |
| 25 | 'type' => 'string', |
| 26 | 'format' => 'date', |
| 27 | 'description' => __( 'Period start (Y-m-d).', 'latepoint' ), |
| 28 | ], |
| 29 | 'date_to' => [ |
| 30 | 'type' => 'string', |
| 31 | 'format' => 'date', |
| 32 | 'description' => __( 'Period end (Y-m-d).', 'latepoint' ), |
| 33 | ], |
| 34 | 'agent_id' => [ 'type' => 'integer' ], |
| 35 | 'service_id' => [ 'type' => 'integer' ], |
| 36 | 'location_id' => [ 'type' => 'integer' ], |
| 37 | 'group_by' => [ |
| 38 | 'type' => 'string', |
| 39 | 'enum' => [ 'agent_id', 'service_id', 'location_id' ], |
| 40 | 'description' => __( 'Optional grouping dimension.', 'latepoint' ), |
| 41 | ], |
| 42 | ], |
| 43 | 'required' => [ 'stat', 'date_from', 'date_to' ], |
| 44 | ]; |
| 45 | } |
| 46 | |
| 47 | public function get_output_schema(): array { |
| 48 | return [ |
| 49 | 'type' => 'object', |
| 50 | 'properties' => [ |
| 51 | 'stat' => [ 'type' => 'string' ], |
| 52 | 'result' => [ |
| 53 | 'type' => 'number', |
| 54 | 'description' => __( 'Numeric total when no group_by; array of {value, group_by_value} objects when group_by is set.', 'latepoint' ), |
| 55 | ], |
| 56 | ], |
| 57 | ]; |
| 58 | } |
| 59 | |
| 60 | public function execute( array $args ) { |
| 61 | $filter = new \LatePoint\Misc\Filter(); |
| 62 | $filter->agent_id = ! empty( $args['agent_id'] ) ? (int) $args['agent_id'] : 0; |
| 63 | $filter->service_id = ! empty( $args['service_id'] ) ? (int) $args['service_id'] : 0; |
| 64 | $filter->location_id = ! empty( $args['location_id'] ) ? (int) $args['location_id'] : 0; |
| 65 | // Scope aggregate stats to the agents/services/locations the current user is allowed to access. |
| 66 | $filter = OsRolesHelper::filter_allowed_records_from_arguments_or_filter( $filter ); |
| 67 | |
| 68 | $group_by = ! empty( $args['group_by'] ) ? sanitize_text_field( $args['group_by'] ) : false; |
| 69 | $result = OsBookingHelper::get_stat_for_period( |
| 70 | sanitize_text_field( $args['stat'] ), |
| 71 | sanitize_text_field( $args['date_from'] ), |
| 72 | sanitize_text_field( $args['date_to'] ), |
| 73 | $filter, |
| 74 | $group_by |
| 75 | ); |
| 76 | |
| 77 | if ( $result === false ) { |
| 78 | return new WP_Error( 'invalid_stat', __( 'Invalid stat or group_by parameter.', 'latepoint' ), [ 'status' => 400 ] ); |
| 79 | } |
| 80 | |
| 81 | return [ |
| 82 | 'stat' => $args['stat'], |
| 83 | 'result' => $result, |
| 84 | ]; |
| 85 | } |
| 86 | } |
| 87 |