Skip to content

A free open source IT asset/license management system

Added routes and logging

snipef17f34f7301c
diff --git a/app/Enums/ActionType.php b/app/Enums/ActionType.php
index c6d53306db..2b8b66cc90 100644
--- a/app/Enums/ActionType.php
+++ b/app/Enums/ActionType.php
@@ -13,6 +13,7 @@ enum ActionType: string
// Assets/Accessories/Components/Licenses/Consumables
case Checkout = 'checkout';
case CheckinFrom = 'checkin from';
+ case ForceCheckin = 'force checkin';
case Requested = 'requested';
case RequestCanceled = 'request canceled';
case Accepted = 'accepted';
diff --git a/app/Http/Controllers/Assets/AssetCheckinController.php b/app/Http/Controllers/Assets/AssetCheckinController.php
index f34fd46217..a24204fa0c 100644
--- a/app/Http/Controllers/Assets/AssetCheckinController.php
+++ b/app/Http/Controllers/Assets/AssetCheckinController.php
@@ -185,4 +185,34 @@ class AssetCheckinController extends Controller
// Redirect to the asset management page with error
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.checkin.error').$asset->getErrors());
}
+
+ /**
+ * This would only be used if the target is actually hard-deleted
+ * and literally does not exist in the database anymore. This will null out the assigned_to
+ * and assigned_type fields, but will not trigger any events or do any of the other things that a
+ * normal checkin would do, since the target itself is now invalid.
+ */
+ public function forceCheckin(Asset $asset)
+ {
+
+ $this->authorize('checkin', $asset);
+
+ if (! $asset->hasOrphanedAssignment()) {
+ return redirect()->route('hardware.show', $asset->id)
+ ->with('error', trans('admin/hardware/message.checkin.force_checkin_not_orphaned'));
+ }
+
+ $asset->assigned_to = null;
+ $asset->assigned_type = null;
+
+ if ($asset->save()) {
+ $asset->logForceCheckin(note: trans('general.force_checkin'));
+
+ return redirect()->route('hardware.show', $asset->id)
+ ->with('success', trans('admin/hardware/message.checkin.force_checkin_orphaned_success'));
+ }
+
+ return redirect()->route('hardware.show', $asset->id)
+ ->with('error', trans('admin/hardware/message.checkin.force_checkin_error'));
+ }
}
diff --git a/app/Models/Asset.php b/app/Models/Asset.php
index 88af0ed058..e04db03175 100644
--- a/app/Models/Asset.php
+++ b/app/Models/Asset.php
@@ -1299,7 +1299,6 @@ class Asset extends Depreciable
);
}
-
public function getAccessoryCost()
{
return (float) $this->accessories()->sum('purchase_cost');
@@ -2167,4 +2166,16 @@ class Asset extends Depreciable
->join('depreciations', 'models.depreciation_id', '=', 'depreciations.id')->where('models.depreciation_id', '=', $search);
}
+
+ /**
+ * Determines if the asset has an orphaned assignment where the assigned target no longer exists.
+ * This occurs when:
+ * 1. assigned_to is set but assigned_type is missing/null
+ * 2. assigned_to and assigned_type are both set, but the relationship cannot be resolved (target was hard-deleted)
+ */
+ public function hasOrphanedAssignment(): bool
+ {
+ return ($this->assigned_to && ! $this->assigned_type)
+ || ($this->assigned_to && $this->assigned_type && ! $this->assignedTo);
+ }
}
diff --git a/app/Models/Traits/Loggable.php b/app/Models/Traits/Loggable.php
index f866a78812..26ecb07ae8 100644
--- a/app/Models/Traits/Loggable.php
+++ b/app/Models/Traits/Loggable.php
@@ -303,6 +303,33 @@ trait Loggable
return $log;
}
+ /**
+ * Logs a force checkin action for orphaned assignments.
+ *
+ * Force checkin only records an explicit action log entry and intentionally
+ * skips checkin counters and changed-field metadata.
+ *
+ * @return Actionlog
+ */
+ public function logForceCheckin($note = null)
+ {
+ $log = new Actionlog;
+
+ $log = $this->determineLogItemType($log);
+ $log->location_id = null;
+ $log->note = $note;
+ $log->action_date = date('Y-m-d H:i:s');
+
+ if (auth()->user()) {
+ $log->created_by = auth()->id();
+ }
+
+
+ $log->logaction('force checkin');
+
+ return $log;
+ }
+
/**
* @author A. Gianotto <[email protected]>
*
diff --git a/resources/lang/en-US/admin/hardware/message.php b/resources/lang/en-US/admin/hardware/message.php
index e267e02367..db0bdcb266 100644
--- a/resources/lang/en-US/admin/hardware/message.php
+++ b/resources/lang/en-US/admin/hardware/message.php
@@ -99,6 +99,9 @@ return [
'success' => 'Asset checked in successfully.',
'user_does_not_exist' => 'That user is invalid. Please try again.',
'already_checked_in' => 'That asset is already checked in.',
+ 'force_checkin_orphaned_success' => 'Invalid assignment cleared successfully.',
+ 'force_checkin_not_orphaned' => 'Item is not in an invalid assignment state.',
+ 'force_checkin_error' => 'Could not clear invalid assignment.',
],
diff --git a/resources/lang/en-US/general.php b/resources/lang/en-US/general.php
index 6a63085bd9..897e7078f5 100644
--- a/resources/lang/en-US/general.php
+++ b/resources/lang/en-US/general.php
@@ -600,6 +600,9 @@ return [
'status_compatibility' => 'If assets are already assigned, they cannot be changed to a non-deployable status type and this value change will be skipped.',
'rtd_location_help' => 'This is the location of the asset when it is not checked out',
'item_not_found' => ':item_type ID :id does not exist or has been deleted',
+ 'item_target_not_found_hard' => ':item_type ID :id does not exist or has been hard-deleted. Would you like to force a checkin?',
+ 'force_checkin' => 'Force Checkin',
+ 'item_not_found_short' => ':item_type ID :id does not exist',
'action_permission_denied' => 'You do not have permission to :action :item_type ID :id',
'action_permission_generic' => 'You do not have permission to :action this :item_type',
'edit' => 'edit',
diff --git a/resources/views/blade/info-element/status.blade.php b/resources/views/blade/info-element/status.blade.php
index 50f340e910..9bbad2e8ae 100644
--- a/resources/views/blade/info-element/status.blade.php
+++ b/resources/views/blade/info-element/status.blade.php
@@ -4,13 +4,23 @@
@if (($infoObject) && ($infoObject->status))
- @if (($infoObject->assignedTo) && ($infoObject->deleted_at==''))
+ @if ($infoObject->assignedTo)
<x-icon type="circle-solid" class="text-blue"/>
{{ $infoObject->status->name }}
<label class="label label-default">{{ trans('general.deployed') }}</label>
<x-icon type="long-arrow-right"/>
<x-icon type="{{ $infoObject->assignedType() }}" class="fa-fw"/>
{!! $infoObject->assignedTo->present()->nameUrl() !!}
+ @elseif ($infoObject->hasOrphanedAssignment())
+ <x-icon type="circle-solid" class="text-blue"/>
+ {{ $infoObject->status->name }}
+ <label class="label label-default">{{ trans('general.deployed') }}</label>
+ <x-icon type="long-arrow-right"/>
+ <span class="text-danger">
+ <x-icon type="{{ $infoObject->assignedType() }}"/>
+ <x-icon type="x"/>
+ {{ trans('general.deleted') }}
+ </span>
@else
@if (($infoObject->status) && ($infoObject->status->deployable=='1'))
<x-icon type="circle-solid" class="text-green"/>
diff --git a/routes/web/hardware.php b/routes/web/hardware.php
index c2aed9a915..74db72d515 100644
--- a/routes/web/hardware.php
+++ b/routes/web/hardware.php
@@ -1,14 +1,14 @@
<?php
-use App\Http\Controllers\MaintenancesController;
+use App\Http\Controllers\Assets\AssetCheckinController;
+use App\Http\Controllers\Assets\AssetCheckoutController;
use App\Http\Controllers\Assets\AssetsController;
use App\Http\Controllers\Assets\BulkAssetsController;
-use App\Http\Controllers\Assets\AssetCheckoutController;
-use App\Http\Controllers\Assets\AssetCheckinController;
+use App\Http\Controllers\MaintenancesController;
+use App\Models\Asset;
use App\Models\Setting;
-use Tabuna\Breadcrumbs\Trail;
use Illuminate\Support\Facades\Route;
-use App\Models\Asset;
+use Tabuna\Breadcrumbs\Trail;
/*
|--------------------------------------------------------------------------
@@ -21,51 +21,45 @@ use App\Models\Asset;
Route::group(
[
'prefix' => 'hardware',
- 'middleware' => ['auth'],
+ 'middleware' => ['auth'],
],
-
+
function () {
-
+
Route::get('bulkaudit', [AssetsController::class, 'quickScan'])
->name('assets.bulkaudit')
- ->breadcrumbs(fn (Trail $trail) =>
- $trail->parent('hardware.index')
+ ->breadcrumbs(fn (Trail $trail) => $trail->parent('hardware.index')
->push(trans('general.bulkaudit'), route('asset.import-history'))
);
Route::get('quickscancheckin', [AssetsController::class, 'quickScanCheckin'])
->name('hardware/quickscancheckin')
- ->breadcrumbs(fn (Trail $trail) =>
- $trail->parent('hardware.index')
+ ->breadcrumbs(fn (Trail $trail) => $trail->parent('hardware.index')
->push('Quickscan Checkin', route('hardware/quickscancheckin'))
);
Route::get('requested', [AssetsController::class, 'getRequestedIndex'])
->name('assets.requested')
- ->breadcrumbs(fn (Trail $trail) =>
- $trail->parent('hardware.index')
+ ->breadcrumbs(fn (Trail $trail) => $trail->parent('hardware.index')
->push(trans('admin/hardware/general.requested'), route('assets.requested'))
);
Route::get('audit/due', [AssetsController::class, 'dueForAudit'])
->name('assets.audit.due')
- ->breadcrumbs(fn (Trail $trail) =>
- $trail->parent('hardware.index')
+ ->breadcrumbs(fn (Trail $trail) => $trail->parent('hardware.index')
->push(trans_choice('general.audit_due_days', Setting::getSettings()->audit_warning_days, ['days' => Setting::getSettings()->audit_warning_days]), route('assets.audit.due'))
);
Route::get('checkins/due',
[AssetsController::class, 'dueForCheckin']
)->name('assets.checkins.due')
- ->breadcrumbs(fn (Trail $trail) =>
- $trail->parent('hardware.index')
+ ->breadcrumbs(fn (Trail $trail) => $trail->parent('hardware.index')
->push(trans_choice('general.checkin_due_days', Setting::getSettings()->due_checkin_days, ['days' => Setting::getSettings()->due_checkin_days]), route('assets.audit.due'))
);
-
+
Route::get('{asset}/audit', [AssetsController::class, 'audit'])
->name('asset.audit.create')
- ->breadcrumbs(fn (Trail $trail, Asset $asset) =>
- $trail->parent('hardware.show', $asset)
+ ->breadcrumbs(fn (Trail $trail, Asset $asset) => $trail->parent('hardware.show', $asset)
->push(trans('general.audit'))
);
@@ -73,10 +67,13 @@ Route::group(
[AssetsController::class, 'auditStore']
)->name('asset.audit.store');
+ Route::post('{asset}/forcecheckin',
+ [AssetCheckinController::class, 'forceCheckin']
+ )->name('asset.checkin.force');
+
Route::get('history', [AssetsController::class, 'getImportHistory'])
->name('asset.import-history')
- ->breadcrumbs(fn (Trail $trail) =>
- $trail->parent('hardware.index')
+ ->breadcrumbs(fn (Trail $trail) => $trail->parent('hardware.index')
->push(trans('general.import-history'), route('asset.import-history'))
);
@@ -102,8 +99,7 @@ Route::group(
Route::get('{asset}/checkout', [AssetCheckoutController::class, 'create'])
->name('hardware.checkout.create')
- ->breadcrumbs(fn (Trail $trail, Asset $asset) =>
- $trail->parent('hardware.show', $asset)
+ ->breadcrumbs(fn (Trail $trail, Asset $asset) => $trail->parent('hardware.show', $asset)
->push(trans('admin/hardware/general.checkout'), route('hardware.index'))
);
@@ -114,10 +110,9 @@ Route::group(
Route::get('{asset}/checkin/{backto?}',
[AssetCheckinController::class, 'create']
)->name('hardware.checkin.create')
- ->breadcrumbs(fn (Trail $trail, Asset $asset) =>
- $trail->parent('hardware.show', $asset)
- ->push(trans('admin/hardware/general.checkin'), route('hardware.index'))
- );
+ ->breadcrumbs(fn (Trail $trail, Asset $asset) => $trail->parent('hardware.show', $asset)
+ ->push(trans('admin/hardware/general.checkin'), route('hardware.index'))
+ );
Route::post('{assetId}/checkin/{backto?}',
[AssetCheckinController::class, 'store']
@@ -144,9 +139,8 @@ Route::group(
'bulkedit',
[BulkAssetsController::class, 'edit']
)->name('hardware.bulkedit.show')
- ->breadcrumbs(fn (Trail $trail) =>
- $trail->parent('hardware.index')
- ->push(trans('general.bulk_edit'), route('hardware.index')));
+ ->breadcrumbs(fn (Trail $trail) => $trail->parent('hardware.index')
+ ->push(trans('general.bulk_edit'), route('hardware.index')));
Route::post(
'bulkdelete',
@@ -166,8 +160,7 @@ Route::group(
// Bulk checkout / checkin
Route::get('bulkcheckout', [BulkAssetsController::class, 'showCheckout'])
->name('hardware.bulkcheckout.show')
- ->breadcrumbs(fn (Trail $trail) =>
- $trail->parent('hardware.index')
+ ->breadcrumbs(fn (Trail $trail) => $trail->parent('hardware.index')
->push(trans('admin/hardware/general.bulk_checkout'), route('hardware.index'))
);
@@ -178,15 +171,14 @@ Route::group(
});
Route::resource('hardware',
- AssetsController::class,
- ['middleware' => ['auth']
-])->parameters(['hardware' => 'asset'])->withTrashed();
-
+ AssetsController::class,
+ ['middleware' => ['auth'],
+ ])->parameters(['hardware' => 'asset'])->withTrashed();
// Asset Maintenances
Route::resource('maintenances',
MaintenancesController::class,
- ['middleware' => ['auth']
+ ['middleware' => ['auth'],
])->parameters(['maintenance' => 'maintenance', 'asset' => 'asset_id']);
Route::get('ht/{any?}',
diff --git a/tests/Feature/Assets/Ui/ForceCheckinAssetTest.php b/tests/Feature/Assets/Ui/ForceCheckinAssetTest.php
index 341db449cc..0721691e3a 100644
--- a/tests/Feature/Assets/Ui/ForceCheckinAssetTest.php
+++ b/tests/Feature/Assets/Ui/ForceCheckinAssetTest.php
@@ -3,6 +3,7 @@
namespace Tests\Feature\Assets\Ui;
use App\Models\Asset;
+use App\Models\Actionlog;
use App\Models\User;
use Tests\TestCase;
@@ -20,18 +21,37 @@ class ForceCheckinAssetTest extends TestCase
public function test_can_force_checkin_asset_with_orphaned_assigned_to_and_missing_type()
{
$asset = Asset::factory()->create();
+ $originalCheckinCounter = (int) $asset->checkin_counter;
$asset->assigned_to = 999; // Non-existent ID
$asset->assigned_type = null; // Missing type
- $asset->save();
+ $asset->forceSave();
$response = $this->actingAs(User::factory()->checkinAssets()->create())
->post(route('asset.checkin.force', $asset));
- $response->assertRedirect(route('hardware.show', $asset));
+ $response->assertRedirect(route('hardware.show', $asset))
+ ->assertSessionHas('success');
$asset->refresh();
$this->assertNull($asset->assigned_to);
$this->assertNull($asset->assigned_type);
+ $this->assertSame($originalCheckinCounter, (int) $asset->checkin_counter);
+
+ $this->assertDatabaseHas((new Actionlog)->getTable(), [
+ 'item_type' => Asset::class,
+ 'item_id' => $asset->id,
+ 'action_type' => 'force checkin',
+ ]);
+
+ $forceCheckinLog = Actionlog::query()
+ ->where('item_type', Asset::class)
+ ->where('item_id', $asset->id)
+ ->where('action_type', 'force checkin')
+ ->latest('id')
+ ->first();
+
+ $this->assertNotNull($forceCheckinLog);
+ $this->assertNull($forceCheckinLog->log_meta);
}
public function test_can_force_checkin_asset_with_hard_deleted_assigned_to()
diff --git a/tests/Unit/Models/AssetOrphanedAssignmentTest.php b/tests/Unit/Models/AssetOrphanedAssignmentTest.php
index 026cff2f4e..05139b1f13 100644
--- a/tests/Unit/Models/AssetOrphanedAssignmentTest.php
+++ b/tests/Unit/Models/AssetOrphanedAssignmentTest.php
@@ -14,7 +14,9 @@ class AssetOrphanedAssignmentTest extends TestCase
$asset = Asset::factory()->create();
$asset->assigned_to = 999;
$asset->assigned_type = null;
- $asset->save();
+ $asset->forceSave();
+
+ $asset->refresh();
$this->assertTrue($asset->hasOrphanedAssignment());
}