PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.1.91
LatePoint – Calendar Booking Plugin for Appointments and Events v5.1.91
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 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 / customer_model.php
latepoint / lib / models Last commit date
activity_model.php 1 year ago agent_meta_model.php 1 year ago agent_model.php 1 year ago booking_meta_model.php 1 year ago booking_model.php 1 year ago bundle_model.php 1 year ago cart_item_model.php 1 year ago cart_meta_model.php 1 year ago cart_model.php 1 year ago connector_model.php 1 year ago customer_meta_model.php 1 year ago customer_model.php 1 year ago invoice_model.php 1 year ago join_bundles_services_model.php 1 year ago location_category_model.php 1 year ago location_model.php 1 year ago meta_model.php 1 year ago model.php 1 year ago off_period_model.php 1 year ago order_intent_meta_model.php 1 year ago order_intent_model.php 1 year ago order_item_model.php 1 year ago order_meta_model.php 1 year ago order_model.php 1 year ago payment_request_model.php 1 year ago process_job_model.php 1 year ago process_model.php 1 year ago recurrence_model.php 1 year ago service_category_model.php 1 year ago service_meta_model.php 1 year ago service_model.php 1 year ago session_model.php 1 year ago settings_model.php 1 year ago step_settings_model.php 1 year ago transaction_intent_model.php 1 year ago transaction_model.php 1 year ago transaction_refund_model.php 1 year ago work_period_model.php 1 year ago
customer_model.php
432 lines
1 <?php
2
3 /**
4 * @property string $full_name
5 */
6 class OsCustomerModel extends OsModel {
7 var $id,
8 $first_name,
9 $last_name,
10 $password,
11 $email,
12 $phone,
13 $account_nonse,
14 $status,
15 $activation_key,
16 $google_user_id,
17 $facebook_user_id,
18 $avatar_image_id,
19 $is_guest,
20 $notes,
21 $admin_notes,
22 $wordpress_user_id,
23 $meta_class = 'OsCustomerMetaModel',
24 $updated_at,
25 $created_at;
26
27 function __construct( $id = false ) {
28 $this->table_name = LATEPOINT_TABLE_CUSTOMERS;
29 $this->nice_names = array(
30 'first_name' => __( 'Customer First Name', 'latepoint' ),
31 'email' => __( 'Email Address', 'latepoint' ),
32 'phone' => __( 'Phone Number', 'latepoint' ),
33 'last_name' => __( 'Customer Last Name', 'latepoint' )
34 );
35
36 parent::__construct( $id );
37 }
38
39 public function generate_data_vars(): array {
40 return [
41 'id' => $this->id,
42 'first_name' => $this->first_name,
43 'last_name' => $this->last_name,
44 'full_name' => $this->full_name,
45 'email' => $this->email,
46 'phone' => $this->phone
47 ];
48 }
49
50 public function get_meta_by_key( $meta_key, $default = false ) {
51 if ( $this->is_new_record() ) {
52 return $default;
53 }
54
55 $meta = new OsCustomerMetaModel();
56
57 return $meta->get_by_key( $meta_key, $this->id, $default );
58 }
59
60 /**
61 * @param int $limit
62 * @param bool $filter_allowed_records
63 *
64 * @return OsOrderModel[]
65 */
66 public function get_orders( int $limit = 0, bool $filter_allowed_records = false ): array {
67 $orders = new OsOrderModel();
68 if ( $limit ) {
69 $orders = $orders->set_limit( $limit );
70 }
71 if ( $filter_allowed_records ) {
72 $orders->filter_allowed_records();
73 }
74
75 return $orders->where( [ 'customer_id' => $this->id ] )->order_by( 'created_at desc' )->get_results_as_models();
76 }
77
78 public function get_initials() {
79 return mb_substr( $this->first_name, 0, 1 ) . mb_substr( $this->last_name, 0, 1 );
80 }
81
82 public function delete_meta_by_key( $meta_key ) {
83 if ( $this->is_new_record() ) {
84 return true;
85 }
86
87 $meta = new OsCustomerMetaModel();
88
89 return $meta->delete_by_key( $meta_key, $this->id );
90 }
91
92 public function save_meta_by_key( $meta_key, $meta_value ) {
93 if ( $this->is_new_record() ) {
94 return false;
95 }
96
97 $meta = new OsCustomerMetaModel();
98
99 return $meta->save_by_key( $meta_key, $meta_value, $this->id );
100 }
101
102 public function set_timezone_name( $timezone_name = false ) {
103 if ( ! $timezone_name ) {
104 $timezone_name = OsTimeHelper::get_timezone_name_from_session();
105 }
106 $this->save_meta_by_key( 'timezone_name', $timezone_name );
107 }
108
109 public function get_selected_timezone_name() {
110 return $this->get_meta_by_key( 'timezone_name', OsTimeHelper::get_timezone_name_from_session() );
111 }
112
113 public function get_selected_timezone_obj() {
114 $timezone_obj = new DateTimeZone( $this->get_selected_timezone_name() );
115
116 return $timezone_obj;
117 }
118
119
120 public function delete( $id = false ) {
121 if ( ! $id && isset( $this->id ) ) {
122 $id = $this->id;
123 }
124 $bookings = new OsBookingModel();
125 $bookings_to_delete = $bookings->where( [ 'customer_id' => $id ] )->get_results_as_models();
126 if ( $bookings_to_delete ) {
127 foreach ( $bookings_to_delete as $booking ) {
128 $booking->delete();
129 }
130 }
131 $orders = new OsOrderModel();
132 $orders_to_delete = $orders->where( [ 'customer_id' => $id ] )->get_results_as_models();
133 if ( $orders_to_delete ) {
134 foreach ( $orders_to_delete as $order ) {
135 $order->delete();
136 }
137 }
138 $transactions = new OsTransactionModel();
139 $transactions_to_delete = $transactions->where( [ 'customer_id' => $id ] )->get_results_as_models();
140 if ( $transactions_to_delete ) {
141 foreach ( $transactions_to_delete as $transaction ) {
142 $transaction->delete();
143 }
144 }
145 $customer_metas = new OsCustomerMetaModel();
146 $customer_metas_to_delete = $customer_metas->where( [ 'object_id' => $id ] )->get_results_as_models();
147 if ( $customer_metas_to_delete ) {
148 foreach ( $customer_metas_to_delete as $customer_meta ) {
149 $customer_meta->delete();
150 }
151 }
152
153 return parent::delete( $id );
154 }
155
156
157 public function get_bookings( $limit = false, $filter_allowed_records = false ) {
158 $bookings = new OsBookingModel();
159 if ( $limit ) {
160 $bookings = $bookings->set_limit( $limit );
161 }
162 if ( $filter_allowed_records ) {
163 $bookings->filter_allowed_records();
164 }
165
166 return $bookings->where( [ 'customer_id' => $this->id ] )->get_results_as_models();
167 }
168
169 public function get_past_bookings( $limit = false, $filter_allowed_records = false ) {
170 $bookings = new OsBookingModel();
171 if ( $limit ) {
172 $bookings = $bookings->set_limit( $limit );
173 }
174 if ( $filter_allowed_records ) {
175 $bookings->filter_allowed_records();
176 }
177
178 return $bookings->should_not_be_cancelled()->where( array(
179 'customer_id' => $this->id,
180 'OR' => array(
181 'start_date <' => OsTimeHelper::today_date( 'Y-m-d' ),
182 'AND' => array(
183 'start_date' => OsTimeHelper::today_date( 'Y-m-d' ),
184 'start_time <' => OsTimeHelper::get_current_minutes()
185 )
186 )
187 ) )->get_results_as_models();
188 }
189
190 /**
191 * @return OsBundleModel[]
192 */
193 public function get_not_scheduled_bundles(): array {
194 $non_scheduled_bundles = [];
195 $orders = new OsOrderModel();
196 $orders = $orders->where( [ 'customer_id' => $this->id ] )->get_results_as_models();
197 if ( $orders ) {
198 foreach ( $orders as $order ) {
199 $bundles = $order->get_bundles_from_order_items();
200 if ( $bundles ) {
201 foreach ( $bundles as $order_item_id => $bundle ) {
202 $bundle_services = $bundle->get_services();
203 foreach ( $bundle_services as $bundle_service ) {
204 $bookings = new OsBookingModel();
205 $total_scheduled_bookings = $bookings->where( [ 'order_item_id' => $order_item_id, 'service_id' => $bundle_service->id ] )->should_not_be_cancelled()->count();
206 if ( $total_scheduled_bookings < $bundle_service->join_attributes['quantity'] ) {
207 $non_scheduled_bundles[ $order_item_id ] = $bundle;
208 break;
209 }
210 }
211 }
212 }
213 }
214 }
215
216 return $non_scheduled_bundles;
217 }
218
219 public function get_cancelled_bookings( $limit = false, $filter_allowed_records = false ) {
220 $bookings = new OsBookingModel();
221 if ( $limit ) {
222 $bookings = $bookings->set_limit( $limit );
223 }
224 if ( $filter_allowed_records ) {
225 $bookings->filter_allowed_records();
226 }
227
228 return $bookings->should_be_cancelled()->order_by( 'start_date, start_time asc' )->where( [ 'customer_id' => $this->id ] )->get_results_as_models();
229 }
230
231 public function get_future_bookings( $limit = false, $filter_allowed_records = false ) {
232 $bookings = new OsBookingModel();
233 if ( $limit ) {
234 $bookings = $bookings->set_limit( $limit );
235 }
236 if ( $filter_allowed_records ) {
237 $bookings->filter_allowed_records();
238 }
239
240 return $bookings->should_not_be_cancelled()->order_by( 'start_date, start_time asc' )->where( [ 'customer_id' => $this->id ] )->should_be_in_future()->get_results_as_models();
241 }
242
243
244 public function get_future_bookings_count( $filter_allowed_records = false ) {
245 $bookings = new OsBookingModel();
246 if ( $filter_allowed_records ) {
247 $bookings->filter_allowed_records();
248 }
249
250 return $bookings->should_not_be_cancelled()->where( array(
251 'customer_id' => $this->id,
252 'OR' => array(
253 'start_date >' => OsTimeHelper::today_date( 'Y-m-d' ),
254 'AND' => array(
255 'start_date' => OsTimeHelper::today_date( 'Y-m-d' ),
256 'start_time >' => OsTimeHelper::get_current_minutes()
257 )
258 )
259 ) )->count();
260 }
261
262 public function get_total_bookings_count( $filter_allowed_records = false ) {
263 $bookings = new OsBookingModel();
264 if ( $filter_allowed_records ) {
265 $bookings->filter_allowed_records();
266 }
267
268 return $bookings->select( 'count(id) as total_bookings' )->where( array( 'customer_id' => $this->id ) )->count();
269 }
270
271
272 public function filter_allowed_records(): OsModel {
273 if ( ! OsRolesHelper::are_all_records_allowed() ) {
274 $this->select( LATEPOINT_TABLE_CUSTOMERS . '.*' )->join( LATEPOINT_TABLE_BOOKINGS, [ 'customer_id' => LATEPOINT_TABLE_CUSTOMERS . '.id' ] )->group_by( LATEPOINT_TABLE_CUSTOMERS . '.id' );
275 if ( ! OsRolesHelper::are_all_records_allowed( 'agent' ) ) {
276 $this->filter_where_conditions( [ LATEPOINT_TABLE_BOOKINGS . '.agent_id' => OsRolesHelper::get_allowed_records( 'agent' ) ] );
277 }
278 if ( ! OsRolesHelper::are_all_records_allowed( 'location' ) ) {
279 $this->filter_where_conditions( [ LATEPOINT_TABLE_BOOKINGS . '.location_id' => OsRolesHelper::get_allowed_records( 'location' ) ] );
280 }
281 if ( ! OsRolesHelper::are_all_records_allowed( 'service' ) ) {
282 $this->filter_where_conditions( [ LATEPOINT_TABLE_BOOKINGS . '.service_id' => OsRolesHelper::get_allowed_records( 'service' ) ] );
283 }
284 }
285
286 return $this;
287 }
288
289 public function update_password( $password, $is_hashed = false ) {
290 if ( ! $is_hashed ) {
291 $password = OsAuthHelper::hash_password( $password );
292 }
293
294 return $this->update_attributes( [ 'password' => $password, 'is_guest' => false ] );
295 }
296
297 protected function get_full_name() {
298 return trim( join( ' ', array( $this->first_name, $this->last_name ) ) );
299 }
300
301 protected function get_default_status() {
302 return 'pending_verification';
303 }
304
305 protected function before_create() {
306 if ( ! isset( $this->is_guest ) ) {
307 $this->is_guest = true;
308 }
309 if ( empty( $this->status ) ) {
310 $this->status = $this->get_default_status();
311 }
312 if ( empty( $this->password ) ) {
313 $this->password = wp_hash_password( bin2hex( openssl_random_pseudo_bytes( 8 ) ) );
314 }
315 if ( empty( $this->activation_key ) ) {
316 $this->activation_key = sha1( wp_rand( 10000, 99999 ) . time() . $this->email );
317 }
318 if ( empty( $this->account_nonse ) ) {
319 $this->account_nonse = sha1( wp_rand( 10000, 99999 ) . time() . $this->activation_key );
320 }
321 }
322
323
324 public function get_avatar_url() {
325 return OsCustomerHelper::get_avatar_url( $this );
326 }
327
328 public function get_avatar_image() {
329 return OsCustomerHelper::get_avatar_image( $this );
330 }
331
332 // if this was a guest account without a set password and social login was not used, you can login just by email
333 public function can_login_without_password() {
334 return ( $this->is_guest && empty( $this->google_user_id ) && empty( $this->facebook_user_id ) );
335 }
336
337 public function prepare_data_before_it_is_set( $data ) {
338 if ( isset( $data['phone'] ) ) {
339 $data['phone'] = OsUtilHelper::sanitize_phone_number( $data['phone'] );
340 }
341
342 return $data;
343 }
344
345
346 protected function allowed_params( $role = 'admin' ) {
347 switch ( $role ) {
348 case 'admin':
349 $allowed_params = [
350 'id',
351 'first_name',
352 'last_name',
353 'email',
354 'phone',
355 'avatar_image_id',
356 'is_guest',
357 'notes',
358 'admin_notes',
359 'wordpress_user_id',
360 'password'
361 ];
362 break;
363 case 'customer':
364 case 'public':
365 $allowed_params = [
366 'first_name',
367 'last_name',
368 'email',
369 'phone',
370 'avatar_image_id',
371 'notes',
372 'password'
373 ];
374 break;
375 }
376
377 return $allowed_params;
378 }
379
380 protected function params_to_save( $role = 'admin' ) {
381 $params_to_save = array(
382 'id',
383 'first_name',
384 'last_name',
385 'email',
386 'phone',
387 'password',
388 'activation_key',
389 'account_nonse',
390 'avatar_image_id',
391 'status',
392 'is_guest',
393 'notes',
394 'admin_notes',
395 'wordpress_user_id',
396 'google_user_id',
397 'facebook_user_id'
398 );
399
400 return $params_to_save;
401 }
402
403 protected function properties_to_validate( $alternative_validation = false ) {
404 // if alternative validation is enabled - use a different scope of rules (useful when you don't need to run all validations for example on social login)
405 if ( $alternative_validation ) {
406 $validations = array(
407 'email' => array( 'presence', 'email', 'uniqueness' ),
408 );
409 } else {
410 $validations = array(
411 'first_name' => array( 'presence' ),
412 'last_name' => array( 'presence' ),
413 'email' => array( 'presence', 'email', 'uniqueness' ),
414 );
415
416 $default_fields = OsSettingsHelper::get_default_fields_for_customer();
417 foreach ( $default_fields as $name => $field ) {
418 if ( $field['required'] && $field['active'] ) {
419 $validations[ $name ][] = 'presence';
420 $validations[ $name ] = array_unique( $validations[ $name ] );
421 } else {
422 if ( isset( $validations[ $name ] ) ) {
423 $validations[ $name ] = array_diff( $validations[ $name ], [ 'presence' ] );
424 }
425 }
426 }
427 $validations = apply_filters( 'latepoint_customer_model_validations', $validations );
428 }
429
430 return $validations;
431 }
432 }