Skip to content

A free open source IT asset/license management system

Allow checkin from deleted view

snipee61143f746e4
diff --git a/app/Http/Controllers/Assets/AssetCheckinController.php b/app/Http/Controllers/Assets/AssetCheckinController.php
index 0e14928a6d..fb3b81eacb 100644
--- a/app/Http/Controllers/Assets/AssetCheckinController.php
+++ b/app/Http/Controllers/Assets/AssetCheckinController.php
@@ -84,7 +84,7 @@ class AssetCheckinController extends Controller
public function store(AssetCheckinRequest $request, $assetId = null, $backto = null): RedirectResponse
{
// Check if the asset exists
- if (is_null($asset = Asset::find($assetId))) {
+ if (is_null($asset = Asset::withTrashed()->find($assetId))) {
// Redirect to the asset management page with error
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist'));
}
diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php
index 825f0c4bd0..1d6f72cf8f 100644
--- a/app/Http/Controllers/Assets/BulkAssetsController.php
+++ b/app/Http/Controllers/Assets/BulkAssetsController.php
@@ -783,7 +783,7 @@ class BulkAssetsController extends Controller
$notAssigned = collect();
if (old('selected_assets') && is_array(old('selected_assets'))) {
- $assets = Asset::findMany(old('selected_assets'));
+ $assets = Asset::withTrashed()->findMany(old('selected_assets'));
[$assigned, $notAssigned] = $assets->partition(function (Asset $asset) {
return $asset->assigned_to;
@@ -814,7 +814,7 @@ class BulkAssetsController extends Controller
$asset_ids = array_filter($request->input('selected_assets'));
- $assets = Asset::findOrFail($asset_ids);
+ $assets = Asset::withTrashed()->findOrFail($asset_ids);
$checkin_at = date('Y-m-d H:i:s');
if ($request->filled('checkin_at') && $request->input('checkin_at') != date('Y-m-d')) {
diff --git a/app/Models/Asset.php b/app/Models/Asset.php
index 3d68d2136a..3f79efad32 100644
--- a/app/Models/Asset.php
+++ b/app/Models/Asset.php
@@ -487,16 +487,18 @@ class Asset extends Depreciable
public function availableForCheckIn()
{
+ if ($this->assigned_to == '') {
+ return false;
+ }
- // This asset is currently assigned to anyone and is not deleted...
- if (($this->assigned_to != '') && ($this->status) && ($this->status->archived == '0')
- && ($this->status->deployable == '1')
- ) {
+ // Deleted assets that are still checked out should always allow checkin
+ if ($this->deleted_at != '') {
return true;
-
}
- return false;
+ return $this->status
+ && ($this->status->archived == '0')
+ && ($this->status->deployable == '1');
}
/**
diff --git a/resources/views/blade/table/bulk-assets.blade.php b/resources/views/blade/table/bulk-assets.blade.php
index 11b25d101b..be28dfb160 100644
--- a/resources/views/blade/table/bulk-assets.blade.php
+++ b/resources/views/blade/table/bulk-assets.blade.php
@@ -24,6 +24,9 @@
@can('delete', \App\Models\Asset::class)
<option value="restore">{{trans('button.restore')}}</option>
@endcan
+ @can('checkin', \App\Models\Asset::class)
+ <option value="checkin">{{ trans('admin/hardware/general.bulk_checkin') }}</option>
+ @endcan
@else
@can('update', \App\Models\Asset::class)
diff --git a/routes/web/hardware.php b/routes/web/hardware.php
index 6dba6a8a76..a9bffd6b19 100644
--- a/routes/web/hardware.php
+++ b/routes/web/hardware.php
@@ -109,7 +109,7 @@ Route::group(
Route::get('{asset}/checkin/{backto?}',
[AssetCheckinController::class, 'create']
- )->name('hardware.checkin.create')
+ )->name('hardware.checkin.create')->withTrashed()
->breadcrumbs(fn (Trail $trail, Asset $asset) => $trail->parent('hardware.show', $asset)
->push(trans('admin/hardware/general.checkin'), route('hardware.index'))
);
diff --git a/tests/Feature/Checkins/Ui/AssetCheckinTest.php b/tests/Feature/Checkins/Ui/AssetCheckinTest.php
index 1179aa721d..1e2f479e08 100644
--- a/tests/Feature/Checkins/Ui/AssetCheckinTest.php
+++ b/tests/Feature/Checkins/Ui/AssetCheckinTest.php
@@ -377,4 +377,37 @@ class AssetCheckinTest extends TestCase
->assertSessionHasNoErrors()
->assertRedirect(route('hardware.show', $asset));
}
+
+ public function test_deleted_checked_out_asset_checkin_page_renders()
+ {
+ $asset = Asset::factory()->deleted()->assignedToUser()->create();
+
+ $this->actingAs(User::factory()->checkinAssets()->create())
+ ->get(route('hardware.checkin.create', $asset))
+ ->assertOk();
+ }
+
+ public function test_deleted_checked_out_asset_can_be_checked_in()
+ {
+ Event::fake([CheckoutableCheckedIn::class]);
+
+ $user = User::factory()->create();
+ $asset = Asset::factory()->deleted()->assignedToUser($user)->create();
+
+ $this->assertTrue($asset->assignedTo->is($user));
+ $this->assertNotNull($asset->deleted_at);
+
+ $this->actingAs(User::factory()->checkinAssets()->create())
+ ->post(route('hardware.checkin.store', $asset))
+ ->assertRedirect()
+ ->assertSessionHasNoErrors();
+
+ $asset->refresh();
+
+ $this->assertNull($asset->assignedTo);
+ $this->assertNull($asset->assigned_to);
+ $this->assertNotNull($asset->deleted_at, 'Asset should remain deleted after checkin');
+
+ Event::assertDispatched(CheckoutableCheckedIn::class);
+ }
}