Skip to content

Commit

Permalink
Merge pull request #16099 from snipe/add_id_to_locations_importer
Browse files Browse the repository at this point in the history
Fixed #16097 - added location ID to location importer
  • Loading branch information
snipe authored Jan 20, 2025
2 parents 67ab602 + 5273408 commit 5afcd8d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/Importer/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ abstract class Importer
* @var array
*/
private $defaultFieldMap = [
'id' => 'id',
'asset_tag' => 'asset tag',
'activated' => 'activated',
'category' => 'category',
Expand Down
3 changes: 1 addition & 2 deletions app/Importer/ItemImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,14 +456,13 @@ public function createOrFetchLocation($asset_location)
{
if (empty($asset_location)) {
$this->log('No location given, so none created.');

return null;
}

$location = Location::where(['name' => $asset_location])->first();

if ($location) {
$this->log('Location '.$asset_location.' already exists');

return $location->id;
}
// No matching locations in the collection, create a new one.
Expand Down
9 changes: 9 additions & 0 deletions app/Importer/LocationImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,16 @@ public function createLocationIfNotExists(array $row)
{

$editingLocation = false;

$location = Location::where('name', '=', $this->findCsvMatch($row, 'name'))->first();

if ($this->findCsvMatch($row, 'id')!='') {
// Override location if an ID was given
\Log::debug('Finding location by ID: '.$this->findCsvMatch($row, 'id'));
$location = Location::find($this->findCsvMatch($row, 'id'));
}


if ($location) {
if (! $this->updating) {
$this->log('A matching Location '.$this->item['name'].' already exists');
Expand Down Expand Up @@ -95,6 +103,7 @@ public function createLocationIfNotExists(array $row)

} else {
Log::debug($location->getErrors());
$this->logError($location, 'Location "'.$this->item['name'].'"');
return $location->errors;
}

Expand Down
2 changes: 1 addition & 1 deletion app/Livewire/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ public function mount()
];

$this->locations_fields = [
'id' => trans('general.id'),
'name' => trans('general.item_name_var', ['item' => trans('general.location')]),
'address' => trans('general.address'),
'address2' => trans('general.importer.address2'),
Expand Down Expand Up @@ -400,7 +401,6 @@ public function mount()
'requestable',
'Requestable',
],

'gravatar' =>
[
'gravatar',
Expand Down

0 comments on commit 5afcd8d

Please sign in to comment.