diff --git a/app/Importer/AssetImporter.php b/app/Importer/AssetImporter.php index 1235a88f86..2c913006b8 100644 --- a/app/Importer/AssetImporter.php +++ b/app/Importer/AssetImporter.php @@ -66,16 +66,14 @@ class AssetImporter extends ItemImporter $editingAsset = false; $asset_tag = $this->findCsvMatch($row, 'asset_tag'); - if(empty($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; } @@ -85,6 +83,13 @@ class AssetImporter extends ItemImporter $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; @@ -92,13 +97,12 @@ class AssetImporter extends ItemImporter $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; - - - // If no status ID is found - if (! array_key_exists('status_id', $this->item) && ! $editingAsset) { - $this->log('No status field found, defaulting to first status.'); - $this->item['status_id'] = $this->defaultStatusLabelId; - } + $this->item['last_checkout'] = trim($this->findCsvMatch($row, 'last_checkout')); + $this->item['last_checkin'] = trim($this->findCsvMatch($row, 'last_checkin')); + $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; @@ -106,8 +110,10 @@ class AssetImporter extends ItemImporter // 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. @@ -115,16 +121,60 @@ class AssetImporter extends ItemImporter $item['rtd_location_id'] = $this->item['location_id']; } - $item['last_audit_date'] = null; - if (isset($this->item['last_audit_date'])) { - $item['last_audit_date'] = $this->item['last_audit_date']; + $checkin_date = date('Y-m-d H:i:s'); + if ($this->item['last_checkin']!='') { + try { + $checkin_date = CarbonImmutable::parse($this->item['last_checkin'])->format('Y-m-d H:i:s'); + $this->item['last_checkout'] = $checkin_date; + } catch (\Exception $e) { + Log::info($e->getMessage()); + $this->log('Unable to parse date: '.$this->item['last_checkout']); + } } - $item['next_audit_date'] = null; - if (isset($this->item['next_audit_date'])) { - $item['next_audit_date'] = $this->item['next_audit_date']; + /** + * We use this to backdate the checkout action further down + */ + $checkout_date = date('Y-m-d H:i:s'); + if ($this->item['last_checkout']!='') { + + try { + $checkout_date = CarbonImmutable::parse($this->item['last_checkout'])->format('Y-m-d H:i:s'); + $this->item['last_checkout'] = $checkout_date; + } catch (\Exception $e) { + Log::info($e->getMessage()); + $this->log('Unable to parse date: '.$this->item['last_checkout']); + } } - + + if ($this->item['expected_checkin']!='') { + try { + $this->item['expected_checkin'] = CarbonImmutable::parse($this->item['expected_checkin'])->format('Y-m-d'); + } catch (\Exception $e) { + Log::info($e->getMessage()); + $this->log('Unable to parse date: '.$this->item['expected_checkin']); + } + } + + if ($this->item['last_audit_date']!='') { + try { + $this->item['last_audit_date'] = CarbonImmutable::parse($this->item['last_audit_date'])->format('Y-m-d'); + } catch (\Exception $e) { + Log::info($e->getMessage()); + $this->log('Unable to parse date: '.$this->item['last_audit_date']); + } + } + + if ($this->item['next_audit_date']!='') { + try { + $this->item['next_audit_date'] = CarbonImmutable::parse($this->item['next_audit_date'])->format('Y-m-d'); + } catch (\Exception $e) { + Log::info($e->getMessage()); + $this->log('Unable to parse date: '.$this->item['next_audit_date']); + } + } + + if ($editingAsset) { $asset->update($item); } else { @@ -137,23 +187,27 @@ class AssetImporter extends ItemImporter $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 setted by + //-- 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(), $asset->notes, date('Y-m-d H:i:s'))); + if ($asset->assigned_to != $target->id) { + event(new CheckoutableCheckedIn($asset, User::find($asset->assigned_to), Auth::user(), 'Checkin from CSV Importer', $checkin_date)); + $this->log('Checking this asset in'); } } - $asset->fresh()->checkOut($target, $this->user_id, date('Y-m-d H:i:s'), null, $asset->notes, $asset->name); + $asset->fresh()->checkOut($target, $this->user_id, $checkout_date, null, 'Checkout from CSV Importer', $asset->name); + $this->log('Checking this asset out'); } return;