abstract-analytics-ability.php
3 months ago
get-daily-chart-data.php
3 months ago
get-dashboard-stats.php
3 months ago
get-pending-bookings-count.php
3 months ago
get-top-services.php
3 months ago
get-top-services.php
102 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | class LatePointAbilityGetTopServices extends LatePointAbstractAnalyticsAbility { |
| 7 | |
| 8 | protected function configure(): void { |
| 9 | $this->id = 'latepoint/get-top-services'; |
| 10 | $this->label = __( 'Get top services', 'latepoint' ); |
| 11 | $this->description = __( 'Returns the most-booked services with booking counts and revenue for a period.', 'latepoint' ); |
| 12 | $this->permission = 'booking__view'; |
| 13 | $this->read_only = true; |
| 14 | } |
| 15 | |
| 16 | public function get_input_schema(): array { |
| 17 | return [ |
| 18 | 'type' => 'object', |
| 19 | 'properties' => [ |
| 20 | 'date_from' => [ |
| 21 | 'type' => 'string', |
| 22 | 'format' => 'date', |
| 23 | ], |
| 24 | 'date_to' => [ |
| 25 | 'type' => 'string', |
| 26 | 'format' => 'date', |
| 27 | ], |
| 28 | 'limit' => [ |
| 29 | 'type' => 'integer', |
| 30 | 'default' => 5, |
| 31 | ], |
| 32 | ], |
| 33 | 'required' => [ 'date_from', 'date_to' ], |
| 34 | ]; |
| 35 | } |
| 36 | |
| 37 | public function get_output_schema(): array { |
| 38 | return [ |
| 39 | 'type' => 'object', |
| 40 | 'properties' => [ |
| 41 | 'services' => [ |
| 42 | 'type' => 'array', |
| 43 | 'items' => [ |
| 44 | 'type' => 'object', |
| 45 | 'properties' => [ |
| 46 | 'service_id' => [ 'type' => 'integer' ], |
| 47 | 'service_name' => [ 'type' => 'string' ], |
| 48 | 'count' => [ 'type' => 'integer' ], |
| 49 | 'revenue' => [ 'type' => 'number' ], |
| 50 | ], |
| 51 | ], |
| 52 | ], |
| 53 | ], |
| 54 | ]; |
| 55 | } |
| 56 | |
| 57 | public function execute( array $args ) { |
| 58 | global $wpdb; |
| 59 | |
| 60 | $date_from = sanitize_text_field( $args['date_from'] ); |
| 61 | $date_to = sanitize_text_field( $args['date_to'] ); |
| 62 | $limit = ! empty( $args['limit'] ) ? max( 1, min( 50, (int) $args['limit'] ) ) : 5; |
| 63 | $bookings_table = LATEPOINT_TABLE_BOOKINGS; |
| 64 | $services_table = LATEPOINT_TABLE_SERVICES; |
| 65 | |
| 66 | $order_items_table = LATEPOINT_TABLE_ORDER_ITEMS; |
| 67 | |
| 68 | $rows = $wpdb->get_results( |
| 69 | $wpdb->prepare( |
| 70 | "SELECT b.service_id, s.name AS service_name, |
| 71 | COUNT(*) AS booking_count, |
| 72 | SUM(oi.subtotal) AS total_revenue |
| 73 | FROM {$bookings_table} b |
| 74 | LEFT JOIN {$services_table} s ON s.id = b.service_id |
| 75 | LEFT JOIN {$order_items_table} oi ON oi.id = b.order_item_id |
| 76 | WHERE b.start_date >= %s AND b.start_date <= %s |
| 77 | AND b.status != %s |
| 78 | GROUP BY b.service_id |
| 79 | ORDER BY booking_count DESC |
| 80 | LIMIT %d", |
| 81 | $date_from, |
| 82 | $date_to, |
| 83 | LATEPOINT_BOOKING_STATUS_CANCELLED, |
| 84 | $limit |
| 85 | ), |
| 86 | ARRAY_A |
| 87 | ); |
| 88 | |
| 89 | $services = array_map( |
| 90 | fn( $row ) => [ |
| 91 | 'service_id' => (int) $row['service_id'], |
| 92 | 'service_name' => $row['service_name'] ?? '', |
| 93 | 'count' => (int) $row['booking_count'], |
| 94 | 'revenue' => (float) ( $row['total_revenue'] ?? 0 ), |
| 95 | ], |
| 96 | $rows ?? [] |
| 97 | ); |
| 98 | |
| 99 | return [ 'services' => $services ]; |
| 100 | } |
| 101 | } |
| 102 |