public function workingWith() { // we have a shift object in $this // find any shift that overlaps && employee_id != $this->employee_id $start = date("Y-m-d 00:00:00", strtotime($this->start_time)); $end = date("Y-m-d 23:59:59", strtotime($this->start_time)); $list = []; $shifts = WiwShift::find()->where(['<>', 'employee_id', $this->employee_id])->andWhere(['between', 'start_time', $start, $end])->andWhere(['>=', 'end_time', $this->start_time])->andWhere(['<=', 'start_time', $this->end_time])->all(); // we could return the entire employee object but for now lets just supply the name foreach ($shifts as $key => $shift) { $list[] = $shift->employee->name; } $this->with = $list; }
/** * Gets a summary of hours worked per week for an employee * * As an employee, I want to know how much I worked, by being able to get a summary of hours worked for each week. * @example GET http://api.domain.com/shifts/weeklysummary?user_id=3 * * @param integer $user_id ID of the WiwUser model * * @return array Returns summation of hours worked grouped by week * */ public function actionWeeklysummary($user_id) { $shifts = WiwShift::find() ->select([ 'WEEK(start_time,1) AS week_number', 'YEAR(start_time) as year', 'SUM(HOUR(end_time)-HOUR(start_time)) AS hours_worked']) ->where(['employee_id'=>$user_id]) ->groupBy(['WEEK(start_time,1)']) ->orderBy('start_time ASC') ->all(); $date = new \DateTime(); foreach ($shifts as $key => $shift) { $date = $date->setISODate($shift->year, $shift->week_number); $end = date(DATE_RFC2822,strtotime($date->format('Y-m-d') . " +6 days")); $data['week_begin'] = $date->format(DATE_RFC2822); $data['week_end'] = $end; $data['total_hours'] = $shift->hours_worked; $this->results[] = $data; } return ["summary" => $this->results]; }