public function index(Request $request)
 {
     $now = new Carbon();
     if ($request->isMethod('post')) {
         $from = new Carbon($request->input('from', $now->toDateString()));
         $to = new Carbon($request->input('to', $now->toDateString()));
         $lessons = \App\Lesson::orderBy('given_at', 'asc')->where('given_at', '>=', $from->startOfDay()->toDateTimeString())->where('given_at', '<=', $to->endOfDay()->toDateTimeString())->get();
     } else {
         $lessons = \App\Lesson::orderBy('given_at', 'asc')->get();
     }
     $groups = \App\CustomerGroup::orderBy('groupname', 'desc')->get();
     return view('lesson.index')->with(['lessons' => $lessons, 'groups' => $groups, 'now' => $now->toDateString(), 'from' => isset($from) ? $from->toDateString() : '', 'to' => isset($to) ? $to->toDateString() : '']);
 }
 /**
  * Display a listing of tarefas
  *
  * @return Response
  */
 public function index()
 {
     $data = Input::get();
     $data['view'] = Input::has('view') ? Input::get('view') : 'today';
     // today, late, next, done
     $data['paginate'] = Input::has('paginate') ? Input::get('paginate') : 10;
     $dt = new Carbon();
     $tarefas = Tarefa::where(function ($query) use($data, $dt) {
         switch ($data['view']) {
             case 'late':
                 $query->where('date', '<', $dt->format('Y-m-d'))->where('done', false);
                 break;
             case 'next':
                 $query->where('date', '>', $dt->format('Y-m-d'))->where('done', false);
                 break;
             case 'done':
                 $query->where('done', true);
                 break;
             default:
                 // TODAY
                 $query->where('date', '>=', $dt->startOfDay()->format('Y-m-d'))->where('date', '<=', $dt->endOfDay()->format('Y-m-d'));
                 break;
         }
     })->orderBy(Input::get('order_by', 'date'), Input::get('order', 'DESC'))->with('cliente', 'conversas')->paginate(Input::get('paginate', 10));
     // $tarefas = Tarefa::orderBy('date', 'DESC')->with('cliente')->get();
     $hoje = date('Y-m-d');
     $ontem = Carbon::create(date('Y'), date('m'), date('d'))->subDay();
     $amanha = Carbon::create(date('Y'), date('m'), date('d'))->addDay();
     $proximo = Carbon::create(date('Y'), date('m'), date('d'))->addDay();
     //Igual amanhã?
     if ($proximo->isWeekend()) {
         $proximo = new Carbon('next monday');
     }
     $tarefas->pendentes = Tarefa::where('date', '<', $hoje)->where('done', 0)->orderBy('date', 'ASC')->with('cliente', 'conversas')->get();
     $tarefas->hoje = Tarefa::where('date', '<', $amanha->startOfDay())->where('date', '>', $ontem)->where('done', 0)->with('cliente', 'conversas')->get();
     $tarefas->nextDay = Tarefa::where('done', 0)->where('date', '>=', $amanha)->where('date', '<', $proximo->addDay())->orderBy('date', 'DESC')->with('cliente', 'conversas')->get();
     $tarefas->proximas = Tarefa::where('date', '>=', $amanha)->orderBy('date', 'ASC')->where('done', 0)->with('cliente', 'conversas')->get();
     $tarefas->concluidas = Tarefa::where('done', 1)->orderBy('updated_at', 'DESC')->with('cliente', 'conversas')->get();
     $tarefas->days = $tarefas->groupBy(function ($tarefa) {
         return date('Y-m-d', strtotime($tarefa->date));
     });
     if (Request::ajax()) {
         return $tarefas;
     }
     if (Route::is('tarefas.print')) {
         return View::make('tarefas.print', compact('tarefas'));
     } else {
         return View::make('tarefas.index', compact('tarefas'));
     }
 }
Example #3
0
 /**
  * @param string $period
  *
  * @return Carbon
  */
 public function getStartDateForPeriod($period)
 {
     $start = new Carbon();
     switch ($period) {
         default:
         case 'month':
             $start->startOfDay();
             $start->subMonth();
             break;
         case 'year':
             $start->startOfMonth();
             $start->subYear();
             break;
     }
     return $start;
 }
