Skip to content

A free open source IT asset/license management system

Accessories: Added bulk delete of accessories

snipe59665c3cf87b
diff --git a/app/Http/Controllers/BulkAccessoriesController.php b/app/Http/Controllers/BulkAccessoriesController.php
new file mode 100644
index 0000000000..a0cab5ace5
--- /dev/null
+++ b/app/Http/Controllers/BulkAccessoriesController.php
@@ -0,0 +1,62 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Models\Accessory;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Storage;
+
+class BulkAccessoriesController extends Controller
+{
+ public function destroy(Request $request)
+ {
+ $this->authorize('delete', Accessory::class);
+
+ $errors = [];
+ $success_count = 0;
+
+ foreach ((array) $request->input('ids', []) as $id) {
+ $accessory = Accessory::find($id);
+ if (is_null($accessory)) {
+ $errors[] = trans('admin/accessories/message.does_not_exist', ['id' => $id]);
+
+ continue;
+ }
+
+ $accessory->loadCount('checkouts as checkouts_count');
+ if (! $accessory->isDeletable()) {
+ $errors[] = trans('general.bulk_delete_associations.assoc_checkouts_no_count', [
+ 'item_name' => $accessory->name,
+ 'item' => trans('general.accessory'),
+ ]);
+
+ continue;
+ }
+
+ if ($accessory->image) {
+ try {
+ Storage::disk('public')->delete('accessories/'.$accessory->image);
+ } catch (\Exception $e) {
+ Log::debug($e);
+ }
+ }
+
+ $accessory->delete();
+ $success_count++;
+ }
+
+ if (count($errors) > 0) {
+ if ($success_count > 0) {
+ return redirect()->route('accessories.index')
+ ->with('success', trans_choice('admin/accessories/message.delete.partial_success', $success_count, ['count' => $success_count]))
+ ->with('multi_error_messages', $errors);
+ }
+
+ return redirect()->route('accessories.index')->with('multi_error_messages', $errors);
+ }
+
+ return redirect()->route('accessories.index')
+ ->with('success', trans_choice('admin/accessories/message.delete.bulk_success', $success_count, ['count' => $success_count]));
+ }
+}
diff --git a/app/Http/Transformers/AccessoriesTransformer.php b/app/Http/Transformers/AccessoriesTransformer.php
index 13f0cba8e7..5444907c26 100644
--- a/app/Http/Transformers/AccessoriesTransformer.php
+++ b/app/Http/Transformers/AccessoriesTransformer.php
@@ -80,7 +80,9 @@ class AccessoriesTransformer
'update' => Gate::allows('update', Accessory::class),
'delete' => $accessory->checkouts_count === 0 && Gate::allows('delete', Accessory::class),
'clone' => Gate::allows('create', Accessory::class),
-
+ 'bulk_selectable' => [
+ 'delete' => $accessory->checkouts_count === 0,
+ ],
];
$permissions_array['user_can_checkout'] = false;
diff --git a/app/Presenters/AccessoryPresenter.php b/app/Presenters/AccessoryPresenter.php
index 0093e0e7ce..dd4afc282f 100644
--- a/app/Presenters/AccessoryPresenter.php
+++ b/app/Presenters/AccessoryPresenter.php
@@ -16,6 +16,13 @@ class AccessoryPresenter extends Presenter
{
$layout = [
[
+ 'field' => 'checkbox',
+ 'checkbox' => true,
+ 'formatter' => 'checkboxEnabledFormatter',
+ 'titleTooltip' => trans('general.select_all_none'),
+ 'printIgnore' => true,
+ 'class' => 'hidden-print',
+ ], [
'field' => 'id',
'searchable' => false,
'sortable' => true,
diff --git a/resources/lang/en-US/admin/accessories/message.php b/resources/lang/en-US/admin/accessories/message.php
index d8dbbeb29a..aac963bdc7 100644
--- a/resources/lang/en-US/admin/accessories/message.php
+++ b/resources/lang/en-US/admin/accessories/message.php
@@ -20,6 +20,8 @@ return [
'confirm' => 'Are you sure you wish to delete this accessory?',
'error' => 'There was an issue deleting the accessory. Please try again.',
'success' => 'The accessory was deleted successfully.',
+ 'bulk_success' => 'Accessory deleted successfully.|:count accessories were deleted successfully.',
+ 'partial_success' => ':count accessory was deleted successfully, but others could not be deleted. See below for details.|:count accessories were deleted successfully, but others could not be deleted. See below for details.',
],
'checkout' => [
diff --git a/resources/lang/en-US/general.php b/resources/lang/en-US/general.php
index abd3d5b3d7..dbe2e9d85b 100644
--- a/resources/lang/en-US/general.php
+++ b/resources/lang/en-US/general.php
@@ -743,6 +743,7 @@ return [
'assoc_licenses_no_count' => ':item_name is currently associated with other licenses and cannot be deleted. Please update your licenses to no longer reference this :item and try again.',
'assoc_users_no_count' => ':item_name is currently associated with other users and cannot be deleted. Please update your users to no longer reference this :item and try again.',
'assoc_child_companies_no_count' => ':item_name is currently a parent to other companies and cannot be deleted. Please reassign or delete the child companies before trying again.',
+ 'assoc_checkouts_no_count' => ':item_name is currently checked out. Please check in all items before deleting this :item.',
],
'breadcrumb_button_actions' => [
diff --git a/resources/views/accessories/index.blade.php b/resources/views/accessories/index.blade.php
index a6d5018682..9e1976524b 100755
--- a/resources/views/accessories/index.blade.php
+++ b/resources/views/accessories/index.blade.php
@@ -10,7 +10,7 @@
{{-- Page content --}}
@section('content')
<x-container>
- <x-box>
+ <x-box name="accessory">
<x-table.accessories name="accessories" :route="route('api.accessories.index')" fixed_right_number="3" />
</x-box>
</x-container>
diff --git a/resources/views/blade/table/accessories.blade.php b/resources/views/blade/table/accessories.blade.php
index 5bab0ba332..4215a2ce4e 100644
--- a/resources/views/blade/table/accessories.blade.php
+++ b/resources/views/blade/table/accessories.blade.php
@@ -3,7 +3,7 @@
'name' => 'default',
'presenter' => \App\Presenters\AccessoryPresenter::dataTableLayout(),
'fixed_right_number' => 2,
- 'fixed_number' => 1,
+ 'fixed_number' => 2,
'table_header' => trans('general.accessories'),
])
@@ -15,7 +15,18 @@
<x-slot:table_header>
{{ $table_header }}
</x-slot:table_header>
-
+
+ <x-slot:bulkactions>
+ <x-table.bulk-actions
+ name='accessory'
+ action_route="{{ route('accessories.bulk.delete') }}"
+ model_name="accessory">
+ @can('delete', App\Models\Accessory::class)
+ <option>{{ trans('general.delete') }}</option>
+ @endcan
+ </x-table.bulk-actions>
+ </x-slot:bulkactions>
+
<x-table
:$presenter
:$fixed_right_number
diff --git a/routes/web/accessories.php b/routes/web/accessories.php
index 15b89572a0..e2ae08204c 100644
--- a/routes/web/accessories.php
+++ b/routes/web/accessories.php
@@ -1,6 +1,7 @@
<?php
use App\Http\Controllers\Accessories;
+use App\Http\Controllers\BulkAccessoriesController;
use Illuminate\Support\Facades\Route;
/*
@@ -28,8 +29,8 @@ Route::group(['prefix' => 'accessories', 'middleware' => ['auth']], function ()
)->name('accessories.checkin.store');
Route::get('{accessory}/clone',
- [Accessories\AccessoriesController::class, 'getClone']
- )->name('clone/accessories');
+ [Accessories\AccessoriesController::class, 'getClone']
+ )->name('clone/accessories');
Route::post('{accessory}/clone',
[Accessories\AccessoriesController::class, 'postCreate']
@@ -38,5 +39,9 @@ Route::group(['prefix' => 'accessories', 'middleware' => ['auth']], function ()
});
Route::resource('accessories', Accessories\AccessoriesController::class, [
- 'middleware' => ['auth']
+ 'middleware' => ['auth'],
]);
+
+Route::post('accessories/bulk/delete', [BulkAccessoriesController::class, 'destroy'])
+ ->middleware(['auth'])
+ ->name('accessories.bulk.delete');
diff --git a/tests/Feature/Accessories/Api/BulkSelectableTest.php b/tests/Feature/Accessories/Api/BulkSelectableTest.php
new file mode 100644
index 0000000000..0dd2eb161a
--- /dev/null
+++ b/tests/Feature/Accessories/Api/BulkSelectableTest.php
@@ -0,0 +1,40 @@
+<?php
+
+namespace Tests\Feature\Accessories\Api;
+
+use App\Models\Accessory;
+use App\Models\AccessoryCheckout;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * Verifies the JS-visible flag that drives the bulk-delete checkbox on the
+ * accessories index. The bootstrap-table `checkboxEnabledFormatter` reads
+ * `available_actions.bulk_selectable.delete` and disables the row's checkbox
+ * when every entry there is false. An accessory that has any active checkout
+ * must therefore report `bulk_selectable.delete === false`; a clean accessory
+ * must report `true`.
+ */
+class BulkSelectableTest extends TestCase
+{
+ public function test_clean_accessory_is_bulk_selectable()
+ {
+ $accessory = Accessory::factory()->create();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.accessories.show', $accessory))
+ ->assertOk()
+ ->assertJsonPath('available_actions.bulk_selectable.delete', true);
+ }
+
+ public function test_checked_out_accessory_is_not_bulk_selectable()
+ {
+ $accessory = Accessory::factory()->create();
+ AccessoryCheckout::factory()->create(['accessory_id' => $accessory->id]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.accessories.show', $accessory))
+ ->assertOk()
+ ->assertJsonPath('available_actions.bulk_selectable.delete', false);
+ }
+}
diff --git a/tests/Feature/Accessories/Ui/BulkDeleteAccessoriesTest.php b/tests/Feature/Accessories/Ui/BulkDeleteAccessoriesTest.php
new file mode 100644
index 0000000000..d46b1df493
--- /dev/null
+++ b/tests/Feature/Accessories/Ui/BulkDeleteAccessoriesTest.php
@@ -0,0 +1,105 @@
+<?php
+
+namespace Tests\Feature\Accessories\Ui;
+
+use App\Models\Accessory;
+use App\Models\AccessoryCheckout;
+use App\Models\User;
+use Tests\TestCase;
+
+class BulkDeleteAccessoriesTest extends TestCase
+{
+ public function test_requires_permission()
+ {
+ $this->actingAs(User::factory()->create())
+ ->post(route('accessories.bulk.delete'), [
+ 'ids' => [1, 2, 3],
+ ])
+ ->assertForbidden();
+ }
+
+ public function test_checked_out_accessory_is_not_bulk_deleted()
+ {
+ $accessory = Accessory::factory()->create();
+ AccessoryCheckout::factory()->create(['accessory_id' => $accessory->id]);
+
+ $this->actingAs(User::factory()->deleteAccessories()->create())
+ ->post(route('accessories.bulk.delete'), [
+ 'ids' => [$accessory->id],
+ ])
+ ->assertSessionHas('multi_error_messages');
+
+ $this->assertDatabaseHas('accessories', ['id' => $accessory->id, 'deleted_at' => null]);
+ }
+
+ public function test_deletable_accessories_are_bulk_deleted()
+ {
+ $accessory1 = Accessory::factory()->create();
+ $accessory2 = Accessory::factory()->create();
+ $accessory3 = Accessory::factory()->create();
+
+ $this->actingAs(User::factory()->deleteAccessories()->create())
+ ->post(route('accessories.bulk.delete'), [
+ 'ids' => [$accessory1->id, $accessory2->id, $accessory3->id],
+ ])
+ ->assertRedirect(route('accessories.index'));
+
+ // Accessories are soft-deleted.
+ $this->assertSoftDeleted('accessories', ['id' => $accessory1->id]);
+ $this->assertSoftDeleted('accessories', ['id' => $accessory2->id]);
+ $this->assertSoftDeleted('accessories', ['id' => $accessory3->id]);
+ }
+
+ public function test_partial_success_deletes_the_clean_ones_and_reports_the_rest()
+ {
+ $deletable = Accessory::factory()->create();
+ $blocked = Accessory::factory()->create();
+ AccessoryCheckout::factory()->create(['accessory_id' => $blocked->id]);
+
+ $this->actingAs(User::factory()->deleteAccessories()->create())
+ ->post(route('accessories.bulk.delete'), [
+ 'ids' => [$deletable->id, $blocked->id],
+ ])
+ ->assertRedirect(route('accessories.index'))
+ ->assertSessionHas('success')
+ ->assertSessionHas('multi_error_messages');
+
+ $this->assertSoftDeleted('accessories', ['id' => $deletable->id]);
+ $this->assertDatabaseHas('accessories', ['id' => $blocked->id, 'deleted_at' => null]);
+ }
+
+ public function test_nonexistent_ids_are_reported_and_do_not_break_the_batch()
+ {
+ $deletable = Accessory::factory()->create();
+
+ $this->actingAs(User::factory()->deleteAccessories()->create())
+ ->post(route('accessories.bulk.delete'), [
+ 'ids' => [$deletable->id, 999999],
+ ])
+ ->assertRedirect(route('accessories.index'))
+ ->assertSessionHas('multi_error_messages');
+
+ $this->assertSoftDeleted('accessories', ['id' => $deletable->id]);
+ }
+
+ public function test_bulk_success_message_pluralizes_by_count()
+ {
+ $solo = Accessory::factory()->create();
+
+ $this->actingAs(User::factory()->deleteAccessories()->create())
+ ->post(route('accessories.bulk.delete'), [
+ 'ids' => [$solo->id],
+ ])
+ ->assertSessionHas('success', trans_choice('admin/accessories/message.delete.bulk_success', 1, ['count' => 1]));
+
+ $a = Accessory::factory()->create();
+ $b = Accessory::factory()->create();
+ $c = Accessory::factory()->create();
+
+ $this->actingAs(User::factory()->deleteAccessories()->create())
+ ->post(route('accessories.bulk.delete'), [
+ 'ids' => [$a->id, $b->id, $c->id],
+ ])
+ ->assertSessionHas('success', trans_choice('admin/accessories/message.delete.bulk_success', 3, ['count' => 3]));
+ }
+}