# booktics/1.0.3/base/models/appointment-model.php

Booktics – Appointment Booking Calendar for Service Businesses, version 1.0.3. 210 lines.

- Page: https://pluginprobe.com/plugins/booktics/1.0.3/code/base/models/appointment-model.php
- Raw: https://pluginprobe.com/plugins/booktics/1.0.3/raw/base/models/appointment-model.php
- Modified: 2025-07-10T13:13:56+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/booktics/1.0.3/code/base/models/appointment-model.php#L10-L20`.

```php
<?php
/**
 * Appoint Model
 *
 * @package Booktics/Models
 */

namespace Booktics\Models;

use Booktics\Abstracts\Booktics_Database;
use Booktics\Abstracts\Post_Model;
use Exception;

/**
 * AppointmentModel class
 */
class Appointment_Model extends Post_Model {
    /**
     * Store appointment data
     *
     * @var array
     */
    protected $fillable = [
        'id'                     => '',
        '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;
    }
}

```