Example #4
0
 /**
  * @param Device $thermostat
  *
  * @return array
  */
 public function getHeatDistribution(Device $thermostat)
 {
     $return = ['today' => ['night' => 0, 'morning' => 0, 'afternoon' => 0, 'evening' => 0], 'yesterday' => ['night' => 0, 'morning' => 0, 'afternoon' => 0, 'evening' => 0], 'average' => ['night' => 0, 'morning' => 0, 'afternoon' => 0, 'evening' => 0]];
     // get total for today, grouped per hour
     // today:
     $result = $thermostat->logEntries()->onDay(new Carbon())->withLogValue('is_heating_on')->groupBy('hour')->whereNotNull('is_heating_on.value')->get([DB::Raw('date_format(log_entries.time,"%H") as `hour`'), DB::Raw('SUM(is_heating_on.value) * 5 as is_heating_on')]);
     $total = $result->sum('is_heating_on');
     foreach ($result as $entry) {
         $hour = intval($entry->hour);
         $total += $entry->is_heating_on;
         if ($hour >= 0 && $hour < 6) {
             $return['today']['night'] += intval($entry->is_heating_on);
         }
         if ($hour >= 6 && $hour < 12) {
             $return['today']['morning'] += intval($entry->is_heating_on);
         }
         if ($hour >= 12 && $hour < 18) {
             $return['today']['afternoon'] += intval($entry->is_heating_on);
         }
         if ($hour >= 18 && $hour <= 23) {
             $return['today']['evening'] += intval($entry->is_heating_on);
         }
     }
     // for each hour loop and group.
     // get total for yesterday, grouped per hour
     $result = $thermostat->logEntries()->onDay(Carbon::now()->subDay())->withLogValue('is_heating_on')->groupBy('hour')->whereNotNull('is_heating_on.value')->get([DB::Raw('date_format(log_entries.time,"%H") as `hour`'), DB::Raw('SUM(is_heating_on.value) * 5 as is_heating_on')]);
     $total = $result->sum('is_heating_on');
     foreach ($result as $entry) {
         $hour = intval($entry->hour);
         $total += $entry->is_heating_on;
         if ($hour >= 0 && $hour < 6) {
             $return['yesterday']['night'] += intval($entry->is_heating_on);
         }
         if ($hour >= 6 && $hour < 12) {
             $return['yesterday']['morning'] += intval($entry->is_heating_on);
         }
         if ($hour >= 12 && $hour < 18) {
             $return['yesterday']['afternoon'] += intval($entry->is_heating_on);
         }
         if ($hour >= 18 && $hour <= 23) {
             $return['yesterday']['evening'] += intval($entry->is_heating_on);
         }
     }
     // get average (using log entries AND summaries).
     // log entries first:
     // (summaries start after five days)
     // go back five days.
     $today = new Carbon();
     $today->startOfDay();
     $today->subDays(5);
     $result = $thermostat->logEntries()->withLogValue('is_heating_on')->after($today)->groupBy('hour')->whereNotNull('is_heating_on.value')->get([DB::Raw('date_format(log_entries.time,"%H") as `hour`'), DB::Raw('SUM(is_heating_on.value) * 5 as is_heating_on')]);
     $total = $result->sum('is_heating_on');
     foreach ($result as $entry) {
         $hour = intval($entry->hour);
         $total += $entry->is_heating_on;
         if ($hour >= 0 && $hour < 6) {
             $return['average']['night'] += intval($entry->is_heating_on);
         }
         if ($hour >= 6 && $hour < 12) {
             $return['average']['morning'] += intval($entry->is_heating_on);
         }
         if ($hour >= 12 && $hour < 18) {
             $return['average']['afternoon'] += intval($entry->is_heating_on);
         }
         if ($hour >= 18 && $hour <= 23) {
             $return['average']['evening'] += intval($entry->is_heating_on);
         }
     }
     // then, summaries!
     $result = $thermostat->summaryEntries()->withSummaryValue('is_heating_on')->before($today)->groupBy('hour')->whereNotNull('is_heating_on.value')->get([DB::Raw('date_format(summary_entries.time,"%H") as `hour`'), DB::Raw('SUM(is_heating_on.value) as is_heating_on')]);
     $total = $result->sum('is_heating_on');
     foreach ($result as $entry) {
         $hour = intval($entry->hour);
         $total += $entry->is_heating_on;
         if ($hour >= 0 && $hour < 6) {
             $return['average']['night'] += intval($entry->is_heating_on);
         }
         if ($hour >= 6 && $hour < 12) {
             $return['average']['morning'] += intval($entry->is_heating_on);
         }
         if ($hour >= 12 && $hour < 18) {
             $return['average']['afternoon'] += intval($entry->is_heating_on);
         }
         if ($hour >= 18 && $hour <= 23) {
             $return['average']['evening'] += intval($entry->is_heating_on);
         }
     }
     // do percentage for each
     foreach ($return as $type => $entry) {
         $sum = $entry['night'] + $entry['morning'] + $entry['afternoon'] + $entry['evening'];
         if ($sum > 0) {
             $return[$type]['night'] = round($entry['night'] / $sum * 100);
             $return[$type]['morning'] = round($entry['morning'] / $sum * 100);
             $return[$type]['afternoon'] = round($entry['afternoon'] / $sum * 100);
             $return[$type]['evening'] = round($entry['evening'] / $sum * 100);
         }
     }
     return $return;
 }
