2024-08-16 18:31:03 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Importer;
|
|
|
|
|
|
|
|
use App\Models\AssetModel;
|
2024-11-13 07:16:08 -08:00
|
|
|
use App\Models\Depreciation;
|
|
|
|
use App\Models\CustomFieldset;
|
2024-08-16 18:31:03 -07:00
|
|
|
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;
|
2024-11-13 09:08:20 -08:00
|
|
|
$assetModel = AssetModel::where('name', '=', $this->findCsvMatch($row, 'name'))->first();
|
2024-08-16 18:31:03 -07:00
|
|
|
|
2024-11-13 09:08:20 -08:00
|
|
|
if ($assetModel) {
|
2024-08-16 18:31:03 -07:00
|
|
|
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');
|
2024-11-13 09:08:20 -08:00
|
|
|
$assetModel = new AssetModel();
|
2024-08-16 18:31:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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'));
|
2024-11-12 14:40:00 -08:00
|
|
|
$this->item['eol'] = trim($this->findCsvMatch($row, 'eol'));
|
2024-08-16 18:31:03 -07:00
|
|
|
$this->item['notes'] = trim($this->findCsvMatch($row, 'notes'));
|
2024-11-13 07:16:08 -08:00
|
|
|
$this->item['fieldset'] = trim($this->findCsvMatch($row, 'fieldset'));
|
|
|
|
$this->item['depreciation'] = trim($this->findCsvMatch($row, 'depreciation'));
|
2024-11-12 14:40:00 -08:00
|
|
|
$this->item['created_by'] = auth()->id();
|
2024-11-13 07:16:08 -08:00
|
|
|
$this->item['requestable'] = trim(($this->fetchHumanBoolean($this->findCsvMatch($row, 'requestable'))) == 1) ? 1 : 0;
|
2024-08-16 18:31:03 -07:00
|
|
|
|
|
|
|
if (!empty($this->item['category'])) {
|
2024-11-12 14:40:00 -08:00
|
|
|
if ($category = $this->createOrFetchCategory($this->item['category'])) {
|
|
|
|
$this->item['category_id'] = $category;
|
2024-08-16 18:31:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($this->item['manufacturer'])) {
|
2024-11-12 14:40:00 -08:00
|
|
|
if ($manufacturer = $this->createOrFetchManufacturer($this->item['manufacturer'])) {
|
|
|
|
$this->item['manufacturer_id'] = $manufacturer;
|
2024-08-16 18:31:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-13 07:16:08 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-16 18:31:03 -07:00
|
|
|
Log::debug('Item array is: ');
|
|
|
|
Log::debug(print_r($this->item, true));
|
|
|
|
|
|
|
|
|
|
|
|
if ($editingAssetModel) {
|
|
|
|
Log::debug('Updating existing model');
|
2024-11-13 09:08:20 -08:00
|
|
|
$assetModel->update($this->sanitizeItemForUpdating($assetModel));
|
2024-08-16 18:31:03 -07:00
|
|
|
} else {
|
|
|
|
Log::debug('Creating model');
|
2024-11-13 09:08:20 -08:00
|
|
|
$assetModel->fill($this->sanitizeItemForStoring($assetModel));
|
2024-08-16 18:31:03 -07:00
|
|
|
}
|
|
|
|
|
2024-11-13 09:08:20 -08:00
|
|
|
if ($assetModel->save()) {
|
|
|
|
$this->log('AssetModel '.$assetModel->name.' created or updated from CSV import');
|
|
|
|
return $assetModel;
|
2024-08-16 18:31:03 -07:00
|
|
|
|
|
|
|
} else {
|
2024-11-13 09:08:20 -08:00
|
|
|
$this->log($assetModel->getErrors()->first());
|
|
|
|
$this->addErrorToBag($assetModel, $assetModel->getErrors()->keys()[0], $assetModel->getErrors()->first());
|
|
|
|
return $assetModel->getErrors();
|
2024-08-16 18:31:03 -07:00
|
|
|
}
|
|
|
|
|
2024-11-13 07:16:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch an existing depreciation, or create new if it doesn't exist.
|
|
|
|
*
|
|
|
|
* 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');
|
|
|
|
}
|
2024-08-16 18:31:03 -07:00
|
|
|
|
2024-11-13 07:16:08 -08:00
|
|
|
return null;
|
2024-08-16 18:31:03 -07:00
|
|
|
}
|
|
|
|
}
|