AssetImportCommand.php
Go to the documentation of this file.
1 <?php
12 
15 
16 class AssetImportCommand extends Command {
17 
23  protected $name = 'asset-import:csv';
24 
30  protected $description = 'Import Assets from CSV';
31 
37  public function __construct()
38  {
39  parent::__construct();
40  }
41 
47  public function fire()
48  {
49  $filename = $this->argument('filename');
50 
51 
52  if (!$this->option('testrun')=='true') {
53  $this->comment('======= Importing Assets from '.$filename.' =========');
54  } else {
55  $this->comment('====== TEST ONLY Asset Import for '.$filename.' ====');
56  $this->comment('============== NO DATA WILL BE WRITTEN ==============');
57  }
58 
59  if (! ini_get("auto_detect_line_endings")) {
60  ini_set("auto_detect_line_endings", '1');
61  }
62 
63  $csv = Reader::createFromPath($this->argument('filename'));
64  $csv->setNewline("\r\n");
65  $csv->setOffset(1);
66  $duplicates = '';
67 
68  // Loop through the records
69  $nbInsert = $csv->each(function ($row) use ($duplicates) {
70  $status_id = 1;
71 
72  // Let's just map some of these entries to more user friendly words
73 
74  // User's name
75  if (array_key_exists('0',$row)) {
76  $user_name = trim($row[0]);
77  } else {
78  $user_name = '';
79  }
80 
81  // User's email
82  if (array_key_exists('1',$row)) {
83  $user_email = trim($row[1]);
84  } else {
85  $user_email = '';
86  }
87 
88  // User's email
89  if (array_key_exists('2',$row)) {
90  $user_username = trim($row[2]);
91  } else {
92  $user_username = '';
93  }
94 
95  // Asset Name
96  if (array_key_exists('3',$row)) {
97  $user_asset_asset_name = trim($row[3]);
98  } else {
99  $user_asset_asset_name = '';
100  }
101 
102  // Asset Category
103  if (array_key_exists('4',$row)) {
104  $user_asset_category = trim($row[4]);
105  } else {
106  $user_asset_category = '';
107  }
108 
109  // Asset Name
110  if (array_key_exists('5',$row)) {
111  $user_asset_name = trim($row[5]);
112  } else {
113  $user_asset_name = '';
114  }
115 
116  // Asset Manufacturer
117  if (array_key_exists('6',$row)) {
118  $user_asset_mfgr = trim($row[6]);
119  } else {
120  $user_asset_mfgr = '';
121  }
122 
123  // Asset model number
124  if (array_key_exists('7',$row)) {
125  $user_asset_modelno = trim($row[7]);
126  } else {
127  $user_asset_modelno = '';
128  }
129 
130  // Asset serial number
131  if (array_key_exists('8',$row)) {
132  $user_asset_serial = trim($row[8]);
133  } else {
134  $user_asset_serial = '';
135  }
136 
137  // Asset tag
138  if (array_key_exists('9',$row)) {
139  $user_asset_tag = trim($row[9]);
140  } else {
141  $user_asset_tag = '';
142  }
143 
144  // Asset location
145  if (array_key_exists('10',$row)) {
146  $user_asset_location = trim($row[10]);
147  } else {
148  $user_asset_location = '';
149  }
150 
151  // Asset notes
152  if (array_key_exists('11',$row)) {
153  $user_asset_notes = trim($row[11]);
154  } else {
155  $user_asset_notes = '';
156  }
157 
158  // Asset purchase date
159  if (array_key_exists('12',$row)) {
160  if ($row[12]!='') {
161  $user_asset_purchase_date = date("Y-m-d 00:00:01", strtotime($row[12]));
162  } else {
163  $user_asset_purchase_date = '';
164  }
165  } else {
166  $user_asset_purchase_date = '';
167  }
168 
169  // Asset purchase cost
170  if (array_key_exists('13',$row)) {
171  if ($row[13]!='') {
172  $user_asset_purchase_cost = trim($row[13]);
173  } else {
174  $user_asset_purchase_cost = '';
175  }
176  } else {
177  $user_asset_purchase_cost = '';
178  }
179 
180  // Asset Company Name
181  if (array_key_exists('14',$row)) {
182  if ($row[14]!='') {
183  $user_asset_company_name = trim($row[14]);
184  } else {
185  $user_asset_company_name= '';
186  }
187  } else {
188  $user_asset_company_name = '';
189  }
190 
191 
192  // A number was given instead of a name
193  if (is_numeric($user_name)) {
194  $this->comment('User '.$user_name.' is not a name - assume this user already exists');
195  $user_username = '';
196  $first_name = '';
197  $last_name = '';
198 
199  // No name was given
200  } elseif ($user_name=='') {
201  $this->comment('No user data provided - skipping user creation, just adding asset');
202  $first_name = '';
203  $last_name = '';
204  //$user_username = '';
205 
206  } else {
207  $user_email_array = User::generateFormattedNameFromFullName($this->option('email_format'), $user_name);
208  $first_name = $user_email_array['first_name'];
209  $last_name = $user_email_array['last_name'];
210 
211  if ($user_email=='') {
212  $user_email = $user_email_array['username'].'@'.config('app.domain');
213  }
214 
215  if ($user_username=='') {
216  if ($this->option('username_format')=='email') {
217  $user_username = $user_email;
218  } else {
219  $user_name_array = User::generateFormattedNameFromFullName($this->option('username_format'), $user_name);
220  $user_username = $user_name_array['username'];
221  }
222 
223  }
224 
225  }
226 
227  $this->comment('Full Name: '.$user_name);
228  $this->comment('First Name: '.$first_name);
229  $this->comment('Last Name: '.$last_name);
230  $this->comment('Username: '.$user_username);
231  $this->comment('Email: '.$user_email);
232  $this->comment('Category Name: '.$user_asset_category);
233  $this->comment('Item: '.$user_asset_name);
234  $this->comment('Manufacturer ID: '.$user_asset_mfgr);
235  $this->comment('Model No: '.$user_asset_modelno);
236  $this->comment('Serial No: '.$user_asset_serial);
237  $this->comment('Asset Tag: '.$user_asset_tag);
238  $this->comment('Location: '.$user_asset_location);
239  $this->comment('Purchase Date: '.$user_asset_purchase_date);
240  $this->comment('Purchase Cost: '.$user_asset_purchase_cost);
241  $this->comment('Notes: '.$user_asset_notes);
242  $this->comment('Company Name: '.$user_asset_company_name);
243 
244  $this->comment('------------- Action Summary ----------------');
245 
246  if ($user_username!='') {
247  if ($user = User::MatchEmailOrUsername($user_username, $user_email)
248  ->whereNotNull('username')->first()) {
249  $this->comment('User '.$user_username.' already exists');
250  } else {
251  $user = new \App\Models\User;
252  $password = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 20);
253 
254  $user->first_name = $first_name;
255  $user->last_name = $last_name;
256  $user->username = $user_username;
257  $user->email = $user_email;
258  $user->permissions = '{user":1}';
259  $user->password = bcrypt($password);
260  $user->activated = 1;
261  if ($user->save()) {
262  $this->comment('User '.$first_name.' created');
263  } else {
264  $this->comment('ERROR CREATING User '.$first_name.' '.$last_name);
265  $this->comment($user->getErrors());
266  }
267 
268  }
269  } else {
270  $user = new User;
271  }
272 
273  // Check for the location match and create it if it doesn't exist
274  if ($location = Location::where('name', e($user_asset_location))->first()) {
275  $this->comment('Location '.$user_asset_location.' already exists');
276  } else {
277 
278  $location = new Location();
279 
280  if ($user_asset_location!='')
281  {
282  $location->name = e($user_asset_location);
283  $location->address = '';
284  $location->city = '';
285  $location->state = '';
286  $location->country = '';
287  $location->user_id = 1;
288 
289  if (!$this->option('testrun')=='true') {
290 
291  if ($location->save()) {
292  $this->comment('Location '.$user_asset_location.' was created');
293  } else {
294  $this->comment('Something went wrong! Location '.$user_asset_location.' was NOT created');
295  $this->comment($location->getErrors());
296  }
297 
298  }
299  else
300  {
301  $this->comment('Location '.$user_asset_location.' was (not) created - test run only');
302  }
303  }
304  else
305  {
306  $this->comment('No location given, so none created.');
307  }
308 
309  }
310 
311  if (e($user_asset_category)=='') {
312  $category_name = 'Unnamed Category';
313  } else {
314  $category_name = e($user_asset_category);
315  }
316 
317  // Check for the category match and create it if it doesn't exist
318  if ($category = Category::where('name', e($category_name))->where('category_type', 'asset')->first()) {
319  $this->comment('Category '.$category_name.' already exists');
320 
321  } else {
322  $category = new Category();
323  $category->name = e($category_name);
324  $category->category_type = 'asset';
325  $category->user_id = 1;
326 
327  if ($category->save()) {
328  $this->comment('Category '.$user_asset_category.' was created');
329  } else {
330  $this->comment('Something went wrong! Category '.$user_asset_category.' was NOT created');
331  $this->comment($category->getErrors());
332  }
333 
334  }
335 
336  // Check for the manufacturer match and create it if it doesn't exist
337  if ($manufacturer = Manufacturer::where('name', e($user_asset_mfgr))->first()) {
338  $this->comment('Manufacturer '.$user_asset_mfgr.' already exists');
339  } else {
340  $manufacturer = new Manufacturer();
341  $manufacturer->name = e($user_asset_mfgr);
342  $manufacturer->user_id = 1;
343 
344  if ($manufacturer->save()) {
345  $this->comment('Manufacturer '.$user_asset_mfgr.' was created');
346  } else {
347  $this->comment('Something went wrong! Manufacturer '.$user_asset_mfgr.' was NOT created: '. $manufacturer->getErrors()->first());
348  }
349 
350  }
351 
352  // Check for the asset model match and create it if it doesn't exist
353  if ($asset_model = AssetModel::where('name', e($user_asset_name))->where('modelno', e($user_asset_modelno))->where('category_id', $category->id)->where('manufacturer_id', $manufacturer->id)->first()) {
354  $this->comment('The Asset Model '.$user_asset_name.' with model number '.$user_asset_modelno.' already exists');
355  } else {
356  $asset_model = new AssetModel();
357  $asset_model->name = e($user_asset_name);
358  $asset_model->manufacturer_id = $manufacturer->id;
359  $asset_model->modelno = e($user_asset_modelno);
360  $asset_model->category_id = $category->id;
361  $asset_model->user_id = 1;
362 
363  if ($asset_model->save()) {
364  $this->comment('Asset Model '.$user_asset_name.' with model number '.$user_asset_modelno.' was created');
365  } else {
366  $this->comment('Something went wrong! Asset Model '.$user_asset_name.' was NOT created: '.$asset_model->getErrors()->first());
367  }
368 
369  }
370 
371  // Check for the asset company match and create it if it doesn't exist
372  if ($user_asset_company_name!='') {
373  if ($company = Company::where('name', e($user_asset_company_name))->first()) {
374  $this->comment('Company '.$user_asset_company_name.' already exists');
375  } else {
376  $company = new Company();
377  $company->name = e($user_asset_company_name);
378 
379  if ($company->save()) {
380  $this->comment('Company '.$user_asset_company_name.' was created');
381  } else {
382  $this->comment('Something went wrong! Company '.$user_asset_company_name.' was NOT created: '.$company->getErrors()->first());
383  }
384  }
385 
386  } else {
387  $company = new Company();
388  }
389 
390  // Check for the asset match and create it if it doesn't exist
391  if ($asset = Asset::where('asset_tag', e($user_asset_tag))->first()) {
392  $this->comment('The Asset with asset tag '.$user_asset_tag.' already exists');
393  } else {
394  $asset = new Asset();
395  $asset->name = e($user_asset_asset_name);
396  if ($user_asset_purchase_date!='') {
397  $asset->purchase_date = $user_asset_purchase_date;
398  } else {
399  $asset->purchase_date = NULL;
400  }
401  if ($user_asset_purchase_cost!='') {
402  $asset->purchase_cost = ParseFloat(e($user_asset_purchase_cost));
403  } else {
404  $asset->purchase_cost = 0.00;
405  }
406  $asset->serial = e($user_asset_serial);
407  $asset->asset_tag = e($user_asset_tag);
408  $asset->model_id = $asset_model->id;
409  $asset->assigned_to = $user->id;
410  $asset->rtd_location_id = $location->id;
411  $asset->user_id = 1;
412  $asset->status_id = $status_id;
413  $asset->company_id = $company->id;
414  if ($user_asset_purchase_date!='') {
415  $asset->purchase_date = $user_asset_purchase_date;
416  } else {
417  $asset->purchase_date = NULL;
418  }
419  $asset->notes = e($user_asset_notes);
420 
421  if ($asset->save()) {
422  $this->comment('Asset '.$user_asset_name.' with serial number '.$user_asset_serial.' was created');
423  } else {
424  $this->comment('Something went wrong! Asset '.$user_asset_name.' was NOT created: '.$asset->getErrors()->first());
425  }
426 
427  }
428 
429 
430 
431  $this->comment('=====================================');
432 
433  return true;
434 
435  });
436 
437 
438  }
439 
445  protected function getArguments()
446  {
447  return array(
448  array('filename', InputArgument::REQUIRED, 'File for the CSV import.'),
449  );
450  }
451 
452 
458  protected function getOptions()
459  {
460  return array(
461  array('email_format', null, InputOption::VALUE_REQUIRED, 'The format of the email addresses that should be generated. Options are firstname.lastname, firstname, filastname', null),
462  array('username_format', null, InputOption::VALUE_REQUIRED, 'The format of the username that should be generated. Options are firstname.lastname, firstname, filastname, email', null),
463  array('testrun', null, InputOption::VALUE_REQUIRED, 'Test the output without writing to the database or not.', null),
464  );
465  }
466 
467 
468 }
getArguments()
Get the console command arguments.
Model for Companies.
Definition: Company.php:12
Model for Assets.
Definition: Asset.php:21
Model for Asset Models.
Definition: AssetModel.php:14
static generateFormattedNameFromFullName($format= 'filastname', $users_name)
Definition: User.php:266
getOptions()
Get the console command options.
fire()
Execute the console command.
__construct()
Create a new command instance.