Merge pull request #2176 from dmeltzer/fix-importer-user

Assorted Importer Fixes
This commit is contained in:
snipe 2016-06-21 10:04:06 -07:00 committed by GitHub
commit c9bdc7da8e
2 changed files with 31 additions and 18 deletions

View file

@ -103,8 +103,10 @@ class ObjectImportCommand extends Command {
$this->accessories = Accessory::All(['name']); $this->accessories = Accessory::All(['name']);
$this->consumables = Consumable::All(['name']); $this->consumables = Consumable::All(['name']);
$this->customfields = CustomField::All(['name']); $this->customfields = CustomField::All(['name']);
$bar = NULL;
$bar = $this->output->createProgressBar(count($newarray)); if(!$this->option('web-importer')) {
$bar = $this->output->createProgressBar(count($newarray));
}
// Loop through the records // Loop through the records
DB::transaction(function() use (&$newarray, $bar){ DB::transaction(function() use (&$newarray, $bar){
Model::unguard(); Model::unguard();
@ -180,12 +182,16 @@ class ObjectImportCommand extends Command {
break; break;
} }
$bar->advance(); if(!$this->option('web-importer')) {
$bar->advance();
}
$this->log('------------- Action Summary ----------------'); $this->log('------------- Action Summary ----------------');
} }
}); });
$bar->finish(); if(!$this->option('web-importer')) {
$bar->finish();
}
$this->log('====================================='); $this->log('=====================================');
@ -321,7 +327,7 @@ class ObjectImportCommand extends Command {
$asset_model->manufacturer_id = $manufacturer->id; $asset_model->manufacturer_id = $manufacturer->id;
$asset_model->modelno = $asset_modelno; $asset_model->modelno = $asset_modelno;
$asset_model->category_id = $category->id; $asset_model->category_id = $category->id;
$asset_model->user_id = 1; $asset_model->user_id = $this->option('user_id');
if(!$this->option('testrun')) { if(!$this->option('testrun')) {
@ -367,7 +373,7 @@ class ObjectImportCommand extends Command {
$category->name = $asset_category; $category->name = $asset_category;
$category->category_type = $item_type; $category->category_type = $item_type;
$category->user_id = 1; $category->user_id = $this->option('user_id');
if(!$this->option('testrun')) { if(!$this->option('testrun')) {
@ -491,7 +497,7 @@ class ObjectImportCommand extends Command {
$manufacturer = new Manufacturer(); $manufacturer = new Manufacturer();
$manufacturer->name = $asset_mfgr; $manufacturer->name = $asset_mfgr;
$manufacturer->user_id = 1; $manufacturer->user_id = $this->option('user_id');
if (!$this->option('testrun')) { if (!$this->option('testrun')) {
if ($manufacturer->save()) { if ($manufacturer->save()) {
@ -538,7 +544,7 @@ class ObjectImportCommand extends Command {
$location->city = ''; $location->city = '';
$location->state = ''; $location->state = '';
$location->country = ''; $location->country = '';
$location->user_id = 1; $location->user_id = $this->option('user_id');
if (!$this->option('testrun')) { if (!$this->option('testrun')) {
if ($location->save()) { if ($location->save()) {
@ -584,7 +590,7 @@ class ObjectImportCommand extends Command {
$supplier = new Supplier(); $supplier = new Supplier();
$supplier->name = $supplier_name; $supplier->name = $supplier_name;
$supplier->user_id = 1; $supplier->user_id = $this->option('user_id');
if(!$this->option('testrun')) { if(!$this->option('testrun')) {
if ($supplier->save()) { if ($supplier->save()) {
@ -721,6 +727,7 @@ class ObjectImportCommand extends Command {
$this->log('Serial No: '.$asset_serial); $this->log('Serial No: '.$asset_serial);
$this->log('Asset Tag: '.$asset_tag); $this->log('Asset Tag: '.$asset_tag);
$this->log('Notes: '.$item["notes"]); $this->log('Notes: '.$item["notes"]);
$this->log('Warranty Months: ' . $asset_warranty_months);
foreach ($this->assets as $tempasset) { foreach ($this->assets as $tempasset) {
if (strcasecmp($tempasset->asset_tag, $asset_tag ) == 0 ) { if (strcasecmp($tempasset->asset_tag, $asset_tag ) == 0 ) {
@ -747,9 +754,11 @@ class ObjectImportCommand extends Command {
$asset->purchase_date = NULL; $asset->purchase_date = NULL;
} }
foreach ($item['custom_fields'] as $custom_field => $val) { if( array_key_exists('custom_fields', $item)) {
$asset->{$custom_field} = $val; foreach ($item['custom_fields'] as $custom_field => $val) {
} $asset->{$custom_field} = $val;
}
}
if (!empty($item["purchase_cost"])) { if (!empty($item["purchase_cost"])) {
//TODO How to generalize this for not USD? //TODO How to generalize this for not USD?
@ -761,6 +770,7 @@ class ObjectImportCommand extends Command {
} }
$asset->serial = $asset_serial; $asset->serial = $asset_serial;
$asset->asset_tag = $asset_tag; $asset->asset_tag = $asset_tag;
$asset->warranty_months = $asset_warranty_months;
if($asset_model) if($asset_model)
$asset->model_id = $asset_model->id; $asset->model_id = $asset_model->id;
@ -768,7 +778,7 @@ class ObjectImportCommand extends Command {
$asset->assigned_to = $item["user"]->id; $asset->assigned_to = $item["user"]->id;
if($item["location"]) if($item["location"])
$asset->rtd_location_id = $item["location"]->id; $asset->rtd_location_id = $item["location"]->id;
$asset->user_id = 1; $asset->user_id = $this->option('user_id');
$this->log("status_id: " . $status_id); $this->log("status_id: " . $status_id);
$asset->status_id = $status_id; $asset->status_id = $status_id;
if($item["company"]) if($item["company"])
@ -827,7 +837,7 @@ class ObjectImportCommand extends Command {
} }
if($item["location"]) if($item["location"])
$accessory->location_id = $item["location"]->id; $accessory->location_id = $item["location"]->id;
$accessory->user_id = 1; $accessory->user_id = $this->option('user_id');
if($item["company"]) if($item["company"])
$accessory->company_id = $item["company"]->id; $accessory->company_id = $item["company"]->id;
$accessory->order_number = $item["order_number"]; $accessory->order_number = $item["order_number"];
@ -893,7 +903,7 @@ class ObjectImportCommand extends Command {
$consumable->purchase_cost = 0.00; $consumable->purchase_cost = 0.00;
} }
$consumable->location_id = $item["location"]->id; $consumable->location_id = $item["location"]->id;
$consumable->user_id = 1; // TODO: What user_id should we use for imports? $consumable->user_id = $this->option('user_id');
$consumable->company_id = $item["company"]->id; $consumable->company_id = $item["company"]->id;
$consumable->order_number = $item["order_number"]; $consumable->order_number = $item["order_number"];
$consumable->category_id = $item["category"]->id; $consumable->category_id = $item["category"]->id;
@ -950,7 +960,8 @@ class ObjectImportCommand extends Command {
array('testrun', null, InputOption::VALUE_NONE, 'If set, will parse and output data without adding to database', null), array('testrun', null, InputOption::VALUE_NONE, 'If set, will parse and output data without adding to database', null),
array('logfile', null, InputOption::VALUE_REQUIRED, 'The path to log output to. storage/logs/importer.log by default', storage_path('logs/importer.log') ), array('logfile', null, InputOption::VALUE_REQUIRED, 'The path to log output to. storage/logs/importer.log by default', storage_path('logs/importer.log') ),
array('item-type', null, InputOption::VALUE_REQUIRED, 'Item Type To import. Valid Options are Asset, Consumable, Or Accessory', 'Asset'), array('item-type', null, InputOption::VALUE_REQUIRED, 'Item Type To import. Valid Options are Asset, Consumable, Or Accessory', 'Asset'),
array('web-importer', null, InputOption::VALUE_NONE, 'Internal: packages output for use with the web importer') array('web-importer', null, InputOption::VALUE_NONE, 'Internal: packages output for use with the web importer'),
array('user_id', null, InputOption::VALUE_REQUIRED, 'ID of user creating items', 1)
); );
} }

View file

@ -878,15 +878,17 @@ class AssetsController extends Controller
['filename'=> config('app.private_uploads').'/imports/assets/'.$filename, ['filename'=> config('app.private_uploads').'/imports/assets/'.$filename,
'--email_format'=>'firstname.lastname', '--email_format'=>'firstname.lastname',
'--username_format'=>'firstname.lastname', '--username_format'=>'firstname.lastname',
'--web-importer' => true '--web-importer' => true,
'--user_id' => Auth::user()->id
]); ]);
$display_output = Artisan::output(); $display_output = Artisan::output();
$file = config('app.private_uploads').'/imports/assets/'.str_replace('.csv', '', $filename).'-output-'.date("Y-m-d-his").'.txt'; $file = config('app.private_uploads').'/imports/assets/'.str_replace('.csv', '', $filename).'-output-'.date("Y-m-d-his").'.txt';
file_put_contents($file, $display_output); file_put_contents($file, $display_output);
if( $return === 0) //Success if( $return === 0) //Success
return redirect()->to('hardware')->with('success', trans('admin/hardware/message.import.success')); return redirect()->to('hardware')->with('success', trans('admin/hardware/message.import.success'));
else if( $return === 1) // Failure else if( $return === 1) { // Failure
return redirect()->back()->with('import_errors', json_decode($display_output))->with('error', trans('admin/hardware/message.import.error')); return redirect()->back()->with('import_errors', json_decode($display_output))->with('error', trans('admin/hardware/message.import.error'));
}
dd("Shouldn't be here"); dd("Shouldn't be here");
} }