PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.5.2
LatePoint – Calendar Booking Plugin for Appointments and Events v5.5.2
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 5.1.8 5.1.9 5.1.91 5.1.92 5.1.93 5.1.94 5.2.0 5.2.1 5.2.10 5.2.11 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.2 5.4.0 5.4.1
latepoint / lib / models / bundle_model.php
latepoint / lib / models Last commit date
activity_model.php 3 months ago agent_meta_model.php 3 months ago agent_model.php 3 months ago booking_meta_model.php 3 months ago booking_model.php 3 months ago bundle_meta_model.php 3 months ago bundle_model.php 3 months ago cart_item_model.php 3 months ago cart_meta_model.php 3 months ago cart_model.php 3 months ago connector_model.php 3 months ago customer_meta_model.php 3 months ago customer_model.php 1 month ago invoice_model.php 3 months ago join_bundles_services_model.php 3 months ago location_category_model.php 3 months ago location_model.php 3 months ago meta_model.php 3 months ago model.php 3 months ago off_period_model.php 3 months ago order_intent_meta_model.php 3 months ago order_intent_model.php 3 months ago order_item_model.php 3 months ago order_meta_model.php 3 months ago order_model.php 3 months ago otp_model.php 3 months ago payment_request_model.php 3 months ago process_job_model.php 3 months ago process_model.php 3 months ago recurrence_model.php 3 months ago service_category_model.php 3 months ago service_meta_model.php 3 months ago service_model.php 3 months ago session_model.php 3 months ago settings_model.php 3 months ago step_settings_model.php 3 months ago transaction_intent_model.php 3 months ago transaction_model.php 3 months ago transaction_refund_model.php 3 months ago work_period_model.php 3 months ago
bundle_model.php
332 lines
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 $status,
15 $visibility,
16 $order_number,
17 $updated_at,
18 $created_at;
19
20 function __construct( $id = false ) {
21 parent::__construct();
22 $this->table_name = LATEPOINT_TABLE_BUNDLES;
23 $this->join_table_name_bundles_services = LATEPOINT_TABLE_JOIN_BUNDLES_SERVICES;
24
25 if ( $id ) {
26 $this->load_by_id( $id );
27 }
28 }
29
30 public function generate_data_vars(): array {
31 $vars = [
32 'id' => $this->id,
33 'name' => $this->name,
34 ];
35
36 return $vars;
37 }
38
39 function has_service( $service_id ): bool {
40 $services = $this->get_services();
41 foreach ( $services as $service ) {
42 if ( $service->id == $service_id ) {
43 return true;
44 }
45 }
46 return false;
47 }
48
49 function quantity_for_service( $service_id ): int {
50 $services = $this->get_services();
51 foreach ( $services as $service ) {
52 if ( $service->id == $service_id ) {
53 return ( ! empty( $service->join_attributes['quantity'] ) ? $service->join_attributes['quantity'] : 0 );
54 }
55 }
56 return 0;
57 }
58
59 function duration_for_service( $service_id ): int {
60 $services = $this->get_services();
61 foreach ( $services as $service ) {
62 if ( $service->id == $service_id ) {
63 return ( ! empty( $service->join_attributes['duration'] ) ? $service->join_attributes['duration'] : $service->duration );
64 }
65 }
66 return 0;
67 }
68
69 function total_attendees_for_service( $service_id ): int {
70 $services = $this->get_services();
71 foreach ( $services as $service ) {
72 if ( $service->id == $service_id ) {
73 return ( ! empty( $service->join_attributes['total_attendees'] ) ? $service->join_attributes['total_attendees'] : 1 );
74 }
75 }
76 return 0;
77 }
78
79 public function generate_params_for_booking_form() {
80 $params = [
81 'bundle_id' => $this->id,
82 ];
83
84 /**
85 * Returns an array of params generated from OsBundleModel to be used in a booking form
86 *
87 * @since 5.0.0
88 * @hook latepoint_generated_bundle_params_for_booking_form
89 *
90 * @param {array} $params Array of booking params
91 * @param {OsBundleModel} $bundle Instance of <code>OsBundleModel</code> that params are being generated for
92 *
93 * @returns {array} Filtered array of booking params
94 */
95 return apply_filters( 'latepoint_generated_bundle_params_for_booking_form', $params, $this );
96 }
97
98
99 /**
100 * @return mixed|void
101 *
102 * Returns full amount to charge in database format 1999.0000
103 *
104 */
105 public function full_amount_to_charge() {
106 return OsBundlesHelper::calculate_full_amount_for_bundle( $this );
107 }
108
109 /**
110 * @return mixed|void
111 *
112 * Returns deposit amount to charge in database format 1999.0000
113 *
114 */
115 public function deposit_amount_to_charge() {
116 return OsBundlesHelper::calculate_deposit_amount_for_bundle( $this );
117 }
118
119
120 public function save_services( $services ) {
121 if ( ! $services ) {
122 return true;
123 }
124 $connections_to_save = [];
125 $connections_to_remove = [];
126 foreach ( $services as $service_key => $service ) {
127 $service_id = str_replace( 'service_', '', $service_key );
128 $connection = [
129 'bundle_id' => $this->id,
130 'service_id' => $service_id,
131 'quantity' => $service['quantity'],
132 'total_attendees' => $service['total_attendees'],
133 'duration' => $service['duration'],
134 ];
135 if ( $service['connected'] == 'yes' ) {
136 $connections_to_save[] = $connection;
137 } else {
138 $connections_to_remove[] = $connection;
139 }
140 }
141 if ( ! empty( $connections_to_save ) ) {
142 foreach ( $connections_to_save as $connection_to_save ) {
143 $join_bundle_service = new OsJoinBundlesServicesModel();
144 $existing = $join_bundle_service->where(
145 [
146 'bundle_id' => $connection_to_save['bundle_id'],
147 'service_id' => $connection_to_save['service_id'],
148 ]
149 )->set_limit( 1 )->get_results_as_models();
150 if ( $existing ) {
151 $existing->quantity = $connection_to_save['quantity'];
152 $existing->total_attendees = $connection_to_save['total_attendees'];
153 $existing->duration = $connection_to_save['duration'];
154 $existing->save();
155 } else {
156 $join_bundle_service->set_data( $connection_to_save );
157 $join_bundle_service->save();
158 }
159 }
160 }
161 if ( ! empty( $connections_to_remove ) ) {
162 foreach ( $connections_to_remove as $connection_to_remove ) {
163 $join_bundle_service = new OsJoinBundlesServicesModel();
164 $join_bundle_service->delete_where(
165 [
166 'bundle_id' => $connection_to_remove['bundle_id'],
167 'service_id' => $connection_to_remove['service_id'],
168 ]
169 );
170 }
171 }
172 return true;
173 }
174
175
176 public function get_formatted_charge_amount() {
177 if ( $this->charge_amount > 0 ) {
178 return OsMoneyHelper::format_price( $this->charge_amount );
179 } else {
180 return 0;
181 }
182 }
183
184 public function get_service_and_quantity_descriptions(): array {
185 $bundle_services = $this->get_services();
186 $bundle_services_descriptions = [];
187 foreach ( $bundle_services as $service ) {
188 $qty = $service->join_attributes['quantity'];
189 $qty_html = $qty > 1 ? ' [' . $qty . ']' : '';
190 $bundle_services_descriptions[] = $service->name . $qty_html;
191 }
192 return $bundle_services_descriptions;
193 }
194
195
196 public function get_services( $order_item_id = false ): array {
197 if ( ! isset( $this->services ) ) {
198 $bundle_services = new OsJoinBundlesServicesModel();
199 $bundle_services = $bundle_services->get_services_for_bundle_id( $this->id );
200
201 $this->services = [];
202
203 if ( $bundle_services ) {
204 foreach ( $bundle_services as $bundle_service ) {
205 $service = new OsServiceModel( $bundle_service->service_id );
206 $service->join_attributes['quantity'] = $bundle_service->quantity;
207 $service->join_attributes['total_attendees'] = $bundle_service->total_attendees;
208 $service->join_attributes['duration'] = $bundle_service->duration;
209 if ( $order_item_id ) {
210 $bookings = new OsBookingModel();
211 $service->join_attributes['total_scheduled_bookings'] = $bookings->where(
212 [
213 'order_item_id' => $order_item_id,
214 'service_id' => $service->id,
215 ]
216 )->should_not_be_cancelled()->count();
217 }
218 $this->services[] = $service;
219 }
220 }
221 }
222 return $this->services;
223 }
224
225
226 public function is_hidden() {
227 return ( $this->visibility == LATEPOINT_BUNDLE_VISIBILITY_HIDDEN );
228 }
229
230 public function should_be_active() {
231 return $this->where( [ 'status' => LATEPOINT_BUNDLE_STATUS_ACTIVE ] );
232 }
233
234 public function should_not_be_hidden() {
235 return $this->where( [ 'visibility !=' => LATEPOINT_BUNDLE_VISIBILITY_HIDDEN ] );
236 }
237
238 public function is_active() {
239 return ( $this->status == LATEPOINT_BUNDLE_STATUS_ACTIVE );
240 }
241
242 public function delete_meta_by_key( $meta_key ) {
243 if ( $this->is_new_record() ) {
244 return false;
245 }
246
247 $meta = new OsBundleMetaModel();
248 return $meta->delete_by_key( $meta_key, $this->id );
249 }
250
251 public function get_meta_by_key( $meta_key, $default = false ) {
252 if ( $this->is_new_record() ) {
253 return $default;
254 }
255
256 $meta = new OsBundleMetaModel();
257 return $meta->get_by_key( $meta_key, $this->id, $default );
258 }
259
260 public function save_meta_by_key( $meta_key, $meta_value ) {
261 if ( $this->is_new_record() ) {
262 return false;
263 }
264
265 $meta = new OsBundleMetaModel();
266 return $meta->save_by_key( $meta_key, $meta_value, $this->id );
267 }
268
269 public function delete( $id = false ) {
270 if ( ! $id && isset( $this->id ) ) {
271 $id = $this->id;
272 }
273
274 if ( $id && $this->db->delete( $this->table_name, array( 'id' => $id ), array( '%d' ) ) ) {
275 $this->db->delete( LATEPOINT_TABLE_BUNDLE_META, array( 'object_id' => $id ), array( '%d' ) );
276 do_action( 'latepoint_bundle_deleted', $id );
277 return true;
278 }
279
280 return false;
281 }
282
283
284 protected function properties_to_validate() {
285 $validations = array(
286 'name' => array( 'presence' ),
287 );
288 return $validations;
289 }
290
291
292 protected function params_to_sanitize() {
293 return [
294 'charge_amount' => 'money',
295 'deposit_amount' => 'money',
296 ];
297 }
298
299 protected function allowed_params( $role = 'admin' ) {
300 $allowed_params = array(
301 'id',
302 'name',
303 'short_description',
304 'charge_amount',
305 'deposit_amount',
306 'status',
307 'visibility',
308 'order_number',
309 'updated_at',
310 'created_at',
311 );
312 return $allowed_params;
313 }
314
315
316 protected function params_to_save( $role = 'admin' ) {
317 $params_to_save = array(
318 'id',
319 'name',
320 'short_description',
321 'charge_amount',
322 'deposit_amount',
323 'status',
324 'visibility',
325 'order_number',
326 'updated_at',
327 'created_at',
328 );
329 return $params_to_save;
330 }
331 }
332