1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SET\Handlers\Excel; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Illuminate\Support\Facades\Input; |
7
|
|
|
use Maatwebsite\Excel\Concerns\ToCollection; |
8
|
|
|
use Maatwebsite\Excel\Concerns\WithHeadingRow; |
9
|
|
|
use SET\Handlers\DateFormat; |
10
|
|
|
use SET\User; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class JpasImportHandler. |
14
|
|
|
* |
15
|
|
|
* Going to handle importing our JPAS excel file. |
16
|
|
|
*/ |
17
|
|
|
class JpasImportHandler implements ToCollection, WithHeadingRow |
18
|
|
|
{ |
19
|
|
|
use DateFormat; |
20
|
|
|
|
21
|
|
|
private $unique; |
22
|
|
|
private $changes; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Set our variables as new collections. |
26
|
|
|
*/ |
27
|
|
|
public function __construct() |
28
|
|
|
{ |
29
|
|
|
$this->unique = new Collection(); |
30
|
|
|
$this->changes = new Collection(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Initial call from UserController. |
35
|
|
|
* Based on what we get, we will either call an initial import or the resolve import. |
36
|
|
|
* |
37
|
|
|
* @param $import |
38
|
|
|
* |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
public function collection(Collection $excel) |
42
|
|
|
{ |
43
|
|
|
$data = Input::all(); |
44
|
|
|
|
45
|
|
|
//We always pass a token and the file. If there is anything more, then we are resolving. |
46
|
|
|
if ($data['resolveImport']) { |
47
|
|
|
$this->resolveImport($excel, $data); |
48
|
|
|
} else { |
49
|
|
|
$this->initialImport($excel); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
//Return this data. It is just used for the initial import, but still nice to see. |
53
|
|
|
return ['unique' => $this->unique, 'changes' => $this->changes]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Cycle through our excel spreadsheet. |
58
|
|
|
* If we can't figure where data goes, we will push it to a collection so the user can map it. |
59
|
|
|
* Otherwise, we will update the record. |
60
|
|
|
* |
61
|
|
|
* @param $excel |
62
|
|
|
*/ |
63
|
|
|
private function initialImport($excel) |
64
|
|
|
{ |
65
|
|
|
foreach ($excel as $row) { |
66
|
|
|
$row['eligibility_date'] = $this->transformDateVal($row['eligibility_date']); |
67
|
|
|
$row['prev_inves'] = $this->transformDateVal($row['prev_inves']); |
68
|
|
|
|
69
|
|
|
$row['name'] = preg_replace('/(,\s|\s)/', '_', $row['name']); |
70
|
|
|
if (!is_null($row['name']) && $row['name'] != '') { //we get a bunch of null records that we can ignore. |
71
|
|
|
$user = User::where('jpas_name', $row['name'])->first(); //see if the record maps to a user |
72
|
|
|
if (is_null($user)) { |
73
|
|
|
$this->unique->push($row); //if no results, we need to have the admin map this record to a user. |
74
|
|
|
} else { |
75
|
|
|
$this->getUserChanges($this->mapJpasToUser($user, $row)); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* User has mapped some accounts. We need to update those records. |
83
|
|
|
* |
84
|
|
|
* @param $excel |
85
|
|
|
* @param $data |
86
|
|
|
*/ |
87
|
|
|
private function resolveImport($excel, $data) |
88
|
|
|
{ |
89
|
|
|
foreach ($data as $jpasName => $userGroup) { |
90
|
|
|
if ($jpasName == 'approve') { |
91
|
|
|
$this->updateUserData($userGroup); |
92
|
|
|
} elseif ($userGroup != '' && is_numeric($userGroup)) { |
93
|
|
|
$this->importNewData($excel, $userGroup, $jpasName); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Place changes so the user can approve/reject them. |
100
|
|
|
* |
101
|
|
|
* @param $user |
102
|
|
|
*/ |
103
|
|
|
private function getUserChanges($user) |
104
|
|
|
{ |
105
|
|
|
if ($user->isDirty()) { |
106
|
|
|
foreach ($user->getDirty() as $attribute => $newValue) { |
107
|
|
|
$this->changes->push([ |
108
|
|
|
'user' => $user, |
109
|
|
|
'field' => $attribute, |
110
|
|
|
'original' => $user->getOriginal($attribute), |
111
|
|
|
'new' => $newValue, |
112
|
|
|
]); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Update user. If there are changes, let's log them to an array. |
119
|
|
|
* |
120
|
|
|
* @param $user |
121
|
|
|
*/ |
122
|
|
|
private function updateAndLogUser($user) |
123
|
|
|
{ |
124
|
|
|
//We will let the user know what changes were made. |
125
|
|
|
if ($user->isDirty()) { |
126
|
|
|
$log = "The following changes have been made for $user->userFullName: \n"; |
127
|
|
|
foreach ($user->getDirty() as $attribute => $value) { |
128
|
|
|
$original = $user->getOriginal($attribute); |
129
|
|
|
$log .= "Changed $attribute from '$original' to '$value'.\n"; |
130
|
|
|
} |
131
|
|
|
$user->save(); |
132
|
|
|
$this->changes->push($log); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Map the data from our JPAS Excel to our User model. |
138
|
|
|
* |
139
|
|
|
* @param $user |
140
|
|
|
* @param $data |
141
|
|
|
*/ |
142
|
|
|
private function mapJpasToUser($user, $data) |
143
|
|
|
{ |
144
|
|
|
$user->jpas_name = $data['name']; |
145
|
|
|
$user->clearance = $data['eligibility']; |
146
|
|
|
$user->elig_date = $this->dateFormat($data['eligibility_date']); |
147
|
|
|
$user->inv = $data['inves']; |
148
|
|
|
$user->inv_close = $this->dateFormat($data['prev_inves']); |
149
|
|
|
|
150
|
|
|
return $user; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param $excel |
155
|
|
|
* @param $userGroup |
156
|
|
|
* @param $jpasName |
157
|
|
|
* |
158
|
|
|
* @return mixed |
159
|
|
|
*/ |
160
|
|
|
private function importNewData($excel, $userGroup, $jpasName) |
161
|
|
|
{ |
162
|
|
|
$user = User::find($userGroup); |
163
|
|
|
foreach ($excel as $row) { |
164
|
|
|
$row['eligibility_date'] = $this->transformDateVal($row['eligibility_date']); |
165
|
|
|
$row['prev_inves'] = $this->transformDateVal($row['prev_inves']); |
166
|
|
|
$row['name'] = preg_replace('/(,\s|\s)/', '_', $row['name']); |
167
|
|
|
if ($row['name'] == $jpasName) { |
168
|
|
|
$this->updateAndLogUser($this->mapJpasToUser($user, $row)); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param $userGroup |
175
|
|
|
*/ |
176
|
|
|
private function updateUserData($userGroup) |
177
|
|
|
{ |
178
|
|
|
foreach ($userGroup as $userId => $changesGroup) { |
179
|
|
|
$user = User::find($userId); |
180
|
|
|
foreach ($changesGroup as $field => $newValue) { |
181
|
|
|
if ($newValue != '0') { |
182
|
|
|
$user[$field] = $newValue; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
$this->updateAndLogUser($user); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Transform excel date values into a datetime string. |
191
|
|
|
* |
192
|
|
|
*/ |
193
|
|
|
public function transformDateVal($value, $format = 'Y-m-d H:i:s') |
194
|
|
|
{ |
195
|
|
|
try { |
196
|
|
|
return \Carbon\Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value)); |
197
|
|
|
} catch (\ErrorException $e) { |
|
|
|
|
198
|
|
|
return \Carbon\Carbon::createFromFormat($format, $value); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
Scrutinizer analyzes your
composer.json
/composer.lock
file if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.