From fc36e0cbca083b3923546366ae8e641531e83c23 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Thu, 27 Feb 2025 12:59:04 -0800 Subject: [PATCH] prevents deletion of singular parent assets --- app/Http/Controllers/Api/AssetsController.php | 3 ++- app/Http/Transformers/AssetsTransformer.php | 6 +++--- resources/lang/en-US/general.php | 1 + resources/views/partials/bootstrap-table.blade.php | 8 +++++--- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index 87ecdcb607..78f1680059 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -139,7 +139,8 @@ class AssetsController extends Controller 'model.category', 'model.manufacturer', 'model.fieldset', - 'supplier' + 'supplier', + 'assignedAssets', ); // it might be tempting to add 'assetlog' here, but don't. It blows up update-heavy users. diff --git a/app/Http/Transformers/AssetsTransformer.php b/app/Http/Transformers/AssetsTransformer.php index 378fba2873..8e930202c6 100644 --- a/app/Http/Transformers/AssetsTransformer.php +++ b/app/Http/Transformers/AssetsTransformer.php @@ -7,6 +7,7 @@ use App\Models\Accessory; use App\Models\AccessoryCheckout; use App\Models\Asset; use App\Models\Setting; +use Barryvdh\Debugbar\Controllers\AssetController; use Illuminate\Support\Facades\Gate; use Illuminate\Database\Eloquent\Collection; use Carbon\Carbon; @@ -28,7 +29,6 @@ class AssetsTransformer { // This uses the getSettings() method so we're pulling from the cache versus querying the settings on single asset $setting = Setting::getSettings(); - $array = [ 'id' => (int) $asset->id, 'name' => e($asset->name), @@ -40,7 +40,7 @@ class AssetsTransformer ] : null, 'byod' => ($asset->byod ? true : false), 'requestable' => ($asset->requestable ? true : false), - + 'child_asset_count' => $asset->assignedAssets->count(), 'model_number' => (($asset->model) && ($asset->model->model_number)) ? e($asset->model->model_number) : null, 'eol' => (($asset->asset_eol_date != '') && ($asset->purchase_date != '')) ? Carbon::parse($asset->asset_eol_date)->diffInMonths($asset->purchase_date).' months' : null, 'asset_eol_date' => ($asset->asset_eol_date != '') ? Helper::getFormattedDateObject($asset->asset_eol_date, 'date') : null, @@ -155,7 +155,7 @@ class AssetsTransformer 'clone' => Gate::allows('create', Asset::class) ? true : false, 'restore' => ($asset->deleted_at!='' && Gate::allows('create', Asset::class)) ? true : false, 'update' => ($asset->deleted_at=='' && Gate::allows('update', Asset::class)) ? true : false, - 'delete' => ($asset->deleted_at=='' && $asset->assigned_to =='' && Gate::allows('delete', Asset::class) && ($asset->deleted_at == '')) ? true : false, + 'delete' => ($asset->deleted_at=='' && $asset->assigned_to =='' && Gate::allows('delete', Asset::class) && ($asset->deleted_at == '')) && ($asset->assignedAssets->count() === 0) ? true : false, ]; diff --git a/resources/lang/en-US/general.php b/resources/lang/en-US/general.php index b10e3f4f62..cf0c5d7460 100644 --- a/resources/lang/en-US/general.php +++ b/resources/lang/en-US/general.php @@ -496,6 +496,7 @@ return [ 'no_autoassign_licenses_help' => 'Do not include user for bulk-assigning through the license UI or cli tools.', 'modal_confirm_generic' => 'Are you sure?', 'cannot_be_deleted' => 'This item cannot be deleted', + 'cannot_delete_parent_asset' => 'Cannot delete asset until attached assets are checked in.', 'cannot_be_edited' => 'This item cannot be edited.', 'undeployable_tooltip' => 'This item cannot be checked out. Check the quantity remaining.', 'serial_number' => 'Serial Number', diff --git a/resources/views/partials/bootstrap-table.blade.php b/resources/views/partials/bootstrap-table.blade.php index 70c10edff9..a09dc74c2e 100644 --- a/resources/views/partials/bootstrap-table.blade.php +++ b/resources/views/partials/bootstrap-table.blade.php @@ -299,7 +299,6 @@ return '{{ trans('general.audit') }}'; } - // Make the edit/delete buttons function genericActionsFormatter(owner_name, element_name) { if (!element_name) { @@ -351,9 +350,12 @@ } else { // Do not show the delete button on things that are already deleted if ((row.available_actions) && (row.available_actions.restore != true)) { - actions += '{{ trans('general.cannot_be_deleted') }} '; + var message = "{{trans('general.cannot_be_deleted')}}"; + if(row.child_asset_count > 0){ + message = "{{trans('general.cannot_delete_parent_asset')}}"; + } + actions += '{{ trans('general.cannot_be_deleted') }} '; } - }