'', 'title' => '', 'content' => '', 'start_time' => '', 'end_time' => '', 'date' => '', 'buffer_before' => '', 'buffer_after' => '', 'duration' => '', 'status' => '', 'payment_status' => '', 'customer_id' => '', 'customer_name' => '', 'team_member_id' => '', 'service_id' => '', 'additional_duration_id' => '', 'group_booking' => [], 'order_id' => '', 'quantity' => 0, 'price' => 0, 'subtotal' => 0, 'total' => 0, 'note' => '', 'source' => '', ]; /** * Accepted post statuses for appointments * * @var array */ public static $accepted_statuses = [ 'pending', 'active', 'cancelled', 'canceled', 'completed', 'approved', 'not_now', ]; /** * Get post type * * @return string Post type name */ public function get_post_type() { return 'booktics-appointment'; } /** * Get accepted statuses * * @return array Accepted statuses */ public static function get_accepted_statuses() { return self::$accepted_statuses; } /** * Specify data which should be serialized to JSON. * * @return array */ public function jsonSerialize(): array { $data = $this->to_array(); $service = Service_Model::find( $this->service_id ); $customer = ( new Customer_Model() )->find( $this->customer_id ); $team_member = ( new Team_Member_Model() )->find( $this->team_member_id ); $appointment_data = [ 'service' => $service, 'team_member' => $team_member, 'customer' => $customer ]; return array_merge( $data, $appointment_data ); } /** * Get appointments for a specific date and service/team member * * @param string $date * @param int $team_member_id * * @return array * @throws Exception */ public function get_appointments_for_slot( string $date, int $team_member_id ): array { $args = [ 'post_type' => $this->get_post_type(), 'post_status' => [ 'publish', 'active', 'pending' ], 'posts_per_page' => - 1, 'meta_query' => [ 'relation' => 'AND', [ 'key' => 'date', 'value' => $date, 'compare' => '=' ], [ 'key' => 'team_member_id', 'value' => $team_member_id, 'compare' => '=' ] ] ]; $query = new \WP_Query( $args ); return array_map( function ( $post ) { return new static( $post ); }, $query->posts ); } /** * Override load_attributes to include payment status from orders * * @return void */ protected function load_attributes() { parent::load_attributes(); if ( $this->order_id ) { $db = new Booktics_Database(); $order_model = ( new Order_Model( $db ) )->find( [ 'id' => $this->order_id ] ); $this->attributes['payment_status'] = $order_model->payment_status ?? 'pending'; } else { $this->attributes['payment_status'] = 'pending'; } } /** * Get appointments count by order ID * * @param int $order_id Order ID * * @return int Number of appointments */ public function get_appointments_count_by_order_id( $order_id ) { $args = [ 'post_type' => $this->get_post_type(), 'post_status' => [ 'publish', 'active', 'pending' ], 'posts_per_page' => - 1, 'meta_query' => [ [ 'key' => 'order_id', 'value' => $order_id, 'compare' => '=' ] ] ]; $query = new \WP_Query( $args ); return $query->found_posts; } /** * Get appointments count by team member ID * * @param int $team_member_id Team member ID * * @return int Number of appointments */ public function get_appointments_count_by_team_member_id( $team_member_id ) { $args = [ 'post_type' => $this->get_post_type(), 'post_status' => [ 'publish', 'active', 'pending' ], 'posts_per_page' => - 1, 'meta_query' => [ [ 'key' => 'team_member_id', 'value' => $team_member_id, 'compare' => '=' ] ] ]; $query = new \WP_Query( $args ); return $query->found_posts; } }