mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-25 05:34:06 -08:00
f432f98e12
* Fix Bug in User::generateFormattedNameFromFullName In a name "John Doe", this method would split it into "John" and " Doe", Leaving a space in the last name when importing to the database. Strip this space. * Cleanup/fix some item mapping. Also make some changes to the importer schema to allow for unit testing. Generate a default item mapping, and then merge that with any custom mappings. * Beginning work on importer unit tests. * Strip out testrun branches from importer. It added a lot of complexity and was not terribly useful with web importer as it stood, might reconsider down the road however. * Normalize the mapped keys when using custom field mappings. * Add test for custom asset import mapping. * Make all unit tests inherit from a new custom base. This baseclass currently calls Artisan::migrate() and seeds a Settings instance. This fixes unit tests after the autoincrement bits. * Store requestable as a boolean. Fixes some import oddities * Work on tests for accessory importer. * Test for custom mapping of accessory import, also adjust the internal field for purchase date. * Update default locale fallback for currency detection * Fix Reassignable in consumable as well. * More importer tests and fixes.
96 lines
3.1 KiB
PHP
96 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Importer;
|
|
|
|
use App\Helpers\Helper;
|
|
use App\Models\Asset;
|
|
use App\Models\Category;
|
|
use App\Models\Manufacturer;
|
|
use App\Models\Statuslabel;
|
|
|
|
class AssetImporter extends ItemImporter
|
|
{
|
|
protected $defaultStatusLabelId;
|
|
|
|
public function __construct($filename)
|
|
{
|
|
parent::__construct($filename);
|
|
$this->defaultStatusLabelId = Statuslabel::first()->id;
|
|
}
|
|
|
|
protected function handle($row)
|
|
{
|
|
// ItemImporter handles the general fetching.
|
|
parent::handle($row);
|
|
|
|
foreach ($this->customFields as $customField) {
|
|
$customFieldValue = $this->array_smart_custom_field_fetch($row, $customField);
|
|
if ($customFieldValue) {
|
|
$this->item['custom_fields'][$customField->db_column_name()] = $customFieldValue;
|
|
$this->log('Custom Field '. $customField->name.': '.$customFieldValue);
|
|
}
|
|
}
|
|
|
|
$this->createAssetIfNotExists($row);
|
|
}
|
|
|
|
/**
|
|
* Create the asset if it does not exist.
|
|
*
|
|
* @author Daniel Melzter
|
|
* @since 3.0
|
|
* @param array $row
|
|
* @return Asset|mixed|null
|
|
*/
|
|
public function createAssetIfNotExists(array $row)
|
|
{
|
|
$editingAsset = false;
|
|
$asset_tag = $this->findCsvMatch($row, "asset_tag");
|
|
$asset = Asset::where(['asset_tag'=> $asset_tag])->first();
|
|
if ($asset) {
|
|
if (!$this->updating) {
|
|
$this->log('A matching Asset ' . $asset_tag . ' already exists');
|
|
return;
|
|
}
|
|
|
|
$this->log("Updating Asset");
|
|
$editingAsset = true;
|
|
} else {
|
|
$this->log("No Matching Asset, Creating a new one");
|
|
$asset = new Asset;
|
|
}
|
|
|
|
$this->item['image'] = $this->findCsvMatch($row, "image");
|
|
$this->item['warranty_months'] = intval($this->findCsvMatch($row, "warranty"));
|
|
$this->item['model_id'] = $this->createOrFetchAssetModel($row);
|
|
if (!$this->item['status_id'] && !$editingAsset) {
|
|
$this->log("No status field found, defaulting to first status.");
|
|
$this->item['status_id'] = $this->defaultStatusLabelId;
|
|
}
|
|
|
|
$this->item['asset_tag'] = $asset_tag;
|
|
// By default we're set this to location_id in the item.
|
|
$item = $this->sanitizeItemForStoring($asset, $editingAsset);
|
|
if (isset($this->item["location_id"])) {
|
|
$item['rtd_location_id'] = $this->item['location_id'];
|
|
unset($item['location_id']);
|
|
}
|
|
if ($editingAsset) {
|
|
$asset->update($item);
|
|
} else {
|
|
$asset->fill($item);
|
|
}
|
|
if (array_key_exists('custom_fields', $this->item)) {
|
|
foreach ($this->item['custom_fields'] as $custom_field => $val) {
|
|
$asset->{$custom_field} = $val;
|
|
}
|
|
}
|
|
if ($asset->save()) {
|
|
$asset->logCreate('Imported using csv importer');
|
|
$this->log('Asset ' . $this->item["name"] . ' with serial number ' . $this->item['serial'] . ' was created');
|
|
return;
|
|
}
|
|
$this->logError($asset, 'Asset "' . $this->item['name'].'"');
|
|
}
|
|
}
|