From 5066eb58f4590821bf2d4e1deaf7f87c3eb68815 Mon Sep 17 00:00:00 2001 From: snipe Date: Sat, 17 Aug 2024 02:31:03 +0100 Subject: [PATCH] Added asset model import to importer Signed-off-by: snipe --- app/Importer/AssetModelImporter.php | 100 ++++++++++++++++++++ app/Importer/ItemImporter.php | 12 ++- app/Livewire/Importer.php | 36 +++++-- resources/lang/en-US/general.php | 2 +- resources/views/livewire/importer.blade.php | 3 + sample_csvs/models-sample.csv | 22 +++++ 6 files changed, 164 insertions(+), 11 deletions(-) create mode 100644 app/Importer/AssetModelImporter.php create mode 100644 sample_csvs/models-sample.csv diff --git a/app/Importer/AssetModelImporter.php b/app/Importer/AssetModelImporter.php new file mode 100644 index 0000000000..888265a95e --- /dev/null +++ b/app/Importer/AssetModelImporter.php @@ -0,0 +1,100 @@ +createAssetModelIfNotExists($row); + } + + /** + * Create a model if a duplicate does not exist. + * @todo Investigate how this should interact with Importer::createModelIfNotExists + * + * @author A. Gianotto + * @since 6.1.0 + * @param array $row + */ + public function createAssetModelIfNotExists(array $row) + { + + $editingAssetModel = false; + $assetmodel = AssetModel::where('name', '=', $this->findCsvMatch($row, 'name'))->first(); + + if ($assetmodel) { + if (! $this->updating) { + $this->log('A matching Model '.$this->item['name'].' already exists'); + return; + } + + $this->log('Updating Model'); + $editingAssetModel = true; + } else { + $this->log('No Matching Model, Create a new one'); + $assetmodel = new AssetModel(); + } + + // Pull the records from the CSV to determine their values + $this->item['name'] = trim($this->findCsvMatch($row, 'name')); + $this->item['category'] = trim($this->findCsvMatch($row, 'category')); + $this->item['manufacturer'] = trim($this->findCsvMatch($row, 'manufacturer')); + $this->item['min_amt'] = trim($this->findCsvMatch($row, 'min_amt')); + $this->item['model_number'] = trim($this->findCsvMatch($row, 'model_number')); + $this->item['notes'] = trim($this->findCsvMatch($row, 'notes')); + $this->item['user_id'] = auth()->id(); + + + if (!empty($this->item['category'])) { + if ($category = $this->createOrFetchCategory($row, 'category')) { + $this->item['category_id'] = $category->id; + } + } + if (!empty($this->item['manufacturer'])) { + if ($manufacturer = $this->createOrFetchManufacturer($row, 'manufacturer')) { + $this->item['manufacturer_id'] = $manufacturer->id; + } + } + + Log::debug('Item array is: '); + Log::debug(print_r($this->item, true)); + + + if ($editingAssetModel) { + Log::debug('Updating existing model'); + $assetmodel->update($this->sanitizeItemForUpdating($assetmodel)); + } else { + Log::debug('Creating model'); + $assetmodel->fill($this->sanitizeItemForStoring($assetmodel)); + } + + if ($assetmodel->save()) { + $this->log('AssetModel '.$assetmodel->name.' created or updated from CSV import'); + return $assetmodel; + + } else { + Log::debug($assetmodel->getErrors()); + return $assetmodel->errors; + } + + + } +} \ No newline at end of file diff --git a/app/Importer/ItemImporter.php b/app/Importer/ItemImporter.php index 29197ca5dc..56c9a3cb09 100644 --- a/app/Importer/ItemImporter.php +++ b/app/Importer/ItemImporter.php @@ -113,7 +113,7 @@ class ItemImporter extends Importer protected function determineCheckout($row) { // Locations don't get checked out to anyone/anything - if (get_class($this) == LocationImporter::class) { + if ((get_class($this) == LocationImporter::class) || (get_class($this) == AssetModelImporter::class)) { return; } @@ -287,14 +287,22 @@ class ItemImporter extends Importer $classname = class_basename(get_class($this)); $item_type = strtolower(substr($classname, 0, strpos($classname, 'Importer'))); + if ($item_type == 'assetmodel') { + $item_type = 'asset'; + } + + \Log::error('Item Type: '.$item_type); + if (empty($asset_category)) { $asset_category = 'Unnamed Category'; } + + $category = Category::where(['name' => $asset_category, 'category_type' => $item_type])->first(); + if ($category) { $this->log('A matching category: '.$asset_category.' already exists'); - return $category->id; } diff --git a/app/Livewire/Importer.php b/app/Livewire/Importer.php index 32dd7912fb..5af99b35ba 100644 --- a/app/Livewire/Importer.php +++ b/app/Livewire/Importer.php @@ -79,6 +79,7 @@ class Importer extends Component private function getColumns($type) { + \Log::error($type); switch ($type) { case 'asset': $results = $this->assets_fields; @@ -101,10 +102,14 @@ class Importer extends Component case 'location': $results = $this->locations_fields; break; + case 'assetmodel': + $results = $this->assetmodels_fields; + break; default: $results = []; } asort($results, SORT_FLAG_CASE | SORT_STRING); + if ($type == "asset") { // add Custom Fields after a horizontal line $results['-'] = "———" . trans('admin/custom_fields/general.custom_fields') . "———’"; @@ -143,6 +148,7 @@ class Importer extends Component } // if you got here, we didn't find a match. Try the $aliases_fields foreach ($this->aliases_fields as $key => $alias_values) { + \Log::error('No matches'); foreach ($alias_values as $alias_value) { if (strcasecmp($alias_value, $header) === 0) { // aLsO CaSe-INSENSitiVE! // Make *absolutely* sure that this key actually _exists_ in this import type - @@ -172,13 +178,14 @@ class Importer extends Component $this->progress = -1; // '-1' means 'don't show the progressbar' $this->progress_bar_class = 'progress-bar-warning'; $this->importTypes = [ - 'asset' => trans('general.assets'), - 'accessory' => trans('general.accessories'), - 'consumable' => trans('general.consumables'), - 'component' => trans('general.components'), - 'license' => trans('general.licenses'), - 'user' => trans('general.users'), - 'location' => trans('general.locations'), + 'asset' => trans('general.assets'), + 'accessory' => trans('general.accessories'), + 'consumable' => trans('general.consumables'), + 'component' => trans('general.components'), + 'license' => trans('general.licenses'), + 'user' => trans('general.users'), + 'location' => trans('general.locations'), + 'assetmodel' => trans('general.asset_models'), ]; /** @@ -352,6 +359,18 @@ class Importer extends Component 'parent_location' => trans('admin/locations/table.parent'), ]; + $this->assetmodels_fields = [ + 'item_name' => trans('general.item_name_var', ['item' => trans('general.asset_model')]), + 'category' => trans('general.category'), + 'manufacturer' => trans('general.manufacturer'), + 'model_number' => trans('general.model_no'), + 'notes' => trans('general.item_notes', ['item' => trans('admin/hardware/form.model')]), + 'min_amt' => trans('mail.min_QTY'), + 'fieldset' => trans('admin/models/general.fieldset'), + 'category_type' => 'category type', + + ]; + // "real fieldnames" to a list of aliases for that field $this->aliases_fields = [ 'item_name' => @@ -527,18 +546,19 @@ class Importer extends Component if (!$this->activeFile) { $this->message = trans('admin/hardware/message.import.file_missing'); $this->message_type = 'danger'; - return; } $this->field_map = null; foreach($this->activeFile->header_row as $element) { + if(isset($this->activeFile->field_map[$element])) { $this->field_map[] = $this->activeFile->field_map[$element]; } else { $this->field_map[] = null; // re-inject the 'nulls' if a file was imported with some 'Do Not Import' settings } } + $this->file_id = $id; $this->import_errors = null; $this->statusText = null; diff --git a/resources/lang/en-US/general.php b/resources/lang/en-US/general.php index 7634387906..5e54bab83e 100644 --- a/resources/lang/en-US/general.php +++ b/resources/lang/en-US/general.php @@ -557,7 +557,7 @@ return [ 'something_went_wrong' => 'Something went wrong with your request.', 'close' => 'Close', 'expires' => 'Expires', - 'map_fields'=> 'Map :item_type Field', + 'map_fields'=> 'Map :item_type Fields', 'remaining_var' => ':count Remaining', 'assets_in_var' => 'Assets in :name :type', 'label' => 'Label', diff --git a/resources/views/livewire/importer.blade.php b/resources/views/livewire/importer.blade.php index 92dfd128ad..7371dd2fe8 100644 --- a/resources/views/livewire/importer.blade.php +++ b/resources/views/livewire/importer.blade.php @@ -170,16 +170,19 @@ {{ trans('general.update_existing_values') }} + @if ($activeFile->import_type === 'asset' && $snipeSettings->auto_increment_assets == 1 && $update)

{{ trans('general.auto_incrementing_asset_tags_enabled_so_now_assets_will_be_created') }}

@endif + @if ($activeFile->import_type != 'location' && $activeFile->import_type == 'assetmodel' && $update) + @endif