diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index 93f7255c0..14e5463ef 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -525,21 +525,31 @@ class BulkAssetsController extends Controller $this->authorize('delete', Asset::class); $bulk_back_url = route('hardware.index'); + if ($request->session()->has('bulk_back_url')) { $bulk_back_url = $request->session()->pull('bulk_back_url'); } + $assetIds = $request->get('ids'); - if ($request->filled('ids')) { - $assets = Asset::find($request->get('ids')); - foreach ($assets as $asset) { - $asset->delete(); - } // endforeach - - return redirect($bulk_back_url)->with('success', trans('admin/hardware/message.delete.success')); - // no values given, nothing to update + if(empty($assetIds)) { + return redirect($bulk_back_url)->with('error', trans('admin/hardware/message.delete.nothing_updated')); } - return redirect($bulk_back_url)->with('error', trans('admin/hardware/message.delete.nothing_updated')); + $assignedAssets = Asset::whereIn('id', $assetIds)->whereNotNull('assigned_to')->get(); + if($assignedAssets->isNotEmpty()) { + + //if assets are checked out, return a list of asset tags that would need to be checked in first. + $assetTags = $assignedAssets->pluck('asset_tag')->implode(', '); + return redirect($bulk_back_url)->with('error', trans_choice('admin/hardware/message.delete.assigned_to_error', $assignedAssets->count(), ['asset_tag' => $assetTags] )); + } + + foreach (Asset::wherein('id', $assetIds)->get() as $asset) { + $asset->delete(); + } + + return redirect($bulk_back_url)->with('success', trans('admin/hardware/message.delete.success')); + // no values given, nothing to update + } /** diff --git a/resources/lang/en-US/admin/hardware/message.php b/resources/lang/en-US/admin/hardware/message.php index 222cbc439..605c1fe23 100644 --- a/resources/lang/en-US/admin/hardware/message.php +++ b/resources/lang/en-US/admin/hardware/message.php @@ -72,6 +72,7 @@ return [ 'delete' => [ 'confirm' => 'Are you sure you wish to delete this asset?', 'error' => 'There was an issue deleting the asset. Please try again.', + 'assigned_to_error' => '{1}Asset Tag: :asset_tag is currently checked out. Check in this device before deletion.|[2,*]Asset Tags: :asset_tag are currently checked out. Check in these devices before deletion.', 'nothing_updated' => 'No assets were selected, so nothing was deleted.', 'success' => 'The asset was deleted successfully.', ], diff --git a/tests/Feature/Assets/Ui/BulkDeleteAssetsTest.php b/tests/Feature/Assets/Ui/BulkDeleteAssetsTest.php index d1375c539..38c69f3b9 100644 --- a/tests/Feature/Assets/Ui/BulkDeleteAssetsTest.php +++ b/tests/Feature/Assets/Ui/BulkDeleteAssetsTest.php @@ -162,5 +162,28 @@ class BulkDeleteAssetsTest extends TestCase ); } + public function testBulkDeleteAssignedAssetTriggersError(){ + $user = User::factory()->viewAssets()->deleteAssets()->editAssets()->create(); + $asset = Asset::factory()->create([ + 'id' => 5, + 'assigned_to' => $user->id, + 'asset_tag' => '12345', + ]); + + $response = $this->actingAs($user) + ->from(route('hardware/bulkedit')) + ->post('/hardware/bulkdelete', [ + 'ids' => [$asset->id], + 'bulk_actions' => 'delete', + ]); + + $this->assertEquals(302, $response->getStatusCode()); + $this->assertEquals(route('hardware.index'), $response->headers->get('Location')); + + + $errorMessage = session('error'); + $expectedMessage = trans_choice('admin/hardware/message.delete.assigned_to_error',1, ['asset_tag' => $asset->asset_tag]); + $this->assertEquals($expectedMessage, $errorMessage); + } }