Merge pull request #2083 from dmeltzer/importer-improvements

Importer improvements
This commit is contained in:
snipe 2016-05-31 14:25:33 -07:00
commit ac10a199ea
4 changed files with 287 additions and 107 deletions

View file

@ -14,6 +14,7 @@ use App\Models\Company;
use App\Models\Consumable; use App\Models\Consumable;
use App\Models\Location; use App\Models\Location;
use App\Models\Manufacturer; use App\Models\Manufacturer;
use App\Models\Statuslabel;
use App\Models\Supplier; use App\Models\Supplier;
use App\Models\User; use App\Models\User;
use DB; use DB;
@ -56,12 +57,16 @@ class ObjectImportCommand extends Command {
$filename = $this->argument('filename'); $filename = $this->argument('filename');
if ($this->option('testrun')) { if(!$this->option('web-importer')) {
$this->comment('====== TEST ONLY Asset Import for '.$filename.' ===='); $logFile = $this->option('logfile');
$this->comment('============== NO DATA WILL BE WRITTEN =============='); \Log::useFiles($logFile);
} else { if ($this->option('testrun')) {
$this->comment('====== TEST ONLY Asset Import for '.$filename.' ====');
$this->comment('============== NO DATA WILL BE WRITTEN ==============');
} else {
$this->comment('======= Importing Assets from '.$filename.' ========='); $this->comment('======= Importing Assets from '.$filename.' =========');
}
} }
if (! ini_get("auto_detect_line_endings")) { if (! ini_get("auto_detect_line_endings")) {
@ -85,12 +90,15 @@ class ObjectImportCommand extends Command {
$this->manufacturers = Manufacturer::All(['name', 'id']); $this->manufacturers = Manufacturer::All(['name', 'id']);
$this->asset_models = AssetModel::All(['name','modelno','category_id','manufacturer_id', 'id']); $this->asset_models = AssetModel::All(['name','modelno','category_id','manufacturer_id', 'id']);
$this->companies = Company::All(['name', 'id']); $this->companies = Company::All(['name', 'id']);
$this->status_labels = Statuslabel::All(['name', 'id']);
$this->suppliers = Supplier::All(['name', 'id']);
$this->assets = Asset::all(['asset_tag']); $this->assets = Asset::all(['asset_tag']);
$this->suppliers = Supplier::All(['name']);
$this->accessories = Accessory::All(['name']); $this->accessories = Accessory::All(['name']);
$this->consumables = Consumable::All(['name']); $this->consumables = Consumable::All(['name']);
// Loop through the records // Loop through the records
DB::transaction(function() use (&$newarray){ DB::transaction(function() use (&$newarray){
$item_type = strtolower($this->option('item-type'));
foreach( $newarray as $row ) { foreach( $newarray as $row ) {
// Let's just map some of these entries to more user friendly words // Let's just map some of these entries to more user friendly words
@ -102,11 +110,7 @@ class ObjectImportCommand extends Command {
$item_company_name = $this->array_smart_fetch($row, "company"); $item_company_name = $this->array_smart_fetch($row, "company");
$item_location = $this->array_smart_fetch($row, "location"); $item_location = $this->array_smart_fetch($row, "location");
$item["item_type"] = strtolower($this->array_smart_fetch($row, "item type")); $item_status_name = $this->array_smart_fetch($row, "status");
if(empty($item["item_type"])) {
$this->comment("Item Type not set. Assuming asset");
$item["item_type"] = 'asset';
}
$item["item_name"] = $this->array_smart_fetch($row, "item name"); $item["item_name"] = $this->array_smart_fetch($row, "item name");
$item["purchase_date"] = date("Y-m-d 00:00:01", strtotime($this->array_smart_fetch($row, "purchase date"))); $item["purchase_date"] = date("Y-m-d 00:00:01", strtotime($this->array_smart_fetch($row, "purchase date")));
@ -115,23 +119,27 @@ class ObjectImportCommand extends Command {
$item["notes"] = $this->array_smart_fetch($row, "notes"); $item["notes"] = $this->array_smart_fetch($row, "notes");
$item["quantity"] = $this->array_smart_fetch($row, "quantity"); $item["quantity"] = $this->array_smart_fetch($row, "quantity");
$item["requestable"] = $this->array_smart_fetch($row, "requestable"); $item["requestable"] = $this->array_smart_fetch($row, "requestable");
$this->comment("Item Type: " . $item["item_type"]); $this->current_assetId = $item["item_name"];
$this->comment('Category Name: ' . $item_category); $this->log('Category Name: ' . $item_category);
$this->comment('Location: ' . $item_location); $this->log('Location: ' . $item_location);
$this->comment('Purchase Date: ' . $item["purchase_date"]); $this->log('Purchase Date: ' . $item["purchase_date"]);
$this->comment('Purchase Cost: ' . $item["purchase_cost"]); $this->log('Purchase Cost: ' . $item["purchase_cost"]);
$this->comment('Company Name: ' . $item_company_name); $this->log('Company Name: ' . $item_company_name);
$this->log('Status: ' . $item_status_name);
$item["user"] = $this->createOrFetchUser($row); $item["user"] = $this->createOrFetchUser($row);
$item["location"] = $this->createOrFetchLocation($item_location); $item["location"] = $this->createOrFetchLocation($item_location);
$item["category"] = $this->createOrFetchCategory($item_category, $item["item_type"]); $item["category"] = $this->createOrFetchCategory($item_category, $item_type);
$item["manufacturer"] = $this->createOrFetchManufacturer($row); $item["manufacturer"] = $this->createOrFetchManufacturer($row);
$item["company"] = $this->createOrFetchCompany($item_company_name); $item["company"] = $this->createOrFetchCompany($item_company_name);
switch ($item["item_type"]) { $item["status_label"] = $this->createOrFetchStatusLabel($item_status_name);
switch ($item_type) {
case "asset": case "asset":
$this->createAssetIfNotExists($row, $item); $this->createAssetIfNotExists($row, $item);
break; break;
@ -142,13 +150,69 @@ class ObjectImportCommand extends Command {
$this->createConsumableIfNotExists($item); $this->createConsumableIfNotExists($item);
break; break;
} }
$this->comment('------------- Action Summary ----------------'); $this->log('------------- Action Summary ----------------');
} }
}); });
$this->comment('=====================================');
return true; $this->log('=====================================');
if(!$this->option('web-importer'))
{
if(!empty($this->errors)) {
$this->comment("The following Errors were encountered.");
foreach($this->errors as $asset => $error)
{
$this->comment('Error: Item: ' . $asset . 'failed validation: ' . json_encode($error));
}
} else {
$this->comment("All Items imported successfully!");
}
} else {
if(empty($this->errors))
return 0;
else {
$this->comment(json_encode($this->errors)); //Send a big string to the
return 1;
}
}
$this->comment("");
return 2;
}
// Tracks the current item for error messages
private $current_assetId;
// An array of errors encountered while parsing
private $errors;
public function jsonError($field, $errorString)
{
$this->errors[$this->current_assetId] = array($field => $errorString);
if($this->option('verbose'))
parent::error($errorString);
}
/**
* Log a message to file, configurable by the --log-file parameter.
* If a warning message is passed, we'll spit it to the console as well.
* @param string $string
* @param string $level
*/
private function log($string, $level = 'info')
{
if($this->option('web-importer'))
return;
if($level === 'warning')
{
\Log::warning($string);
$this->comment($string);
}
else {
\Log::Info($string);
if($this->option('verbose')) {
$this->comment($string);
}
}
} }
/** /**
@ -179,8 +243,8 @@ class ObjectImportCommand extends Command {
$asset_model_name='Unknown'; $asset_model_name='Unknown';
if(empty($asset_modelno)) if(empty($asset_modelno))
$asset_modelno=0; $asset_modelno=0;
$this->comment('Model Name: ' . $asset_model_name); $this->log('Model Name: ' . $asset_model_name);
$this->comment('Model No: ' . $asset_modelno); $this->log('Model No: ' . $asset_modelno);
foreach ($this->asset_models as $tempmodel) { foreach ($this->asset_models as $tempmodel) {
@ -189,7 +253,7 @@ class ObjectImportCommand extends Command {
&& $tempmodel->category_id == $category->id && $tempmodel->category_id == $category->id
&& $tempmodel->manufacturer_id == $manufacturer->id ) && $tempmodel->manufacturer_id == $manufacturer->id )
{ {
$this->comment('A matching model ' . $asset_model_name . ' with model number ' . $asset_modelno . ' already exists'); $this->log('A matching model ' . $asset_model_name . ' with model number ' . $asset_modelno . ' already exists');
return $tempmodel; return $tempmodel;
} }
} }
@ -199,18 +263,19 @@ class ObjectImportCommand extends Command {
$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 = 1;
$this->asset_models->add($asset_model);
if(!$this->option('testrun')) { if(!$this->option('testrun')) {
if ($asset_model->save()) { if ($asset_model->save()) {
$this->comment('Asset Model ' . $asset_model_name . ' with model number ' . $asset_modelno . ' was created'); $this->asset_models->add($asset_model);
$this->log('Asset Model ' . $asset_model_name . ' with model number ' . $asset_modelno . ' was created');
return $asset_model; return $asset_model;
} else { } else {
$this->comment('Something went wrong! Asset Model ' . $asset_model_name . ' was NOT created'); $this->jsonError('Asset Model', $asset_model->getErrors());
dd($asset_model);
return $asset_model; return $asset_model;
} }
} else { } else {
$this->asset_models->add($asset_model);
return $asset_model; return $asset_model;
} }
@ -231,7 +296,7 @@ class ObjectImportCommand extends Command {
foreach($this->categories as $tempcategory) { foreach($this->categories as $tempcategory) {
if( $tempcategory->name === $asset_category && $tempcategory->category_type === $item_type) { if( $tempcategory->name === $asset_category && $tempcategory->category_type === $item_type) {
$this->comment('Category ' . $asset_category . ' already exists'); $this->log('Category ' . $asset_category . ' already exists');
return $tempcategory; return $tempcategory;
} }
} }
@ -241,18 +306,19 @@ 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 = 1;
$this->categories->add($category);
if(!$this->option('testrun')) { if(!$this->option('testrun')) {
if ($category->save()) { if ($category->save()) {
$this->comment('Category ' . $asset_category . ' was created'); $this->categories->add($category);
$this->log('Category ' . $asset_category . ' was created');
return $category; return $category;
} else { } else {
$this->comment('Something went wrong! Category ' . $asset_category . ' was NOT created'); $this->jsonError('Category', $category->getErrors());
dd($category); return $category;
return $category;
} }
} else { } else {
$this->categories->add($category);
return $category; return $category;
} }
@ -268,29 +334,61 @@ class ObjectImportCommand extends Command {
{ {
foreach ($this->companies as $tempcompany) { foreach ($this->companies as $tempcompany) {
if ($tempcompany->name === $asset_company_name) { if ($tempcompany->name === $asset_company_name) {
$this->comment('A matching Company ' . $asset_company_name . ' already exists'); $this->log('A matching Company ' . $asset_company_name . ' already exists');
return $tempcompany; return $tempcompany;
} }
} }
$company = new Company(); $company = new Company();
$company->name = $asset_company_name; $company->name = $asset_company_name;
$this->companies->add($company);
if(!$this->option('testrun')) { if(!$this->option('testrun')) {
if ($company->save()) { if ($company->save()) {
$this->comment('Company ' . $asset_company_name . ' was created'); $this->companies->add($company);
$this->log('Company ' . $asset_company_name . ' was created');
return $company; return $company;
} else { } else {
$this->comment('Something went wrong! Company ' . $asset_company_name . ' was NOT created'); $this->jsonError('Company', $company->getErrors());
return $company;
} }
} else { } else {
$this->companies->add($company);
return $company; return $company;
} }
} }
private $status_labels;
/**
* @param string $asset_statuslabel_name
* @return Company
*/
public function createOrFetchStatusLabel($asset_statuslabel_name)
{
if(empty($asset_statuslabel_name))
return;
foreach ($this->status_labels as $tempstatus) {
if ($tempstatus->name === $asset_statuslabel_name) {
$this->log('A matching Status ' . $asset_statuslabel_name . ' already exists');
return $tempstatus;
}
}
$status = new Statuslabel();
$status->name = $asset_statuslabel_name;
if(!$this->option('testrun')) {
if ($status->save()) {
$this->status_labels->add($status);
$this->log('Status ' . $asset_statuslabel_name . ' was created');
return $status;
} else {
$this->jsonError('Status', $status->getErrors());
return $status;
}
} else {
$this->status_labels->add($status);
return $status;
}
}
private $manufacturers; private $manufacturers;
/** /**
@ -307,11 +405,11 @@ class ObjectImportCommand extends Command {
if(empty($asset_mfgr)) { if(empty($asset_mfgr)) {
$asset_mfgr='Unknown'; $asset_mfgr='Unknown';
} }
$this->comment('Manufacturer ID: ' . $asset_mfgr); $this->log('Manufacturer ID: ' . $asset_mfgr);
foreach ($this->manufacturers as $tempmanufacturer) { foreach ($this->manufacturers as $tempmanufacturer) {
if ($tempmanufacturer->name === $asset_mfgr) { if ($tempmanufacturer->name === $asset_mfgr) {
$this->comment('Manufacturer ' . $asset_mfgr . ' already exists'); $this->log('{Manufacturer [' . $asset_mfgr . ' already exists') . ']}';
return $tempmanufacturer; return $tempmanufacturer;
} }
} }
@ -321,19 +419,19 @@ 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 = 1;
$this->manufacturers->add($manufacturer);
if (!$this->option('testrun')) { if (!$this->option('testrun')) {
if ($manufacturer->save()) { if ($manufacturer->save()) {
$this->comment('Manufacturer ' . $manufacturer->name . ' was created'); $this->manufacturers->add($manufacturer);
$this->log('Manufacturer ' . $manufacturer->name . ' was created');
return $manufacturer; return $manufacturer;
} else { } else {
$this->comment('Something went wrong! Manufacturer ' . $asset_mfgr . ' was NOT created'); $this->jsonError('Manufacturer', $manufacturer->getErrors());
dd($manufacturer);
return $manufacturer; return $manufacturer;
} }
} else { } else {
$this->manufacturers->add($manufacturer);
return $manufacturer; return $manufacturer;
} }
} }
@ -351,7 +449,7 @@ class ObjectImportCommand extends Command {
{ {
foreach($this->locations as $templocation) { foreach($this->locations as $templocation) {
if( $templocation->name === $asset_location ) { if( $templocation->name === $asset_location ) {
$this->comment('Location ' . $asset_location . ' already exists'); $this->log('Location ' . $asset_location . ' already exists');
return $templocation; return $templocation;
} }
} }
@ -365,22 +463,22 @@ class ObjectImportCommand extends Command {
$location->state = ''; $location->state = '';
$location->country = ''; $location->country = '';
$location->user_id = 1; $location->user_id = 1;
$this->locations->add($location);
if (!$this->option('testrun')) { if (!$this->option('testrun')) {
if ($location->save()) { if ($location->save()) {
$this->comment('Location ' . $asset_location . ' was created'); $this->locations->add($location);
$this->log('Location ' . $asset_location . ' was created');
return $location; return $location;
} else { } else {
$this->comment('Something went wrong! Location ' . $asset_location . ' was NOT created'); $this->jsonError('Location', $location->getErrors()) ;
dd($location);
return $location; return $location;
} }
} else { } else {
$this->locations->add($location);
return $location; return $location;
} }
} else { } else {
$this->comment('No location given, so none created.'); $this->log('No location given, so none created.');
return $location; return $location;
} }
@ -399,7 +497,7 @@ class ObjectImportCommand extends Command {
$supplier_name='Unknown'; $supplier_name='Unknown';
foreach ($this->suppliers as $tempsupplier) { foreach ($this->suppliers as $tempsupplier) {
if ($tempsupplier->name === $supplier_name) { if ($tempsupplier->name === $supplier_name) {
$this->comment('A matching Company ' . $supplier_name . ' already exists'); $this->log('A matching Company ' . $supplier_name . ' already exists');
return $tempsupplier; return $tempsupplier;
} }
} }
@ -407,18 +505,18 @@ 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 = 1;
$this->suppliers->add($supplier);
if(!$this->option('testrun')) { if(!$this->option('testrun')) {
if ($supplier->save()) { if ($supplier->save()) {
$this->comment('Supplier ' . $supplier_name . ' was created'); $this->suppliers->add($supplier);
$this->log('Supplier ' . $supplier_name . ' was created');
return $supplier; return $supplier;
} else { } else {
$this->comment('Something went wrong! Supplier ' . $supplier_name . ' was NOT created'); $this->jsonError('Supplier', $supplier->getErrors());
dd($supplier); return $supplier;
return $supplier;
} }
} else { } else {
$this->suppliers->add($supplier);
return $supplier; return $supplier;
} }
} }
@ -440,14 +538,14 @@ class ObjectImportCommand extends Command {
// A number was given instead of a name // A number was given instead of a name
if (is_numeric($user_name)) { if (is_numeric($user_name)) {
$this->comment('User '.$user_name.' is not a name - assume this user already exists'); $this->log('User '.$user_name.' is not a name - assume this user already exists');
$user_username = ''; $user_username = '';
$first_name = ''; $first_name = '';
$last_name = ''; $last_name = '';
// No name was given // No name was given
} elseif (empty($user_name)) { } elseif (empty($user_name)) {
$this->comment('No user data provided - skipping user creation, just adding asset'); $this->log('No user data provided - skipping user creation, just adding asset');
$first_name = ''; $first_name = '';
$last_name = ''; $last_name = '';
//$user_username = ''; //$user_username = '';
@ -471,13 +569,13 @@ class ObjectImportCommand extends Command {
} }
} }
$this->comment("--- User Data ---"); $this->log("--- User Data ---");
$this->comment('Full Name: ' . $user_name); $this->log('Full Name: ' . $user_name);
$this->comment('First Name: ' . $first_name); $this->log('First Name: ' . $first_name);
$this->comment('Last Name: ' . $last_name); $this->log('Last Name: ' . $last_name);
$this->comment('Username: ' . $user_username); $this->log('Username: ' . $user_username);
$this->comment('Email: ' . $user_email); $this->log('Email: ' . $user_email);
$this->comment('--- End User Data ---'); $this->log('--- End User Data ---');
if($this->option('testrun')) if($this->option('testrun'))
return new User; return new User;
@ -485,8 +583,9 @@ class ObjectImportCommand extends Command {
if (!empty($user_username)) { if (!empty($user_username)) {
if ($user = User::MatchEmailOrUsername($user_username, $user_email) if ($user = User::MatchEmailOrUsername($user_username, $user_email)
->whereNotNull('username')->first()) { ->whereNotNull('username')->first()) {
$this->comment('User '.$user_username.' already exists');
} else { $this->log('User '.$user_username.' already exists');
} else if(( $first_name != '') && ($last_name != '') && ($user_username != '')) {
$user = new \App\Models\User; $user = new \App\Models\User;
$password = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 20); $password = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 20);
@ -497,12 +596,13 @@ class ObjectImportCommand extends Command {
$user->password = bcrypt($password); $user->password = bcrypt($password);
$user->activated = 1; $user->activated = 1;
if ($user->save()) { if ($user->save()) {
$this->comment('User '.$first_name.' created'); $this->log('User '.$first_name.' created');
} else { } else {
$this->error('ERROR CREATING User '.$first_name.' '.$last_name); $this->jsonError('User', $user->getErrors());
$this->error($user->getErrors());
} }
} else {
$user = new User;
} }
} else { } else {
$user = new User; $user = new User;
@ -518,25 +618,40 @@ class ObjectImportCommand extends Command {
*/ */
public function createAssetIfNotExists(array $row, array $item ) public function createAssetIfNotExists(array $row, array $item )
{ {
$status_id = 1;
$asset_serial = $this->array_smart_fetch($row, "serial number"); $asset_serial = $this->array_smart_fetch($row, "serial number");
$asset_tag = $this->array_smart_fetch($row, "asset tag"); $asset_tag = $this->array_smart_fetch($row, "asset tag");
$asset_image = $this->array_smart_fetch($row, "image"); $asset_image = $this->array_smart_fetch($row, "image");
$asset_warranty_months = intval($this->array_smart_fetch($row, "warranty months"));
if(empty($asset_warranty_months)) {
$asset_warranty_months = NULL;
}
// Check for the asset model match and create it if it doesn't exist // Check for the asset model match and create it if it doesn't exist
$asset_model = $this->createOrFetchAssetModel($row, $item["category"], $item["manufacturer"]); $asset_model = $this->createOrFetchAssetModel($row, $item["category"], $item["manufacturer"]);
$supplier = $this->createOrFetchSupplier($row); $supplier = $this->createOrFetchSupplier($row);
$this->comment('Serial No: '.$asset_serial); $this->current_assetId = $asset_tag;
$this->comment('Asset Tag: '.$asset_tag);
$this->comment('Notes: '.$item["notes"]); $this->log('Serial No: '.$asset_serial);
$this->log('Asset Tag: '.$asset_tag);
$this->log('Notes: '.$item["notes"]);
foreach ($this->assets as $tempasset) { foreach ($this->assets as $tempasset) {
if ($tempasset->asset_tag === $asset_tag ) { if ($tempasset->asset_tag === $asset_tag ) {
$this->comment('A matching Asset ' . $asset_tag . ' already exists'); $this->log('A matching Asset ' . $asset_tag . ' already exists');
// $this->comment('A matching Asset ' . $asset_tag . ' already exists');
return; return;
} }
} }
if($item["status_label"]) {
$status_id = $item["status_label"]->id;
} else {
$this->log("No status field found, defaulting to id 1.");
$status_id = 1;
}
$asset = new Asset(); $asset = new Asset();
$asset->name = $item["item_name"]; $asset->name = $item["item_name"];
if ($item["purchase_date"] != '') { if ($item["purchase_date"] != '') {
@ -545,32 +660,40 @@ class ObjectImportCommand extends Command {
$asset->purchase_date = NULL; $asset->purchase_date = NULL;
} }
if (!empty($item_purchase_cost)) { if (!empty($item["purchase_cost"])) {
$asset->purchase_cost = number_format($item["purchase_cost"],2); //TODO How to generalize this for not USD?
$this->comment("Asset cost parsed: " . $asset->purchase_cost); $purchase_cost = substr($item["purchase_cost"],0,1) === '$' ? substr($item["purchase_cost"],1) : $item["purchase_cost"];
$asset->purchase_cost = number_format($purchase_cost,2);
$this->log("Asset cost parsed: " . $asset->purchase_cost);
} else { } else {
$asset->purchase_cost = 0.00; $asset->purchase_cost = 0.00;
} }
$asset->serial = $asset_serial; $asset->serial = $asset_serial;
$asset->asset_tag = $asset_tag; $asset->asset_tag = $asset_tag;
$asset->model_id = $asset_model->id;
$asset->assigned_to = $item["user"]->id; if($asset_model)
$asset->rtd_location_id = $item["location"]->id; $asset->model_id = $asset_model->id;
if($item["user"])
$asset->assigned_to = $item["user"]->id;
if($item["location"])
$asset->rtd_location_id = $item["location"]->id;
$asset->user_id = 1; $asset->user_id = 1;
$this->log("status_id: " . $status_id);
$asset->status_id = $status_id; $asset->status_id = $status_id;
$asset->company_id = $item["company"]->id; if($item["company"])
$asset->company_id = $item["company"]->id;
$asset->order_number = $item["order_number"]; $asset->order_number = $item["order_number"];
$asset->supplier_id = $supplier->id; if($supplier)
$asset->supplier_id = $supplier->id;
$asset->notes = $item["notes"]; $asset->notes = $item["notes"];
$asset->image = $asset_image; $asset->image = $asset_image;
$this->assets->add($asset); $this->assets->add($asset);
if (!$this->option('testrun')) { if (!$this->option('testrun')) {
if ($asset->save()) { if ($asset->save()) {
$this->comment('Asset ' . $item["item_name"] . ' with serial number ' . $asset_serial . ' was created'); $this->log('Asset ' . $item["item_name"] . ' with serial number ' . $asset_serial . ' was created');
} else { } else {
$this->comment('Something went wrong! Asset ' . $item["item_name"] . ' was NOT created'); $this->jsonError('Asset', $asset->getErrors());
// dd($asset);
} }
} else { } else {
@ -586,10 +709,10 @@ class ObjectImportCommand extends Command {
*/ */
public function createAccessoryIfNotExists(array $item ) public function createAccessoryIfNotExists(array $item )
{ {
$this->comment("Creating Accessory"); $this->log("Creating Accessory");
foreach ($this->accessories as $tempaccessory) { foreach ($this->accessories as $tempaccessory) {
if ($tempaccessory->name === $item["item_name"] ) { if ($tempaccessory->name === $item["item_name"] ) {
$this->comment('A matching Accessory ' . $item["item_name"] . ' already exists. '); $this->log('A matching Accessory ' . $item["item_name"] . ' already exists. ');
// FUTURE: Adjust quantity on import maybe? // FUTURE: Adjust quantity on import maybe?
return; return;
} }
@ -597,6 +720,7 @@ class ObjectImportCommand extends Command {
$accessory = new Accessory(); $accessory = new Accessory();
$accessory->name = $item["item_name"]; $accessory->name = $item["item_name"];
if (!empty($item["purchase_date"])) { if (!empty($item["purchase_date"])) {
$accessory->purchase_date = $item["purchase_date"]; $accessory->purchase_date = $item["purchase_date"];
} else { } else {
@ -607,11 +731,14 @@ class ObjectImportCommand extends Command {
} else { } else {
$accessory->purchase_cost = 0.00; $accessory->purchase_cost = 0.00;
} }
$accessory->location_id = $item["location"]->id; if($item["location"])
$accessory->location_id = $item["location"]->id;
$accessory->user_id = 1; $accessory->user_id = 1;
$accessory->company_id = $item["company"]->id; if($item["company"])
$accessory->company_id = $item["company"]->id;
$accessory->order_number = $item["order_number"]; $accessory->order_number = $item["order_number"];
$accessory->category_id = $item["category"]->id; if($item["category"])
$accessory->category_id = $item["category"]->id;
//TODO: Implement //TODO: Implement
// $accessory->notes = e($item_notes); // $accessory->notes = e($item_notes);
@ -626,12 +753,14 @@ class ObjectImportCommand extends Command {
if (!$this->option('testrun')) { if (!$this->option('testrun')) {
if ($accessory->save()) { if ($accessory->save()) {
$this->comment('Accessory ' . $item["item_name"] . ' was created'); $this->log('Accessory ' . $item["item_name"] . ' was created');
// $this->comment('Accessory ' . $item["item_name"] . ' was created');
} else { } else {
$this->comment('Something went wrong! Accessory ' . $item["item_name"] . ' was NOT created'); $this->jsonError('Accessory', $accessory->getErrors()) ;
} }
} else { } else {
$this->comment('TEST RUN - Accessory ' . $item["item_name"] . ' not created'); $this->log('TEST RUN - Accessory ' . $item["item_name"] . ' not created');
} }
} }
@ -643,10 +772,10 @@ class ObjectImportCommand extends Command {
*/ */
public function createConsumableIfNotExists(array $item) public function createConsumableIfNotExists(array $item)
{ {
$this->comment("Creating Consumable"); $this->log("Creating Consumable");
foreach($this->consumables as $tempconsumable) { foreach($this->consumables as $tempconsumable) {
if($tempconsumable->name === $item["item_name"]) { if($tempconsumable->name === $item["item_name"]) {
$this->comment("A matching sumable " . $item["item_name"] . " already exists"); $this->log("A matching consumable " . $item["item_name"] . " already exists");
//TODO: Adjust quantity if different maybe? //TODO: Adjust quantity if different maybe?
return; return;
} }
@ -683,12 +812,14 @@ class ObjectImportCommand extends Command {
if(!$this->option("testrun")) { if(!$this->option("testrun")) {
if($consumable->save()) { if($consumable->save()) {
$this->comment("Consumable " . $item["item_name"] . ' was created'); $this->log("Consumable " . $item["item_name"] . ' was created');
// $this->comment("Consumable " . $item["item_name"] . ' was created');
} else { } else {
$this->comment('Something went wrong! Consumable ' . $item["item_name"] . ' not created'); $this->jsonError('Consumable', $consumable->getErrors());
} }
} else { } else {
$this->comment('TEST RUN - Consumable ' . $item['item_name'] . ' not created'); $this->log('TEST RUN - Consumable ' . $item['item_name'] . ' not created');
} }
} }
@ -716,6 +847,9 @@ class ObjectImportCommand extends Command {
array('email_format', null, InputOption::VALUE_REQUIRED, 'The format of the email addresses that should be generated. Options are firstname.lastname, firstname, filastname', null), array('email_format', null, InputOption::VALUE_REQUIRED, 'The format of the email addresses that should be generated. Options are firstname.lastname, firstname, filastname', null),
array('username_format', null, InputOption::VALUE_REQUIRED, 'The format of the username that should be generated. Options are firstname.lastname, firstname, filastname, email', null), array('username_format', null, InputOption::VALUE_REQUIRED, 'The format of the username that should be generated. Options are firstname.lastname, firstname, filastname, email', null),
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('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')
); );
} }

View file

@ -744,6 +744,9 @@ class AssetsController extends Controller
return redirect()->to('hardware')->with('error', trans('general.insufficient_permissions')); return redirect()->to('hardware')->with('error', trans('general.insufficient_permissions'));
} }
// Check if the uploads directory exists. If not, try to create it.
if(!file_exists($path))
mkdir($path, 0755);
if ($handle = opendir($path)) { if ($handle = opendir($path)) {
/* This is the correct way to loop over the directory. */ /* This is the correct way to loop over the directory. */
@ -840,14 +843,20 @@ class AssetsController extends Controller
return redirect()->to('hardware')->with('error', trans('general.insufficient_permissions')); return redirect()->to('hardware')->with('error', trans('general.insufficient_permissions'));
} }
$output = new BufferedOutput; $return = Artisan::call('snipeit:import',
Artisan::call('asset-import:csv', ['filename'=> config('app.private_uploads').'/imports/assets/'.$filename, '--email_format'=>'firstname.lastname', '--username_format'=>'firstname.lastname'], $output); ['filename'=> config('app.private_uploads').'/imports/assets/'.$filename,
$display_output = $output->fetch(); '--email_format'=>'firstname.lastname',
'--username_format'=>'firstname.lastname',
'--web-importer' => true
]);
$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
return redirect()->to('hardware')->with('success', trans('admin/hardware/message.import.success'));
return redirect()->to('hardware')->with('success', 'Your file has been imported'); else if( $return === 1) // Failure
return redirect()->back()->with('import_errors', json_decode($display_output))->with('error', trans('admin/hardware/message.import.error'));
dd("Shouldn't be here");
} }

View file

@ -36,6 +36,12 @@ return array(
'invalidfiles' => 'One or more of your files is too large or is a filetype that is not allowed. Allowed filetypes are png, gif, jpg, doc, docx, pdf, and txt.', 'invalidfiles' => 'One or more of your files is too large or is a filetype that is not allowed. Allowed filetypes are png, gif, jpg, doc, docx, pdf, and txt.',
), ),
'import' => array(
'error' => 'Some Items did not import Correctly.',
'errorDetail' => 'The Following Items were not imported because of errors.',
'success' => "Your File has been imported",
),
'delete' => array( 'delete' => array(
'confirm' => 'Are you sure you wish to delete this asset?', 'confirm' => 'Are you sure you wish to delete this asset?',

View file

@ -66,7 +66,38 @@
</div> </div>
</div> </div>
</div> </div>
</div> </div>
@if (session()->has('import_errors'))
<div class="errors-table">
<div class="alert alert-warning">
<strong>Warning</strong> {{trans('admin/hardware/message.import.errorDetail')}}
</div>
<table class="table table-striped" id="errors-table">
<thead>
<th>Asset</th>
<th colspan="1">Field</th>
<th colspan ="1">Parameter</th>
<th colspan ="1">Errors</th>
</thead>
<tbody>
@foreach (session('import_errors') as $asset => $error)
<tr>
<td> {{ $asset }}</td>
@foreach ($error as $field => $values )
<td> {{ $field }} </td>
@foreach( $values as $fieldName=>$errorString)
<td>{{$fieldName}}</td>
<td>{{$errorString[0]}}</td>
@endforeach
@endforeach
</tr>
@endforeach
</tbody>
</table>
</div>
@endif
</div> </div>
</div> </div>
@section('moar_scripts') @section('moar_scripts')