public function shifts($startTime = null, $endTime = null, $withManager = false) { if ($employeeId = $this->id) { $Shifts = ShiftGateway::where('employee_id', $employeeId); if ($startTime) { $Shifts = $Shifts->where('start_time', '>=', $startTime); } if ($endTime) { $Shifts = $Shifts->where('end_time', '<=', $endTime); } if ($withManager) { $Shifts = $Shifts->with('manager'); } if ($Shifts = $Shifts->get()) { foreach ($Shifts as $Shift) { $newShift = ['id' => $Shift->id, 'break' => $Shift->break, 'startTime' => $Shift->start_time, 'endTime' => $Shift->end_time]; if ($withManager) { $newShift['manager'] = $Shift->manager->name; $newShift['managerPhone'] = $Shift->manager->phone; $newShift['managerEmail'] = $Shift->manager->email; } $shifts[] = $newShift; } } } return $shifts; }
public function listCoworkers() { if ($this->data) { $data = $this->data; $Shifts = ShiftGateway::where('employee_id', '<>', $this->employee_id)->whereNotNull('employee_id'); $Shifts = $Shifts->where(function ($query) use($data) { // search for shift's start_time inside of another shift $query->where(function ($query) use($data) { $query->where('start_time', '<=', $data->start_time)->where('end_time', '>=', $data->start_time); // search for shift's end_time inside of another shift })->orWhere(function ($query) use($data) { $query->where('start_time', '<=', $data->end_time)->where('end_time', '>=', $data->end_time); // search for shifts that are fully contained within this shift's time range })->orWhere(function ($query) use($data) { $query->where('start_time', '>', $data->start_time)->where('end_time', '<', $data->end_time); }); }); $coworkers = []; if ($Shifts = $Shifts->with('employee')->get()) { foreach ($Shifts as $Shift) { $coworkers[$Shift->employee->name] = $Shift->employee->name; } } $coworkers = array_values($coworkers); sort($coworkers); return ['id' => $this->id, 'break' => $this->break, 'startTime' => $this->start_time, 'endTime' => $this->end_time, 'coworkers' => $coworkers]; } }