A free open source IT asset/license management system
First shot
snipe7658f7c41daf
diff --git a/app/Importer/AssetImporter.php b/app/Importer/AssetImporter.phpindex 4bb887bcd8..4f5bffc560 100644--- a/app/Importer/AssetImporter.php+++ b/app/Importer/AssetImporter.php@@ -71,8 +71,10 @@ class AssetImporter extends ItemImporter$asset = Asset::where(['asset_tag'=> (string) $asset_tag])->first();if ($asset) {if (! $this->updating) {- $this->log('A matching Asset '.$asset_tag.' already exists');- return;+ $exists_error = 'A matching Asset '.$asset_tag.' already exists';+ $this->log($exists_error);+ $this->addErrorToBag($asset, 'asset_tag', $exists_error);+ return $exists_error;}$this->log('Updating Asset');diff --git a/app/Importer/Importer.php b/app/Importer/Importer.phpindex 9738723509..c2214ef37d 100644--- a/app/Importer/Importer.php+++ b/app/Importer/Importer.php@@ -281,6 +281,13 @@ abstract class Importer}}+ protected function addErrorToBag($item, $field, $error_message)+ {+ if ($this->errorCallback) {+ call_user_func($this->errorCallback, $item, $field, [$field => [$error_message]]);+ }+ }+/*** Finds the user matching given data, or creates a new one if there is no match.* This is NOT used by the User Import, only for Asset/Accessory/etc wherediff --git a/app/Importer/ItemImporter.php b/app/Importer/ItemImporter.phpindex ee680413d8..a00602fb7d 100644--- a/app/Importer/ItemImporter.php+++ b/app/Importer/ItemImporter.php@@ -196,55 +196,70 @@ class ItemImporter extends Importer{$condition = array();$asset_model_name = $this->findCsvMatch($row, 'asset_model');+ $asset_model_category = $this->findCsvMatch($row, 'category');$asset_modelNumber = $this->findCsvMatch($row, 'model_number');+// TODO: At the moment, this means we can't update the model number if the model name stays the same.if (! $this->shouldUpdateField($asset_model_name)) {return;}+if ((empty($asset_model_name)) && (! empty($asset_modelNumber))) {$asset_model_name = $asset_modelNumber;} elseif ((empty($asset_model_name)) && (empty($asset_modelNumber))) {$asset_model_name = 'Unknown';}- if ((!empty($asset_model_name)) && (empty($asset_modelNumber))) {- $condition[] = ['name', '=', $asset_model_name];- } elseif ((!empty($asset_model_name)) && (!empty($asset_modelNumber))) {- $condition[] = ['name', '=', $asset_model_name];- $condition[] = ['model_number', '=', $asset_modelNumber];+ $asset_model = AssetModel::select('id');++ if (!empty($asset_model_name)) {+ $asset_model = $asset_model->where('name', '=', $asset_model_name);++ if (!empty($asset_modelNumber)) {+ $asset_model = $asset_model->where('model_number', '=', $asset_modelNumber);+ }}$editingModel = $this->updating;- $asset_model = AssetModel::where($condition)->first();+ $this->log('SQL: '.$asset_model->toSql());+ $asset_model = $asset_model->first();++ $this->log('Name: '.$asset_model_name);+ $this->log('Number: '.$asset_modelNumber);+ $this->log('Category: '.$asset_model_category);+ $this->log('Model Info: '.print_r($asset_model, true));if ($asset_model) {+if (! $this->updating) {$this->log('A matching model already exists, returning it.');-return $asset_model->id;}+$this->log('Matching Model found, updating it.');$item = $this->sanitizeItemForStoring($asset_model, $editingModel);$item['name'] = $asset_model_name;$item['notes'] = $this->findCsvMatch($row, 'model_notes');- if(!empty($asset_modelNumber)){+ if (!empty($asset_modelNumber)){$item['model_number'] = $asset_modelNumber;}$asset_model->update($item);$asset_model->save();$this->log('Asset Model Updated');-+return $asset_model->id;+}- $this->log('No Matching Model, Creating a new one');+ $this->log('No Matching Model, Creating a new one');$asset_model = new AssetModel();$item = $this->sanitizeItemForStoring($asset_model, $editingModel);$item['name'] = $asset_model_name;$item['model_number'] = $asset_modelNumber;$item['notes'] = $this->findCsvMatch($row, 'model_notes');+ $item['category_id'] = $this->createOrFetchCategory($asset_model_category);$asset_model->fill($item);$item = null;@@ -254,6 +269,7 @@ class ItemImporter extends Importerreturn $asset_model->id;}+ $this->log('Asset Model Errors: '.$asset_model->getErrors());$this->logError($asset_model, 'Asset Model "'.$asset_model_name.'"');return null;diff --git a/app/Livewire/Importer.php b/app/Livewire/Importer.phpindex e164af36d6..32dd7912fb 100644--- a/app/Livewire/Importer.php+++ b/app/Livewire/Importer.php@@ -374,6 +374,12 @@ class Importer extends Component'model name','model',],+ 'eol_date' =>+ [+ 'eol',+ 'eol date',+ 'asset eol date',+ ],'gravatar' =>['gravatar',diff --git a/resources/views/livewire/importer.blade.php b/resources/views/livewire/importer.blade.phpindex ab4646041b..92dfd128ad 100644--- a/resources/views/livewire/importer.blade.php+++ b/resources/views/livewire/importer.blade.php@@ -33,6 +33,7 @@<table class="table table-striped table-bordered" id="errors-table"><thead><th>{{ trans('general.item') }}</th>+ <th>Field</th><th>{{ trans('general.error') }}</th></thead><tbody>@@ -41,8 +42,8 @@@foreach($error_bag as $field => $error_list)<tr><td><b>{{ $key }}</b></td>+ <td><b>{{ $field }}</b></td><td>- <b>{{ $field }}:</b><span>{{ implode(", ",$error_list) }}</span><br /></td>