1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SET\Http\ViewComposers; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\View\View; |
8
|
|
|
use SET\Training; |
9
|
|
|
use SET\User; |
10
|
|
|
|
11
|
|
|
class ActionItemsComposer |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Bind data to the view. |
15
|
|
|
* |
16
|
|
|
* @param View $view |
17
|
|
|
* |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
|
|
public function compose(View $view) |
21
|
|
|
{ |
22
|
|
|
$dueTraining = $this->getDueTraining(); |
23
|
|
|
$expiringVisits = $this->getExpiringVisits(); |
24
|
|
|
$eligibilityRenewal = $this->getEligibilityRenewal(); |
25
|
|
|
|
26
|
|
|
$view->with('dueTraining', $dueTraining) |
27
|
|
|
->with('expiringVisits', $expiringVisits) |
28
|
|
|
->with('eligibilityRenewal', $eligibilityRenewal); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|static[] |
33
|
|
|
*/ |
34
|
|
|
private function getDueTraining() |
35
|
|
|
{ |
36
|
|
|
return Training::with(['users', 'assignedUsers.user', 'assignedUsers' => function ($query) { |
37
|
|
|
//filter the assignedusers we get back |
38
|
|
|
$query->ActiveUsers() |
39
|
|
|
->whereNull('completed_date') |
40
|
|
|
->where('due_date', '<=', Carbon::now()); |
41
|
|
|
}]) |
42
|
|
|
->whereHas('assignedUsers', function ($q) { |
43
|
|
|
//filter the training we get. |
44
|
|
|
$q->ActiveUsers() |
45
|
|
|
->whereNull('completed_date') |
46
|
|
|
->where('due_date', '<=', Carbon::now()); |
47
|
|
|
}) |
48
|
|
|
->get(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|static[] |
53
|
|
|
*/ |
54
|
|
|
private function getExpiringVisits() |
55
|
|
|
{ |
56
|
|
|
return User::with(['visits' => function ($query) { |
57
|
|
|
$query->whereBetween('expiration_date', [Carbon::now(), Carbon::now()->addWeek()]); |
58
|
|
|
}])->whereHas('visits', function ($q) { |
59
|
|
|
$q->whereBetween('expiration_date', [Carbon::now(), Carbon::now()->addWeek()]); |
60
|
|
|
})->Active()->get(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return Collection |
65
|
|
|
*/ |
66
|
|
|
private function getEligibilityRenewal() |
67
|
|
|
{ |
68
|
|
|
$builtUser = new Collection(); |
69
|
|
|
$users = User::where('inv_close', '<=', Carbon::now())->active()->get(); |
70
|
|
|
|
71
|
|
|
foreach ($users as $user) { |
72
|
|
|
$calculatedDays = $this->calculateDaysToRenewClearance($user); |
73
|
|
|
|
74
|
|
|
$this->buildUserArray($calculatedDays, $builtUser, $user); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->sortUserCollection($builtUser); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $user |
82
|
|
|
* |
83
|
|
|
* @return int |
84
|
|
|
*/ |
85
|
|
|
private function calculateDaysToRenewClearance($user) |
86
|
|
|
{ |
87
|
|
|
$years = 100; |
88
|
|
|
|
89
|
|
|
if ($user->access_level == 'TS' || $user->access_level == 'Int TS') { |
90
|
|
|
$years = 6; |
91
|
|
|
} elseif ($user->access_level == 'S' || $user->clearance == 'S') { |
92
|
|
|
$years = 10; |
93
|
|
|
} elseif ($user->clearance = 'TS' || $user->clearance = 'Int TS') { |
|
|
|
|
94
|
|
|
$years = 6; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$calculatedDays = Carbon::now()->diffInDays( |
98
|
|
|
Carbon::createFromFormat('Y-m-d', $user->inv_close)->addYears($years), false); |
99
|
|
|
|
100
|
|
|
return $calculatedDays; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param int $calculatedDays |
105
|
|
|
* @param $user |
106
|
|
|
* @param $builtUser |
107
|
|
|
*/ |
108
|
|
|
private function buildUserArray($calculatedDays, Collection $builtUser, User $user) |
109
|
|
|
{ |
110
|
|
|
if ($calculatedDays <= 90) { |
111
|
|
|
$builtUser->push([ |
112
|
|
|
'id' => $user->id, |
113
|
|
|
'userFullName' => $user->userFullName, |
114
|
|
|
'days' => $calculatedDays, |
115
|
|
|
]); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param Collection $userArray |
121
|
|
|
*/ |
122
|
|
|
private function sortUserCollection($userArray) |
123
|
|
|
{ |
124
|
|
|
return $userArray->sortByDesc(function ($array) { |
125
|
|
|
return $array['days']; |
126
|
|
|
}); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|