diff --git a/app/Console/Commands/ObjectImportCommand.php b/app/Console/Commands/ObjectImportCommand.php index 7d31620542..c7d5e2fd02 100644 --- a/app/Console/Commands/ObjectImportCommand.php +++ b/app/Console/Commands/ObjectImportCommand.php @@ -68,23 +68,9 @@ class ObjectImportCommand extends Command public function fire() { $filename = $this->argument('filename'); - $importerClass = Importer::class; - switch (strtolower($this->option('item-type'))) { - case "asset": - $importerClass = AssetImporter::class; - break; - case "accessory": - $importerClass = AccessoryImporter::class; - break; - case "component": - die("This is not implemented yet"); - $importerClass = ComponentImporter::class; - break; - case "consumable": - $importerClass = ConsumableImporter::class; - break; - } - $importer = new $importerClass( + $class = title_case($this->option('item-type')); + $classString = "App\\Importer\\{$class}Importer"; + $importer = new $classString( $filename, [$this, 'log'], [$this, 'progress'], diff --git a/app/Http/Controllers/AssetsController.php b/app/Http/Controllers/AssetsController.php index 9e59359abd..5cf62333e9 100755 --- a/app/Http/Controllers/AssetsController.php +++ b/app/Http/Controllers/AssetsController.php @@ -812,6 +812,9 @@ class AssetsController extends Controller case "consumable": $redirectTo = "consumables.index"; break; + case "component": + $redirectTo = "components.index"; + break; } if ($errors) { //Failure diff --git a/app/Http/Requests/ItemImportRequest.php b/app/Http/Requests/ItemImportRequest.php index 4e0489e42f..882b8a1607 100644 --- a/app/Http/Requests/ItemImportRequest.php +++ b/app/Http/Requests/ItemImportRequest.php @@ -31,25 +31,11 @@ class ItemImportRequest extends FormRequest public function import() { - $filename = config('app.private_uploads') . '/imports/assets/' . $this->get('filename'); - $importerClass = Importer::class; - switch ($this->get('import-type')) { - case "asset": - $importerClass = 'App\Importer\AssetImporter'; - break; - case "accessory": - $importerClass = 'App\Importer\AccessoryImporter'; - break; - case "component": - die("This is not implemented yet"); - $importerClass = ComponentImporter::class; - break; - case "consumable": - $importerClass = 'App\Importer\ConsumableImporter'; - break; - } - $importer = new $importerClass( + $filename = config('app.private_uploads') . '/imports/assets/' . $this->get('filename'); + $class = title_case($this->input('import-type')); + $classString = "App\\Importer\\{$class}Importer"; + $importer = new $classString( $filename, [$this, 'log'], [$this, 'progress'], diff --git a/app/Importer/AccessoryImporter.php b/app/Importer/AccessoryImporter.php index 14f2dc194b..e1471afd32 100644 --- a/app/Importer/AccessoryImporter.php +++ b/app/Importer/AccessoryImporter.php @@ -40,7 +40,7 @@ class AccessoryImporter extends ItemImporter $accessory = $this->accessories->search(function ($key) { return strcasecmp($key->name, $this->item['item_name']) == 0; }); - if ($accessory) { + if ($accessory !== false) { $editingAccessory = true; if (!$this->updating) { $this->log('A matching Accessory ' . $this->item["item_name"] . ' already exists. '); diff --git a/app/Importer/AssetImporter.php b/app/Importer/AssetImporter.php index f70cf4c95d..3ba8c59c2b 100644 --- a/app/Importer/AssetImporter.php +++ b/app/Importer/AssetImporter.php @@ -51,7 +51,7 @@ class AssetImporter extends ItemImporter $asset = $this->assets->search(function ($key) { return strcasecmp($key->asset_tag, $this->item['asset_tag']) == 0; }); - if ($asset) { + if ($asset !== false) { $editingAsset = true; if (!$this->updating) { $this->log('A matching Asset ' . $this->item['asset_tag'] . ' already exists'); diff --git a/app/Importer/ComponentImporter.php b/app/Importer/ComponentImporter.php new file mode 100644 index 0000000000..7bb84a5786 --- /dev/null +++ b/app/Importer/ComponentImporter.php @@ -0,0 +1,114 @@ +components = Component::all(); + } + + protected function handle($row) + { + parent::handle($row); // TODO: Change the autogenerated stub + $this->createComponentIfNotExists(); + } + + /** + * Create a component if a duplicate does not exist + * + * @author Daniel Melzter + * @since 3.0 + */ + public function createComponentIfNotExists() + { + $component = null; + $editingComponent = false; + $this->log("Creating Component"); + $component = $this->components->search(function ($key) { + return strcasecmp($key->name, $this->item['item_name']) == 0; + }); + if ($component !== false) { + $editingComponent = true; + if (!$this->updating) { + $this->log('A matching Component ' . $this->item["item_name"] . ' already exists. '); + return; + } + } else { + $this->log("No matching component, creating one"); + $component = new Component(); + } + + if (!$editingComponent) { + $component->name = $this->item["item_name"]; + } + if (!empty($this->item["purchase_date"])) { + $component->purchase_date = $this->item["purchase_date"]; + } else { + $component->purchase_date = null; + } + + if (!empty($this->item["purchase_cost"])) { + $component->purchase_cost = Helper::ParseFloat($this->item["purchase_cost"]); + } + if (isset($this->item["location"])) { + $component->location_id = $this->item["location"]->id; + } + $component->user_id = $this->user_id; + if (isset($this->item["company"])) { + $component->company_id = $this->item["company"]->id; + } + if (!empty($this->item["order_number"])) { + $component->order_number = $this->item["order_number"]; + } + if (isset($this->item["category"])) { + $component->category_id = $this->item["category"]->id; + } + + // TODO:Implement + //$component->notes= e($this->item_notes); + if (!empty($this->item["requestable"])) { + $component->requestable = filter_var($this->item["requestable"], FILTER_VALIDATE_BOOLEAN); + } + + $component->qty = 1; + if ( (!empty($this->item["quantity"])) && ($this->item["quantity"] > -1)) { + $component->qty = $this->item["quantity"]; + } + + if ($this->testRun) { + $this->log('TEST RUN - Component ' . $this->item['item_name'] . ' not created'); + return; + } + if ($component->save()) { + $component->logCreate('Imported using CSV Importer'); + $this->log("Component " . $this->item["item_name"] . ' was created'); + + // If we have an asset tag, checkout to that asset. + if(isset($this->item['asset_tag']) && ($asset = Asset::where('asset_tag', $this->item['asset_tag'])->first()) ) { + $component->assets()->attach($component->id, [ + 'component_id' => $component->id, + 'user_id' => $this->user_id, + 'created_at' => date('Y-m-d H:i:s'), + 'assigned_qty' => 1, // Only assign the first one to the asset + 'asset_id' => $asset->id + ]); + } + return; + } + $this->jsonError($component, 'Component', $component->getErrors()); + } +} diff --git a/app/Importer/ConsumableImporter.php b/app/Importer/ConsumableImporter.php index f374eb7b76..e4fd5dbc6c 100644 --- a/app/Importer/ConsumableImporter.php +++ b/app/Importer/ConsumableImporter.php @@ -40,7 +40,7 @@ class ConsumableImporter extends ItemImporter $consumable = $this->consumables->search(function ($key) { return strcasecmp($key->name, $this->item['item_name']) == 0; }); - if ($consumable) { + if ($consumable !== false) { $editingConsumable = true; if (!$this->updating) { $this->log('A matching Consumable ' . $this->item["item_name"] . ' already exists. '); diff --git a/app/Importer/Importer.php b/app/Importer/Importer.php index 66c968071a..ea70992c65 100644 --- a/app/Importer/Importer.php +++ b/app/Importer/Importer.php @@ -78,7 +78,7 @@ abstract class Importer $updating = false, $usernameFormat = null ) { - + $this->filename = $filename; $this->csv = Reader::createFromPath($filename); $this->csv->setNewLine('\r\n'); diff --git a/app/Importer/ItemImporter.php b/app/Importer/ItemImporter.php index 2bd3c957ab..f8fbf65331 100644 --- a/app/Importer/ItemImporter.php +++ b/app/Importer/ItemImporter.php @@ -138,7 +138,7 @@ class ItemImporter extends Importer public function createOrFetchCategory($asset_category) { // Magic to transform "AssetImporter" to "asset" or similar. - $classname = get_class($this); + $classname = class_basename(get_class($this)); $item_type = strtolower(substr($classname, 0, strpos($classname, 'Importer'))); if (empty($asset_category)) { $asset_category = 'Unnamed Category'; @@ -150,6 +150,7 @@ class ItemImporter extends Importer // We need strict compare here because the index returned above can be 0. // This casts to false and causes false positives if ($category !== false) { + $this->log("A matching category: " . $asset_category . " already exists"); return $this->categories[$category]; } diff --git a/resources/views/hardware/import.blade.php b/resources/views/hardware/import.blade.php index 1942ebc903..09b1677e3b 100644 --- a/resources/views/hardware/import.blade.php +++ b/resources/views/hardware/import.blade.php @@ -97,7 +97,7 @@ Select Import File... - +