From 30d68309a907ccf37ca596d974eecc5f6bd3c597 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 10 Nov 2020 05:06:47 -0800 Subject: [PATCH 1/2] =?UTF-8?q?Add=20ability=20to=20checkout=20an=20asset?= =?UTF-8?q?=20if=20the=20user=20it=E2=80=99s=20assigned=5Fto=20isn?= =?UTF-8?q?=E2=80=99t=20valid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This would only happen if a merge-users went wonky --- app/Models/Asset.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/app/Models/Asset.php b/app/Models/Asset.php index 9af62593d..1492f5a44 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -232,7 +232,10 @@ class Asset extends Depreciable } /** - * Determines if an asset is available for checkout + * Determines if an asset is available for checkout. + * This checks to see if the it's checked out to an invalid (deleted) user + * OR if the assigned_to and deleted_at fields on the asset are empty AND + * that the status is deployable * * @author [A. Gianotto] [] * @since [v3.0] @@ -241,9 +244,10 @@ class Asset extends Depreciable public function availableForCheckout() { if ( - (empty($this->assigned_to)) && + (!$this->assignedTo) || + ((empty($this->assigned_to)) && (empty($this->deleted_at)) && - (($this->assetstatus) && ($this->assetstatus->deployable == 1))) + (($this->assetstatus) && ($this->assetstatus->deployable == 1)))) { return true; } @@ -424,7 +428,7 @@ class Asset extends Depreciable */ public function assignedTo() { - return $this->morphTo('assigned', 'assigned_type', 'assigned_to'); + return $this->morphTo('assigned', 'assigned_type', 'assigned_to')->withTrashed(); } /** From e110a7b15e43be2bc8fca21174050a3edbb529ad Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 10 Nov 2020 06:16:15 -0800 Subject: [PATCH 2/2] Experimental script to find mismatched IDs based on logs --- .../Commands/FixMismatchedAssetsAndLogs.php | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 app/Console/Commands/FixMismatchedAssetsAndLogs.php diff --git a/app/Console/Commands/FixMismatchedAssetsAndLogs.php b/app/Console/Commands/FixMismatchedAssetsAndLogs.php new file mode 100644 index 000000000..07461e247 --- /dev/null +++ b/app/Console/Commands/FixMismatchedAssetsAndLogs.php @@ -0,0 +1,81 @@ +get(); + foreach ($assets as $asset) { + + // get the last checkout of the asset + if ($checkout_log = Actionlog::where('target_type', '=', 'App\\Models\\User') + ->where('action_type', '=', 'checkout') + ->where('item_id', '=', $asset->id) + ->orderBy('created_at', 'DESC') + ->first()) { + + // Now check for a subsequent checkin log - we want to ignore those + if (!$checkin_log = Actionlog::where('target_type', '=', 'App\\Models\\User') + ->where('action_type', '=', 'checkin from') + ->where('item_id', '=', $asset->id) + ->whereDate('created_at', '>', $checkout_log->created_at) + ->orderBy('created_at', 'DESC') + ->first()) { + + // $this->info($checkout_log->id.' is checked out to '.$checkout_log->target_type.' '.$checkout_log->target_id.' and there is no subsequent checkin log.'); + + + //print_r($asset); + if ($checkout_log->target_id != $asset->assigned_to) { + $this->error('Log ID: '.$checkout_log->id.' -- Asset ID '. $checkout_log->item_id.' SHOULD BE checked out to User '.$checkout_log->target_id.' but its assigned_to is '.$asset->assigned_to ); + $mismatch_count++; + } + } else { + $this->info('Asset ID '.$asset->id.': There is a checkin '.$checkin_log->created_at.' after this checkout '.$checkout_log->created_at); + + } + + } + + } + $this->info($mismatch_count.' mismatched assets.'); + + } +}