-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to import asset models (separate from assets) #15802
Changes from all commits
5066eb5
22c79d8
48713cb
615edbb
b3f84f7
d1ba16d
6c2cfe0
6f25ed3
abab062
f87ebb4
752221e
503d044
3d3e3a9
3740b58
220902a
4afe873
69e3673
74ca561
2f943bc
813b304
85dbbdb
66aaafc
4d3137e
0ae1db6
0a6096e
d7e5fe5
54f7917
8c3b50d
3a1fb61
b6dbb88
0c3b8cb
d4018a2
1aef7ed
d3602c0
7c3f9ba
4a18946
6fee533
6f700cc
1caf73f
304fddb
2e340f9
7ccbc60
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
<?php | ||
|
||
namespace App\Importer; | ||
|
||
use App\Models\AssetModel; | ||
use App\Models\Depreciation; | ||
use App\Models\CustomFieldset; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
/** | ||
* When we are importing users via an Asset/etc import, we use createOrFetchUser() in | ||
* Importer\Importer.php. [ALG] | ||
* | ||
* Class LocationImporter | ||
*/ | ||
class AssetModelImporter extends ItemImporter | ||
{ | ||
protected $models; | ||
|
||
public function __construct($filename) | ||
{ | ||
parent::__construct($filename); | ||
} | ||
|
||
protected function handle($row) | ||
{ | ||
parent::handle($row); | ||
$this->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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is 'name' alone enough to guarantee uniqueness? I seem to remember us having problems where users had two identically-named models, but with different model numbers, and wanted to maintain those separately? Or I could be misremembering. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, yeah, that's a good point... |
||
|
||
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['eol'] = trim($this->findCsvMatch($row, 'eol')); | ||
$this->item['notes'] = trim($this->findCsvMatch($row, 'notes')); | ||
$this->item['fieldset'] = trim($this->findCsvMatch($row, 'fieldset')); | ||
$this->item['depreciation'] = trim($this->findCsvMatch($row, 'depreciation')); | ||
$this->item['requestable'] = trim(($this->fetchHumanBoolean($this->findCsvMatch($row, 'requestable'))) == 1) ? 1 : 0; | ||
|
||
if (!empty($this->item['category'])) { | ||
if ($category = $this->createOrFetchCategory($this->item['category'])) { | ||
$this->item['category_id'] = $category; | ||
} | ||
} | ||
if (!empty($this->item['manufacturer'])) { | ||
if ($manufacturer = $this->createOrFetchManufacturer($this->item['manufacturer'])) { | ||
$this->item['manufacturer_id'] = $manufacturer; | ||
} | ||
} | ||
|
||
if (!empty($this->item['depreciation'])) { | ||
if ($depreciation = $this->fetchDepreciation($this->item['depreciation'])) { | ||
$this->item['depreciation_id'] = $depreciation; | ||
} | ||
} | ||
|
||
if (!empty($this->item['fieldset'])) { | ||
if ($fieldset = $this->createOrFetchCustomFieldset($this->item['fieldset'])) { | ||
$this->item['fieldset_id'] = $fieldset; | ||
} | ||
} | ||
|
||
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)); | ||
$assetModel->created_by = auth()->id(); | ||
} | ||
|
||
if ($assetModel->save()) { | ||
$this->log('AssetModel '.$assetModel->name.' created or updated from CSV import'); | ||
return $assetModel; | ||
|
||
} else { | ||
$this->log($assetModel->getErrors()->first()); | ||
$this->addErrorToBag($assetModel, $assetModel->getErrors()->keys()[0], $assetModel->getErrors()->first()); | ||
return $assetModel->getErrors(); | ||
} | ||
|
||
} | ||
|
||
|
||
/** | ||
* Fetch an existing depreciation, or create new if it doesn't exist. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we don't create a new one if it doesn't exist, this comment should probably change. |
||
* | ||
* We only do a fetch vs create here since Depreciations have additional fields required | ||
* and cannot be created without them (months, for example.)) | ||
* | ||
* @author A. Gianotto | ||
* @since 7.1.3 | ||
* @param $depreciation_name string | ||
* @return int id of depreciation created/found | ||
*/ | ||
public function fetchDepreciation($depreciation_name) : ?int | ||
{ | ||
if ($depreciation_name != '') { | ||
|
||
if ($depreciation = Depreciation::where('name', '=', $depreciation_name)->first()) { | ||
$this->log('A matching Depreciation '.$depreciation_name.' already exists'); | ||
return $depreciation->id; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Fetch an existing fieldset, or create new if it doesn't exist | ||
* | ||
* @author A. Gianotto | ||
* @since 7.1.3 | ||
* @param $fieldset_name string | ||
* @return int id of fieldset created/found | ||
*/ | ||
public function createOrFetchCustomFieldset($fieldset_name) : ?int | ||
{ | ||
if ($fieldset_name != '') { | ||
$fieldset = CustomFieldset::where('name', '=', $fieldset_name)->first(); | ||
|
||
if ($fieldset) { | ||
$this->log('A matching fieldset '.$fieldset_name.' already exists'); | ||
return $fieldset->id; | ||
} | ||
|
||
$fieldset = new CustomFieldset(); | ||
$fieldset->name = $fieldset_name; | ||
|
||
if ($fieldset->save()) { | ||
$this->log('Fieldset '.$fieldset_name.' was created'); | ||
|
||
return $fieldset->id; | ||
} | ||
$this->logError($fieldset, 'Fieldset'); | ||
} | ||
|
||
return null; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we not be using the older 'default is models.created_at' sort variable above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the one above was overriding the sort.