From 0d0984a400c0fb86e9c0a1c3035ad3faa5bb725f Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Tue, 19 Mar 2024 14:26:40 -0700 Subject: [PATCH 1/5] Bulk Delete Locations does not work [sc-25100] --- app/Models/Location.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/Models/Location.php b/app/Models/Location.php index 2965ff2fc..a27f760ea 100755 --- a/app/Models/Location.php +++ b/app/Models/Location.php @@ -106,11 +106,12 @@ class Location extends SnipeModel */ public function isDeletable() { + return Gate::allows('delete', $this) - && ($this->assets_count === 0) - && ($this->assigned_assets_count === 0) - && ($this->children_count === 0) - && ($this->users_count === 0); + && ($this->assets_count == 0) + && ($this->assigned_assets_count == 0) + && ($this->children_count == 0) + && ($this->users_count == 0); } /** From e8dc634a40f9b1eca46a5309adad9616c7791755 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Tue, 19 Mar 2024 14:30:53 -0700 Subject: [PATCH 2/5] fixes translation usage --- app/Http/Controllers/LocationsController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/LocationsController.php b/app/Http/Controllers/LocationsController.php index 7a3691684..83614f7e4 100755 --- a/app/Http/Controllers/LocationsController.php +++ b/app/Http/Controllers/LocationsController.php @@ -351,7 +351,7 @@ class LocationsController extends Controller if ($error_count > 0) { return redirect() ->route('locations.index') - ->with('warning', trans('general.bulk.partial_success', + ->with('warning', trans('general.bulk.partial', ['success' => $success_count, 'error' => $error_count, 'object_type' => trans('general.locations')] )); } From 38a3e36cd67571714264d6ff3dbb304edc0bfa4e Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Tue, 19 Mar 2024 14:32:21 -0700 Subject: [PATCH 3/5] fixes translation usage --- app/Http/Controllers/LocationsController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/LocationsController.php b/app/Http/Controllers/LocationsController.php index 83614f7e4..ce2414ab3 100755 --- a/app/Http/Controllers/LocationsController.php +++ b/app/Http/Controllers/LocationsController.php @@ -351,7 +351,7 @@ class LocationsController extends Controller if ($error_count > 0) { return redirect() ->route('locations.index') - ->with('warning', trans('general.bulk.partial', + ->with('warning', trans('general.bulk.delete.partial', ['success' => $success_count, 'error' => $error_count, 'object_type' => trans('general.locations')] )); } From 090466123f727801279040d1259744d656b672c6 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Tue, 19 Mar 2024 15:18:18 -0700 Subject: [PATCH 4/5] add withCount to query instead --- app/Http/Controllers/LocationsController.php | 7 ++++++- app/Models/Location.php | 8 ++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/LocationsController.php b/app/Http/Controllers/LocationsController.php index ce2414ab3..897d12758 100755 --- a/app/Http/Controllers/LocationsController.php +++ b/app/Http/Controllers/LocationsController.php @@ -320,7 +320,12 @@ class LocationsController extends Controller $locations_raw_array = $request->input('ids'); if ((is_array($locations_raw_array)) && (count($locations_raw_array) > 0)) { - $locations = Location::whereIn('id', $locations_raw_array)->get(); + $locations = Location::whereIn('id', $locations_raw_array) + ->withCount('assignedAssets as assigned_assets_count') + ->withCount('assets as assets_count') + ->withCount('rtd_assets as rtd_assets_count') + ->withCount('children as children_count') + ->withCount('users as users_count')->get(); $success_count = 0; $error_count = 0; diff --git a/app/Models/Location.php b/app/Models/Location.php index a27f760ea..9f4c55126 100755 --- a/app/Models/Location.php +++ b/app/Models/Location.php @@ -108,10 +108,10 @@ class Location extends SnipeModel { return Gate::allows('delete', $this) - && ($this->assets_count == 0) - && ($this->assigned_assets_count == 0) - && ($this->children_count == 0) - && ($this->users_count == 0); + && ($this->assets_count === 0) + && ($this->assigned_assets_count === 0) + && ($this->children_count === 0) + && ($this->users_count === 0); } /** From 62745923cf1a80fcb222981bc4ea8f443cbcdb40 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Tue, 19 Mar 2024 15:25:28 -0700 Subject: [PATCH 5/5] fixes API location delete --- app/Http/Controllers/Api/LocationsController.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/LocationsController.php b/app/Http/Controllers/Api/LocationsController.php index e1c79dfbe..b46c13e13 100644 --- a/app/Http/Controllers/Api/LocationsController.php +++ b/app/Http/Controllers/Api/LocationsController.php @@ -235,7 +235,13 @@ class LocationsController extends Controller public function destroy($id) { $this->authorize('delete', Location::class); - $location = Location::findOrFail($id); + $location = Location::withCount('assignedAssets as assigned_assets_count') + ->withCount('assets as assets_count') + ->withCount('rtd_assets as rtd_assets_count') + ->withCount('children as children_count') + ->withCount('users as users_count') + ->findOrFail($id); + if (! $location->isDeletable()) { return response() ->json(Helper::formatStandardApiResponse('error', null, trans('admin/companies/message.assoc_users')));