2016-12-26 15:16:42 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Importer;
|
|
|
|
|
|
|
|
use App\Helpers\Helper;
|
|
|
|
use App\Models\Asset;
|
|
|
|
use App\Models\Category;
|
|
|
|
use App\Models\Manufacturer;
|
2017-01-25 21:29:23 -08:00
|
|
|
use App\Models\Statuslabel;
|
2016-12-26 15:16:42 -08:00
|
|
|
|
|
|
|
class AssetImporter extends ItemImporter
|
|
|
|
{
|
2017-01-25 21:29:23 -08:00
|
|
|
protected $defaultStatusLabelId;
|
|
|
|
|
2017-01-10 16:19:18 -08:00
|
|
|
public function __construct($filename)
|
2016-12-26 15:16:42 -08:00
|
|
|
{
|
2017-01-10 16:19:18 -08:00
|
|
|
parent::__construct($filename);
|
2017-01-25 21:29:23 -08:00
|
|
|
$this->defaultStatusLabelId = Statuslabel::first()->id;
|
2016-12-26 15:16:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function handle($row)
|
|
|
|
{
|
|
|
|
// ItemImporter handles the general fetching.
|
|
|
|
parent::handle($row);
|
|
|
|
|
2017-10-16 15:29:19 -07:00
|
|
|
if ($this->customFields) {
|
|
|
|
|
|
|
|
foreach ($this->customFields as $customField) {
|
|
|
|
$customFieldValue = $this->array_smart_custom_field_fetch($row, $customField);
|
|
|
|
if ($customFieldValue) {
|
|
|
|
$this->item['custom_fields'][$customField->db_column_name()] = $customFieldValue;
|
|
|
|
$this->log('Custom Field '. $customField->name.': '.$customFieldValue);
|
2017-10-25 18:16:28 -07:00
|
|
|
} else {
|
2017-11-24 10:42:11 -08:00
|
|
|
// This removes custom fields when updating if the column doesn't exist in file.
|
|
|
|
// Commented out becausee not sure if it's needed anywhere.
|
|
|
|
// $this->item['custom_fields'][$customField->db_column_name()] = '';
|
2017-10-16 15:29:19 -07:00
|
|
|
}
|
2016-12-26 15:16:42 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-16 15:29:19 -07:00
|
|
|
|
2016-12-26 15:16:42 -08:00
|
|
|
$this->createAssetIfNotExists($row);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the asset if it does not exist.
|
|
|
|
*
|
|
|
|
* @author Daniel Melzter
|
|
|
|
* @since 3.0
|
|
|
|
* @param array $row
|
|
|
|
* @return Asset|mixed|null
|
|
|
|
*/
|
|
|
|
public function createAssetIfNotExists(array $row)
|
|
|
|
{
|
|
|
|
$editingAsset = false;
|
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
|
|
|
$asset_tag = $this->findCsvMatch($row, "asset_tag");
|
|
|
|
$asset = Asset::where(['asset_tag'=> $asset_tag])->first();
|
2017-01-25 21:29:23 -08:00
|
|
|
if ($asset) {
|
2016-12-26 15:16:42 -08:00
|
|
|
if (!$this->updating) {
|
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
|
|
|
$this->log('A matching Asset ' . $asset_tag . ' already exists');
|
2017-01-10 16:19:18 -08:00
|
|
|
return;
|
2016-12-26 15:16:42 -08:00
|
|
|
}
|
|
|
|
|
2017-01-10 16:19:18 -08:00
|
|
|
$this->log("Updating Asset");
|
|
|
|
$editingAsset = true;
|
|
|
|
} else {
|
2016-12-26 15:16:42 -08:00
|
|
|
$this->log("No Matching Asset, Creating a new one");
|
2017-01-25 21:29:23 -08:00
|
|
|
$asset = new Asset;
|
2016-12-26 15:16:42 -08:00
|
|
|
}
|
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
|
|
|
|
|
|
|
$this->item['image'] = $this->findCsvMatch($row, "image");
|
2017-10-26 16:13:35 -07:00
|
|
|
$this->item['warranty_months'] = intval($this->findCsvMatch($row, "warranty_months"));
|
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
|
|
|
$this->item['model_id'] = $this->createOrFetchAssetModel($row);
|
2017-09-08 17:24:28 -07:00
|
|
|
|
|
|
|
// If no status ID is found
|
|
|
|
if (!array_key_exists('status_id', $this->item) && !$editingAsset) {
|
2017-01-25 21:29:23 -08:00
|
|
|
$this->log("No status field found, defaulting to first status.");
|
|
|
|
$this->item['status_id'] = $this->defaultStatusLabelId;
|
2016-12-26 15:16:42 -08:00
|
|
|
}
|
|
|
|
|
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
|
|
|
$this->item['asset_tag'] = $asset_tag;
|
2017-12-28 20:08:45 -08:00
|
|
|
|
|
|
|
// We need to save the user if it exists so that we can checkout to user later.
|
|
|
|
// Sanitizing the item will remove it.
|
|
|
|
if(array_key_exists('user', $this->item)) {
|
|
|
|
$user = $this->item['user'];
|
|
|
|
}
|
2017-01-10 16:19:18 -08:00
|
|
|
$item = $this->sanitizeItemForStoring($asset, $editingAsset);
|
2017-12-28 20:08:45 -08:00
|
|
|
// The location id fetched by the csv reader is actually the rtd_location_id.
|
|
|
|
// This will also set location_id, but then that will be overridden by the
|
|
|
|
// checkout method if necessary below.
|
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
|
|
|
if (isset($this->item["location_id"])) {
|
2017-01-10 16:19:18 -08:00
|
|
|
$item['rtd_location_id'] = $this->item['location_id'];
|
2016-12-26 15:16:42 -08:00
|
|
|
}
|
2017-11-24 10:42:11 -08:00
|
|
|
|
2017-12-28 20:08:45 -08:00
|
|
|
|
2017-01-10 16:19:18 -08:00
|
|
|
if ($editingAsset) {
|
|
|
|
$asset->update($item);
|
|
|
|
} else {
|
|
|
|
$asset->fill($item);
|
2016-12-26 15:16:42 -08:00
|
|
|
}
|
2017-11-24 10:42:11 -08:00
|
|
|
|
2017-12-28 20:08:45 -08:00
|
|
|
// If we're updating, we don't want to overwrite old fields.
|
2016-12-26 15:16:42 -08:00
|
|
|
if (array_key_exists('custom_fields', $this->item)) {
|
|
|
|
foreach ($this->item['custom_fields'] as $custom_field => $val) {
|
|
|
|
$asset->{$custom_field} = $val;
|
|
|
|
}
|
|
|
|
}
|
2018-01-25 10:29:06 -08:00
|
|
|
//FIXME: this disables model validation. Need to find a way to avoid double-logs without breaking everything.
|
|
|
|
// $asset->unsetEventDispatcher();
|
2017-07-11 20:37:02 -07:00
|
|
|
if ($asset->save()) {
|
|
|
|
$asset->logCreate('Imported using csv importer');
|
|
|
|
$this->log('Asset ' . $this->item["name"] . ' with serial number ' . $this->item['serial'] . ' was created');
|
2017-12-28 20:08:45 -08:00
|
|
|
|
|
|
|
// If we have a user to checkout to, lets do so.
|
|
|
|
if(isset($user)) {
|
|
|
|
$asset->fresh()->checkOut($user);
|
|
|
|
}
|
2017-07-11 20:37:02 -07:00
|
|
|
return;
|
2016-12-26 15:16:42 -08:00
|
|
|
}
|
2017-07-11 20:37:02 -07:00
|
|
|
$this->logError($asset, 'Asset "' . $this->item['name'].'"');
|
2016-12-26 15:16:42 -08:00
|
|
|
}
|
2016-12-29 14:02:18 -08:00
|
|
|
}
|