mirror of
https://github.com/snipe/snipe-it.git
synced 2025-01-12 06:17:28 -08:00
Added date parsers for new date fields
Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
parent
3a0f7eca54
commit
0ee2b74ff3
|
@ -66,16 +66,14 @@ class AssetImporter extends ItemImporter
|
||||||
$editingAsset = false;
|
$editingAsset = false;
|
||||||
$asset_tag = $this->findCsvMatch($row, 'asset_tag');
|
$asset_tag = $this->findCsvMatch($row, 'asset_tag');
|
||||||
|
|
||||||
if(empty($asset_tag)){
|
if (empty($asset_tag)){
|
||||||
$asset_tag = Asset::autoincrement_asset();
|
$asset_tag = Asset::autoincrement_asset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$asset = Asset::where(['asset_tag'=> (string) $asset_tag])->first();
|
$asset = Asset::where(['asset_tag'=> (string) $asset_tag])->first();
|
||||||
if ($asset) {
|
if ($asset) {
|
||||||
if (! $this->updating) {
|
if (! $this->updating) {
|
||||||
$this->log('A matching Asset '.$asset_tag.' already exists');
|
$this->log('A matching Asset '.$asset_tag.' already exists');
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,6 +83,13 @@ class AssetImporter extends ItemImporter
|
||||||
$this->log('No Matching Asset, Creating a new one');
|
$this->log('No Matching Asset, Creating a new one');
|
||||||
$asset = new Asset;
|
$asset = new Asset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
$this->item['notes'] = trim($this->findCsvMatch($row, 'asset_notes'));
|
$this->item['notes'] = trim($this->findCsvMatch($row, 'asset_notes'));
|
||||||
$this->item['image'] = trim($this->findCsvMatch($row, 'image'));
|
$this->item['image'] = trim($this->findCsvMatch($row, 'image'));
|
||||||
$this->item['requestable'] = trim(($this->fetchHumanBoolean($this->findCsvMatch($row, 'requestable'))) == 1) ? '1' : 0;
|
$this->item['requestable'] = trim(($this->fetchHumanBoolean($this->findCsvMatch($row, 'requestable'))) == 1) ? '1' : 0;
|
||||||
|
@ -92,13 +97,12 @@ class AssetImporter extends ItemImporter
|
||||||
$this->item['warranty_months'] = intval(trim($this->findCsvMatch($row, 'warranty_months')));
|
$this->item['warranty_months'] = intval(trim($this->findCsvMatch($row, 'warranty_months')));
|
||||||
$this->item['model_id'] = $this->createOrFetchAssetModel($row);
|
$this->item['model_id'] = $this->createOrFetchAssetModel($row);
|
||||||
$this->item['byod'] = ($this->fetchHumanBoolean(trim($this->findCsvMatch($row, 'byod'))) == 1) ? '1' : 0;
|
$this->item['byod'] = ($this->fetchHumanBoolean(trim($this->findCsvMatch($row, 'byod'))) == 1) ? '1' : 0;
|
||||||
|
$this->item['last_checkout'] = trim($this->findCsvMatch($row, 'last_checkout'));
|
||||||
|
$this->item['last_checkin'] = trim($this->findCsvMatch($row, 'last_checkin'));
|
||||||
// If no status ID is found
|
$this->item['expected_checkin'] = trim($this->findCsvMatch($row, 'expected_checkin'));
|
||||||
if (! array_key_exists('status_id', $this->item) && ! $editingAsset) {
|
$this->item['last_audit_date'] = trim($this->findCsvMatch($row, 'last_audit_date'));
|
||||||
$this->log('No status field found, defaulting to first status.');
|
$this->item['next_audit_date'] = trim($this->findCsvMatch($row, 'next_audit_date'));
|
||||||
$this->item['status_id'] = $this->defaultStatusLabelId;
|
$this->item['asset_eol_date'] = trim($this->findCsvMatch($row, 'asset_eol_date'));
|
||||||
}
|
|
||||||
|
|
||||||
$this->item['asset_tag'] = $asset_tag;
|
$this->item['asset_tag'] = $asset_tag;
|
||||||
|
|
||||||
|
@ -107,7 +111,9 @@ class AssetImporter extends ItemImporter
|
||||||
if (array_key_exists('checkout_target', $this->item)) {
|
if (array_key_exists('checkout_target', $this->item)) {
|
||||||
$target = $this->item['checkout_target'];
|
$target = $this->item['checkout_target'];
|
||||||
}
|
}
|
||||||
|
|
||||||
$item = $this->sanitizeItemForStoring($asset, $editingAsset);
|
$item = $this->sanitizeItemForStoring($asset, $editingAsset);
|
||||||
|
|
||||||
// The location id fetched by the csv reader is actually the rtd_location_id.
|
// 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
|
// This will also set location_id, but then that will be overridden by the
|
||||||
// checkout method if necessary below.
|
// checkout method if necessary below.
|
||||||
|
@ -115,15 +121,59 @@ class AssetImporter extends ItemImporter
|
||||||
$item['rtd_location_id'] = $this->item['location_id'];
|
$item['rtd_location_id'] = $this->item['location_id'];
|
||||||
}
|
}
|
||||||
|
|
||||||
$item['last_audit_date'] = null;
|
$checkin_date = date('Y-m-d H:i:s');
|
||||||
if (isset($this->item['last_audit_date'])) {
|
if ($this->item['last_checkin']!='') {
|
||||||
$item['last_audit_date'] = $this->item['last_audit_date'];
|
try {
|
||||||
|
$checkin_date = CarbonImmutable::parse($this->item['last_checkin'])->format('Y-m-d H:i:s');
|
||||||
|
$this->item['last_checkout'] = $checkin_date;
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::info($e->getMessage());
|
||||||
|
$this->log('Unable to parse date: '.$this->item['last_checkout']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$item['next_audit_date'] = null;
|
/**
|
||||||
if (isset($this->item['next_audit_date'])) {
|
* We use this to backdate the checkout action further down
|
||||||
$item['next_audit_date'] = $this->item['next_audit_date'];
|
*/
|
||||||
|
$checkout_date = date('Y-m-d H:i:s');
|
||||||
|
if ($this->item['last_checkout']!='') {
|
||||||
|
|
||||||
|
try {
|
||||||
|
$checkout_date = CarbonImmutable::parse($this->item['last_checkout'])->format('Y-m-d H:i:s');
|
||||||
|
$this->item['last_checkout'] = $checkout_date;
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::info($e->getMessage());
|
||||||
|
$this->log('Unable to parse date: '.$this->item['last_checkout']);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->item['expected_checkin']!='') {
|
||||||
|
try {
|
||||||
|
$this->item['expected_checkin'] = CarbonImmutable::parse($this->item['expected_checkin'])->format('Y-m-d');
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::info($e->getMessage());
|
||||||
|
$this->log('Unable to parse date: '.$this->item['expected_checkin']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->item['last_audit_date']!='') {
|
||||||
|
try {
|
||||||
|
$this->item['last_audit_date'] = CarbonImmutable::parse($this->item['last_audit_date'])->format('Y-m-d');
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::info($e->getMessage());
|
||||||
|
$this->log('Unable to parse date: '.$this->item['last_audit_date']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->item['next_audit_date']!='') {
|
||||||
|
try {
|
||||||
|
$this->item['next_audit_date'] = CarbonImmutable::parse($this->item['next_audit_date'])->format('Y-m-d');
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::info($e->getMessage());
|
||||||
|
$this->log('Unable to parse date: '.$this->item['next_audit_date']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if ($editingAsset) {
|
if ($editingAsset) {
|
||||||
$asset->update($item);
|
$asset->update($item);
|
||||||
|
@ -137,23 +187,27 @@ class AssetImporter extends ItemImporter
|
||||||
$asset->{$custom_field} = $val;
|
$asset->{$custom_field} = $val;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This sets an attribute on the Loggable trait for the action log
|
// This sets an attribute on the Loggable trait for the action log
|
||||||
$asset->setImported(true);
|
$asset->setImported(true);
|
||||||
|
|
||||||
if ($asset->save()) {
|
if ($asset->save()) {
|
||||||
|
|
||||||
$this->log('Asset '.$this->item['name'].' with serial number '.$this->item['serial'].' was created');
|
$this->log('Asset '.$this->item['name'].' with serial number '.$this->item['serial'].' was created');
|
||||||
|
|
||||||
// If we have a target to checkout to, lets do so.
|
// If we have a target to checkout to, lets do so.
|
||||||
//-- user_id is a property of the abstract class Importer, which this class inherits from and it's setted by
|
//-- user_id is a property of the abstract class Importer, which this class inherits from and it's set by
|
||||||
//-- the class that needs to use it (command importer or GUI importer inside the project).
|
//-- the class that needs to use it (command importer or GUI importer inside the project).
|
||||||
if (isset($target) && ($target !== false)) {
|
if (isset($target) && ($target !== false)) {
|
||||||
if (!is_null($asset->assigned_to)){
|
if (!is_null($asset->assigned_to)){
|
||||||
if ($asset->assigned_to != $target->id){
|
if ($asset->assigned_to != $target->id) {
|
||||||
event(new CheckoutableCheckedIn($asset, User::find($asset->assigned_to), Auth::user(), $asset->notes, date('Y-m-d H:i:s')));
|
event(new CheckoutableCheckedIn($asset, User::find($asset->assigned_to), Auth::user(), 'Checkin from CSV Importer', $checkin_date));
|
||||||
|
$this->log('Checking this asset in');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$asset->fresh()->checkOut($target, $this->user_id, date('Y-m-d H:i:s'), null, $asset->notes, $asset->name);
|
$asset->fresh()->checkOut($target, $this->user_id, $checkout_date, null, 'Checkout from CSV Importer', $asset->name);
|
||||||
|
$this->log('Checking this asset out');
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Reference in a new issue