From d67c931f6a8cc8dd6a63040804c203d07cdee71c Mon Sep 17 00:00:00 2001 From: snipe Date: Mon, 13 May 2019 02:27:19 -0700 Subject: [PATCH] Import locations from CSV via command line (#7021) * Added import locations command * Small fixes to location importer * Added country, LDAP OU * Cleaned up comments, added more clarification to what the script does --- app/Console/Commands/ImportLocations.php | 130 +++++++++++++++++++++++ app/Console/Kernel.php | 1 + 2 files changed, 131 insertions(+) create mode 100644 app/Console/Commands/ImportLocations.php diff --git a/app/Console/Commands/ImportLocations.php b/app/Console/Commands/ImportLocations.php new file mode 100644 index 0000000000..8f566dc4ce --- /dev/null +++ b/app/Console/Commands/ImportLocations.php @@ -0,0 +1,130 @@ +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.'.'); + + // 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.'); + } + + } else { + $this->info('- No parent location for '.$parent_row['Name'].' provided.'); + } + + } + + // 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']); + $location->address = trim($row['Address 1']); + $location->address2 = trim($row['Address 2']); + $location->city = trim($row['City']); + $location->state = trim($row['State']); + $location->zip = trim($row['Zip']); + $location->country = trim($row['Country']); + $location->ldap_ou = trim($row['OU']); + + $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() ); + } + + + + + } + + + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 46b3cde2cb..c5afadfc83 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -33,6 +33,7 @@ class Kernel extends ConsoleKernel Commands\SyncAssetCounters::class, Commands\RestoreDeletedUsers::class, Commands\SendUpcomingAuditReport::class, + Commands\ImportLocations::class, ]; /**