Example #5
0
 /**
  * Summarize the report entries from 5 days ago, to comply with Google Nest's
  * data retention rules.
  */
 public function summarize()
 {
     // go back five days.
     $today = new Carbon();
     $today->startOfDay();
     $today->subDays(5);
     echo "<pre>\n";
     echo "Starting on " . $today->format('d M Y') . "\n";
     // these have our interest.
     $summaries = [];
     $logToSave = ['has_leaf', 'is_heating_on'];
     // indicates that these entries 'add minutes', ie. each entry is +5 minutes.
     $addsMinutes = ['has_leaf', 'is_heating_on'];
     // get all log entries
     $logEntries = $this->_helper->getLogEntriesForDay($today);
     // debug info:
     echo 'Found ' . $logEntries->count() . ' log entries to summarize' . "\n";
     // loop all:
     /** @var Report $logEntry */
     foreach ($logEntries as $logEntry) {
         // get current hour.
         $hour = intval($logEntry->time->format('H'));
         // summary entry:
         if (!isset($summaries[$hour])) {
             // make time:
             $time = clone $logEntry->time;
             $time->hour = $hour;
             $time->minute = 0;
             $time->second = 0;
             // set initial variables:
             $summaries[$hour]['time'] = $time;
             $summaries[$hour]['device'] = intval($logEntry->device_id);
             $summaries[$hour]['entries'] = [];
         }
         // get relevant log entries:
         $values = $this->_helper->getLogValuesByEntry($logEntry, $logToSave);
         // loop them
         /** @var LogValue $value */
         foreach ($values as $value) {
             $name = $value->name;
             // some are booleans, they add to a total time:
             if (in_array($name, $addsMinutes)) {
                 $value = intval($value->value);
                 if ($value == 1) {
                     $summaries[$hour]['entries'][$name] = isset($summaries[$hour]['entries'][$name]) ? $summaries[$hour]['entries'][$name] + 5 : 5;
                 }
             }
             // others are numbers and become an average (like temp).
         }
     }
     // save all summaries built:
     foreach ($summaries as $summary) {
         $entry = $this->_helper->getSummaryEntry($summary['device'], $summary['time']);
         foreach ($summary['entries'] as $name => $value) {
             $value = $this->_helper->createSummaryValue($entry, $name, $value);
             if ($value === false) {
                 echo 'Could not save summary value for time ' . $summary['time']->format('d M Y H:i:s') . ' because value with name "' . $name . '" is already saved for entry #' . $entry->id . "!\n";
             }
         }
         echo "Saved " . $entry->summaryValues()->count() . " summarized fields (+values) for time " . $entry->time->format('d M Y H:i:s') . ".\n";
     }
     echo "Done summarizing.\n";
 }
Example #6
0
 /**
  * Create a Carbon date from a value that should be in the format.
  *
  * @param  mixed $value of date
  * @param  string $format date should be in
  * @return Carbon\Carbon|null
  */
 protected function asDateFromValue($value = null, $format = 'm/d/Y')
 {
     if ($value instanceof Carbon) {
         return $value;
     }
     try {
         return Carbon::createFromFormat($format, $value)->startOfDay();
     } catch (InvalidArgumentException $e) {
     }
     try {
         $date = new Carbon($value);
         return $date->startOfDay();
     } catch (InvalidArgumentException $e) {
         return null;
     }
 }