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