1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SET\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Illuminate\Database\Eloquent\Collection; |
8
|
|
|
use Illuminate\Support\Facades\Event; |
9
|
|
|
use Illuminate\Support\Facades\Mail; |
10
|
|
|
use SET\Events\TrainingAssigned; |
11
|
|
|
use SET\TrainingUser; |
12
|
|
|
use SET\Visit; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class SendReminders. |
16
|
|
|
*/ |
17
|
|
|
class SendReminders extends Command |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* The name and signature of the console command. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $signature = 'emails:reminders'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The console command description. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $description = 'Sends out email reminders a week before they are due.'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var |
35
|
|
|
*/ |
36
|
|
|
protected $trainingUsers; |
37
|
|
|
/** |
38
|
|
|
* @var |
39
|
|
|
*/ |
40
|
|
|
protected $visits; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Execute the console command. |
44
|
|
|
* Sends out reminder emails to all employees. Then sends a reminder to their supervisor. |
45
|
|
|
* |
46
|
|
|
* @return mixed |
47
|
|
|
*/ |
48
|
|
|
public function handle() |
49
|
|
|
{ |
50
|
|
|
$this->setTrainingUsers(); |
51
|
|
|
$this->setVisits(); |
52
|
|
|
|
53
|
|
|
foreach ($this->trainingUsers as $trainingUser) { |
54
|
|
|
Event::fire(new TrainingAssigned($trainingUser)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->emailSupervisor(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Email the employee's supervisor if a user has training that isn't complete yet. |
62
|
|
|
*/ |
63
|
|
|
public function emailSupervisor() |
64
|
|
|
{ |
65
|
|
|
$supervisors = $this->getSupervisors(); |
66
|
|
|
|
67
|
|
|
foreach ($supervisors as $supervisor) { |
68
|
|
|
$newNotes = new Collection(); |
69
|
|
|
|
70
|
|
|
$this->trainingUsers->each(function ($item) use ($supervisor, $newNotes) { |
71
|
|
|
if ($item->user->supervisor_id == $supervisor->id) { |
72
|
|
|
$newNotes->push($item); |
73
|
|
|
} |
74
|
|
|
}); |
75
|
|
|
|
76
|
|
|
if (!$newNotes->isEmpty()) { |
77
|
|
|
Mail::send('emails.supervisor_reminder', ['notes' => $newNotes], function ($m) use ($supervisor) { |
78
|
|
|
$m->to($supervisor->email, $supervisor->userFullName) |
79
|
|
|
->subject($supervisor->email.' Training that your employees need to complete.'); |
80
|
|
|
}); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Stores a list of incomplete training. |
87
|
|
|
*/ |
88
|
|
|
public function setTrainingUsers() |
89
|
|
|
{ |
90
|
|
|
$this->trainingUsers = TrainingUser::with(['training', 'training.attachments', 'user', 'user.supervisor']) |
91
|
|
|
->where('due_date', '<=', Carbon::today()->addWeek(2)) |
92
|
|
|
->whereNull('completed_date') |
93
|
|
|
->activeUsers() |
94
|
|
|
->orderBy('due_date') |
95
|
|
|
->get(); |
96
|
|
|
|
97
|
|
|
return $this->trainingUsers; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Stores a list of visits that are due to expire sometime between yesterday and the next week. |
102
|
|
|
*/ |
103
|
|
|
public function setVisits() |
104
|
|
|
{ |
105
|
|
|
$this->visits = Visit::with('user') |
106
|
|
|
->whereBetween('expiration_date', [ |
107
|
|
|
Carbon::today()->subDay()->toDateString(), Carbon::today()->addWeek()->toDateString(), |
108
|
|
|
]) |
109
|
|
|
->activeUsers() |
110
|
|
|
->orderBy('expiration_date') |
111
|
|
|
->get(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get our Notes list. |
116
|
|
|
* |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
|
|
public function gettrainingUsers() |
120
|
|
|
{ |
121
|
|
|
return $this->trainingUsers; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get our Visits list. |
126
|
|
|
* |
127
|
|
|
* @return mixed |
128
|
|
|
*/ |
129
|
|
|
public function getVisits() |
130
|
|
|
{ |
131
|
|
|
return $this->visits; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Take a list of notes and return a list of supervisors related to those notes. |
136
|
|
|
* |
137
|
|
|
* @return Collection |
138
|
|
|
*/ |
139
|
|
|
private function getSupervisors() |
140
|
|
|
{ |
141
|
|
|
$supervisors = new Collection(); |
142
|
|
|
foreach ($this->trainingUsers as $trainingUser) { |
143
|
|
|
if ($trainingUser->user->supervisor) { |
144
|
|
|
$supervisors->push($trainingUser->user->supervisor); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $supervisors->unique(); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|