2016-12-26 15:16:42 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Importer;
|
|
|
|
|
|
|
|
use App\Models\Asset;
|
2017-01-25 21:29:23 -08:00
|
|
|
use App\Models\Statuslabel;
|
2023-10-25 15:31:23 -07:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Events\CheckoutableCheckedIn;
|
2024-07-19 14:50:24 -07:00
|
|
|
use Illuminate\Support\Facades\Crypt;
|
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);
|
2023-06-22 12:53:58 -07:00
|
|
|
|
2024-06-20 07:05:38 -07:00
|
|
|
$this->defaultStatusLabelId = Statuslabel::first()->id;
|
2024-06-20 06:47:46 -07:00
|
|
|
|
|
|
|
if (!is_null(Statuslabel::deployable()->first())) {
|
2024-05-30 16:13:49 -07:00
|
|
|
$this->defaultStatusLabelId = Statuslabel::deployable()->first()->id;
|
2023-06-22 12:53:58 -07:00
|
|
|
}
|
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);
|
2019-09-30 19:21:57 -07:00
|
|
|
|
2017-10-16 15:29:19 -07:00
|
|
|
if ($customFieldValue) {
|
2019-09-30 19:21:57 -07:00
|
|
|
if ($customField->field_encrypted == 1) {
|
2024-05-29 04:38:15 -07:00
|
|
|
$this->item['custom_fields'][$customField->db_column_name()] = Crypt::encrypt($customFieldValue);
|
|
|
|
$this->log('Custom Field '.$customField->name.': '.Crypt::encrypt($customFieldValue));
|
2019-09-30 19:21:57 -07:00
|
|
|
} else {
|
|
|
|
$this->item['custom_fields'][$customField->db_column_name()] = $customFieldValue;
|
2021-06-10 13:15:52 -07:00
|
|
|
$this->log('Custom Field '.$customField->name.': '.$customFieldValue);
|
2019-09-30 19:21:57 -07:00
|
|
|
}
|
2017-10-25 18:16:28 -07:00
|
|
|
} else {
|
2018-03-27 16:52:44 -07:00
|
|
|
// Clear out previous data.
|
|
|
|
$this->item['custom_fields'][$customField->db_column_name()] = null;
|
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;
|
2021-06-10 13:15:52 -07:00
|
|
|
$asset_tag = $this->findCsvMatch($row, 'asset_tag');
|
2022-08-03 10:48:48 -07:00
|
|
|
|
2024-05-30 16:16:37 -07:00
|
|
|
if (empty($asset_tag)){
|
2022-08-03 10:48:48 -07:00
|
|
|
$asset_tag = Asset::autoincrement_asset();
|
|
|
|
}
|
|
|
|
|
2023-03-29 11:28:04 -07:00
|
|
|
$asset = Asset::where(['asset_tag'=> (string) $asset_tag])->first();
|
2017-01-25 21:29:23 -08:00
|
|
|
if ($asset) {
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! $this->updating) {
|
2024-08-16 14:53:27 -07:00
|
|
|
$exists_error = trans('general.import_asset_tag_exists', ['asset_tag' => $asset_tag]);
|
2024-08-16 12:29:53 -07:00
|
|
|
$this->log($exists_error);
|
|
|
|
$this->addErrorToBag($asset, 'asset_tag', $exists_error);
|
|
|
|
return $exists_error;
|
2016-12-26 15:16:42 -08:00
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
$this->log('Updating Asset');
|
2017-01-10 16:19:18 -08:00
|
|
|
$editingAsset = true;
|
|
|
|
} else {
|
2021-06-10 13:15:52 -07: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
|
|
|
}
|
2024-05-30 16:16:37 -07:00
|
|
|
|
|
|
|
// If no status ID is found
|
|
|
|
if (! array_key_exists('status_id', $this->item) && ! $editingAsset) {
|
|
|
|
$this->log('No status ID field found, defaulting to first deployable status label.');
|
|
|
|
$this->item['status_id'] = $this->defaultStatusLabelId;
|
|
|
|
}
|
|
|
|
|
2023-10-10 03:52:35 -07:00
|
|
|
$this->item['notes'] = trim($this->findCsvMatch($row, 'asset_notes'));
|
|
|
|
$this->item['image'] = trim($this->findCsvMatch($row, 'image'));
|
|
|
|
$this->item['requestable'] = trim(($this->fetchHumanBoolean($this->findCsvMatch($row, 'requestable'))) == 1) ? '1' : 0;
|
2023-03-06 17:11:08 -08:00
|
|
|
$asset->requestable = $this->item['requestable'];
|
2023-10-10 03:52:35 -07:00
|
|
|
$this->item['warranty_months'] = intval(trim($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);
|
2023-10-10 03:52:35 -07:00
|
|
|
$this->item['byod'] = ($this->fetchHumanBoolean(trim($this->findCsvMatch($row, 'byod'))) == 1) ? '1' : 0;
|
2024-05-30 16:16:37 -07:00
|
|
|
$this->item['last_checkin'] = trim($this->findCsvMatch($row, 'last_checkin'));
|
2024-05-30 18:14:40 -07:00
|
|
|
$this->item['last_checkout'] = trim($this->findCsvMatch($row, 'last_checkout'));
|
2024-05-30 16:16:37 -07:00
|
|
|
$this->item['expected_checkin'] = trim($this->findCsvMatch($row, 'expected_checkin'));
|
|
|
|
$this->item['last_audit_date'] = trim($this->findCsvMatch($row, 'last_audit_date'));
|
|
|
|
$this->item['next_audit_date'] = trim($this->findCsvMatch($row, 'next_audit_date'));
|
2024-06-19 03:05:22 -07:00
|
|
|
$this->item['asset_eol_date'] = trim($this->findCsvMatch($row, 'asset_eol_date'));
|
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.
|
2021-06-10 13:15:52 -07:00
|
|
|
if (array_key_exists('checkout_target', $this->item)) {
|
2018-07-05 12:22:24 -07:00
|
|
|
$target = $this->item['checkout_target'];
|
2024-05-30 18:14:40 -07:00
|
|
|
}
|
2024-05-30 16:16:37 -07:00
|
|
|
|
2017-01-10 16:19:18 -08:00
|
|
|
$item = $this->sanitizeItemForStoring($asset, $editingAsset);
|
2024-05-30 16:16:37 -07:00
|
|
|
|
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.
|
2021-06-10 13:15:52 -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
|
|
|
|
2024-05-30 18:14:40 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* We use this to backdate the checkin action further down
|
|
|
|
*/
|
2024-05-30 16:16:37 -07:00
|
|
|
$checkin_date = date('Y-m-d H:i:s');
|
|
|
|
if ($this->item['last_checkin']!='') {
|
2024-05-30 18:14:40 -07:00
|
|
|
$item['last_checkin'] = $this->parseOrNullDate('last_checkin', 'datetime');
|
|
|
|
$checkout_date = $this->item['last_checkin'];
|
2024-05-30 16:16:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We use this to backdate the checkout action further down
|
|
|
|
*/
|
|
|
|
$checkout_date = date('Y-m-d H:i:s');
|
|
|
|
if ($this->item['last_checkout']!='') {
|
2024-05-30 18:14:40 -07:00
|
|
|
$item['last_checkout'] = $this->parseOrNullDate('last_checkout', 'datetime');
|
|
|
|
$checkout_date = $this->item['last_checkout'];
|
2024-05-30 16:16:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->item['expected_checkin']!='') {
|
2024-05-30 18:14:40 -07:00
|
|
|
$item['expected_checkin'] = $this->parseOrNullDate('expected_checkin');
|
2024-05-30 16:16:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->item['last_audit_date']!='') {
|
2024-05-30 18:14:40 -07:00
|
|
|
$item['last_audit_date'] = $this->parseOrNullDate('last_audit_date');
|
2022-01-13 13:10:27 -08:00
|
|
|
}
|
|
|
|
|
2024-05-30 16:16:37 -07:00
|
|
|
if ($this->item['next_audit_date']!='') {
|
2024-05-30 18:14:40 -07:00
|
|
|
$item['next_audit_date'] = $this->parseOrNullDate('next_audit_date');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->item['asset_eol_date']!='') {
|
|
|
|
$item['asset_eol_date'] = $this->parseOrNullDate('asset_eol_date');
|
2022-01-13 13:10:27 -08:00
|
|
|
}
|
2024-05-30 16:16:37 -07: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;
|
|
|
|
}
|
|
|
|
}
|
2024-05-30 16:16:37 -07:00
|
|
|
|
2024-01-25 17:54:53 -08:00
|
|
|
// This sets an attribute on the Loggable trait for the action log
|
2024-01-25 17:34:41 -08:00
|
|
|
$asset->setImported(true);
|
2024-05-30 16:16:37 -07:00
|
|
|
|
2017-07-11 20:37:02 -07:00
|
|
|
if ($asset->save()) {
|
2023-07-17 12:32:03 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
$this->log('Asset '.$this->item['name'].' with serial number '.$this->item['serial'].' was created');
|
2017-12-28 20:08:45 -08:00
|
|
|
|
2018-07-05 12:22:24 -07:00
|
|
|
// If we have a target to checkout to, lets do so.
|
2024-09-19 08:20:42 -07:00
|
|
|
//-- created_by is a property of the abstract class Importer, which this class inherits from and it's set by
|
2021-04-05 19:28:31 -07:00
|
|
|
//-- the class that needs to use it (command importer or GUI importer inside the project).
|
2023-11-08 13:32:33 -08:00
|
|
|
if (isset($target) && ($target !== false)) {
|
2023-10-25 15:41:31 -07:00
|
|
|
if (!is_null($asset->assigned_to)){
|
2024-05-30 16:16:37 -07:00
|
|
|
if ($asset->assigned_to != $target->id) {
|
2024-07-04 12:49:22 -07:00
|
|
|
event(new CheckoutableCheckedIn($asset, User::find($asset->assigned_to), auth()->user(), 'Checkin from CSV Importer', $checkin_date));
|
2023-10-25 15:41:31 -07:00
|
|
|
}
|
2023-10-25 15:31:23 -07:00
|
|
|
}
|
|
|
|
|
2024-09-17 14:16:41 -07:00
|
|
|
$asset->fresh()->checkOut($target, $this->created_by, $checkout_date, null, 'Checkout from CSV Importer', $asset->name);
|
2017-12-28 20:08:45 -08:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-07-11 20:37:02 -07:00
|
|
|
return;
|
2016-12-26 15:16:42 -08:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
$this->logError($asset, 'Asset "'.$this->item['name'].'"');
|
2016-12-26 15:16:42 -08:00
|
|
|
}
|
2024-05-30 18:14:40 -07:00
|
|
|
|
|
|
|
|
2016-12-29 14:02:18 -08:00
|
|
|
}
|