PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.3
Booktics – Appointment Booking Calendar for Service Businesses v1.0.3
1.0.29 1.0.28 1.0.27 1.0.26 1.0.25 1.0.24 1.0.23 1.0.22 1.0.21 1.0.20 1.0.19 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.2 1.0.3 All 31 releases
booktics / trunk / core / appointment / services / appointment-service.php

appointment-service.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.3, at trunk/core/appointment/services/appointment-service.php

132 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Booktics\Appointment\Services;
3
4 use Booktics\Models\Appointment_Model;
5 use Booktics\Models\Customer_Model;
6 use Booktics\Utils\Singleton;
7 use WP_Error;
8
9 class Appointment_Service {
10
11 use Singleton;
12
13 /**
14 * Create a new appointment.
15 * @param array $data
16 * @return mixed (Appointment object or WP_Error)
17 */
18 public static function create(array $data) {
19 // Sanitize and prepare data as needed
20 $appointment = Appointment_Model::create($data);
21 return $appointment;
22 }
23
24 /**
25 * Update an existing appointment.
26 * @param int $id
27 * @param array $data
28 * @return mixed (Updated appointment object or WP_Error)
29 */
30 public static function update(int $id, array $data) {
31 $appointment = Appointment_Model::find($id);
32 if (!$appointment) {
33 return new WP_Error('not_found', __('Appointment not found', 'booktics'));
34 }
35 // Optionally handle customer update
36 if (!empty($data['customer'])) {
37 $customer = (new Customer_Model())->find($appointment->customer_id);
38 if ($customer) {
39 $updated_customer = $customer->update($data['customer']);
40 unset($data['customer']);
41 $appointment->customer = $updated_customer;
42 }
43 }
44 $updated_appointment = $appointment->update($data);
45 return $updated_appointment;
46 }
47
48 /**
49 * Delete an appointment by ID.
50 * @param int $id
51 * @return mixed (Deleted appointment object or WP_Error)
52 */
53 public static function delete(int $id) {
54 $appointment = Appointment_Model::find($id);
55 if (!$appointment) {
56 return new WP_Error('not_found', __('Appointment not found', 'booktics'));
57 }
58 $deleted = $appointment->delete();
59 if (is_wp_error($deleted)) {
60 return $deleted;
61 }
62 return $appointment;
63 }
64
65 /**
66 * Delete multiple appointments by IDs.
67 * @param array $ids
68 * @return array|WP_Error
69 */
70 public static function delete_many(array $ids) {
71 if (!is_array($ids)) {
72 return new WP_Error('invalid_ids', __('Ids should be an array of items ids', 'booktics'));
73 }
74 $count = 0;
75 $total = count($ids);
76 $deleted = [];
77 foreach ($ids as $id) {
78 $appointment = Appointment_Model::find($id);
79 if ($appointment) {
80 $appointment->delete();
81 $deleted[] = $appointment;
82 ++$count;
83 }
84 }
85 if ($count === 0) {
86 return new WP_Error('none_deleted', __('No item deleted.', 'booktics'));
87 }
88 $message = sprintf('Deleted %d items of %d', $count, $total);
89 return [ 'data' => $deleted, 'message' => $message ];
90 }
91
92 /**
93 * Reschedule an appointment.
94 * @param int $id
95 * @param array $data
96 * @return mixed (Updated appointment object or WP_Error)
97 */
98 public static function reschedule(int $id, array $data) {
99 $appointment = Appointment_Model::find($id);
100 if (!$appointment) {
101 return new WP_Error('not_found', __('Appointment not found', 'booktics'));
102 }
103 $updated = $appointment->update([
104 'start_time' => sanitize_text_field($data['start_time']),
105 'end_time' => sanitize_text_field($data['end_time']),
106 'date' => sanitize_text_field($data['date']),
107 ]);
108 if (is_wp_error($updated)) {
109 return new WP_Error('reschedule_error', __('Appointment reschedule error.', 'booktics'));
110 }
111 // Optionally fire hooks here
112 return $appointment;
113 }
114
115 /**
116 * Cancel an appointment.
117 * @param int $id
118 * @return mixed (Updated appointment object or WP_Error)
119 */
120 public static function cancel(int $id) {
121 $appointment = Appointment_Model::find($id);
122 if (!$appointment) {
123 return new WP_Error('not_found', __('Appointment not found', 'booktics'));
124 }
125 $updated = $appointment->update(['status' => 'cancelled']);
126 if (is_wp_error($updated)) {
127 return new WP_Error('cancel_error', __('Appointment cancellation error.', 'booktics'));
128 }
129 // Optionally fire hooks here
130 return $appointment;
131 }
132 }