| 1 |
<?php |
| 2 |
/** |
| 3 |
* Appoint Model |
| 4 |
* |
| 5 |
* @package Booktics/Models |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Booktics\Models; |
| 9 |
|
| 10 |
use Booktics\Abstracts\Booktics_Database; |
| 11 |
use Booktics\Abstracts\Post_Model; |
| 12 |
use Exception; |
| 13 |
|
| 14 |
/** |
| 15 |
* AppointmentModel class |
| 16 |
*/ |
| 17 |
class Appointment_Model extends Post_Model { |
| 18 |
/** |
| 19 |
* Store appointment data |
| 20 |
* |
| 21 |
* @var array |
| 22 |
*/ |
| 23 |
protected $fillable = [ |
| 24 |
'id' => '', |
| 25 |
'title' => '', |
| 26 |
'content' => '', |
| 27 |
'start_time' => '', |
| 28 |
'end_time' => '', |
| 29 |
'date' => '', |
| 30 |
'buffer_before' => '', |
| 31 |
'buffer_after' => '', |
| 32 |
'duration' => '', |
| 33 |
'status' => '', |
| 34 |
'payment_status' => '', |
| 35 |
'customer_id' => '', |
| 36 |
'customer_name' => '', |
| 37 |
'team_member_id' => '', |
| 38 |
'service_id' => '', |
| 39 |
'additional_duration_id' => '', |
| 40 |
'group_booking' => [], |
| 41 |
'order_id' => '', |
| 42 |
'quantity' => 0, |
| 43 |
'price' => 0, |
| 44 |
'subtotal' => 0, |
| 45 |
'total' => 0, |
| 46 |
'note' => '', |
| 47 |
'source' => '', |
| 48 |
]; |
| 49 |
|
| 50 |
/** |
| 51 |
* Accepted post statuses for appointments |
| 52 |
* |
| 53 |
* @var array |
| 54 |
*/ |
| 55 |
public static $accepted_statuses = [ |
| 56 |
'pending', |
| 57 |
'active', |
| 58 |
'cancelled', |
| 59 |
'canceled', |
| 60 |
'completed', |
| 61 |
'approved', |
| 62 |
'not_now', |
| 63 |
]; |
| 64 |
|
| 65 |
/** |
| 66 |
* Get post type |
| 67 |
* |
| 68 |
* @return string Post type name |
| 69 |
*/ |
| 70 |
public function get_post_type() { |
| 71 |
return 'booktics-appointment'; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Get accepted statuses |
| 76 |
* |
| 77 |
* @return array Accepted statuses |
| 78 |
*/ |
| 79 |
public static function get_accepted_statuses() { |
| 80 |
return self::$accepted_statuses; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Specify data which should be serialized to JSON. |
| 85 |
* |
| 86 |
* @return array |
| 87 |
*/ |
| 88 |
public function jsonSerialize(): array { |
| 89 |
$data = $this->to_array(); |
| 90 |
|
| 91 |
$service = Service_Model::find( $this->service_id ); |
| 92 |
$customer = ( new Customer_Model() )->find( $this->customer_id ); |
| 93 |
$team_member = ( new Team_Member_Model() )->find( $this->team_member_id ); |
| 94 |
|
| 95 |
$appointment_data = [ |
| 96 |
'service' => $service, |
| 97 |
'team_member' => $team_member, |
| 98 |
'customer' => $customer |
| 99 |
]; |
| 100 |
|
| 101 |
return array_merge( $data, $appointment_data ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Get appointments for a specific date and service/team member |
| 106 |
* |
| 107 |
* @param string $date |
| 108 |
* @param int $team_member_id |
| 109 |
* |
| 110 |
* @return array |
| 111 |
* @throws Exception |
| 112 |
*/ |
| 113 |
public function get_appointments_for_slot( string $date, int $team_member_id ): array { |
| 114 |
$args = [ |
| 115 |
'post_type' => $this->get_post_type(), |
| 116 |
'post_status' => [ 'publish', 'active', 'pending' ], |
| 117 |
'posts_per_page' => - 1, |
| 118 |
'meta_query' => [ |
| 119 |
'relation' => 'AND', |
| 120 |
[ |
| 121 |
'key' => 'date', |
| 122 |
'value' => $date, |
| 123 |
'compare' => '=' |
| 124 |
], |
| 125 |
[ |
| 126 |
'key' => 'team_member_id', |
| 127 |
'value' => $team_member_id, |
| 128 |
'compare' => '=' |
| 129 |
] |
| 130 |
] |
| 131 |
]; |
| 132 |
|
| 133 |
$query = new \WP_Query( $args ); |
| 134 |
|
| 135 |
return array_map( function ( $post ) { |
| 136 |
return new static( $post ); |
| 137 |
}, $query->posts ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Override load_attributes to include payment status from orders |
| 142 |
* |
| 143 |
* @return void |
| 144 |
*/ |
| 145 |
protected function load_attributes() { |
| 146 |
parent::load_attributes(); |
| 147 |
|
| 148 |
if ( $this->order_id ) { |
| 149 |
$db = new Booktics_Database(); |
| 150 |
$order_model = ( new Order_Model( $db ) )->find( [ 'id' => $this->order_id ] ); |
| 151 |
|
| 152 |
$this->attributes['payment_status'] = $order_model->payment_status ?? 'pending'; |
| 153 |
} else { |
| 154 |
$this->attributes['payment_status'] = 'pending'; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Get appointments count by order ID |
| 160 |
* |
| 161 |
* @param int $order_id Order ID |
| 162 |
* |
| 163 |
* @return int Number of appointments |
| 164 |
*/ |
| 165 |
public function get_appointments_count_by_order_id( $order_id ) { |
| 166 |
$args = [ |
| 167 |
'post_type' => $this->get_post_type(), |
| 168 |
'post_status' => [ 'publish', 'active', 'pending' ], |
| 169 |
'posts_per_page' => - 1, |
| 170 |
'meta_query' => [ |
| 171 |
[ |
| 172 |
'key' => 'order_id', |
| 173 |
'value' => $order_id, |
| 174 |
'compare' => '=' |
| 175 |
] |
| 176 |
] |
| 177 |
]; |
| 178 |
|
| 179 |
$query = new \WP_Query( $args ); |
| 180 |
|
| 181 |
return $query->found_posts; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Get appointments count by team member ID |
| 186 |
* |
| 187 |
* @param int $team_member_id Team member ID |
| 188 |
* |
| 189 |
* @return int Number of appointments |
| 190 |
*/ |
| 191 |
public function get_appointments_count_by_team_member_id( $team_member_id ) { |
| 192 |
$args = [ |
| 193 |
'post_type' => $this->get_post_type(), |
| 194 |
'post_status' => [ 'publish', 'active', 'pending' ], |
| 195 |
'posts_per_page' => - 1, |
| 196 |
'meta_query' => [ |
| 197 |
[ |
| 198 |
'key' => 'team_member_id', |
| 199 |
'value' => $team_member_id, |
| 200 |
'compare' => '=' |
| 201 |
] |
| 202 |
] |
| 203 |
]; |
| 204 |
|
| 205 |
$query = new \WP_Query( $args ); |
| 206 |
|
| 207 |
return $query->found_posts; |
| 208 |
} |
| 209 |
} |
| 210 |
|