/**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     //Close records that were left open
     $records = $this->equipmentLogRepository->getActiveRecords();
     foreach ($records as $log) {
         if ($log->last_update && $log->last_update->lt(\Carbon\Carbon::now()->subHour())) {
             //Last update received over an hour ago
             //End the session with the end date being the last update date
             $this->equipmentLogRepository->endSession($log->id, $log->last_update);
         } elseif (!$log->last_update && $log->started->lt(\Carbon\Carbon::now()->subHour())) {
             //started over an hour ago, no updates
             //We don't know how long the user was active so record a minute
             $this->equipmentLogRepository->endSession($log->id, $log->started->addMinute());
         }
     }
     //Combine logs that are very close to each other - this will run over all inactive records that haven't been billed
     $this->combineEquipmentLogs->run();
     //check through all the unbilled inactive records and remove the small ones
     $unbilledRecords = $this->equipmentLogRepository->getUnbilledRecords();
     foreach ($unbilledRecords as $record) {
         $secondsActive = $record->finished->diffInSeconds($record->started);
         //If the record is less than 60 seconds ignore it
         if ($secondsActive <= 60) {
             $record->removed = true;
         }
         //Processing is finished
         //$record->processed = true;
         $record->save();
     }
 }
 /**
  * @return bool
  */
 private function updateLogEntries()
 {
     foreach ($this->logEntries as $entry) {
         //look all the subsequent records for related entries
         //See if there is a record we can join with
         $nextRecord = $this->fetchNextRecord($entry['user_id'], $entry['device'], $entry['reason'], $entry['finished']);
         if ($nextRecord) {
             $this->equipmentLogRepository->update($entry['id'], ['finished' => $nextRecord['finished']]);
             $this->equipmentLogRepository->delete($nextRecord['id']);
             //The array is now dirty - re-fetch it and return to restart the check
             $this->logEntries = $this->equipmentLogRepository->getUnbilledRecords();
             return true;
         }
     }
     return false;
 }