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
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
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
436 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 substr( $this->first_name, 0, 1 ) . 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 | public function get_timeshift_in_minutes() { |
| 120 | return OsTimeHelper::get_timezone_shift_in_minutes( $this->get_selected_timezone_name() ); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | public function delete( $id = false ) { |
| 125 | if ( ! $id && isset( $this->id ) ) { |
| 126 | $id = $this->id; |
| 127 | } |
| 128 | $bookings = new OsBookingModel(); |
| 129 | $bookings_to_delete = $bookings->where( [ 'customer_id' => $id ] )->get_results_as_models(); |
| 130 | if ( $bookings_to_delete ) { |
| 131 | foreach ( $bookings_to_delete as $booking ) { |
| 132 | $booking->delete(); |
| 133 | } |
| 134 | } |
| 135 | $orders = new OsOrderModel(); |
| 136 | $orders_to_delete = $orders->where( [ 'customer_id' => $id ] )->get_results_as_models(); |
| 137 | if ( $orders_to_delete ) { |
| 138 | foreach ( $orders_to_delete as $order ) { |
| 139 | $order->delete(); |
| 140 | } |
| 141 | } |
| 142 | $transactions = new OsTransactionModel(); |
| 143 | $transactions_to_delete = $transactions->where( [ 'customer_id' => $id ] )->get_results_as_models(); |
| 144 | if ( $transactions_to_delete ) { |
| 145 | foreach ( $transactions_to_delete as $transaction ) { |
| 146 | $transaction->delete(); |
| 147 | } |
| 148 | } |
| 149 | $customer_metas = new OsCustomerMetaModel(); |
| 150 | $customer_metas_to_delete = $customer_metas->where( [ 'object_id' => $id ] )->get_results_as_models(); |
| 151 | if ( $customer_metas_to_delete ) { |
| 152 | foreach ( $customer_metas_to_delete as $customer_meta ) { |
| 153 | $customer_meta->delete(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | return parent::delete( $id ); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | public function get_bookings( $limit = false, $filter_allowed_records = false ) { |
| 162 | $bookings = new OsBookingModel(); |
| 163 | if ( $limit ) { |
| 164 | $bookings = $bookings->set_limit( $limit ); |
| 165 | } |
| 166 | if ( $filter_allowed_records ) { |
| 167 | $bookings->filter_allowed_records(); |
| 168 | } |
| 169 | |
| 170 | return $bookings->where( [ 'customer_id' => $this->id ] )->get_results_as_models(); |
| 171 | } |
| 172 | |
| 173 | public function get_past_bookings( $limit = false, $filter_allowed_records = false ) { |
| 174 | $bookings = new OsBookingModel(); |
| 175 | if ( $limit ) { |
| 176 | $bookings = $bookings->set_limit( $limit ); |
| 177 | } |
| 178 | if ( $filter_allowed_records ) { |
| 179 | $bookings->filter_allowed_records(); |
| 180 | } |
| 181 | |
| 182 | return $bookings->should_not_be_cancelled()->where( array( |
| 183 | 'customer_id' => $this->id, |
| 184 | 'OR' => array( |
| 185 | 'start_date <' => OsTimeHelper::today_date( 'Y-m-d' ), |
| 186 | 'AND' => array( |
| 187 | 'start_date' => OsTimeHelper::today_date( 'Y-m-d' ), |
| 188 | 'start_time <' => OsTimeHelper::get_current_minutes() |
| 189 | ) |
| 190 | ) |
| 191 | ) )->get_results_as_models(); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * @return OsBundleModel[] |
| 196 | */ |
| 197 | public function get_not_scheduled_bundles(): array { |
| 198 | $non_scheduled_bundles = []; |
| 199 | $orders = new OsOrderModel(); |
| 200 | $orders = $orders->where( [ 'customer_id' => $this->id ] )->get_results_as_models(); |
| 201 | if ( $orders ) { |
| 202 | foreach ( $orders as $order ) { |
| 203 | $bundles = $order->get_bundles_from_order_items(); |
| 204 | if ( $bundles ) { |
| 205 | foreach ( $bundles as $order_item_id => $bundle ) { |
| 206 | $bundle_services = $bundle->get_services(); |
| 207 | foreach ( $bundle_services as $bundle_service ) { |
| 208 | $bookings = new OsBookingModel(); |
| 209 | $total_scheduled_bookings = $bookings->where( [ 'order_item_id' => $order_item_id, 'service_id' => $bundle_service->id ] )->should_not_be_cancelled()->count(); |
| 210 | if ( $total_scheduled_bookings < $bundle_service->join_attributes['quantity'] ) { |
| 211 | $non_scheduled_bundles[ $order_item_id ] = $bundle; |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | return $non_scheduled_bundles; |
| 221 | } |
| 222 | |
| 223 | public function get_cancelled_bookings( $limit = false, $filter_allowed_records = false ) { |
| 224 | $bookings = new OsBookingModel(); |
| 225 | if ( $limit ) { |
| 226 | $bookings = $bookings->set_limit( $limit ); |
| 227 | } |
| 228 | if ( $filter_allowed_records ) { |
| 229 | $bookings->filter_allowed_records(); |
| 230 | } |
| 231 | |
| 232 | return $bookings->should_be_cancelled()->order_by( 'start_date, start_time asc' )->where( [ 'customer_id' => $this->id ] )->get_results_as_models(); |
| 233 | } |
| 234 | |
| 235 | public function get_future_bookings( $limit = false, $filter_allowed_records = false ) { |
| 236 | $bookings = new OsBookingModel(); |
| 237 | if ( $limit ) { |
| 238 | $bookings = $bookings->set_limit( $limit ); |
| 239 | } |
| 240 | if ( $filter_allowed_records ) { |
| 241 | $bookings->filter_allowed_records(); |
| 242 | } |
| 243 | |
| 244 | 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(); |
| 245 | } |
| 246 | |
| 247 | |
| 248 | public function get_future_bookings_count( $filter_allowed_records = false ) { |
| 249 | $bookings = new OsBookingModel(); |
| 250 | if ( $filter_allowed_records ) { |
| 251 | $bookings->filter_allowed_records(); |
| 252 | } |
| 253 | |
| 254 | return $bookings->should_not_be_cancelled()->where( array( |
| 255 | 'customer_id' => $this->id, |
| 256 | 'OR' => array( |
| 257 | 'start_date >' => OsTimeHelper::today_date( 'Y-m-d' ), |
| 258 | 'AND' => array( |
| 259 | 'start_date' => OsTimeHelper::today_date( 'Y-m-d' ), |
| 260 | 'start_time >' => OsTimeHelper::get_current_minutes() |
| 261 | ) |
| 262 | ) |
| 263 | ) )->count(); |
| 264 | } |
| 265 | |
| 266 | public function get_total_bookings_count( $filter_allowed_records = false ) { |
| 267 | $bookings = new OsBookingModel(); |
| 268 | if ( $filter_allowed_records ) { |
| 269 | $bookings->filter_allowed_records(); |
| 270 | } |
| 271 | |
| 272 | return $bookings->select( 'count(id) as total_bookings' )->where( array( 'customer_id' => $this->id ) )->count(); |
| 273 | } |
| 274 | |
| 275 | |
| 276 | public function filter_allowed_records(): OsModel { |
| 277 | if ( ! OsRolesHelper::are_all_records_allowed() ) { |
| 278 | $this->select( LATEPOINT_TABLE_CUSTOMERS . '.*' )->join( LATEPOINT_TABLE_BOOKINGS, [ 'customer_id' => LATEPOINT_TABLE_CUSTOMERS . '.id' ] )->group_by( LATEPOINT_TABLE_CUSTOMERS . '.id' ); |
| 279 | if ( ! OsRolesHelper::are_all_records_allowed( 'agent' ) ) { |
| 280 | $this->filter_where_conditions( [ LATEPOINT_TABLE_BOOKINGS . '.agent_id' => OsRolesHelper::get_allowed_records( 'agent' ) ] ); |
| 281 | } |
| 282 | if ( ! OsRolesHelper::are_all_records_allowed( 'location' ) ) { |
| 283 | $this->filter_where_conditions( [ LATEPOINT_TABLE_BOOKINGS . '.location_id' => OsRolesHelper::get_allowed_records( 'location' ) ] ); |
| 284 | } |
| 285 | if ( ! OsRolesHelper::are_all_records_allowed( 'service' ) ) { |
| 286 | $this->filter_where_conditions( [ LATEPOINT_TABLE_BOOKINGS . '.service_id' => OsRolesHelper::get_allowed_records( 'service' ) ] ); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | return $this; |
| 291 | } |
| 292 | |
| 293 | public function update_password( $password, $is_hashed = false ) { |
| 294 | if ( ! $is_hashed ) { |
| 295 | $password = OsAuthHelper::hash_password( $password ); |
| 296 | } |
| 297 | |
| 298 | return $this->update_attributes( [ 'password' => $password, 'is_guest' => false ] ); |
| 299 | } |
| 300 | |
| 301 | protected function get_full_name() { |
| 302 | return trim( join( ' ', array( $this->first_name, $this->last_name ) ) ); |
| 303 | } |
| 304 | |
| 305 | protected function get_default_status() { |
| 306 | return 'pending_verification'; |
| 307 | } |
| 308 | |
| 309 | protected function before_create() { |
| 310 | if ( ! isset( $this->is_guest ) ) { |
| 311 | $this->is_guest = true; |
| 312 | } |
| 313 | if ( empty( $this->status ) ) { |
| 314 | $this->status = $this->get_default_status(); |
| 315 | } |
| 316 | if ( empty( $this->password ) ) { |
| 317 | $this->password = wp_hash_password( bin2hex( openssl_random_pseudo_bytes( 8 ) ) ); |
| 318 | } |
| 319 | if ( empty( $this->activation_key ) ) { |
| 320 | $this->activation_key = sha1( wp_rand( 10000, 99999 ) . time() . $this->email ); |
| 321 | } |
| 322 | if ( empty( $this->account_nonse ) ) { |
| 323 | $this->account_nonse = sha1( wp_rand( 10000, 99999 ) . time() . $this->activation_key ); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | |
| 328 | public function get_avatar_url() { |
| 329 | return OsCustomerHelper::get_avatar_url( $this ); |
| 330 | } |
| 331 | |
| 332 | public function get_avatar_image() { |
| 333 | return OsCustomerHelper::get_avatar_image( $this ); |
| 334 | } |
| 335 | |
| 336 | // if this was a guest account without a set password and social login was not used, you can login just by email |
| 337 | public function can_login_without_password() { |
| 338 | return ( $this->is_guest && empty( $this->google_user_id ) && empty( $this->facebook_user_id ) ); |
| 339 | } |
| 340 | |
| 341 | public function prepare_data_before_it_is_set( $data ) { |
| 342 | if ( isset( $data['phone'] ) ) { |
| 343 | $data['phone'] = OsUtilHelper::sanitize_phone_number( $data['phone'] ); |
| 344 | } |
| 345 | |
| 346 | return $data; |
| 347 | } |
| 348 | |
| 349 | |
| 350 | protected function allowed_params( $role = 'admin' ) { |
| 351 | switch ( $role ) { |
| 352 | case 'admin': |
| 353 | $allowed_params = [ |
| 354 | 'id', |
| 355 | 'first_name', |
| 356 | 'last_name', |
| 357 | 'email', |
| 358 | 'phone', |
| 359 | 'avatar_image_id', |
| 360 | 'is_guest', |
| 361 | 'notes', |
| 362 | 'admin_notes', |
| 363 | 'wordpress_user_id', |
| 364 | 'password' |
| 365 | ]; |
| 366 | break; |
| 367 | case 'customer': |
| 368 | case 'public': |
| 369 | $allowed_params = [ |
| 370 | 'first_name', |
| 371 | 'last_name', |
| 372 | 'email', |
| 373 | 'phone', |
| 374 | 'avatar_image_id', |
| 375 | 'notes', |
| 376 | 'password' |
| 377 | ]; |
| 378 | break; |
| 379 | } |
| 380 | |
| 381 | return $allowed_params; |
| 382 | } |
| 383 | |
| 384 | protected function params_to_save( $role = 'admin' ) { |
| 385 | $params_to_save = array( |
| 386 | 'id', |
| 387 | 'first_name', |
| 388 | 'last_name', |
| 389 | 'email', |
| 390 | 'phone', |
| 391 | 'password', |
| 392 | 'activation_key', |
| 393 | 'account_nonse', |
| 394 | 'avatar_image_id', |
| 395 | 'status', |
| 396 | 'is_guest', |
| 397 | 'notes', |
| 398 | 'admin_notes', |
| 399 | 'wordpress_user_id', |
| 400 | 'google_user_id', |
| 401 | 'facebook_user_id' |
| 402 | ); |
| 403 | |
| 404 | return $params_to_save; |
| 405 | } |
| 406 | |
| 407 | protected function properties_to_validate( $alternative_validation = false ) { |
| 408 | // 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) |
| 409 | if ( $alternative_validation ) { |
| 410 | $validations = array( |
| 411 | 'email' => array( 'presence', 'email', 'uniqueness' ), |
| 412 | ); |
| 413 | } else { |
| 414 | $validations = array( |
| 415 | 'first_name' => array( 'presence' ), |
| 416 | 'last_name' => array( 'presence' ), |
| 417 | 'email' => array( 'presence', 'email', 'uniqueness' ), |
| 418 | ); |
| 419 | |
| 420 | $default_fields = OsSettingsHelper::get_default_fields_for_customer(); |
| 421 | foreach ( $default_fields as $name => $field ) { |
| 422 | if ( $field['required'] && $field['active'] ) { |
| 423 | $validations[ $name ][] = 'presence'; |
| 424 | $validations[ $name ] = array_unique( $validations[ $name ] ); |
| 425 | } else { |
| 426 | if ( isset( $validations[ $name ] ) ) { |
| 427 | $validations[ $name ] = array_diff( $validations[ $name ], [ 'presence' ] ); |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | $validations = apply_filters( 'latepoint_customer_model_validations', $validations ); |
| 432 | } |
| 433 | |
| 434 | return $validations; |
| 435 | } |
| 436 | } |