Skip to content

A free open source IT asset/license management system

Depreciations: Added bulk delete from index

snipeaf7f920a998a
diff --git a/app/Actions/Depreciations/DestroyDepreciationAction.php b/app/Actions/Depreciations/DestroyDepreciationAction.php
new file mode 100644
index 0000000000..f6a3ef4291
--- /dev/null
+++ b/app/Actions/Depreciations/DestroyDepreciationAction.php
@@ -0,0 +1,42 @@
+<?php
+
+namespace App\Actions\Depreciations;
+
+use App\Exceptions\ItemStillHasAssetModels;
+use App\Exceptions\ItemStillHasAssets;
+use App\Exceptions\ItemStillHasLicenses;
+use App\Models\Depreciation;
+
+class DestroyDepreciationAction
+{
+ /**
+ * @throws ItemStillHasAssets
+ * @throws ItemStillHasAssetModels
+ * @throws ItemStillHasLicenses
+ */
+ public static function run(Depreciation $depreciation): bool
+ {
+ $depreciation->loadCount([
+ 'assets as assets_count',
+ 'models as models_count',
+ 'licenses as licenses_count',
+ ]);
+
+ if ($depreciation->assets_count > 0) {
+ throw new ItemStillHasAssets($depreciation);
+ }
+
+ if ($depreciation->models_count > 0) {
+ throw new ItemStillHasAssetModels($depreciation);
+ }
+
+ if ($depreciation->licenses_count > 0) {
+ throw new ItemStillHasLicenses($depreciation);
+ }
+
+ // Depreciation is not soft-deleted; this is a hard delete.
+ $depreciation->delete();
+
+ return true;
+ }
+}
diff --git a/app/Http/Controllers/Api/DepreciationsController.php b/app/Http/Controllers/Api/DepreciationsController.php
index bd248bd6d8..d58dbc2935 100644
--- a/app/Http/Controllers/Api/DepreciationsController.php
+++ b/app/Http/Controllers/Api/DepreciationsController.php
@@ -99,7 +99,13 @@ class DepreciationsController extends Controller
public function show($id): JsonResponse|array
{
$this->authorize('view', Depreciation::class);
- $depreciation = Depreciation::findOrFail($id);
+ // Match index()'s eager load so isDeletable() (via available_actions.delete
+ // and bulk_selectable.delete) reads the preloaded counts instead of firing
+ // three fallback ->count() queries per show call.
+ $depreciation = Depreciation::withCount('assets as assets_count')
+ ->withCount('models as models_count')
+ ->withCount('licenses as licenses_count')
+ ->findOrFail($id);
return (new DepreciationsTransformer)->transformDepreciation($depreciation);
}
diff --git a/app/Http/Controllers/BulkDepreciationsController.php b/app/Http/Controllers/BulkDepreciationsController.php
new file mode 100644
index 0000000000..571beba083
--- /dev/null
+++ b/app/Http/Controllers/BulkDepreciationsController.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Actions\Depreciations\DestroyDepreciationAction;
+use App\Exceptions\ItemStillHasAssetModels;
+use App\Exceptions\ItemStillHasAssets;
+use App\Exceptions\ItemStillHasLicenses;
+use App\Models\Depreciation;
+use Illuminate\Http\Request;
+
+class BulkDepreciationsController extends Controller
+{
+ public function destroy(Request $request)
+ {
+ $this->authorize('delete', Depreciation::class);
+
+ $errors = [];
+ $success_count = 0;
+
+ foreach ($request->ids as $id) {
+ $depreciation = Depreciation::find($id);
+ if (is_null($depreciation)) {
+ $errors[] = trans('admin/depreciations/message.does_not_exist');
+
+ continue;
+ }
+ try {
+ DestroyDepreciationAction::run(depreciation: $depreciation);
+ $success_count++;
+ } catch (ItemStillHasAssets $e) {
+ $errors[] = trans('general.bulk_delete_associations.assoc_assets_no_count', ['item_name' => $depreciation->name, 'item' => trans('general.depreciation')]);
+ } catch (ItemStillHasAssetModels $e) {
+ $errors[] = trans('general.bulk_delete_associations.asset_models_no_count', ['item_name' => $depreciation->name, 'item' => trans('general.depreciation')]);
+ } catch (ItemStillHasLicenses $e) {
+ $errors[] = trans('general.bulk_delete_associations.assoc_licenses_no_count', ['item_name' => $depreciation->name, 'item' => trans('general.depreciation')]);
+ } catch (\Exception $e) {
+ report($e);
+ $errors[] = trans('general.something_went_wrong');
+ }
+ }
+
+ if (count($errors) > 0) {
+ if ($success_count > 0) {
+ return redirect()->route('depreciations.index')
+ ->with('success', trans_choice('admin/depreciations/message.delete.partial_success', $success_count, ['count' => $success_count]))
+ ->with('multi_error_messages', $errors);
+ }
+
+ return redirect()->route('depreciations.index')->with('multi_error_messages', $errors);
+ }
+
+ return redirect()->route('depreciations.index')
+ ->with('success', trans_choice('admin/depreciations/message.delete.bulk_success', $success_count, ['count' => $success_count]));
+ }
+}
diff --git a/app/Http/Transformers/DepreciationsTransformer.php b/app/Http/Transformers/DepreciationsTransformer.php
index 9aa2ed7123..4a2cb48b6c 100644
--- a/app/Http/Transformers/DepreciationsTransformer.php
+++ b/app/Http/Transformers/DepreciationsTransformer.php
@@ -39,7 +39,10 @@ class DepreciationsTransformer
$permissions_array['available_actions'] = [
'update' => Gate::allows('update', Depreciation::class),
- 'delete' => Gate::allows('delete', Depreciation::class),
+ 'delete' => Gate::allows('delete', Depreciation::class) && $depreciation->isDeletable(),
+ 'bulk_selectable' => [
+ 'delete' => $depreciation->isDeletable(),
+ ],
];
$array += $permissions_array;
diff --git a/app/Presenters/DepreciationPresenter.php b/app/Presenters/DepreciationPresenter.php
index 6bbb1fa3f6..310a0f4d53 100644
--- a/app/Presenters/DepreciationPresenter.php
+++ b/app/Presenters/DepreciationPresenter.php
@@ -16,6 +16,13 @@ class DepreciationPresenter 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/depreciations/message.php b/resources/lang/en-US/admin/depreciations/message.php
index af34805f56..31e5effb83 100644
--- a/resources/lang/en-US/admin/depreciations/message.php
+++ b/resources/lang/en-US/admin/depreciations/message.php
@@ -19,6 +19,8 @@ return [
'confirm' => 'Are you sure you wish to delete this depreciation class?',
'error' => 'There was an issue deleting the depreciation class. Please try again.',
'success' => 'The depreciation class was deleted successfully.',
+ 'bulk_success' => 'Depreciation class deleted successfully.|:count depreciation classes were deleted successfully.',
+ 'partial_success' => 'Depreciation class deleted successfully. See additional information below.|:count depreciation classes were deleted successfully. See additional information below.',
],
];
diff --git a/resources/views/depreciations/index.blade.php b/resources/views/depreciations/index.blade.php
index d07ec5ed6b..b5745d3c4e 100755
--- a/resources/views/depreciations/index.blade.php
+++ b/resources/views/depreciations/index.blade.php
@@ -10,8 +10,22 @@
{{-- Page content --}}
@section('content')
<x-container>
- <x-box>
+ <x-box name="depreciation">
+
+ <x-slot:bulkactions>
+ <x-table.bulk-actions
+ name='depreciation'
+ action_route="{{ route('depreciations.bulk.delete') }}"
+ model_name="depreciation"
+ >
+ @can('delete', App\Models\Depreciation::class)
+ <option>{{ trans('general.delete') }}</option>
+ @endcan
+ </x-table.bulk-actions>
+ </x-slot:bulkactions>
+
<x-table
+ name="depreciation"
show_column_search="false"
buttons="depreciationButtons"
fixed_right_number="1"
diff --git a/routes/web.php b/routes/web.php
index ab723c9085..582e0cf1bc 100644
--- a/routes/web.php
+++ b/routes/web.php
@@ -9,6 +9,7 @@ use App\Http\Controllers\Auth\ResetPasswordController;
use App\Http\Controllers\BulkCategoriesController;
use App\Http\Controllers\BulkCompaniesController;
use App\Http\Controllers\BulkDepartmentsController;
+use App\Http\Controllers\BulkDepreciationsController;
use App\Http\Controllers\BulkManufacturersController;
use App\Http\Controllers\BulkStatuslabelsController;
use App\Http\Controllers\BulkSuppliersController;
@@ -105,6 +106,8 @@ Route::group(['middleware' => 'auth'], function () {
*/
Route::resource('depreciations', DepreciationsController::class);
+ Route::post('depreciations/bulk/delete', [BulkDepreciationsController::class, 'destroy'])->name('depreciations.bulk.delete');
+
/*
* Status Labels
*/
diff --git a/tests/Feature/Depreciations/Api/BulkSelectableTest.php b/tests/Feature/Depreciations/Api/BulkSelectableTest.php
new file mode 100644
index 0000000000..bbde06ceb4
--- /dev/null
+++ b/tests/Feature/Depreciations/Api/BulkSelectableTest.php
@@ -0,0 +1,52 @@
+<?php
+
+namespace Tests\Feature\Depreciations\Api;
+
+use App\Models\AssetModel;
+use App\Models\Depreciation;
+use App\Models\License;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * Verifies the JS-visible flag that drives the bulk-delete checkbox on the
+ * depreciations index. The bootstrap-table `checkboxEnabledFormatter` reads
+ * `available_actions.bulk_selectable.delete` and disables the row's checkbox
+ * when every entry there is false. A depreciation attached to any asset,
+ * asset model, or license must therefore report `bulk_selectable.delete === false`;
+ * a clean depreciation must report `true`.
+ */
+class BulkSelectableTest extends TestCase
+{
+ public function test_clean_depreciation_is_bulk_selectable()
+ {
+ $depreciation = Depreciation::factory()->create();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.depreciations.show', $depreciation))
+ ->assertOk()
+ ->assertJsonPath('available_actions.bulk_selectable.delete', true);
+ }
+
+ public function test_depreciation_with_asset_models_is_not_bulk_selectable()
+ {
+ $depreciation = Depreciation::factory()->create();
+ AssetModel::factory()->create(['depreciation_id' => $depreciation->id]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.depreciations.show', $depreciation))
+ ->assertOk()
+ ->assertJsonPath('available_actions.bulk_selectable.delete', false);
+ }
+
+ public function test_depreciation_with_licenses_is_not_bulk_selectable()
+ {
+ $depreciation = Depreciation::factory()->create();
+ License::factory()->create(['depreciation_id' => $depreciation->id]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.depreciations.show', $depreciation))
+ ->assertOk()
+ ->assertJsonPath('available_actions.bulk_selectable.delete', false);
+ }
+}
diff --git a/tests/Feature/Depreciations/Ui/BulkDeleteDepreciationsTest.php b/tests/Feature/Depreciations/Ui/BulkDeleteDepreciationsTest.php
new file mode 100644
index 0000000000..7114490568
--- /dev/null
+++ b/tests/Feature/Depreciations/Ui/BulkDeleteDepreciationsTest.php
@@ -0,0 +1,120 @@
+<?php
+
+namespace Tests\Feature\Depreciations\Ui;
+
+use App\Models\AssetModel;
+use App\Models\Depreciation;
+use App\Models\License;
+use App\Models\User;
+use Tests\TestCase;
+
+class BulkDeleteDepreciationsTest extends TestCase
+{
+ public function test_requires_permission()
+ {
+ $this->actingAs(User::factory()->create())
+ ->post(route('depreciations.bulk.delete'), [
+ 'ids' => [1, 2, 3],
+ ])
+ ->assertForbidden();
+ }
+
+ public function test_depreciation_with_asset_models_is_not_bulk_deleted()
+ {
+ $depreciation = Depreciation::factory()->create();
+ AssetModel::factory()->create(['depreciation_id' => $depreciation->id]);
+
+ $this->actingAs(User::factory()->deleteDepreciations()->create())
+ ->post(route('depreciations.bulk.delete'), [
+ 'ids' => [$depreciation->id],
+ ])
+ ->assertSessionHas('multi_error_messages');
+
+ $this->assertDatabaseHas('depreciations', ['id' => $depreciation->id]);
+ }
+
+ public function test_depreciation_with_licenses_is_not_bulk_deleted()
+ {
+ $depreciation = Depreciation::factory()->create();
+ License::factory()->create(['depreciation_id' => $depreciation->id]);
+
+ $this->actingAs(User::factory()->deleteDepreciations()->create())
+ ->post(route('depreciations.bulk.delete'), [
+ 'ids' => [$depreciation->id],
+ ])
+ ->assertSessionHas('multi_error_messages');
+
+ $this->assertDatabaseHas('depreciations', ['id' => $depreciation->id]);
+ }
+
+ public function test_deletable_depreciations_are_bulk_deleted()
+ {
+ $depreciation1 = Depreciation::factory()->create();
+ $depreciation2 = Depreciation::factory()->create();
+ $depreciation3 = Depreciation::factory()->create();
+
+ $this->actingAs(User::factory()->deleteDepreciations()->create())
+ ->post(route('depreciations.bulk.delete'), [
+ 'ids' => [$depreciation1->id, $depreciation2->id, $depreciation3->id],
+ ])
+ ->assertRedirect(route('depreciations.index'));
+
+ // Depreciations are hard-deleted, not soft-deleted.
+ $this->assertDatabaseMissing('depreciations', ['id' => $depreciation1->id]);
+ $this->assertDatabaseMissing('depreciations', ['id' => $depreciation2->id]);
+ $this->assertDatabaseMissing('depreciations', ['id' => $depreciation3->id]);
+ }
+
+ public function test_partial_success_deletes_the_clean_ones_and_reports_the_rest()
+ {
+ $deletable = Depreciation::factory()->create();
+ $blocked = Depreciation::factory()->create();
+ AssetModel::factory()->create(['depreciation_id' => $blocked->id]);
+
+ $this->actingAs(User::factory()->deleteDepreciations()->create())
+ ->post(route('depreciations.bulk.delete'), [
+ 'ids' => [$deletable->id, $blocked->id],
+ ])
+ ->assertRedirect(route('depreciations.index'))
+ ->assertSessionHas('success')
+ ->assertSessionHas('multi_error_messages');
+
+ $this->assertDatabaseMissing('depreciations', ['id' => $deletable->id]);
+ $this->assertDatabaseHas('depreciations', ['id' => $blocked->id]);
+ }
+
+ public function test_nonexistent_ids_are_reported_and_do_not_break_the_batch()
+ {
+ $deletable = Depreciation::factory()->create();
+
+ $this->actingAs(User::factory()->deleteDepreciations()->create())
+ ->post(route('depreciations.bulk.delete'), [
+ 'ids' => [$deletable->id, 999999],
+ ])
+ ->assertRedirect(route('depreciations.index'))
+ ->assertSessionHas('multi_error_messages');
+
+ $this->assertDatabaseMissing('depreciations', ['id' => $deletable->id]);
+ }
+
+ public function test_bulk_success_message_pluralizes_by_count()
+ {
+ $solo = Depreciation::factory()->create();
+
+ $this->actingAs(User::factory()->deleteDepreciations()->create())
+ ->post(route('depreciations.bulk.delete'), [
+ 'ids' => [$solo->id],
+ ])
+ ->assertSessionHas('success', trans_choice('admin/depreciations/message.delete.bulk_success', 1, ['count' => 1]));
+
+ $a = Depreciation::factory()->create();
+ $b = Depreciation::factory()->create();
+ $c = Depreciation::factory()->create();
+
+ $this->actingAs(User::factory()->deleteDepreciations()->create())
+ ->post(route('depreciations.bulk.delete'), [
+ 'ids' => [$a->id, $b->id, $c->id],
+ ])
+ ->assertSessionHas('success', trans_choice('admin/depreciations/message.delete.bulk_success', 3, ['count' => 3]));
+ }
+}