A free open source IT asset/license management system
Check for child companies in isDeletable()
snipee35ed82b12de
diff --git a/app/Actions/Companies/DestroyCompanyAction.php b/app/Actions/Companies/DestroyCompanyAction.phpindex 2a236886fd..58f2fe0def 100644--- a/app/Actions/Companies/DestroyCompanyAction.php+++ b/app/Actions/Companies/DestroyCompanyAction.php@@ -4,6 +4,7 @@ namespace App\Actions\Companies;use App\Exceptions\ItemStillHasAccessories;use App\Exceptions\ItemStillHasAssets;+use App\Exceptions\ItemStillHasChildCompanies;use App\Exceptions\ItemStillHasComponents;use App\Exceptions\ItemStillHasConsumables;use App\Exceptions\ItemStillHasLicenses;@@ -21,6 +22,7 @@ class DestroyCompanyAction* @throws ItemStillHasComponents* @throws ItemStillHasConsumables* @throws ItemStillHasUsers+ * @throws ItemStillHasChildCompanies*/public static function run(Company $company): bool{@@ -31,6 +33,10 @@ class DestroyCompanyAction'components as components_count','consumables as consumables_count','users as users_count',+ // Self-relation: children() drops CompanyableScope (which hardcodes+ // companies.id and collides with Eloquent's laravel_reserved_0+ // alias). Match that here or the count aliases fight the scope.+ 'children as children_count' => fn ($q) => $q->withoutGlobalScopes(),]);if ($company->assets_count > 0) {@@ -57,6 +63,10 @@ class DestroyCompanyActionthrow new ItemStillHasUsers($company);}+ if ($company->children_count > 0) {+ throw new ItemStillHasChildCompanies($company);+ }+if ($company->image) {try {Storage::disk('public')->delete('companies/'.$company->image);diff --git a/app/Exceptions/ItemStillHasChildCompanies.php b/app/Exceptions/ItemStillHasChildCompanies.phpnew file mode 100644index 0000000000..6255d38439--- /dev/null+++ b/app/Exceptions/ItemStillHasChildCompanies.php@@ -0,0 +1,5 @@+<?php++namespace App\Exceptions;++class ItemStillHasChildCompanies extends ItemStillHasChildren {}diff --git a/app/Http/Controllers/Api/CompaniesController.php b/app/Http/Controllers/Api/CompaniesController.phpindex 60083df7b1..cd0aa50880 100644--- a/app/Http/Controllers/Api/CompaniesController.php+++ b/app/Http/Controllers/Api/CompaniesController.php@@ -140,8 +140,8 @@ class CompaniesController extends Controller$company = $request->handleImages($company);if ($company->save()) {+ // parent is worth loading so the response reflects the hierarchy the caller just set.$company->loadMissing('parent');- $company->loadCount(['children as children_count' => fn ($q) => $q->withoutGlobalScopes()]);return response()->json(Helper::formatStandardApiResponse('success', (new CompaniesTransformer)->transformCompany($company), trans('admin/companies/message.create.success')));}@@ -163,6 +163,10 @@ class CompaniesController extends Controller{$this->authorize('view', Company::class);$company = Company::with('parent')+ // Same seven counts as index(). isDeletable() reads *_count and+ // falls back to per-relation ->count() queries if these aren't+ // preloaded, which would be six extra hits per show call.+ ->withCount('assets as assets_count', 'licenses as licenses_count', 'accessories as accessories_count', 'consumables as consumables_count', 'components as components_count', 'users as users_count')->withCount(['children as children_count' => fn ($q) => $q->withoutGlobalScopes()])->findOrFail($id);$this->authorize('view', $company);@@ -189,6 +193,9 @@ class CompaniesController extends Controllerif ($company->save()) {$company->loadMissing('parent');+ // Match the index()/show() eager loads so isDeletable() in the+ // transformer doesn't fall through to per-relation count queries.+ $company->loadCount(['assets as assets_count', 'licenses as licenses_count', 'accessories as accessories_count', 'consumables as consumables_count', 'components as components_count', 'users as users_count']);$company->loadCount(['children as children_count' => fn ($q) => $q->withoutGlobalScopes()]);return response()diff --git a/app/Http/Controllers/BulkCompaniesController.php b/app/Http/Controllers/BulkCompaniesController.phpindex 9fdd3542a1..11b5ef78a6 100644--- a/app/Http/Controllers/BulkCompaniesController.php+++ b/app/Http/Controllers/BulkCompaniesController.php@@ -5,6 +5,7 @@ namespace App\Http\Controllers;use App\Actions\Companies\DestroyCompanyAction;use App\Exceptions\ItemStillHasAccessories;use App\Exceptions\ItemStillHasAssets;+use App\Exceptions\ItemStillHasChildCompanies;use App\Exceptions\ItemStillHasComponents;use App\Exceptions\ItemStillHasConsumables;use App\Exceptions\ItemStillHasLicenses;@@ -43,6 +44,8 @@ class BulkCompaniesController extends Controller$errors[] = trans('general.bulk_delete_associations.assoc_consumables_no_count', ['item_name' => $company->name, 'item' => trans('general.company')]);} catch (ItemStillHasUsers $e) {$errors[] = trans('general.bulk_delete_associations.assoc_users_no_count', ['item_name' => $company->name, 'item' => trans('general.company')]);+ } catch (ItemStillHasChildCompanies $e) {+ $errors[] = trans('general.bulk_delete_associations.assoc_child_companies_no_count', ['item_name' => $company->name, 'item' => trans('general.company')]);} catch (\Exception $e) {report($e);$errors[] = trans('general.something_went_wrong');diff --git a/resources/lang/en-US/general.php b/resources/lang/en-US/general.phpindex 8a263c975d..1472bd5d6f 100644--- a/resources/lang/en-US/general.php+++ b/resources/lang/en-US/general.php@@ -740,6 +740,7 @@ return ['assoc_components_no_count' => ':item_name is currently associated with other components and cannot be deleted. Please update your components to no longer reference this :item and try again.','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.',],'breadcrumb_button_actions' => [diff --git a/tests/Feature/Companies/Api/BulkSelectableTest.php b/tests/Feature/Companies/Api/BulkSelectableTest.phpindex 7f456c5ab2..e88d93354d 100644--- a/tests/Feature/Companies/Api/BulkSelectableTest.php+++ b/tests/Feature/Companies/Api/BulkSelectableTest.php@@ -48,4 +48,15 @@ class BulkSelectableTest extends TestCase->assertOk()->assertJsonPath('available_actions.bulk_selectable.delete', false);}++ public function test_parent_company_with_children_is_not_bulk_selectable()+ {+ $parent = Company::factory()->create();+ Company::factory()->create(['parent_id' => $parent->id]);++ $this->actingAsForApi(User::factory()->superuser()->create())+ ->getJson(route('api.companies.show', $parent))+ ->assertOk()+ ->assertJsonPath('available_actions.bulk_selectable.delete', false);+ }}diff --git a/tests/Feature/Companies/Ui/BulkDeleteCompaniesTest.php b/tests/Feature/Companies/Ui/BulkDeleteCompaniesTest.phpindex 60b62bc3bd..75fdb95e48 100644--- a/tests/Feature/Companies/Ui/BulkDeleteCompaniesTest.php+++ b/tests/Feature/Companies/Ui/BulkDeleteCompaniesTest.php@@ -83,6 +83,21 @@ class BulkDeleteCompaniesTest extends TestCase$this->assertNotSoftDeleted($blocked);}+ public function test_parent_company_with_children_is_not_bulk_deleted()+ {+ $parent = Company::factory()->create();+ Company::factory()->create(['parent_id' => $parent->id]);++ $this->actingAs(User::factory()->deleteCompanies()->create())+ ->post(route('companies.bulk.delete'), [+ 'ids' => [$parent->id],+ ])+ ->assertSessionHas('multi_error_messages');++ $this->assertModelExists($parent);+ $this->assertNotSoftDeleted($parent);+ }+public function test_nonexistent_ids_are_reported_and_do_not_break_the_batch(){$deletable = Company::factory()->create();