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\Mail\EmailSupervisorReminder; |
12
|
|
|
use SET\TrainingUser; |
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
|
|
|
/** |
39
|
|
|
* Execute the console command. |
40
|
|
|
* Sends out reminder emails to all employees. Then sends a reminder to their supervisor. |
41
|
|
|
* |
42
|
|
|
* @return SendReminders |
43
|
|
|
*/ |
44
|
4 |
|
public function handle() |
45
|
|
|
{ |
46
|
4 |
|
$this->setTrainingUsers(); |
47
|
|
|
|
48
|
4 |
|
foreach ($this->trainingUsers as $trainingUser) { |
49
|
2 |
|
Event::fire(new TrainingAssigned($trainingUser)); |
50
|
|
|
} |
51
|
|
|
|
52
|
4 |
|
$this->emailSupervisor(); |
53
|
|
|
|
54
|
4 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Email the employee's supervisor if a user has training that isn't complete yet. |
59
|
|
|
*/ |
60
|
4 |
|
public function emailSupervisor() |
61
|
|
|
{ |
62
|
4 |
|
$supervisors = $this->getSupervisors(); |
63
|
|
|
|
64
|
4 |
|
foreach ($supervisors as $supervisor) { |
65
|
2 |
|
$newNotes = new Collection(); |
66
|
|
|
|
67
|
2 |
|
$this->trainingUsers->each(function ($item) use ($supervisor, $newNotes) { |
68
|
2 |
|
if ($item->user->supervisor_id == $supervisor->id) { |
69
|
2 |
|
$newNotes->push($item); |
70
|
|
|
} |
71
|
2 |
|
}); |
72
|
|
|
|
73
|
2 |
|
if (!$newNotes->isEmpty()) { |
74
|
2 |
|
Mail::to($supervisor)->send(new EmailSupervisorReminder($newNotes)); |
75
|
|
|
} |
76
|
|
|
} |
77
|
4 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Stores a list of incomplete training. |
81
|
|
|
*/ |
82
|
4 |
|
public function setTrainingUsers() |
83
|
|
|
{ |
84
|
4 |
|
$this->trainingUsers = TrainingUser::with([ |
85
|
4 |
|
'training', 'training.attachments', 'user', 'user.supervisor', |
86
|
|
|
]) |
87
|
4 |
|
->where('due_date', '<=', Carbon::today()->addWeek()) |
88
|
4 |
|
->whereNull('completed_date') |
89
|
4 |
|
->activeUsers() |
90
|
4 |
|
->orderBy('due_date') |
91
|
4 |
|
->get(); |
92
|
|
|
|
93
|
4 |
|
return $this->trainingUsers; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get our Notes list. |
98
|
|
|
* |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
1 |
|
public function getList() |
102
|
|
|
{ |
103
|
1 |
|
return $this->trainingUsers; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Take a list of notes and return a list of supervisors related to those notes. |
108
|
|
|
* |
109
|
|
|
* @return Collection |
110
|
|
|
*/ |
111
|
4 |
|
private function getSupervisors() |
112
|
|
|
{ |
113
|
4 |
|
$supervisors = new Collection(); |
114
|
4 |
|
foreach ($this->trainingUsers as $trainingUser) { |
115
|
2 |
|
if ($trainingUser->user->supervisor) { |
116
|
2 |
|
$supervisors->push($trainingUser->user->supervisor); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
4 |
|
return $supervisors->unique(); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|