Skip to content

Commit 7db11dc

Browse files
authored
Merge pull request #16277 from Godmartinz/bulk_delete_asset_bug
Fixes deletion of assigned assets through bulk delete
2 parents e022130 + a275391 commit 7db11dc

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

app/Http/Controllers/Assets/BulkAssetsController.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -525,21 +525,31 @@ public function destroy(Request $request) : RedirectResponse
525525
$this->authorize('delete', Asset::class);
526526

527527
$bulk_back_url = route('hardware.index');
528+
528529
if ($request->session()->has('bulk_back_url')) {
529530
$bulk_back_url = $request->session()->pull('bulk_back_url');
530531
}
532+
$assetIds = $request->get('ids');
531533

532-
if ($request->filled('ids')) {
533-
$assets = Asset::find($request->get('ids'));
534-
foreach ($assets as $asset) {
534+
if(empty($assetIds)) {
535+
return redirect($bulk_back_url)->with('error', trans('admin/hardware/message.delete.nothing_updated'));
536+
}
537+
538+
$assignedAssets = Asset::whereIn('id', $assetIds)->whereNotNull('assigned_to')->get();
539+
if($assignedAssets->isNotEmpty()) {
540+
541+
//if assets are checked out, return a list of asset tags that would need to be checked in first.
542+
$assetTags = $assignedAssets->pluck('asset_tag')->implode(', ');
543+
return redirect($bulk_back_url)->with('error', trans_choice('admin/hardware/message.delete.assigned_to_error', $assignedAssets->count(), ['asset_tag' => $assetTags] ));
544+
}
545+
546+
foreach (Asset::wherein('id', $assetIds)->get() as $asset) {
535547
$asset->delete();
536-
} // endforeach
548+
}
537549

538-
return redirect($bulk_back_url)->with('success', trans('admin/hardware/message.delete.success'));
550+
return redirect($bulk_back_url)->with('success', trans('admin/hardware/message.delete.success'));
539551
// no values given, nothing to update
540-
}
541552

542-
return redirect($bulk_back_url)->with('error', trans('admin/hardware/message.delete.nothing_updated'));
543553
}
544554

545555
/**

resources/lang/en-US/admin/hardware/message.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
'delete' => [
7373
'confirm' => 'Are you sure you wish to delete this asset?',
7474
'error' => 'There was an issue deleting the asset. Please try again.',
75+
'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.',
7576
'nothing_updated' => 'No assets were selected, so nothing was deleted.',
7677
'success' => 'The asset was deleted successfully.',
7778
],

tests/Feature/Assets/Ui/BulkDeleteAssetsTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,28 @@ public function testActionLogCreatedUponBulkRestore()
162162
);
163163
}
164164

165+
public function testBulkDeleteAssignedAssetTriggersError(){
166+
$user = User::factory()->viewAssets()->deleteAssets()->editAssets()->create();
167+
$asset = Asset::factory()->create([
168+
'id' => 5,
169+
'assigned_to' => $user->id,
170+
'asset_tag' => '12345',
171+
]);
172+
173+
$response = $this->actingAs($user)
174+
->from(route('hardware/bulkedit'))
175+
->post('/hardware/bulkdelete', [
176+
'ids' => [$asset->id],
177+
'bulk_actions' => 'delete',
178+
]);
179+
180+
$this->assertEquals(302, $response->getStatusCode());
181+
$this->assertEquals(route('hardware.index'), $response->headers->get('Location'));
182+
183+
184+
$errorMessage = session('error');
185+
$expectedMessage = trans_choice('admin/hardware/message.delete.assigned_to_error',1, ['asset_tag' => $asset->asset_tag]);
186+
$this->assertEquals($expectedMessage, $errorMessage);
187+
}
165188

166189
}

0 commit comments

Comments
 (0)