1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SET\Handlers\Excel; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Illuminate\Support\Facades\Input; |
7
|
|
|
use Maatwebsite\Excel\Files\ImportHandler; |
8
|
|
|
use SET\Handlers\DateFormat; |
9
|
|
|
use SET\User; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class JpasImportHandler. |
13
|
|
|
* |
14
|
|
|
* Going to handle importing our JPAS excel file. |
15
|
|
|
*/ |
16
|
|
|
class JpasImportHandler implements ImportHandler |
17
|
|
|
{ |
18
|
|
|
use DateFormat; |
19
|
|
|
|
20
|
|
|
private $unique; |
21
|
|
|
private $changes; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Set our variables as new collections. |
25
|
|
|
*/ |
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
$this->unique = new Collection(); |
29
|
|
|
$this->changes = new Collection(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Initial call from UserController. |
34
|
|
|
* Based on what we get, we will either call an initial import or the resolve import. |
35
|
|
|
* |
36
|
|
|
* @param $import |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
public function handle($import) |
41
|
|
|
{ |
42
|
|
|
$excel = $import->get(); |
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->name = preg_replace('/(,\s|\s)/', '_', $row->name); |
67
|
|
|
if (!is_null($row->name) && $row->name != '') { //we get a bunch of null records that we can ignore. |
68
|
|
|
$user = User::where('jpas_name', $row->name)->first(); //see if the record maps to a user |
69
|
|
|
if (is_null($user)) { |
70
|
|
|
$this->unique->push($row); //if no results, we need to have the admin map this record to a user. |
71
|
|
|
} else { |
72
|
|
|
$this->getUserChanges($this->mapJpasToUser($user, $row)); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* User has mapped some accounts. We need to update those records. |
80
|
|
|
* |
81
|
|
|
* @param $excel |
82
|
|
|
* @param $data |
83
|
|
|
*/ |
84
|
|
|
private function resolveImport($excel, $data) |
85
|
|
|
{ |
86
|
|
|
foreach ($data as $jpasName => $userGroup) { |
87
|
|
|
if ($jpasName == 'approve') { |
88
|
|
|
$this->updateUserData($userGroup); |
89
|
|
|
} elseif ($userGroup != '' && is_numeric($userGroup)) { |
90
|
|
|
$this->importNewData($excel, $userGroup, $jpasName); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Place changes so the user can approve/reject them. |
97
|
|
|
* |
98
|
|
|
* @param $user |
99
|
|
|
*/ |
100
|
|
|
private function getUserChanges($user) |
101
|
|
|
{ |
102
|
|
|
if ($user->isDirty()) { |
103
|
|
|
foreach ($user->getDirty() as $attribute => $newValue) { |
104
|
|
|
$this->changes->push([ |
105
|
|
|
'user' => $user, |
106
|
|
|
'field' => $attribute, |
107
|
|
|
'original' => $user->getOriginal($attribute), |
108
|
|
|
'new' => $newValue, |
109
|
|
|
]); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Update user. If there are changes, let's log them to an array. |
116
|
|
|
* |
117
|
|
|
* @param $user |
118
|
|
|
*/ |
119
|
|
|
private function updateAndLogUser($user) |
120
|
|
|
{ |
121
|
|
|
//We will let the user know what changes were made. |
122
|
|
|
if ($user->isDirty()) { |
123
|
|
|
$log = "The following changes have been made for $user->userFullName: \n"; |
124
|
|
|
foreach ($user->getDirty() as $attribute => $value) { |
125
|
|
|
$original = $user->getOriginal($attribute); |
126
|
|
|
$log .= "Changed $attribute from '$original' to '$value'.\n"; |
127
|
|
|
} |
128
|
|
|
$user->save(); |
129
|
|
|
$this->changes->push($log); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Map the data from our JPAS Excel to our User model. |
135
|
|
|
* |
136
|
|
|
* @param $user |
137
|
|
|
* @param $data |
138
|
|
|
*/ |
139
|
|
|
private function mapJpasToUser($user, $data) |
140
|
|
|
{ |
141
|
|
|
$user->jpas_name = $data->name; |
142
|
|
|
$user->clearance = $data->eligibility; |
143
|
|
|
$user->elig_date = $this->dateFormat($data->eligibility_date); |
144
|
|
|
$user->inv = $data->inves; |
145
|
|
|
$user->inv_close = $this->dateFormat($data->prev_inves); |
146
|
|
|
|
147
|
|
|
return $user; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param $excel |
152
|
|
|
* @param $userGroup |
153
|
|
|
* @param $jpasName |
154
|
|
|
* |
155
|
|
|
* @return mixed |
156
|
|
|
*/ |
157
|
|
|
private function importNewData($excel, $userGroup, $jpasName) |
158
|
|
|
{ |
159
|
|
|
$user = User::find($userGroup); |
160
|
|
|
foreach ($excel as $row) { |
161
|
|
|
$row->name = preg_replace('/(,\s|\s)/', '_', $row->name); |
162
|
|
|
if ($row->name == $jpasName) { |
163
|
|
|
$this->updateAndLogUser($this->mapJpasToUser($user, $row)); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param $userGroup |
170
|
|
|
*/ |
171
|
|
|
private function updateUserData($userGroup) |
172
|
|
|
{ |
173
|
|
|
foreach ($userGroup as $userId => $changesGroup) { |
174
|
|
|
$user = User::find($userId); |
175
|
|
|
foreach ($changesGroup as $field => $newValue) { |
176
|
|
|
if ($newValue != '0') { |
177
|
|
|
$user[$field] = $newValue; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
$this->updateAndLogUser($user); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|