/**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     // send email
     $user = User::find(1);
     $data = array('user' => $user);
     $email = Mailman::make('emails.trialWillEnd')->with($data)->to('*****@*****.**')->subject('[Fruit Analytics] Your free trial is ending.')->show();
     //->send();
     File::put(public_path() . '/development_email.html', $email);
 }
 public function fire($job, $data)
 {
     // we only get the user ID, get the user from it
     $user = User::find($data['userID']);
     Calculator::calculateMetricsOnConnect($user);
     Log::info('Sending "ready" email for user: '******'emails.connected')->to($user->email)->subject('Your metrics are ready!')->send();
     $user->ready = 'connected';
     $user->save();
     $job->delete();
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     $users = User::all();
     foreach ($users as $user) {
         if ($user->isTrialEnded()) {
             if ($user->plan == 'trial') {
                 // this is the first time we are checking it
                 $user->plan = 'trial_ended';
                 $user->save();
                 // create intercom event
                 IntercomHelper::trialEnded($user, 'now');
                 // send email
             }
         }
         if ($user->trialWillEndExactlyInDays(3)) {
             // create intercom event
             IntercomHelper::trialWillEnd($user, 3);
             // send email
             $data = array('user' => $user);
             Mailman::make('emails.trialWillEnd')->with($data)->to($user->email)->subject('[Fruit Analytics] Your free trial is ending.')->send();
         }
         if ($user->trialWillEndExactlyInDays(-1)) {
             // create intercom event
             IntercomHelper::trialEnded($user, '1-day-ago');
             // send email
             $data = array('user' => $user);
             Mailman::make('emails.trialEndedFirst')->with($data)->to($user->email)->subject('[Fruit Analytics] Your free trial is ended')->send();
         }
         if ($user->trialWillEndExactlyInDays(-7)) {
             // create intercom event
             IntercomHelper::trialEnded($user, '7-days-ago');
             // send email
             $data = array('user' => $user);
             Mailman::make('emails.trialEndedSecond')->with($data)->to($user->email)->subject('[Fruit Analytics] ')->send();
         }
         if ($user->trialWillEndExactlyInDays(-14)) {
             /// create intercom event
             IntercomHelper::trialEnded($user, '14-days-ago');
             // send email
             $data = array('user' => $user);
             Mailman::make('emails.trialEndedThird')->with($data)->to($user->email)->subject('[Fruit Analytics]')->send();
         }
     }
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     // get all the users
     $users = User::all();
     $previousDayDate = Carbon::yesterday()->toDateString();
     // currently calculated metrics
     $currentMetrics = Calculator::currentMetrics();
     // for each user send email with their metrics
     $dailyEmailSent = 0;
     $weeklyEmailSent = 0;
     foreach ($users as $user) {
         // check if user finished the connect process
         if ($user->isConnected() && $user->ready == 'connected' && !$user->isTrialEnded()) {
             switch ($user->summaryEmailFrequency) {
                 case 'none':
                     // no summary email
                     break;
                 case 'daily':
                     // default behavior, send yesterday's data
                     // get the user's metrics
                     $metric = Metric::where('user', $user->id)->where('date', $previousDayDate)->first();
                     if ($metric) {
                         $previousMetrics = Metric::where('user', $user->id)->where('date', '<=', Carbon::yesterday()->subDays(30)->toDateString())->orderBy('date', 'desc')->first();
                         $changes = array();
                         foreach ($currentMetrics as $metricID => $metricDetails) {
                             // get the correct color
                             $changes[$metricID]['positiveIsGood'] = $metricDetails['metricClass']::POSITIVE_IS_GOOD;
                             $date = $metric->date;
                             if ($previousMetrics) {
                                 if ($previousMetrics->{$metricID} != 0) {
                                     $value = $metric->{$metricID} / $previousMetrics->{$metricID} * 100 - 100;
                                     $changes[$metricID][$date]['isBigger'] = $value > 0 ? true : false;
                                     $changes[$metricID][$date]['value'] = round($value) . ' %';
                                 } else {
                                     $changes[$metricID][$date]['value'] = null;
                                 }
                             } else {
                                 $changes[$metricID][$date]['value'] = null;
                             }
                         }
                         // format metrics to presentable data
                         $metric->formatMetrics();
                         // this line is for making the daily email the same format as the weekly
                         // so we only need one email template
                         $metrics = array($metric->date => $metric);
                         $data = array('metrics' => $metrics, 'currentMetrics' => $currentMetrics, 'changes' => $changes, 'isDaily' => true, 'index' => 0);
                         $email = Mailman::make('emails.summary')->with($data)->to($user->email)->subject('Daily summary')->send();
                         //File::put(public_path().'/summary_email.html',$email);
                         $dailyEmailSent++;
                     }
                     break;
                 case 'weekly':
                     // send a weekly summary to the user with their numbers
                     // check if today is monday (we send weekly emails on monday)
                     /* improvment idea
                     				change this if to switch-case with days
                     				for user controlled daily send
                     			*/
                     if (Carbon::now()->dayOfWeek == Carbon::WEDNESDAY) {
                         // get the user's metrics
                         $metrics = Metric::where('user', $user->id)->where('date', '<=', $previousDayDate)->orderBy('date', 'desc')->take(7)->get();
                         $previousMetrics = Metric::where('user', $user->id)->where('date', '<=', Carbon::yesterday()->subDays(30)->toDateString())->orderBy('date', 'desc')->take(7)->get();
                         $changes = array();
                         foreach ($currentMetrics as $metricID => $metricDetails) {
                             // get the correct color
                             $changes[$metricID]['positiveIsGood'] = $metricDetails['metricClass']::POSITIVE_IS_GOOD;
                             foreach ($previousMetrics as $id => $prevMetric) {
                                 $date = $metrics[$id]->date;
                                 if ($prevMetric->{$metricID} != 0) {
                                     $value = $metrics[$id]->{$metricID} / $prevMetric->{$metricID} * 100 - 100;
                                     $changes[$metricID][$date]['isBigger'] = $value > 0 ? true : false;
                                     $changes[$metricID][$date]['value'] = round($value) . ' %';
                                 } else {
                                     $changes[$metricID][$date]['value'] = null;
                                 }
                             }
                         }
                         // format metrics to presentable data
                         $weeklyMetrics = array();
                         foreach ($metrics as $metric) {
                             $metric->formatMetrics();
                             $weeklyMetrics[$metric->date] = $metric;
                         }
                         $data = array('metrics' => $weeklyMetrics, 'currentMetrics' => $currentMetrics, 'changes' => $changes, 'isDaily' => false, 'index' => 0);
                         // login the user (necessary to get the email address)
                         // Auth::login($user);
                         // send the email to the user
                         $email = Mailman::make('emails.summary')->with($data)->to($user->email)->subject('Weekly summary')->send();
                         //File::put(public_path().'/summary_email.html',$email);
                         $weeklyEmailSent++;
                     }
                     break;
                 default:
                     Log::error('notifications string has been changed, check the email sending code');
                     break;
             }
             // /switch
         }
         // /isConnected
     }
     // /foreach
     Log::info($dailyEmailSent . ' daily summary emails sent out of ' . count($users) . ' users');
     Log::info($weeklyEmailSent . ' daily summary emails sent out of ' . count($users) . ' users');
     Log::info('Total of ' . $dailyEmailSent + $weeklyEmailSent . ' emails sent');
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     $email = Mailman::make('emails.connected')->to('*****@*****.**')->subject('Your metrics are ready!')->send();
     // File::put(public_path().'/connected_email.html',$email);
 }