Skip to content
/ snipe-it Public
  • Rate limit · GitHub

    Access has been restricted

    You have triggered a rate limit.

    Please wait a few minutes before you try again;
    in some cases this may take up to an hour.

  • Notifications You must be signed in to change notification settings
  • Fork 3.3k
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

Import locations from CSV via command line #7021

Merged
merged 4 commits into from
May 13, 2019
Rate limit · GitHub

Access has been restricted

You have triggered a rate limit.

Please wait a few minutes before you try again;
in some cases this may take up to an hour.

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Cleaned up comments, added more clarification to what the script does
Rate limit · GitHub

Access has been restricted

You have triggered a rate limit.

Please wait a few minutes before you try again;
in some cases this may take up to an hour.

snipe committed May 13, 2019
commit 7f86f929010c58f39eeb3033ee3df404351a5b34
23 changes: 16 additions & 7 deletions app/Console/Commands/ImportLocations.php
Original file line number Diff line number Diff line change
@@ -39,35 +39,36 @@ public function __construct()
*/
public function handle()
{
// Import parent location names first if they don't exist
// 2019-05-13-021051-locationscsv


if (!ini_get("auto_detect_line_endings")) {
ini_set("auto_detect_line_endings", '1');
}

$filename = $this->argument('filename');
$csv = Reader::createFromPath(storage_path('private_uploads/imports/').$filename, 'r');

$this->info('Attempting to process: '.storage_path('private_uploads/imports/').$filename);
$csv->setOffset(1); //because we don't want to insert the header
$results = $csv->fetchAssoc();

// Import parent location names first if they don't exist
foreach ($results as $parent_index => $parent_row) {

$parent_name = trim($parent_row['Parent Name']);
// First create any parents if they don't exist

if ($parent_name!='') {

// Save parent location name
// This creates a sort of name-stub that we'll update later on in this script
$parent_location = Location::firstOrCreate(array('name' => $parent_name));
$this->info('Parent for '.$parent_row['Name'].' is '.$parent_name.'. Attempting to save '.$parent_name.'.');


// Save parent location name
// Check if the record was updated or created.
// This is mostly for clearer debugging.
if ($parent_location->exists) {
$this->info('- Parent location '.$parent_name.' already exists.');
} else {

$this->info('- Parent location '.$parent_name.' was created.');
}

@@ -77,8 +78,11 @@ public function handle()

}

// Loop through ALL records and add/update them if there are additional fields
// besides name
foreach ($results as $index => $row) {

// Set the location attributes to save
$location = Location::firstOrNew(array('name' => trim($row['Name'])));
$location->name = trim($row['Name']);
$location->currency = trim($row['Currency']);
@@ -92,21 +96,26 @@ public function handle()

$this->info('Checking location: '.$location->name);


// If a parent name nis provided, we created it earlier in the script,
// so let's grab that ID
if ($parent_name) {
$parent = Location::where('name', '=', $parent_name)->first();
$location->parent_id = $parent->id;
$this->info('Parent ID: '.$parent->id);
}

// Make sure the more advanced (non-name) fields pass validation
if (($location->isValid()) && ($location->save())) {

// Check if the record was updated or created.
// This is mostly for clearer debugging.
if ($location->exists) {
$this->info('Location ' . $location->name . ' already exists. Updating...');
} else {
$this->info('- Location '.$location->name.' was created. ');
}

// If there's a validation error, display that
} else {
$this->error('- Non-parent Location '.$location->name.' could not be created: '.$location->getErrors() );
}