-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathAssetImporter.php
198 lines (161 loc) · 7.5 KB
/
AssetImporter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
namespace App\Importer;
use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\Statuslabel;
use App\Models\User;
use App\Events\CheckoutableCheckedIn;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
class AssetImporter extends ItemImporter
{
protected $defaultStatusLabelId;
public function __construct($filename)
{
parent::__construct($filename);
if (!is_null(Statuslabel::first())) {
$this->defaultStatusLabelId = Statuslabel::deployable()->first()->id;
}
}
protected function handle($row)
{
// ItemImporter handles the general fetching.
parent::handle($row);
if ($this->customFields) {
foreach ($this->customFields as $customField) {
$customFieldValue = $this->array_smart_custom_field_fetch($row, $customField);
if ($customFieldValue) {
if ($customField->field_encrypted == 1) {
$this->item['custom_fields'][$customField->db_column_name()] = Crypt::encrypt($customFieldValue);
$this->log('Custom Field '.$customField->name.': '.Crypt::encrypt($customFieldValue));
} else {
$this->item['custom_fields'][$customField->db_column_name()] = $customFieldValue;
$this->log('Custom Field '.$customField->name.': '.$customFieldValue);
}
} else {
// Clear out previous data.
$this->item['custom_fields'][$customField->db_column_name()] = null;
}
}
}
$this->createAssetIfNotExists($row);
}
/**
* Create the asset if it does not exist.
*
* @author Daniel Melzter
* @since 3.0
* @param array $row
* @return Asset|mixed|null
*/
public function createAssetIfNotExists(array $row)
{
$editingAsset = false;
$asset_tag = $this->findCsvMatch($row, 'asset_tag');
if (empty($asset_tag)){
$asset_tag = Asset::autoincrement_asset();
}
$asset = Asset::where(['asset_tag'=> (string) $asset_tag])->first();
if ($asset) {
if (! $this->updating) {
$this->log('A matching Asset '.$asset_tag.' already exists');
return;
}
$this->log('Updating Asset');
$editingAsset = true;
} else {
$this->log('No Matching Asset, Creating a new one');
$asset = new Asset;
}
// If no status ID is found
if (! array_key_exists('status_id', $this->item) && ! $editingAsset) {
$this->log('No status ID field found, defaulting to first deployable status label.');
$this->item['status_id'] = $this->defaultStatusLabelId;
}
$this->item['notes'] = trim($this->findCsvMatch($row, 'asset_notes'));
$this->item['image'] = trim($this->findCsvMatch($row, 'image'));
$this->item['requestable'] = trim(($this->fetchHumanBoolean($this->findCsvMatch($row, 'requestable'))) == 1) ? '1' : 0;
$asset->requestable = $this->item['requestable'];
$this->item['warranty_months'] = intval(trim($this->findCsvMatch($row, 'warranty_months')));
$this->item['model_id'] = $this->createOrFetchAssetModel($row);
$this->item['byod'] = ($this->fetchHumanBoolean(trim($this->findCsvMatch($row, 'byod'))) == 1) ? '1' : 0;
$this->item['last_checkin'] = trim($this->findCsvMatch($row, 'last_checkin'));
$this->item['last_checkout'] = trim($this->findCsvMatch($row, 'last_checkout'));
$this->item['expected_checkin'] = trim($this->findCsvMatch($row, 'expected_checkin'));
$this->item['last_audit_date'] = trim($this->findCsvMatch($row, 'last_audit_date'));
$this->item['next_audit_date'] = trim($this->findCsvMatch($row, 'next_audit_date'));
$this->item['asset_eol_date'] = trim($this->findCsvMatch($row, 'asset_eol_date'));
$this->item['asset_tag'] = $asset_tag;
// We need to save the user if it exists so that we can checkout to user later.
// Sanitizing the item will remove it.
if (array_key_exists('checkout_target', $this->item)) {
$target = $this->item['checkout_target'];
}
$item = $this->sanitizeItemForStoring($asset, $editingAsset);
// The location id fetched by the csv reader is actually the rtd_location_id.
// This will also set location_id, but then that will be overridden by the
// checkout method if necessary below.
if (isset($this->item['location_id'])) {
$item['rtd_location_id'] = $this->item['location_id'];
}
/**
* We use this to backdate the checkin action further down
*/
$checkin_date = date('Y-m-d H:i:s');
if ($this->item['last_checkin']!='') {
$item['last_checkin'] = $this->parseOrNullDate('last_checkin', 'datetime');
$checkout_date = $this->item['last_checkin'];
}
/**
* We use this to backdate the checkout action further down
*/
$checkout_date = date('Y-m-d H:i:s');
if ($this->item['last_checkout']!='') {
$item['last_checkout'] = $this->parseOrNullDate('last_checkout', 'datetime');
$checkout_date = $this->item['last_checkout'];
}
if ($this->item['expected_checkin']!='') {
$item['expected_checkin'] = $this->parseOrNullDate('expected_checkin');
}
if ($this->item['last_audit_date']!='') {
$item['last_audit_date'] = $this->parseOrNullDate('last_audit_date');
}
if ($this->item['next_audit_date']!='') {
$item['next_audit_date'] = $this->parseOrNullDate('next_audit_date');
}
if ($this->item['asset_eol_date']!='') {
$item['asset_eol_date'] = $this->parseOrNullDate('asset_eol_date');
}
if ($editingAsset) {
$asset->update($item);
} else {
$asset->fill($item);
}
// If we're updating, we don't want to overwrite old fields.
if (array_key_exists('custom_fields', $this->item)) {
foreach ($this->item['custom_fields'] as $custom_field => $val) {
$asset->{$custom_field} = $val;
}
}
// This sets an attribute on the Loggable trait for the action log
$asset->setImported(true);
if ($asset->save()) {
$this->log('Asset '.$this->item['name'].' with serial number '.$this->item['serial'].' was created');
// If we have a target to checkout to, lets do so.
//-- user_id is a property of the abstract class Importer, which this class inherits from and it's set by
//-- the class that needs to use it (command importer or GUI importer inside the project).
if (isset($target) && ($target !== false)) {
if (!is_null($asset->assigned_to)){
if ($asset->assigned_to != $target->id) {
event(new CheckoutableCheckedIn($asset, User::find($asset->assigned_to), Auth::user(), 'Checkin from CSV Importer', $checkin_date));
}
}
$asset->fresh()->checkOut($target, $this->user_id, $checkout_date, null, 'Checkout from CSV Importer', $asset->name);
}
return;
}
$this->logError($asset, 'Asset "'.$this->item['name'].'"');
}
}