Skip to content

prevents parent assets from being bulk deleted #16315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
37 changes: 32 additions & 5 deletions app/Http/Controllers/Assets/BulkAssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,12 +539,39 @@ public function destroy(Request $request) : RedirectResponse
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()) {
$parentAssets = Asset::whereIn('id', $assetIds)
->whereHas('assignedAssets')
->get();

$assignedAssets = Asset::whereIn('id', $assetIds)
->whereNotNull('assigned_to')
->get();

$errorMessages = [];

if ($assignedAssets->isNotEmpty()) {
$assignedTags = $assignedAssets->pluck('asset_tag')->implode(', ');
$errorMessages[] = trans_choice(
'admin/hardware/message.delete.assigned_to_error',
$assignedAssets->count(),
['asset_tag' => e($assignedTags)]
);
}

if ($parentAssets->isNotEmpty()) {
$parentTags = $parentAssets->pluck('asset_tag')->implode(', ');
$errorMessages[] = trans_choice(
'admin/hardware/message.delete.parent_assigned_error',
$parentAssets->count(),
['asset_tag' => e($parentTags)]
);
}

if (!empty($errorMessages)) {
// Combine both messages
$combinedErrorMessage = implode('<br>', $errorMessages);

//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] ));
return redirect($bulk_back_url)->with('error-unescaped', $combinedErrorMessage);
}

foreach (Asset::wherein('id', $assetIds)->get() as $asset) {
Expand Down
3 changes: 2 additions & 1 deletion resources/lang/en-US/admin/hardware/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
'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.',
'assigned_to_error' => 'Asset Tag: :asset_tag is currently checked out. Check in this device before deletion of this asset.|[2,*]Asset Tags: :asset_tag are currently checked out. Check in these devices before deletion of these assets.',
'parent_assigned_error' => 'Asset Tag: :asset_tag currently has items checked out. Check in all items attached before deletion of this asset.|[2,*]Asset Tags: :asset_tag currently have items checked out. Check in these devices before deletion of these assets.',
'nothing_updated' => 'No assets were selected, so nothing was deleted.',
'success' => 'The asset was deleted successfully.',
],
Expand Down
12 changes: 11 additions & 1 deletion resources/views/notifications.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@
</div>
</div>
@endif
@if ($message = session()->get('error-unescaped'))
<div class="col-md-12">
<div class="alert alert-error fade in">
<button type="button" class="close" data-dismiss="alert">&times;</button>
<i class="fas fa-exclamation-triangle faa-pulse animated"></i>
<strong>{{ trans('general.notification_error') }}: </strong>
{!! $message !!}
</div>
</div>
@endif


@if ($messages = session()->get('error_messages'))
Expand Down Expand Up @@ -169,4 +179,4 @@
{{ $message }}
</div>
</div>
@endif
@endif
2 changes: 1 addition & 1 deletion tests/Feature/Assets/Ui/BulkDeleteAssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public function testBulkDeleteAssignedAssetTriggersError(){
$this->assertEquals(route('hardware.index'), $response->headers->get('Location'));


$errorMessage = session('error');
$errorMessage = session('error-unescaped');
$expectedMessage = trans_choice('admin/hardware/message.delete.assigned_to_error',1, ['asset_tag' => $asset->asset_tag]);
$this->assertEquals($expectedMessage, $errorMessage);
}
Expand Down
Loading