Example #1
0
 /**
  * Return attendance of child for the last week
  *
  * @return mixed
  */
 private function lastWeek()
 {
     $checkins = $this->child->checkins()->where(function ($query) {
         $query->where('check_in', '>=', $this->startOfWeek)->whereAnd('check_in', '<=', $this->endOfWeek);
     })->get();
     $days = [];
     foreach ($checkins as $checkin) {
         $index = Carbon::createFromFormat('Y-m-d H:i:s', $checkin['check_in'])->toDateString();
         $check_in = Carbon::createFromFormat('Y-m-d H:i:s', $checkin['check_in']);
         if (is_null($checkin['check_out'])) {
             $date = Carbon::now(Auth::user()->daycare->timezone)->toDateTimeString();
             $check_out = Carbon::createFromFormat('Y-m-d H:i:s', $date);
         } else {
             $check_out = Carbon::createFromFormat('Y-m-d H:i:s', $checkin['check_out']);
         }
         $time = $check_out->diffInMinutes($check_in);
         $days[$index][] = $time;
     }
     foreach ($this->weekArray as $week_day) {
         $dayName = Carbon::createFromFormat('Y-m-d', $week_day)->format('l');
         $attendance[$dayName] = isset($days[$week_day]) ? $this->convertToHours($days[$week_day]) : '0 minutes';
     }
     return $attendance;
 }