prevents deletion of assigned assets in bulk deletion

This commit is contained in:
Godfrey M 2025-02-19 10:09:02 -08:00
parent e814cd5a9e
commit a534b488b2
2 changed files with 14 additions and 8 deletions

View file

@ -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'));
}

View file

@ -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.',
],