| 1 |
<?php |
| 2 |
|
| 3 |
namespace Booktics\Models; |
| 4 |
|
| 5 |
use Booktics\Abstracts\User_Model; |
| 6 |
use Booktics\Team_Member\Services\Team_Member_Availability; |
| 7 |
use Exception; |
| 8 |
|
| 9 |
class Team_Member_Model extends User_Model { |
| 10 |
/** |
| 11 |
* Store specific user role |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
protected $role = 'booktics_team_member'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Store specific meta prefix |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
protected $meta_prefix = '_team_member_'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Store user data |
| 26 |
* |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
protected $fillable = [ |
| 30 |
'id' => '', |
| 31 |
'display_name' => '', |
| 32 |
'first_name' => '', |
| 33 |
'last_name' => '', |
| 34 |
'user_email' => '', |
| 35 |
'image' => '', |
| 36 |
'user_login' => '', |
| 37 |
'phone' => '', |
| 38 |
'enable_custom_holiday' => false, |
| 39 |
'enable_custom_schedule' => false, |
| 40 |
'role' => [], |
| 41 |
'status' => '', |
| 42 |
'services' => [], |
| 43 |
'workdays' => [], |
| 44 |
'schedule' => [], |
| 45 |
'custom' => [], |
| 46 |
'holiday' => [], |
| 47 |
'booking_count' => 0, |
| 48 |
]; |
| 49 |
|
| 50 |
/** |
| 51 |
* Display name attribute |
| 52 |
* |
| 53 |
* @param $team_member |
| 54 |
* |
| 55 |
* @return string |
| 56 |
*/ |
| 57 |
public function get_display_name_attribute( $team_member ) { |
| 58 |
return $team_member->first_name . ' ' . $team_member->last_name; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Get team member status attribute |
| 63 |
* |
| 64 |
* Returns 'pending' if team member status is not active, |
| 65 |
* otherwise returns current availability status for the current date |
| 66 |
* |
| 67 |
* @param object $team_member |
| 68 |
* |
| 69 |
* @return string Status of the team member ('active'|'pending') |
| 70 |
*/ |
| 71 |
public function get_status_attribute( $team_member ) { |
| 72 |
return ( new Team_Member_Availability( $team_member ) ) |
| 73 |
->get_status( gmdate( 'Y-m-d' ) ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* workdays attribute |
| 78 |
* |
| 79 |
* @param $team_member |
| 80 |
* |
| 81 |
* @return array |
| 82 |
* @throws Exception |
| 83 |
*/ |
| 84 |
public function get_workdays_attribute( $team_member ) { |
| 85 |
return ( new Team_Member_Availability( $team_member ) ) |
| 86 |
->get_weekly_schedule( gmdate( 'Y-m-d' ) ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* email attribute |
| 91 |
* |
| 92 |
* @param $team_member |
| 93 |
* |
| 94 |
* @return string |
| 95 |
*/ |
| 96 |
public function get_email_attribute( $team_member ) { |
| 97 |
return $team_member->email ?? $team_member->user_email; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get services attribute |
| 102 |
* |
| 103 |
* @param $team_member |
| 104 |
* |
| 105 |
* @return object |
| 106 |
*/ |
| 107 |
public function get_schedule_attribute( $team_member ) { |
| 108 |
return (object) ( $team_member->schedule ?? [] ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Get booking count attribute |
| 113 |
* |
| 114 |
* Returns the total count of appointments for this team member |
| 115 |
* |
| 116 |
* @param object $team_member |
| 117 |
* |
| 118 |
* @return int Number of appointments |
| 119 |
*/ |
| 120 |
public function get_booking_count_attribute( $team_member ) { |
| 121 |
return ( new Appointment_Model() )->get_appointments_count_by_team_member_id( $team_member->id ); |
| 122 |
} |
| 123 |
} |