PluginProbe
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress / 5.6.9
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress v5.6.9
5.6.11 5.6.10 5.6.9 5.6.8 5.6.7 5.6.6 5.6.5 5.6.4 5.6.3 5.6.2 5.6.1 5.6.0 5.5.2 5.5.1 5.5.0 5.4.2 trunk 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 All 48 releases
latepoint / lib / models / bundle_model.php
bundle_model.php
348 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Copyright (c) 2023 LatePoint LLC. All rights reserved.
4 */
5
6 class OsBundleModel extends OsModel {
7 var $services;
8
9 var $id,
10 $name,
11 $short_description,
12 $charge_amount,
13 $deposit_amount,
14 $price_min,
15 $price_max,
16 $status,
17 $visibility,
18 $order_number,
19 $updated_at,
20 $created_at;
21
22 function __construct( $id = false ) {
23 parent::__construct();
24 $this->table_name = LATEPOINT_TABLE_BUNDLES;
25 $this->join_table_name_bundles_services = LATEPOINT_TABLE_JOIN_BUNDLES_SERVICES;
26
27 if ( $id ) {
28 $this->load_by_id( $id );
29 }
30 }
31
32 public function generate_data_vars(): array {
33 $vars = [
34 'id' => $this->id,
35 'name' => $this->name,
36 ];
37
38 return $vars;
39 }
40
41 function has_service( $service_id ): bool {
42 $services = $this->get_services();
43 foreach ( $services as $service ) {
44 if ( $service->id == $service_id ) {
45 return true;
46 }
47 }
48 return false;
49 }
50
51 function quantity_for_service( $service_id ): int {
52 $services = $this->get_services();
53 foreach ( $services as $service ) {
54 if ( $service->id == $service_id ) {
55 return ( ! empty( $service->join_attributes['quantity'] ) ? $service->join_attributes['quantity'] : 0 );
56 }
57 }
58 return 0;
59 }
60
61 function duration_for_service( $service_id ): int {
62 $services = $this->get_services();
63 foreach ( $services as $service ) {
64 if ( $service->id == $service_id ) {
65 return ( ! empty( $service->join_attributes['duration'] ) ? $service->join_attributes['duration'] : $service->duration );
66 }
67 }
68 return 0;
69 }
70
71 function total_attendees_for_service( $service_id ): int {
72 $services = $this->get_services();
73 foreach ( $services as $service ) {
74 if ( $service->id == $service_id ) {
75 return ( ! empty( $service->join_attributes['total_attendees'] ) ? $service->join_attributes['total_attendees'] : 1 );
76 }
77 }
78 return 0;
79 }
80
81 public function generate_params_for_booking_form() {
82 $params = [
83 'bundle_id' => $this->id,
84 ];
85
86 /**
87 * Returns an array of params generated from OsBundleModel to be used in a booking form
88 *
89 * @since 5.0.0
90 * @hook latepoint_generated_bundle_params_for_booking_form
91 *
92 * @param {array} $params Array of booking params
93 * @param {OsBundleModel} $bundle Instance of <code>OsBundleModel</code> that params are being generated for
94 *
95 * @returns {array} Filtered array of booking params
96 */
97 return apply_filters( 'latepoint_generated_bundle_params_for_booking_form', $params, $this );
98 }
99
100
101 /**
102 * @return mixed|void
103 *
104 * Returns full amount to charge in database format 1999.0000
105 *
106 */
107 public function full_amount_to_charge() {
108 return OsBundlesHelper::calculate_full_amount_for_bundle( $this );
109 }
110
111 /**
112 * @return mixed|void
113 *
114 * Returns deposit amount to charge in database format 1999.0000
115 *
116 */
117 public function deposit_amount_to_charge() {
118 return OsBundlesHelper::calculate_deposit_amount_for_bundle( $this );
119 }
120
121
122 public function save_services( $services ) {
123 if ( ! $services ) {
124 return true;
125 }
126 $connections_to_save = [];
127 $connections_to_remove = [];
128 foreach ( $services as $service_key => $service ) {
129 $service_id = str_replace( 'service_', '', $service_key );
130 $connection = [
131 'bundle_id' => $this->id,
132 'service_id' => $service_id,
133 'quantity' => $service['quantity'],
134 'total_attendees' => $service['total_attendees'],
135 'duration' => $service['duration'],
136 ];
137 if ( $service['connected'] == 'yes' ) {
138 $connections_to_save[] = $connection;
139 } else {
140 $connections_to_remove[] = $connection;
141 }
142 }
143 if ( ! empty( $connections_to_save ) ) {
144 foreach ( $connections_to_save as $connection_to_save ) {
145 $join_bundle_service = new OsJoinBundlesServicesModel();
146 $existing = $join_bundle_service->where(
147 [
148 'bundle_id' => $connection_to_save['bundle_id'],
149 'service_id' => $connection_to_save['service_id'],
150 ]
151 )->set_limit( 1 )->get_results_as_models();
152 if ( $existing ) {
153 $existing->quantity = $connection_to_save['quantity'];
154 $existing->total_attendees = $connection_to_save['total_attendees'];
155 $existing->duration = $connection_to_save['duration'];
156 $existing->save();
157 } else {
158 $join_bundle_service->set_data( $connection_to_save );
159 $join_bundle_service->save();
160 }
161 }
162 }
163 if ( ! empty( $connections_to_remove ) ) {
164 foreach ( $connections_to_remove as $connection_to_remove ) {
165 $join_bundle_service = new OsJoinBundlesServicesModel();
166 $join_bundle_service->delete_where(
167 [
168 'bundle_id' => $connection_to_remove['bundle_id'],
169 'service_id' => $connection_to_remove['service_id'],
170 ]
171 );
172 }
173 }
174 return true;
175 }
176
177
178 public function get_formatted_charge_amount() {
179 if ( $this->charge_amount > 0 ) {
180 return OsMoneyHelper::format_price( $this->charge_amount );
181 } else {
182 return 0;
183 }
184 }
185
186 public function get_service_and_quantity_descriptions(): array {
187 $bundle_services = $this->get_services();
188 $bundle_services_descriptions = [];
189 foreach ( $bundle_services as $service ) {
190 $qty = $service->join_attributes['quantity'];
191 $qty_html = $qty > 1 ? ' [' . $qty . ']' : '';
192 $bundle_services_descriptions[] = $service->name . $qty_html;
193 }
194 return $bundle_services_descriptions;
195 }
196
197
198 public function get_services( $order_item_id = false ): array {
199 if ( ! isset( $this->services ) ) {
200 $bundle_services = new OsJoinBundlesServicesModel();
201 $bundle_services = $bundle_services->get_services_for_bundle_id( $this->id );
202
203 $this->services = [];
204
205 if ( $bundle_services ) {
206 foreach ( $bundle_services as $bundle_service ) {
207 $service = new OsServiceModel( $bundle_service->service_id );
208 $service->join_attributes['quantity'] = $bundle_service->quantity;
209 $service->join_attributes['total_attendees'] = $bundle_service->total_attendees;
210 $service->join_attributes['duration'] = $bundle_service->duration;
211 if ( $order_item_id ) {
212 $bookings = new OsBookingModel();
213 $service->join_attributes['total_scheduled_bookings'] = $bookings->where(
214 [
215 'order_item_id' => $order_item_id,
216 'service_id' => $service->id,
217 ]
218 )->should_not_be_cancelled()->count();
219 }
220 $this->services[] = $service;
221 }
222 }
223 }
224 return $this->services;
225 }
226
227
228 public function is_hidden() {
229 return ( $this->visibility == LATEPOINT_BUNDLE_VISIBILITY_HIDDEN );
230 }
231
232 public function should_be_active() {
233 return $this->where( [ 'status' => LATEPOINT_BUNDLE_STATUS_ACTIVE ] );
234 }
235
236 public function should_not_be_hidden() {
237 return $this->where( [ 'visibility !=' => LATEPOINT_BUNDLE_VISIBILITY_HIDDEN ] );
238 }
239
240 public function is_active() {
241 return ( $this->status == LATEPOINT_BUNDLE_STATUS_ACTIVE );
242 }
243
244 public function delete_meta_by_key( $meta_key ) {
245 if ( $this->is_new_record() ) {
246 return false;
247 }
248
249 $meta = new OsBundleMetaModel();
250 return $meta->delete_by_key( $meta_key, $this->id );
251 }
252
253 public function get_meta_by_key( $meta_key, $default = false ) {
254 if ( $this->is_new_record() ) {
255 return $default;
256 }
257
258 $meta = new OsBundleMetaModel();
259 return $meta->get_by_key( $meta_key, $this->id, $default );
260 }
261
262 public function save_meta_by_key( $meta_key, $meta_value ) {
263 if ( $this->is_new_record() ) {
264 return false;
265 }
266
267 $meta = new OsBundleMetaModel();
268 return $meta->save_by_key( $meta_key, $meta_value, $this->id );
269 }
270
271 public function delete( $id = false ) {
272 if ( ! $id && isset( $this->id ) ) {
273 $id = $this->id;
274 }
275
276 if ( $id && $this->db->delete( $this->table_name, array( 'id' => $id ), array( '%d' ) ) ) {
277 $this->db->delete( LATEPOINT_TABLE_BUNDLE_META, array( 'object_id' => $id ), array( '%d' ) );
278 do_action( 'latepoint_bundle_deleted', $id );
279 return true;
280 }
281
282 return false;
283 }
284
285
286 protected function properties_to_validate() {
287 $validations = array(
288 'name' => array( 'presence' ),
289 );
290 return $validations;
291 }
292
293
294 protected function params_to_sanitize() {
295 return [
296 'charge_amount' => 'money',
297 'deposit_amount' => 'money',
298 'price_min' => 'money',
299 'price_max' => 'money',
300 ];
301 }
302
303 protected function allowed_params( $role = 'admin' ) {
304 $allowed_params = array(
305 'id',
306 'name',
307 'short_description',
308 'charge_amount',
309 'deposit_amount',
310 'price_min',
311 'price_max',
312 'status',
313 'visibility',
314 'order_number',
315 'updated_at',
316 'created_at',
317 );
318 return $allowed_params;
319 }
320
321
322 protected function params_to_save( $role = 'admin' ) {
323 $params_to_save = array(
324 'id',
325 'name',
326 'short_description',
327 'charge_amount',
328 'deposit_amount',
329 'price_min',
330 'price_max',
331 'status',
332 'visibility',
333 'order_number',
334 'updated_at',
335 'created_at',
336 );
337 return $params_to_save;
338 }
339
340 protected function get_price_min_formatted() {
341 if ( $this->price_min > 0 ) {
342 return OsMoneyHelper::format_price( $this->price_min );
343 } else {
344 return OsMoneyHelper::format_price( 0 );
345 }
346 }
347 }
348