From a534b488b27d39279e29f22c96cf5eefdb962030 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Wed, 19 Feb 2025 10:09:02 -0800 Subject: [PATCH] prevents deletion of assigned assets in bulk deletion --- .../Assets/BulkAssetsController.php | 21 ++++++++++++------- .../lang/en-US/admin/hardware/message.php | 1 + 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index 93f7255c0b..d38350f5b2 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -525,20 +525,25 @@ 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'); + $assignedAssets = Asset::whereIn('id', $assetIds)->whereNotNull('assigned_to')->get(); - 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($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()->route('hardware.index')->with('error', trans_choice('admin/hardware/message.delete.assigned_to_error', $assignedAssets->count(), ['asset_tag' => $assetTags] )); } + if($assetIds) { + Asset::wherein('id', $assetIds)->delete(); + return redirect($bulk_back_url)->with('success', trans('admin/hardware/message.delete.success')); + } + // no values given, nothing to update + return redirect($bulk_back_url)->with('error', trans('admin/hardware/message.delete.nothing_updated')); } diff --git a/resources/lang/en-US/admin/hardware/message.php b/resources/lang/en-US/admin/hardware/message.php index 222cbc439e..605c1fe230 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.', ],