2017-01-10 16:19:18 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Importer;
|
|
|
|
|
|
|
|
use App\Models\Asset;
|
|
|
|
use App\Models\License;
|
2020-05-12 11:32:55 -07:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2017-01-10 16:19:18 -08:00
|
|
|
|
|
|
|
class LicenseImporter extends ItemImporter
|
|
|
|
{
|
|
|
|
public function __construct($filename)
|
|
|
|
{
|
|
|
|
parent::__construct($filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function handle($row)
|
|
|
|
{
|
|
|
|
// ItemImporter handles the general fetching.
|
|
|
|
parent::handle($row);
|
|
|
|
$this->createLicenseIfNotExists($row);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the license if it does not exist.
|
|
|
|
*
|
|
|
|
* @author Daniel Melzter
|
|
|
|
* @since 4.0
|
|
|
|
* @param array $row
|
|
|
|
* @return License|mixed|null
|
|
|
|
*/
|
|
|
|
public function createLicenseIfNotExists(array $row)
|
|
|
|
{
|
|
|
|
$editingLicense = false;
|
2017-10-03 08:46:06 -07:00
|
|
|
$license = License::where('name', $this->item['name'])
|
|
|
|
->first();
|
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 ($license) {
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! $this->updating) {
|
|
|
|
$this->log('A matching License '.$this->item['name'].'with serial '.$this->item['serial'].' already exists');
|
|
|
|
|
2017-01-10 16:19:18 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
$this->log('Updating License');
|
2017-01-10 16:19:18 -08:00
|
|
|
$editingLicense = true;
|
|
|
|
} else {
|
2021-06-10 13:15:52 -07:00
|
|
|
$this->log('No Matching License, Creating a new one');
|
2017-07-11 20:37:02 -07:00
|
|
|
$license = new License;
|
2017-01-10 16:19:18 -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
|
|
|
$asset_tag = $this->item['asset_tag'] = $this->findCsvMatch($row, 'asset_tag'); // used for checkout out to an asset.
|
|
|
|
$this->item['expiration_date'] = $this->findCsvMatch($row, 'expiration_date');
|
2021-06-10 13:15:52 -07:00
|
|
|
$this->item['license_email'] = $this->findCsvMatch($row, 'license_email');
|
|
|
|
$this->item['license_name'] = $this->findCsvMatch($row, 'license_name');
|
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['maintained'] = $this->findCsvMatch($row, 'maintained');
|
|
|
|
$this->item['purchase_order'] = $this->findCsvMatch($row, 'purchase_order');
|
|
|
|
$this->item['reassignable'] = $this->findCsvMatch($row, 'reassignable');
|
|
|
|
$this->item['seats'] = $this->findCsvMatch($row, 'seats');
|
|
|
|
$this->item['termination_date'] = $this->findCsvMatch($row, 'termination_date');
|
2017-01-10 16:19:18 -08:00
|
|
|
|
|
|
|
if ($editingLicense) {
|
|
|
|
$license->update($this->sanitizeItemForUpdating($license));
|
|
|
|
} else {
|
|
|
|
$license->fill($this->sanitizeItemForStoring($license));
|
|
|
|
}
|
2018-01-25 10:10:56 -08:00
|
|
|
//FIXME: this disables model validation. Need to find a way to avoid double-logs without breaking everything.
|
|
|
|
// $license->unsetEventDispatcher();
|
2017-07-11 20:37:02 -07:00
|
|
|
if ($license->save()) {
|
|
|
|
$license->logCreate('Imported using csv importer');
|
2021-06-10 13:15:52 -07:00
|
|
|
$this->log('License '.$this->item['name'].' with serial number '.$this->item['serial'].' was created');
|
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
|
|
|
|
2017-07-11 20:37:02 -07:00
|
|
|
// Lets try to checkout seats if the fields exist and we have seats.
|
|
|
|
if ($license->seats > 0) {
|
2018-07-05 12:22:24 -07:00
|
|
|
$checkout_target = $this->item['checkout_target'];
|
2017-07-11 20:37:02 -07:00
|
|
|
$asset = Asset::where('asset_tag', $asset_tag)->first();
|
2020-05-12 11:32:55 -07:00
|
|
|
$targetLicense = $license->freeSeat();
|
2018-07-05 12:22:24 -07:00
|
|
|
if ($checkout_target) {
|
|
|
|
$targetLicense->assigned_to = $checkout_target->id;
|
2020-05-12 11:32:55 -07:00
|
|
|
$targetLicense->user_id = Auth::id();
|
2017-07-11 20:37:02 -07:00
|
|
|
if ($asset) {
|
2017-01-10 16:19:18 -08:00
|
|
|
$targetLicense->asset_id = $asset->id;
|
|
|
|
}
|
2017-07-11 20:37:02 -07:00
|
|
|
$targetLicense->save();
|
|
|
|
} elseif ($asset) {
|
2020-05-12 11:32:55 -07:00
|
|
|
$targetLicense->user_id = Auth::id();
|
2017-07-11 20:37:02 -07:00
|
|
|
$targetLicense->asset_id = $asset->id;
|
|
|
|
$targetLicense->save();
|
2017-01-10 16:19:18 -08:00
|
|
|
}
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-07-11 20:37:02 -07:00
|
|
|
return;
|
2017-01-10 16:19:18 -08:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
$this->logError($license, 'License "'.$this->item['name'].'"');
|
2017-01-10 16:19:18 -08:00
|
|
|
}
|
|
|
|
}
|