| 1 |
<?php |
| 2 |
|
| 3 |
namespace Booktics\Team_Member\Services; |
| 4 |
|
| 5 |
use Booktics\Abstracts\Booktics_Database; |
| 6 |
use Booktics\Services\Schedule_Manager; |
| 7 |
use DateTime; |
| 8 |
use Exception; |
| 9 |
|
| 10 |
class Team_Member_Availability { |
| 11 |
/** |
| 12 |
* Store team member id |
| 13 |
* |
| 14 |
* @var string|int |
| 15 |
*/ |
| 16 |
private $team_member; |
| 17 |
|
| 18 |
/** |
| 19 |
* Booktics database object |
| 20 |
* |
| 21 |
* @var Booktics_Database |
| 22 |
*/ |
| 23 |
private $db; |
| 24 |
|
| 25 |
/** |
| 26 |
* Booktics schedule table name |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
private $table_name; |
| 31 |
|
| 32 |
/** |
| 33 |
* Init team member and db |
| 34 |
* |
| 35 |
* @param $team_member |
| 36 |
*/ |
| 37 |
public function __construct( $team_member ) { |
| 38 |
$this->team_member = $team_member->to_array(); |
| 39 |
$this->db = new Booktics_Database(); |
| 40 |
$this->table_name = booktics_get_table_name( 'booktics_schedules' ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* @param $start_date |
| 45 |
* @param $days |
| 46 |
* |
| 47 |
* @return array |
| 48 |
* @throws Exception |
| 49 |
*/ |
| 50 |
public function get_weekly_schedule( $start_date, $days = 7 ): array { |
| 51 |
// If team member status is pending, return empty array |
| 52 |
if ( $this->team_member['status'] === 'pending' ) { |
| 53 |
return array(); |
| 54 |
} |
| 55 |
|
| 56 |
$results = array(); |
| 57 |
$date = new DateTime( $start_date ); |
| 58 |
|
| 59 |
for ( $i = 0; $i < $days; $i++ ) { |
| 60 |
$date_str = $date->format( 'Y-m-d' ); |
| 61 |
$status = $this->get_status( $date_str ); |
| 62 |
$results[] = array( |
| 63 |
'date' => $date_str, |
| 64 |
'day' => $date->format( 'D' ), |
| 65 |
'status' => $status, |
| 66 |
); |
| 67 |
$date->modify( '+1 day' ); |
| 68 |
} |
| 69 |
|
| 70 |
return $results; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get status for a given date |
| 75 |
* |
| 76 |
* @param $date |
| 77 |
* |
| 78 |
* @return string |
| 79 |
*/ |
| 80 |
public function get_status( $date ): string { |
| 81 |
$schedule = $this->get_schedule_for_date( $date ); |
| 82 |
if ( $this->team_member['status'] === 'pending' ) { |
| 83 |
return 'pending'; |
| 84 |
} |
| 85 |
|
| 86 |
if ( empty( $schedule ) ) { |
| 87 |
return 'holiday'; |
| 88 |
} |
| 89 |
|
| 90 |
foreach ( $schedule as $row ) { |
| 91 |
if ( $row->start_time === '0' && $row->end_time === '0' ) { |
| 92 |
return 'holiday'; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return 'working'; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Get schedule for a given date |
| 101 |
* |
| 102 |
* @param $date |
| 103 |
* |
| 104 |
* @return array |
| 105 |
*/ |
| 106 |
private function get_schedule_for_date( $date ): array { |
| 107 |
$day_name = gmdate( 'D', strtotime( $date ) ); |
| 108 |
$day_number = Schedule_Manager::$day_map[ $day_name ]; |
| 109 |
|
| 110 |
// First check for custom date schedule for this team member |
| 111 |
$custom = $this->db->select( |
| 112 |
$this->table_name, array( |
| 113 |
'team_member_id' => $this->team_member['id'], |
| 114 |
'custom_date' => $date, |
| 115 |
) |
| 116 |
); |
| 117 |
|
| 118 |
if ( ! empty( $custom ) ) { |
| 119 |
return $custom; |
| 120 |
} |
| 121 |
|
| 122 |
// Check for custom date schedule in default schedule |
| 123 |
$default_custom = $this->db->select( |
| 124 |
$this->table_name, array( |
| 125 |
'team_member_id' => 0, |
| 126 |
'service_id' => 0, |
| 127 |
'custom_date' => $date, |
| 128 |
) |
| 129 |
); |
| 130 |
|
| 131 |
if ( ! empty( $default_custom ) ) { |
| 132 |
return $default_custom; |
| 133 |
} |
| 134 |
|
| 135 |
// Check for regular weekly schedule for this team member |
| 136 |
$team_member_schedule = $this->db->select( |
| 137 |
$this->table_name, array( |
| 138 |
'team_member_id' => $this->team_member['id'], |
| 139 |
'week_day' => $day_number, |
| 140 |
'custom_date' => '0', |
| 141 |
) |
| 142 |
); |
| 143 |
|
| 144 |
// If team member has their own schedule, return it |
| 145 |
if ( ! empty( $team_member_schedule ) ) { |
| 146 |
return $team_member_schedule; |
| 147 |
} |
| 148 |
|
| 149 |
// If team member has no schedule, fall back to default schedule |
| 150 |
return $this->db->select( |
| 151 |
$this->table_name, array( |
| 152 |
'team_member_id' => 0, |
| 153 |
'service_id' => 0, |
| 154 |
'week_day' => $day_number, |
| 155 |
'custom_date' => '0', |
| 156 |
) |
| 157 |
); |
| 158 |
} |
| 159 |
} |
| 160 |
|