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-daily-chart-data.php
89 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | class LatePointAbilityGetDailyChartData extends LatePointAbstractAnalyticsAbility { |
| 7 | |
| 8 | protected function configure(): void { |
| 9 | $this->id = 'latepoint/get-daily-chart-data'; |
| 10 | $this->label = __( 'Get daily chart data', 'latepoint' ); |
| 11 | $this->description = __( 'Returns daily booking counts or revenue for charting over a date range.', '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 | 'metric' => [ |
| 29 | 'type' => 'string', |
| 30 | 'enum' => [ 'bookings', 'revenue' ], |
| 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 | 'days' => [ |
| 42 | 'type' => 'array', |
| 43 | 'items' => [ |
| 44 | 'type' => 'object', |
| 45 | 'properties' => [ |
| 46 | 'date' => [ |
| 47 | 'type' => 'string', |
| 48 | 'format' => 'date', |
| 49 | ], |
| 50 | 'value' => [ 'type' => 'number' ], |
| 51 | ], |
| 52 | ], |
| 53 | ], |
| 54 | ], |
| 55 | ]; |
| 56 | } |
| 57 | |
| 58 | public function execute( array $args ) { |
| 59 | $date_from = sanitize_text_field( $args['date_from'] ); |
| 60 | $date_to = sanitize_text_field( $args['date_to'] ); |
| 61 | $metric = ! empty( $args['metric'] ) ? sanitize_text_field( $args['metric'] ) : 'bookings'; |
| 62 | $filter = new \LatePoint\Misc\Filter(); |
| 63 | |
| 64 | if ( $metric === 'revenue' ) { |
| 65 | $rows = OsBookingHelper::get_stat_for_period( 'price', $date_from, $date_to, $filter, 'start_date' ); |
| 66 | $days = []; |
| 67 | if ( is_array( $rows ) ) { |
| 68 | foreach ( $rows as $row ) { |
| 69 | $days[] = [ |
| 70 | 'date' => $row['start_date'] ?? $row[0] ?? '', |
| 71 | 'value' => (float) ( $row['price'] ?? $row['value'] ?? $row[1] ?? 0 ), |
| 72 | ]; |
| 73 | } |
| 74 | } |
| 75 | } else { |
| 76 | $rows = OsBookingHelper::get_total_bookings_per_day_for_period( $date_from, $date_to, $filter ); |
| 77 | $days = array_map( |
| 78 | fn( $row ) => [ |
| 79 | 'date' => ( (array) $row )['start_date'] ?? '', |
| 80 | 'value' => (float) ( ( (array) $row )['bookings_per_day'] ?? 0 ), |
| 81 | ], |
| 82 | $rows |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | return [ 'days' => $days ]; |
| 87 | } |
| 88 | } |
| 89 |