public function find_staff() { /* should have date, start, end set */ $um = new User_Model(); $all_staff = $um->get_staff(); $return = array(); foreach ($all_staff as $u) { $u->warning = array(); $return[$u->id] = $u; } /* find users with shifts that fully overlap this one */ $um->clear(); $bad_staff = $um->distinct()->select('id')->where_in('id', array_keys($return))->where_related('shift', 'date', $this->date)->where_related('shift', 'start <=', $this->start)->where_related('shift', 'end >=', $this->end)->get()->all; foreach ($bad_staff as $u) { unset($return[$u->id]); } /* find overlapping timeoffs */ $tm = new Timeoff_Model(); $timeoffs = $tm->where_related('user', 'id', array_keys($return))->include_related('user', 'id')->where('date <=', $this->date)->where('date_end >=', $this->date)->where('start <', $this->end)->where('end >', $this->start)->get()->all; foreach ($timeoffs as $to) { $return[$to->user_id]->warning = $to; } return $return; }
private function _load_shifts($dates = array(), $staff_id = array(), $location_id = array(), $within = FALSE) { if ($staff_id && count($staff_id) == 1) { $um = new User_Model(); $um->get_by_id($staff_id[0]); $shift_model = $um->shift; $timeoff_model = $um->timeoff; } else { $shift_model = new Shift_Model(); $timeoff_model = new Timeoff_Model(); } if ($dates) { $shift_model->where_in('date', $dates); } $shift_model->include_related('location', 'show_order')->include_related('location', 'id')->include_related('location', 'name')->include_related('user', 'id')->order_by('date', 'ASC')->order_by('start', 'ASC')->order_by('location_show_order', 'ASC'); if ($location_id) { $shift_model->where_related('location', 'id', $location_id); } $shift_model->group_start(); $shift_model->where('status', SHIFT_MODEL::STATUS_ACTIVE); if ($this->auth->check() && $this->app_conf->get('staff_pick_shifts')) { // $shift_model->or_where('user_id IS ', 'NULL', FALSE); } else { $shift_model->where('user_id IS NOT ', 'NULL', FALSE); } $shift_model->group_end(); $this->data['shifts'] = $shift_model->get()->all; if ($within !== FALSE) { $ok_shifts = array(); if ($dates) { $day_starts = array(); reset($dates); foreach ($dates as $d) { $this->hc_time->setDateDb($d); $day_starts[$d] = $this->hc_time->getStartDay(); } } $now = time(); $time_from = $now; $this->hc_time->setTimestamp($time_from); if (strlen($within) && $within) { $this->hc_time->modify('+ ' . $within); } $time_to = $this->hc_time->getTimestamp(); foreach ($this->data['shifts'] as $sh) { if (!isset($day_starts[$sh->date])) { $this->hc_time->setDateDb($sh->date); $day_starts[$sh->date] = $this->hc_time->getStartDay(); } $shift_start = $day_starts[$sh->date] + $sh->start; $shift_end = $day_starts[$sh->date] + $sh->end; $shift_ok = FALSE; if ($shift_end > $time_from && $shift_start < $time_to) { $shift_ok = TRUE; } if ($shift_ok) { $ok_shifts[] = $sh; } } $this->data['shifts'] = $ok_shifts; } $this->data['timeoffs'] = $timeoff_model->where_in('status', array(TIMEOFF_MODEL::STATUS_ACTIVE))->include_related('user', 'id')->order_by('date', 'ASC')->order_by('start', 'ASC')->get()->all; /* load the shifts group ids count and dates*/ $groups = $shift_model->select('group_id, COUNT(id) AS count, MIN(date) AS min_date, MAX(date) AS max_date')->group_by('group_id')->get(); $this->data['shift_groups'] = array(); foreach ($groups as $g) { if (!$g->group_id) { continue; } $this->data['shift_groups'][$g->group_id] = array('count' => $g->count, 'min_date' => $g->min_date, 'max_date' => $g->max_date); } }
private function _load_shifts($date = 0, $staff_id = 0) { if ($staff_id) { $um = new User_Model(); $um->get_by_id($staff_id); $shift_model = $um->shift; $timeoff_model = $um->timeoff; } else { $shift_model = new Shift_Model(); $timeoff_model = new Timeoff_Model(); } if ($date) { if (is_array($date)) { $this->hc_time->setDateDb($date[1]); $this->hc_time->modify('+1 day'); $tomorrow = $this->hc_time->formatDate_Db(); $this->hc_time->setDateDb($date[0]); $this->hc_time->modify('-1 day'); $yesterday = $this->hc_time->formatDate_Db(); } else { $this->hc_time->setDateDb($date); $this->hc_time->modify('+1 day'); $tomorrow = $this->hc_time->formatDate_Db(); $this->hc_time->setDateDb($date); $this->hc_time->modify('-1 day'); $yesterday = $this->hc_time->formatDate_Db(); } $shift_model->where('date <=', $tomorrow); $shift_model->where('date >=', $yesterday); $timeoff_model->where('date <', $tomorrow); $timeoff_model->where('date_end >', $yesterday); } $shift_model->include_related('location', 'show_order')->include_related('location', 'id')->include_related('location', 'name')->include_related('user', 'id')->order_by('date', 'ASC')->order_by('start', 'ASC')->order_by('location_show_order', 'ASC'); $this->data['shifts'] = $shift_model->get()->all; $this->data['timeoffs'] = $timeoff_model->where_in('status', array(TIMEOFF_MODEL::STATUS_ACTIVE))->include_related('user', 'id')->order_by('date', 'ASC')->order_by('start', 'ASC')->get()->all; /* load the shifts group ids count and dates*/ $groups = $shift_model->select('group_id, COUNT(id) AS count, MIN(date) AS min_date, MAX(date) AS max_date')->group_by('group_id')->get(); $this->data['shift_groups'] = array(); foreach ($groups as $g) { if (!$g->group_id) { continue; } $this->data['shift_groups'][$g->group_id] = array('count' => $g->count, 'min_date' => $g->min_date, 'max_date' => $g->max_date); } }