/**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $client = new Client();
     $end_point = 'http://api.serviceu.com/rest/events/occurrences?orgKey=b96cd642-acbb-4eb7-95a2-f18c0f01d5b1&format=json';
     $response = $client->get($end_point);
     $data = json_decode($response->getBody(true));
     $active_records = [];
     CalendarEvent::unguard();
     foreach ($data as $event) {
         array_push($active_records, $event->OccurrenceId);
         $record = CalendarEvent::withPast()->where('id', '=', $event->OccurrenceId)->first() ?: new CalendarEvent();
         $record->{'id'} = $event->OccurrenceId;
         $record->{'event_number'} = $event->EventId;
         $record->{'title'} = $event->Name;
         $record->{'starts_at'} = Carbon::createFromFormat('m/d/Y h:i:s A', $event->OccurrenceStartTime);
         $record->{'ends_at'} = Carbon::createFromFormat('m/d/Y h:i:s A', $event->OccurrenceEndTime);
         $record->{'location'} = $event->LocationName;
         $record->{'address'} = $event->LocationAddress;
         $record->{'address2'} = $event->LocationAddress2;
         $record->{'city'} = $event->LocationCity;
         $record->{'state'} = $event->LocationState;
         $record->{'zip'} = $event->LocationZip;
         $record->{'description'} = $event->Description;
         $record->{'contact'} = $event->ContactName;
         $record->{'contact_email'} = $event->ContactEmail;
         $record->{'contact_phone'} = $event->ContactPhone;
         $record->{'department'} = $event->DepartmentName;
         $record->save();
     }
     CalendarEvent::reguard();
     // Remove non-existing events
     CalendarEvent::withPast()->whereNotIn('id', $active_records)->delete();
     // Purge old events
     CalendarEvent::where('ends_at', '<', Carbon::now()->subMonth(2))->delete();
 }
 public function calendarMonth($year, $month)
 {
     $current_month = Carbon::now()->firstOfMonth();
     $beginning_of_month = Carbon::create($year, $month, 1)->firstOfMonth();
     $end_of_month = $beginning_of_month->copy()->endOfMonth();
     $prev_month = $beginning_of_month->copy()->subMonth(1);
     $next_month = $beginning_of_month->copy()->addMonth(1);
     $next_monthHasEvents = CalendarEvent::monthHasEvents($next_month->year, $next_month->month);
     $days = CalendarEvent::withinRange($beginning_of_month, $end_of_month)->where('title', '<>', 'Worship Service')->get()->groupBy(function ($event) {
         return $event->starts_at->format('Y-m-d');
     });
     if ($days->count() === 0 && $current_month->eq($beginning_of_month)) {
         $redirect_to_month = $beginning_of_month->addMonth(1);
         return redirect()->route('calendarMonth', ['year' => $redirect_to_month->year, 'month' => $redirect_to_month->month]);
     }
     return view('events_calendar', ['start' => $beginning_of_month, 'days' => $days, 'prev_url' => '/events/calendar/' . $prev_month->year . '/' . $prev_month->month, 'next_url' => '/events/calendar/' . $next_month->year . '/' . $next_month->month, 'allow_prev' => $beginning_of_month->gt(Carbon::now()->firstOfMonth()), 'allow_next' => $next_monthHasEvents]);
 }