public function getShifts() { //get pay period $day = date('w'); $startofweeks = strtotime('2015-01-05 00:00:00'); $today = strtotime(date('m/d/Y h:m:s')); $weeknum = floor(($today - $startofweeks) / (60 * 60 * 24 * 7)); if ($weeknum % 2 == 0) { //even week = first week of pay period if ($day == 0) { $payPeriodStart = date('m/d/Y', strtotime('-6 days')); $payPeriodEnd = date('m/d/Y', strtotime('+7 days')); } else { $payPeriodStart = date('m/d/Y', strtotime('-' . ($day - 1) . ' days')); $payPeriodEnd = date('m/d/Y', strtotime('+' . (14 - $day) . ' days')); } } else { if ($day == 0) { $payPeriodStart = date('m/d/Y', strtotime('-13 days')); $payPeriodEnd = date('m/d/Y', strtotime('+0 days')); } else { $payPeriodStart = date('m/d/Y', strtotime('-' . ($day + 6) . ' days')); $payPeriodEnd = date('m/d/Y', strtotime('+' . (7 - $day) . ' days')); } } //set query values to either defaulting pay period range or input filter range $start = Input::get('start', $payPeriodStart); $end = Input::get('end', $payPeriodEnd); //sets the start and end date differently since Firefox wouldn't recognize some strings $start = date('Y-m-d', strtotime($start)); $end = date('Y-m-d', strtotime($end)); $end = date('Y-m-d', strtotime($end . ' + 1 day')); //used to set the shift number for easy css coloring and formatting $shiftNumber = 1; //defines how to sort the shifts, defaulting to order them first by user, then by clockin time. $sortBy = Input::get('sort', 'default'); //will pull shifts in a certain manner depending on how its sorted switch ($sortBy) { //sorts by the user name in ascending order case 'nameAsc': $shifts = Shift::where('clockIn', '<=', $end)->where('clockIn', '>=', $start)->get(); $shifts = $this->setProperties($shifts); $shifts = $shifts->sortBy(function ($shift) { return $shift->name . $shift->clockIn; }); //when sortBy() is called, it adds element numbers to all the objects which backbone collection wont recognize. //This is why you have to call values(), to remove the element numbers from the objects. $shifts->values(); break; //sorts by the user name in ascending order //sorts by the user name in ascending order case 'nameDes': $shifts = Shift::where('clockIn', '<=', $end)->where('clockIn', '>=', $start)->get(); $shifts = $this->setProperties($shifts); $shifts = $shifts->sortBy(function ($shift) { return $shift->name . $shift->clockIn; }); //this will reverse the order to get the collection in descending order $shifts = $shifts->reverse(); //when sortBy() is called, it adds element numbers to all the objects which backbone collection wont recognize. //This is why you have to call values(), to remove the element numbers from the objects. $shifts->values(); break; //sorts by clock in with the most recent on top //sorts by clock in with the most recent on top case "timeInAsc": $shifts = Shift::where('clockIn', '<=', $end)->where('clockIn', '>=', $start)->orderBy('clockIn')->get(); $shifts = $this->setProperties($shifts); break; //sorts by clocked in with the most recent at the bottom //sorts by clocked in with the most recent at the bottom case "timeInDes": $shifts = Shift::where('clockIn', '<=', $end)->where('clockIn', '>=', $start)->orderBy('clockIn', 'DESC')->get(); $shifts = $this->setProperties($shifts); break; //sorts by clocked out with the most recent at the top //sorts by clocked out with the most recent at the top case "timeOutAsc": $shifts = Shift::where('clockIn', '<=', $end)->where('clockIn', '>=', $start)->orderBy('clockOut')->get(); $shifts = $this->setProperties($shifts); break; //sorts by clocked out with the most recent at the bottom //sorts by clocked out with the most recent at the bottom case "timeOutDes": $shifts = Shift::where('clockIn', '<=', $end)->where('clockIn', '>=', $start)->orderBy('clockOut', 'DESC')->get(); $shifts = $this->setProperties($shifts); break; //sorts by time recorded with the longest at the top //sorts by time recorded with the longest at the top case "timeRecAsc": $shifts = Shift::where('clockIn', '<=', $end)->where('clockIn', '>=', $start)->get(); $shifts = $this->setProperties($shifts); $shifts = $shifts->sortBy(function ($shift) { return $shift->timeRec; }); //when sortBy() is called, it adds element numbers to all the objects which backbone collection wont recognize. //This is why you have to call values(), to remove the element numbers from the objects. $shifts->values(); break; //sorts by time recorded with the longest at the bottom //sorts by time recorded with the longest at the bottom case "timeRecDes": $shifts = Shift::where('clockIn', '<=', $end)->where('clockIn', '>=', $start)->get(); $shifts = $this->setProperties($shifts); $shifts = $shifts->sortBy(function ($shift) { return $shift->timeRec; }); //this will reverse the order to get the collection in descending order $shifts = $shifts->reverse(); //when sortBy() is called, it adds element numbers to all the objects which backbone collection wont recognize. //This is why you have to call values(), to remove the element numbers from the objects. $shifts->values(); break; //gets all the shifts that are clocked out first then appends the shifts that are still clocked in //gets all the shifts that are clocked out first then appends the shifts that are still clocked in case "clockedInAsc": $shifts1 = Shift::where('clockIn', '<=', $end)->where('clockIn', '>=', $start)->where('clockOut', '=', '0000-00-00 00:00:00')->orderBy('eid')->orderBy('clockIn')->get(); $shifts = Shift::where('clockIn', '<=', $end)->where('clockIn', '>=', $start)->where('clockOut', '!=', '0000-00-00 00:00:00')->orderBy('eid')->orderBy('clockIn')->get(); foreach ($shifts1 as $shift) { $shifts->push($shift); } $shifts = $this->setProperties($shifts); break; //gets all the shifts that are clocked in first then appends the shifts that are clocked out //gets all the shifts that are clocked in first then appends the shifts that are clocked out case "clockedInDes": $shifts1 = Shift::where('clockIn', '<=', $end)->where('clockIn', '>=', $start)->where('clockOut', '!=', '0000-00-00 00:00:00')->orderBy('eid')->orderBy('clockIn')->get(); $shifts = Shift::where('clockIn', '<=', $end)->where('clockIn', '>=', $start)->where('clockOut', '=', '0000-00-00 00:00:00')->orderBy('eid')->orderBy('clockIn')->get(); foreach ($shifts1 as $shift) { $shifts->push($shift); } $shifts = $this->setProperties($shifts); break; //default sorting, order them first by user, then by clockin time. //default sorting, order them first by user, then by clockin time. case "default": $shifts = Shift::where('clockIn', '<=', $end)->where('clockIn', '>=', $start)->orderBy('eid')->orderBy('clockIn')->get(); $shifts = $this->setProperties($shifts); break; } //if a search criteria is passed in, it sets $search to that string or a blank string $search = Input::get('search', ''); //if search isn't empty if ($search != '') { //for each shift, it the shift contains the string, then it adds the shiftNum to it foreach ($shifts as $shift) { if (stripos($shift->name, $search) !== false || stripos($shift->clockIn, $search) !== false || stripos($shift->clockOut, $search) !== false || stripos($shift->timeRec, $search !== false)) { //sets the shift number $shift->shiftNum = $shiftNumber; //increments the shift number $shiftNumber += 1; } else { $shifts->forget($shiftNumber - 1); $shifts->values(); } } } else { //adds shiftNum to each shift foreach ($shifts as $shift) { //sets the shift number $shift->shiftNum = $shiftNumber; //increments the shift number $shiftNumber += 1; } } //returns shifts, even if empty return $shifts->toJSON(); }
public function newShift() { //gets the input values $eid = Input::get('eid'); $clockin = Input::get('clockin'); $clockout = Input::get('clockout'); // need date time for comparisons $clockinAsDateTime = new DateTime($clockin); $clockoutAsDateTime = new DateTime($clockout); $curDateTime = new DateTime(); // can't clock negative hours if ($clockoutAsDateTime < $clockinAsDateTime) { return json_encode(['error' => 'negative hours', 'info' => 'Clock in time must be before the current time and before the clock out time.']); } // wait til you have worked shift to clock it if ($clockoutAsDateTime > $curDateTime && $clockinAsDateTime > $curDateTime) { return json_encode(['error' => 'future shift', 'info' => 'Please log only shifts that you have worked, not ones that you expect to work.']); } // can't clock more than 24 hours $min_clockin = date_sub($clockoutAsDateTime, date_interval_create_from_date_string('1 day')); if ($clockinAsDateTime < $min_clockin) { return json_encode(['error' => 'too long', 'info' => 'The max shift time is 24 hours']); } // Before saving we should make sure no one is over clocking (i.e., // the new shift times don't overlap with existing shift times); // To do this we query for conflicting shifts and reject if the query // returns shifts. The query is pretty ugly, but it's really not // very complex. 4 steps total: // 1) Get shifts for the user who is updating his/her shifts. // 2) Find any shifts with clockin or clockout times that fall within the // updated shift times. If updated shift time is 2-3pm, this finds any // shift whose clockin or clockout is between 2pm and 3pm. // 3) Find any shifts that extend the updated shift times. If updated // shift time is 2-3pm and another shift is 1:30-3:30pm this finds // it. $conflictingShiftClockInTimes = Shift::where('eid', $eid)->where(function ($query) use($clockin, $clockout) { $query->whereBetween('clockIn', [$clockin, $clockout])->orWhereBetween('clockOut', [$clockin, $clockout])->orWhere(function ($query) use($clockin, $clockout) { $query->where('clockOut', '>', $clockout)->where('clockIn', '<', $clockin); }); })->select('clockIn')->get(); // if there are conflicting shifts return them as a string if (!$conflictingShiftClockInTimes->isEmpty()) { $conflicts = ""; foreach ($conflictingShiftClockInTimes as $time) { $conflicts .= $time->clockIn . "<br>"; } return json_encode(['error' => 'conflict', 'info' => $conflicts]); } $newShift = new Shift(); $newShift->eid = $eid; $newShift->clockIn = $clockin; $newShift->clockout = $clockout; $newShift->save(); return json_encode(['error' => 'none']); }
public function getAllShifts() { //get pay period $day = date('w'); $startofweeks = strtotime('2015-01-05 00:00:00'); $today = strtotime(date('m/d/Y h:m:s')); $weeknum = floor(($today - $startofweeks) / (60 * 60 * 24 * 7)); if ($weeknum % 2 == 0) { //even week = first week of pay period if ($day == 0) { $payPeriodStart = date('m/d/Y', strtotime('-6 days')); $payPeriodEnd = date('m/d/Y', strtotime('+7 days')); } else { $payPeriodStart = date('m/d/Y', strtotime('-' . ($day - 1) . ' days')); $payPeriodEnd = date('m/d/Y', strtotime('+' . (14 - $day) . ' days')); } } else { if ($day == 0) { $payPeriodStart = date('m/d/Y', strtotime('-13 days')); $payPeriodEnd = date('m/d/Y', strtotime('+0 days')); } else { $payPeriodStart = date('m/d/Y', strtotime('-' . ($day + 6) . ' days')); $payPeriodEnd = date('m/d/Y', strtotime('+' . (7 - $day) . ' days')); } } //set query values to either defaulting pay period range or input filter range $start = Input::get('start', $payPeriodStart); $end = Input::get('end', $payPeriodEnd); //sets the start and end date differently since Firefox wouldn't recognize some strings $start = date('Y-m-d', strtotime($start)); $end = date('Y-m-d', strtotime($end)); $end = date('Y-m-d', strtotime($end . ' + 1 day')); $end = new DateTime($end); //this will make the clockout time anything before 2:30 am on Monday. $end->add(new DateInterval('PT2H30M')); //get shifts in specified range $shifts = Shift::where('clockOut', '<=', $end)->where('clockIn', '>=', $start)->get(); //adds the user's name to each shift foreach ($shifts as $shift) { $shift->name = User::where('id', '=', $shift->eid)->pluck('fullname'); } //sorts the shifts first by user's last name then by clockin $shifts = $shifts->sortBy(function ($shift) { //gets the number of names-1. E.g. if the user has first, middle, and last names, it will be 2 //if the user has two last names it will be 3 $names = substr_count($shift->name, ' '); switch ($names) { case 1: //pos is the position of the space before the last name $pos = strpos($shift->name, ' '); break; case 2: case 3: //has to offset by the index of the first space +1 $pos = strpos($shift->name, ' ', strpos($shift->name, ' ') + 1); break; default: //if someone has more than names, it will just sort by the second name $pos = strpos($shift->name, ' '); break; } //this will return the last name concatinated with the clockin. This is the string that its sorted by return substr($shift->name, $pos) . $shift->clockIn; }); //removes the index values from each item. If left, Backbone wont recognize it properly $shifts->values(); //returns the shifts return $shifts->toJSON(); }