Skip to content

A free open source IT asset/license management system

Added supplier importer

snipe6e2eeba0f61c
diff --git a/app/Importer/ItemImporter.php b/app/Importer/ItemImporter.php
index dfcd644c9d..0f3461e19f 100644
--- a/app/Importer/ItemImporter.php
+++ b/app/Importer/ItemImporter.php
@@ -110,7 +110,7 @@ class ItemImporter extends Importer
protected function determineCheckout($row)
{
// Locations don't get checked out to anyone/anything
- if ((get_class($this) == LocationImporter::class) || (get_class($this) == AssetModelImporter::class)) {
+ if ((get_class($this) == LocationImporter::class) || (get_class($this) == AssetModelImporter::class) || (get_class($this) == SupplierImporter::class)) {
return;
}
diff --git a/app/Importer/SupplierImporter.php b/app/Importer/SupplierImporter.php
new file mode 100644
index 0000000000..7878aff835
--- /dev/null
+++ b/app/Importer/SupplierImporter.php
@@ -0,0 +1,105 @@
+<?php
+
+namespace App\Importer;
+
+use App\Models\Supplier;
+use Illuminate\Support\Facades\Log;
+
+/**
+ * When we are importing users via an Asset/etc import, we use createOrFetchUser() in
+ * Importer\Importer.php. [ALG]
+ *
+ * Class SupplierImporter
+ */
+class SupplierImporter extends ItemImporter
+{
+ protected $suppliers;
+
+ public function __construct($filename)
+ {
+ parent::__construct($filename);
+ }
+
+ protected function handle($row)
+ {
+ parent::handle($row);
+ $this->createSupplierIfNotExists($row);
+ }
+
+ /**
+ * Create a supplier if a duplicate does not exist.
+ * @todo Investigate how this should interact with Importer::createSupplierIfNotExists
+ *
+ * @author A. Gianotto
+ * @since 6.1.0
+ * @param array $row
+ */
+ public function createSupplierIfNotExists(array $row)
+ {
+
+ $editingSupplier = false;
+
+ $supplier = Supplier::where('name', '=', $this->findCsvMatch($row, 'name'))->first();
+
+ if ($this->findCsvMatch($row, 'id')!='') {
+ // Override supplier if an ID was given
+ \Log::debug('Finding supplier by ID: '.$this->findCsvMatch($row, 'id'));
+ $supplier = Supplier::find($this->findCsvMatch($row, 'id'));
+ }
+
+
+ if ($supplier) {
+ if (! $this->updating) {
+ $this->log('A matching Supplier '.$this->item['name'].' already exists');
+ return;
+ }
+
+ $this->log('Updating Supplier');
+ $editingSupplier = true;
+ } else {
+ $this->log('No Matching Supplier, Create a new one');
+ $supplier = new Supplier;
+ $supplier->created_by = auth()->id();
+ }
+
+ // Pull the records from the CSV to determine their values
+ $this->item['name'] = trim($this->findCsvMatch($row, 'name'));
+ $this->item['address'] = trim($this->findCsvMatch($row, 'address'));
+ $this->item['address2'] = trim($this->findCsvMatch($row, 'address2'));
+ $this->item['city'] = trim($this->findCsvMatch($row, 'city'));
+ $this->item['state'] = trim($this->findCsvMatch($row, 'state'));
+ $this->item['country'] = trim($this->findCsvMatch($row, 'country'));
+ $this->item['zip'] = trim($this->findCsvMatch($row, 'zip'));
+ $this->item['phone'] = trim($this->findCsvMatch($row, 'phone'));
+ $this->item['fax'] = trim($this->findCsvMatch($row, 'fax'));
+ $this->item['email'] = trim($this->findCsvMatch($row, 'email'));
+ $this->item['contact'] = trim($this->findCsvMatch($row, 'contact'));
+ $this->item['url'] = trim($this->findCsvMatch($row, 'url'));
+ $this->item['notes'] = trim($this->findCsvMatch($row, 'notes'));
+
+
+ Log::debug('Item array is: ');
+ Log::debug(print_r($this->item, true));
+
+
+ if ($editingSupplier) {
+ Log::debug('Updating existing supplier');
+ $supplier->update($this->sanitizeItemForUpdating($supplier));
+ } else {
+ Log::debug('Creating supplier');
+ $supplier->fill($this->sanitizeItemForStoring($supplier));
+ }
+
+ if ($supplier->save()) {
+ $this->log('Supplier '.$supplier->name.' created or updated from CSV import');
+ return $supplier;
+
+ } else {
+ Log::debug($supplier->getErrors());
+ $this->logError($supplier, 'Supplier "'.$this->item['name'].'"');
+ return $supplier->errors;
+ }
+
+
+ }
+}
\ No newline at end of file
diff --git a/app/Livewire/Importer.php b/app/Livewire/Importer.php
index 5f30dca7be..55752a5c2f 100644
--- a/app/Livewire/Importer.php
+++ b/app/Livewire/Importer.php
@@ -35,6 +35,8 @@ class Importer extends Component
public $accessories_fields;
public $assets_fields;
public $users_fields;
+ public $assetmodels_fields;
+ public $suppliers_fields;
public $licenses_fields;
public $locations_fields;
public $consumables_fields;
@@ -85,9 +87,6 @@ class Importer extends Component
case 'component':
$results = $this->components_fields;
break;
- case 'consumable':
- $results = $this->consumables_fields;
- break;
case 'license':
$results = $this->licenses_fields;
break;
@@ -97,8 +96,8 @@ class Importer extends Component
case 'location':
$results = $this->locations_fields;
break;
- case 'user':
- $results = $this->users_fields;
+ case 'supplier':
+ $results = $this->suppliers_fields;
break;
default:
$results = [];
@@ -128,7 +127,7 @@ class Importer extends Component
//yes, this key *is* valid. Continue on to the next field.
continue;
} else {
- //no, this key is *INVALID* for this import type. Better set it to null
+ //no, this key is *INVALID* for this import type. Better set it to null,
// and we'll hope that the $aliases_fields or something else picks it up.
$this->field_map[$i] = null; // fingers crossed! But it's not likely, tbh.
} // TODO - strictly speaking, this isn't necessary here I don't think.
@@ -149,7 +148,7 @@ class Importer extends Component
// in "Accessories"!)
if (array_key_exists($key, $this->columnOptions[$type])) {
$this->field_map[$i] = $key;
- continue 3; // bust out of both of these loops; as well as the surrounding one - e.g. move on to the next header
+ continue 3; // bust out of both of these loops and the surrounding one - e.g. move on to the next header
}
}
}
@@ -171,6 +170,7 @@ class Importer extends Component
'license' => trans('general.licenses'),
'location' => trans('general.locations'),
'user' => trans('general.users'),
+ 'supplier' => trans('general.suppliers'),
];
/**
@@ -347,6 +347,22 @@ class Importer extends Component
'zip' => trans('general.zip'),
];
+ $this->suppliers_fields = [
+ 'id' => trans('general.id'),
+ 'name' => trans('general.item_name_var', ['item' => trans('general.supplier')]),
+ 'address' => trans('general.address'),
+ 'address2' => trans('general.importer.address2'),
+ 'city' => trans('general.city'),
+ 'notes' => trans('general.notes'),
+ 'state' => trans('general.state'),
+ 'zip' => trans('general.zip'),
+ 'phone' => trans('general.phone'),
+ 'fax' => trans('general.fax'),
+ 'url' => trans('general.url'),
+ 'contact' => trans('general.contact'),
+ 'email' => trans('general.email'),
+ ];
+
$this->assetmodels_fields = [
'category' => trans('general.category'),
'eol' => trans('general.eol'),