|
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\DB; |
|
9
|
|
|
use Illuminate\Support\Facades\Event; |
|
10
|
|
|
use Illuminate\Support\Facades\Log; |
|
11
|
|
|
use SET\Events\TrainingAssigned; |
|
12
|
|
|
use SET\TrainingUser; |
|
13
|
|
|
|
|
14
|
|
|
class RenewTraining extends Command |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* The number of days to renew a training before it expires. |
|
18
|
|
|
* |
|
19
|
|
|
* @var int |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $offset = 30; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* List of all renewed trainings. |
|
25
|
|
|
* |
|
26
|
|
|
* @var Collection |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $trainingAdminRecord; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The name and signature of the console command. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $signature = 'training:renew'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The console command description. |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $description = 'Renews training before it expires.'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Create a new command instance. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct() |
|
48
|
|
|
{ |
|
49
|
|
|
parent::__construct(); |
|
50
|
|
|
$this->trainingAdminRecord = new Collection(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Execute the console command. |
|
55
|
|
|
* |
|
56
|
|
|
* @return RenewTraining |
|
57
|
|
|
*/ |
|
58
|
|
|
public function handle() |
|
59
|
|
|
{ |
|
60
|
|
|
$trainingUsers = TrainingUser::with('user', 'training') |
|
61
|
|
|
->where('due_date', '<', Carbon::today()) |
|
62
|
|
|
->RenewableTrainings() // Training is renewed (positive renews_in) |
|
63
|
|
|
->activeUsers() |
|
64
|
|
|
->orderBy(DB::raw('CASE WHEN completed_date IS NULL THEN 0 ELSE 1 END')) |
|
65
|
|
|
->orderBy('completed_date', 'desc') |
|
66
|
|
|
->get() |
|
67
|
|
|
->unique(function ($item) { |
|
68
|
|
|
return $item['user_id'].'-'.$item['training_id']; |
|
69
|
|
|
}); |
|
70
|
|
|
|
|
71
|
|
|
foreach ($trainingUsers as $trainingUser) { |
|
72
|
|
|
if (!$this->renewedAlready($trainingUser) && $this->timeToRenew($trainingUser)) { |
|
73
|
|
|
$this->processRenewal($trainingUser); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getList() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->trainingAdminRecord->sortBy('userFullName'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Check if training note has been renewed already. |
|
87
|
|
|
* |
|
88
|
|
|
* @param $trainingUser |
|
89
|
|
|
* |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
private function renewedAlready($trainingUser) |
|
93
|
|
|
{ |
|
94
|
|
|
if (is_null($trainingUser->completed_date)) { |
|
95
|
|
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// Where due_date is in future or withing past renewal period |
|
99
|
|
|
$trainingRecord = TrainingUser::where('training_id', $trainingUser->training_id) |
|
100
|
|
|
->where('user_id', $trainingUser->user_id) |
|
101
|
|
|
->where('due_date', '>', Carbon::today()->subDays($trainingUser->training->renews_in)) |
|
102
|
|
|
->get(); |
|
103
|
|
|
|
|
104
|
|
|
return !$trainingRecord->isEmpty(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Check if the training is past the renews_in value. |
|
109
|
|
|
* |
|
110
|
|
|
* @param $trainingUser |
|
111
|
|
|
* |
|
112
|
|
|
* @return bool |
|
113
|
|
|
*/ |
|
114
|
|
|
private function timeToRenew($trainingUser) |
|
115
|
|
|
{ |
|
116
|
|
|
if ($trainingUser->training->renews_in == 0) { |
|
117
|
|
|
return false; |
|
118
|
|
|
} |
|
119
|
|
|
if (!$trainingUser->completed_date) { |
|
120
|
|
|
return false; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$today = Carbon::today(); |
|
124
|
|
|
|
|
125
|
|
|
$renewalDate = Carbon::createFromFormat('Y-m-d', $trainingUser->completed_date) |
|
126
|
|
|
->addDays($trainingUser->training->renews_in) |
|
127
|
|
|
->subDays($this->offset); |
|
128
|
|
|
|
|
129
|
|
|
return !($renewalDate >= $today); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Generate a new Training note that will be due $this->offset days from now. |
|
134
|
|
|
* |
|
135
|
|
|
* @param $trainingUser |
|
136
|
|
|
*/ |
|
137
|
|
|
private function processRenewal($trainingUser) |
|
138
|
|
|
{ |
|
139
|
|
|
$dueDate = Carbon::createFromFormat('Y-m-d', $trainingUser->completed_date) |
|
140
|
|
|
->addDays($trainingUser->training->renews_in); |
|
141
|
|
|
|
|
142
|
|
|
$assignedTraining = $this->createRecord($trainingUser, $dueDate); |
|
143
|
|
|
|
|
144
|
|
|
//Email user of new training is due |
|
145
|
|
|
Event::fire(new TrainingAssigned($assignedTraining)); |
|
146
|
|
|
Log::info('User training renewal notification.', [ |
|
147
|
|
|
'user_id' => $assignedTraining->user_id, |
|
148
|
|
|
'user_name' => $assignedTraining->user->userFullName, |
|
149
|
|
|
'training_id' => $assignedTraining->training_id, |
|
150
|
|
|
'training_name' => $assignedTraining->training->name, |
|
151
|
|
|
'due_date' => $assignedTraining->due_date, |
|
152
|
|
|
]); |
|
153
|
|
|
|
|
154
|
|
|
// Add the created traininguser record to trainingAdminRecord |
|
155
|
|
|
$this->trainingAdminRecord->push($assignedTraining); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param $trainingUser |
|
160
|
|
|
* @param Carbon $dueDate |
|
161
|
|
|
* |
|
162
|
|
|
* @return TrainingUser |
|
163
|
|
|
*/ |
|
164
|
|
|
private function createRecord($trainingUser, $dueDate) |
|
165
|
|
|
{ |
|
166
|
|
|
$newNote = TrainingUser::create([ |
|
167
|
|
|
'user_id' => $trainingUser->user_id, |
|
168
|
|
|
'author_id' => 1, |
|
169
|
|
|
'training_id' => $trainingUser->training_id, |
|
170
|
|
|
'due_date' => $dueDate->toDateString(), |
|
171
|
|
|
'completed_date' => null, |
|
172
|
|
|
]); |
|
173
|
|
|
|
|
174
|
|
|
return $newNote; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|