protected function _build_schedule_for_site($site_row) { $site_shift_rows = $this->site_shift_model->get_rows(array('site_id' => $site_row->id)); $work_status_rows = $this->work_status_model->get_rows(array()); $start_date = datetime_start_month(); $end_date = datetime_add_days(60, $start_date); $current_date = $start_date; while ($current_date < $end_date) { $day_of_week_name = date('D', strtotime($current_date)); $used_staff_ids = array(); $relief_counts = array(); foreach ($site_shift_rows as $site_shift_row) { $this->schedule_model->delete_where(array('start_date' => $current_date, 'site_id' => $site_row->id, 'shift_type' => $site_shift_row->shift_type)); $staff_rows = $this->staff_assignment_model->get_staff_rows(array('site_id' => $site_row->id, 'shift_type' => $site_shift_row->shift_type, 'assign_type' => 'FullTime')); $relief_counts[$site_shift_row->shift_type] = 0; for ($count = 0; $count < $site_shift_row->staff_count; $count++) { if (count($staff_rows) > 0) { $staff_row = array_pop($staff_rows); $work_status_row = $this->work_status_model->get_row_from_code('W' . strtoupper(substr($site_shift_row->shift_type, 0, 1))); $off_day_names = explode(",", $staff_row->off_day_names); if (in_array($day_of_week_name, $off_day_names)) { $work_status_row = $this->work_status_model->get_row_from_code('O'); $relief_counts[$site_shift_row->shift_type]++; } else { if (rand(0, 100) > 80) { $index = array_rand($work_status_rows, 1); if ($index > 3) { $work_status_row = $work_status_rows[$index]; $relief_counts[$site_shift_row->shift_type]++; } } } $used_staff_ids[] = $staff_row->staff_id; $data = array('start_date' => $current_date, 'site_id' => $site_row->id, 'staff_id' => $staff_row->staff_id, 'shift_type' => $site_shift_row->shift_type, 'work_status_id' => $work_status_row->id); $this->schedule_model->add_row($data); } } } $work_status_row = $this->work_status_model->get_row_from_code('WD'); foreach ($relief_counts as $shift_type => $relief_count) { if ($relief_count > 0) { $staff_rows = $this->staff_assignment_model->get_staff_rows(array('site_id' => $site_row->id, 'assign_type' => 'Relief')); $staff_indexes = array_rand($staff_rows, $relief_count); if (is_numeric($staff_indexes)) { $staff_indexes = array($staff_indexes); } foreach ($staff_indexes as $staff_index) { $staff_row = $staff_rows[$staff_index]; $data = array('start_date' => $current_date, 'site_id' => $site_row->id, 'staff_id' => $staff_row->staff_id, 'shift_type' => $shift_type, 'work_status_id' => $work_status_row->id); $this->schedule_model->add_row($data); } } } $current_date = datetime_add_days(1, $current_date); } }
/** * @ingroup datetime_helper * Function to return the start of the week based on a day value. * @param string $week_day The week day to find the start date on, can be 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' * @param string $date Date to find the start week date on , if blank then use today's date. * @param boolean $round_to_next_week If set to TRUE then calculate the start of next week. * @return string The start of the week date. * */ function datetime_start_week_day($week_day = '', $date = '', $round_to_next_week = true) { $today = datetime_today($date); $week_days = array('Mon' => 1, 'Tue' => 2, 'Wed' => 3, 'Thu' => 4, 'Fri' => 5, 'Sat' => 6, 'Sun' => 7); $start_tim = strtotime($today); $start_day_no = date('N', $start_tim); $day_no = $start_day_no; if (isset($week_days[$week_day])) { $day_no = $week_days[$week_day]; } $date = date('Y-m-d', mktime(0, 0, 0, date('m', $start_tim), date('d', $start_tim) + ($day_no - $start_day_no), date('Y', $start_tim))); if ($date < $today) { if ($round_to_next_week) { $date = datetime_add_days(7, $date); } } return $date; }