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-dashboard-stats.php
65 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | class LatePointAbilityGetDashboardStats extends LatePointAbstractAnalyticsAbility { |
| 7 | |
| 8 | protected function configure(): void { |
| 9 | $this->id = 'latepoint/get-dashboard-stats'; |
| 10 | $this->label = __( 'Get dashboard stats', 'latepoint' ); |
| 11 | $this->description = __( 'Returns aggregate booking statistics for a date range (defaults to current month).', '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 | 'description' => __( 'Period start (Y-m-d). Defaults to first day of current month.', 'latepoint' ), |
| 24 | ], |
| 25 | 'date_to' => [ |
| 26 | 'type' => 'string', |
| 27 | 'format' => 'date', |
| 28 | 'description' => __( 'Period end (Y-m-d). Defaults to last day of current month.', 'latepoint' ), |
| 29 | ], |
| 30 | ], |
| 31 | ]; |
| 32 | } |
| 33 | |
| 34 | public function get_output_schema(): array { |
| 35 | return [ |
| 36 | 'type' => 'object', |
| 37 | 'properties' => [ |
| 38 | 'bookings_count' => [ 'type' => 'integer' ], |
| 39 | 'revenue' => [ 'type' => 'number' ], |
| 40 | 'customers_count' => [ 'type' => 'integer' ], |
| 41 | 'pending_count' => [ 'type' => 'integer' ], |
| 42 | ], |
| 43 | ]; |
| 44 | } |
| 45 | |
| 46 | public function execute( array $args ) { |
| 47 | $date_from = ! empty( $args['date_from'] ) ? sanitize_text_field( $args['date_from'] ) : wp_date( 'Y-m-01' ); |
| 48 | $date_to = ! empty( $args['date_to'] ) ? sanitize_text_field( $args['date_to'] ) : wp_date( 'Y-m-t' ); |
| 49 | |
| 50 | $filter = new \LatePoint\Misc\Filter(); |
| 51 | |
| 52 | $bookings_count = (int) OsBookingHelper::get_stat_for_period( 'bookings', $date_from, $date_to, $filter ); |
| 53 | $revenue = (float) OsBookingHelper::get_stat_for_period( 'price', $date_from, $date_to, $filter ); |
| 54 | $customers_count = (int) ( new OsCustomerModel() )->count(); |
| 55 | $pending_count = (int) ( new OsBookingModel() )->where( [ 'status' => LATEPOINT_BOOKING_STATUS_PENDING ] )->count(); |
| 56 | |
| 57 | return [ |
| 58 | 'bookings_count' => $bookings_count, |
| 59 | 'revenue' => $revenue, |
| 60 | 'customers_count' => $customers_count, |
| 61 | 'pending_count' => $pending_count, |
| 62 | ]; |
| 63 | } |
| 64 | } |
| 65 |