2019-05-13 02:27:19 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use League\Csv\Reader;
|
|
|
|
use App\Models\Location;
|
|
|
|
|
|
|
|
class ImportLocations extends Command
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'snipeit:import-locations {filename}';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Import locations and their parents';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
2019-06-03 22:05:16 -07:00
|
|
|
$csv->setHeaderOffset(0); //because we don't want to insert the header
|
2019-05-23 19:09:58 -07:00
|
|
|
$results = $csv->getRecords();
|
2019-05-13 02:27:19 -07:00
|
|
|
|
|
|
|
// Import parent location names first if they don't exist
|
|
|
|
foreach ($results as $parent_index => $parent_row) {
|
|
|
|
$parent_name = trim($parent_row['Parent Name']);
|
2019-06-03 22:05:16 -07:00
|
|
|
$this->info('- Parent: '.$parent_name.' in row as: '.trim($parent_row['Parent Name']));
|
2019-05-13 02:27:19 -07:00
|
|
|
// 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.');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-03 22:05:16 -07:00
|
|
|
$this->info('----- Parents Created.... backfilling additional details... --------');
|
2019-05-13 02:27:19 -07:00
|
|
|
// Loop through ALL records and add/update them if there are additional fields
|
|
|
|
// besides name
|
|
|
|
foreach ($results as $index => $row) {
|
|
|
|
|
2019-06-03 22:05:16 -07:00
|
|
|
$parent_name = trim($row['Parent Name']);
|
2019-05-13 02:27:19 -07:00
|
|
|
// 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);
|
|
|
|
|
2019-06-03 22:05:16 -07:00
|
|
|
// If a parent name is provided, we created it earlier in the script,
|
2019-05-13 02:27:19 -07:00
|
|
|
// so let's grab that ID
|
|
|
|
if ($parent_name) {
|
2019-06-03 22:05:16 -07:00
|
|
|
$this->info('-- Searching for Parent Name: '.$parent_name.' - '.$row['Parent Name']);
|
2019-05-13 02:27:19 -07:00
|
|
|
$parent = Location::where('name', '=', $parent_name)->first();
|
|
|
|
$location->parent_id = $parent->id;
|
2019-06-03 22:05:16 -07:00
|
|
|
$this->info('Parent: '.$parent_name.' - ID: '.$parent->id);
|
2019-05-13 02:27:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|