2017-01-05 15:45:12 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Importer;
|
|
|
|
|
|
|
|
use App\Models\Asset;
|
|
|
|
use App\Models\Component;
|
|
|
|
|
|
|
|
class ComponentImporter extends ItemImporter
|
|
|
|
{
|
|
|
|
protected $components;
|
2017-01-10 16:19:18 -08:00
|
|
|
public function __construct($filename)
|
2017-01-05 15:45:12 -08:00
|
|
|
{
|
2017-01-10 16:19:18 -08:00
|
|
|
parent::__construct($filename);
|
2017-01-05 15:45:12 -08:00
|
|
|
$this->components = Component::all();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function handle($row)
|
|
|
|
{
|
2017-01-10 16:19:18 -08:00
|
|
|
parent::handle($row);
|
2017-01-05 15:45:12 -08:00
|
|
|
$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");
|
2017-01-10 16:19:18 -08:00
|
|
|
$componentId = $this->components->search(function ($key) {
|
|
|
|
return strcasecmp($key->name, $this->item['name']) == 0;
|
2017-01-05 15:45:12 -08:00
|
|
|
});
|
2017-01-10 16:19:18 -08:00
|
|
|
|
|
|
|
if ($componentId !== false) {
|
2017-01-05 15:45:12 -08:00
|
|
|
$editingComponent = true;
|
2017-01-10 16:19:18 -08:00
|
|
|
$this->log('A matching Component ' . $this->item["name"] . ' already exists. ');
|
2017-01-05 15:45:12 -08:00
|
|
|
if (!$this->updating) {
|
2017-01-10 16:19:18 -08:00
|
|
|
$this->log("Skipping Component");
|
2017-01-05 15:45:12 -08:00
|
|
|
return;
|
|
|
|
}
|
2017-01-10 16:19:18 -08:00
|
|
|
$this->log("Updating Component");
|
|
|
|
$component = $this->components[$componentId];
|
|
|
|
$component->update($this->sanitizeItemFor($component));
|
|
|
|
if (!$this->testRun) {
|
|
|
|
$component->save();
|
|
|
|
}
|
|
|
|
return;
|
2017-01-05 15:45:12 -08:00
|
|
|
}
|
2017-01-10 16:19:18 -08:00
|
|
|
$this->log("No matching component, creating one");
|
|
|
|
$component = new Component;
|
|
|
|
$component->fill($$this->sanitizeItemForStoring($component));
|
2017-01-05 15:45:12 -08:00
|
|
|
|
|
|
|
if ($this->testRun) {
|
2017-01-10 16:19:18 -08:00
|
|
|
$this->log('TEST RUN - Component ' . $this->item["name"] . ' not created');
|
2017-01-05 15:45:12 -08:00
|
|
|
return;
|
|
|
|
}
|
2017-01-10 16:19:18 -08:00
|
|
|
|
2017-01-05 15:45:12 -08:00
|
|
|
if ($component->save()) {
|
|
|
|
$component->logCreate('Imported using CSV Importer');
|
2017-01-10 16:19:18 -08:00
|
|
|
$this->log("Component " . $this->item["name"] . ' was created');
|
2017-01-05 15:45:12 -08:00
|
|
|
|
|
|
|
// If we have an asset tag, checkout to that asset.
|
2017-01-10 16:19:18 -08:00
|
|
|
if (isset($this->item['asset_tag']) && ($asset = Asset::where('asset_tag', $this->item['asset_tag'])->first())) {
|
2017-01-05 15:45:12 -08:00
|
|
|
$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;
|
|
|
|
}
|
2017-01-10 16:19:18 -08:00
|
|
|
$this->jsonError($component, 'Component');
|
2017-01-05 15:45:12 -08:00
|
|
|
}
|
|
|
|
}
|