# booktics/1.0.3/core/appointment/services/appointment-service.php

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

- Page: https://pluginprobe.com/plugins/booktics/1.0.3/code/core/appointment/services/appointment-service.php
- Raw: https://pluginprobe.com/plugins/booktics/1.0.3/raw/core/appointment/services/appointment-service.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/core/appointment/services/appointment-service.php#L10-L20`.

```php
<?php
namespace Booktics\Appointment\Services;

use Booktics\Models\Appointment_Model;
use Booktics\Models\Customer_Model;
use Booktics\Utils\Singleton;
use WP_Error;

class Appointment_Service {

    use Singleton;

    /**
     * Create a new appointment.
     * @param array $data
     * @return mixed (Appointment object or WP_Error)
     */
    public static function create(array $data) {
        // Sanitize and prepare data as needed
        $appointment = Appointment_Model::create($data);
        return $appointment;
    }

    /**
     * Update an existing appointment.
     * @param int $id
     * @param array $data
     * @return mixed (Updated appointment object or WP_Error)
     */
    public static function update(int $id, array $data) {
        $appointment = Appointment_Model::find($id);
        if (!$appointment) {
            return new WP_Error('not_found', __('Appointment not found', 'booktics'));
        }
        // Optionally handle customer update
        if (!empty($data['customer'])) {
            $customer = (new Customer_Model())->find($appointment->customer_id);
            if ($customer) {
                $updated_customer = $customer->update($data['customer']);
                unset($data['customer']);
                $appointment->customer = $updated_customer;
            }
        }
        $updated_appointment = $appointment->update($data);
        return $updated_appointment;
    }

    /**
     * Delete an appointment by ID.
     * @param int $id
     * @return mixed (Deleted appointment object or WP_Error)
     */
    public static function delete(int $id) {
        $appointment = Appointment_Model::find($id);
        if (!$appointment) {
            return new WP_Error('not_found', __('Appointment not found', 'booktics'));
        }
        $deleted = $appointment->delete();
        if (is_wp_error($deleted)) {
            return $deleted;
        }
        return $appointment;
    }

    /**
     * Delete multiple appointments by IDs.
     * @param array $ids
     * @return array|WP_Error
     */
    public static function delete_many(array $ids) {
        if (!is_array($ids)) {
            return new WP_Error('invalid_ids', __('Ids should be an array of items ids', 'booktics'));
        }
        $count = 0;
        $total = count($ids);
        $deleted = [];
        foreach ($ids as $id) {
            $appointment = Appointment_Model::find($id);
            if ($appointment) {
                $appointment->delete();
                $deleted[] = $appointment;
                ++$count;
            }
        }
        if ($count === 0) {
            return new WP_Error('none_deleted', __('No item deleted.', 'booktics'));
        }
        $message = sprintf('Deleted %d items of %d', $count, $total);
        return [ 'data' => $deleted, 'message' => $message ];
    }

    /**
     * Reschedule an appointment.
     * @param int $id
     * @param array $data
     * @return mixed (Updated appointment object or WP_Error)
     */
    public static function reschedule(int $id, array $data) {
        $appointment = Appointment_Model::find($id);
        if (!$appointment) {
            return new WP_Error('not_found', __('Appointment not found', 'booktics'));
        }
        $updated = $appointment->update([
            'start_time' => sanitize_text_field($data['start_time']),
            'end_time'   => sanitize_text_field($data['end_time']),
            'date'       => sanitize_text_field($data['date']),
        ]);
        if (is_wp_error($updated)) {
            return new WP_Error('reschedule_error', __('Appointment reschedule error.', 'booktics'));
        }
        // Optionally fire hooks here
        return $appointment;
    }

    /**
     * Cancel an appointment.
     * @param int $id
     * @return mixed (Updated appointment object or WP_Error)
     */
    public static function cancel(int $id) {
        $appointment = Appointment_Model::find($id);
        if (!$appointment) {
            return new WP_Error('not_found', __('Appointment not found', 'booktics'));
        }
        $updated = $appointment->update(['status' => 'cancelled']);
        if (is_wp_error($updated)) {
            return new WP_Error('cancel_error', __('Appointment cancellation error.', 'booktics'));
        }
        // Optionally fire hooks here
        return $appointment;
    }
} 
```
