From 44ee287cc0a10a60b08b4590c3ccd380c3b5de4c Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 8 Apr 2025 12:34:00 +0100 Subject: [PATCH 01/10] Nicer formatting Signed-off-by: snipe --- app/Helpers/Helper.php | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php index e7804da0ea..d37f4104dc 100644 --- a/app/Helpers/Helper.php +++ b/app/Helpers/Helper.php @@ -1563,11 +1563,24 @@ class Helper $location_company = $location->company_id; } - // depending on the relationship we must use different operations to retrieve the objects - $keywords_relation = ['many' => ['users', 'assets', 'rtd_assets', 'consumables', 'components', 'accessories', 'assignedAssets', 'assignedAccessories'], - 'one' => ['parent', 'manager']]; + // depending on the relationship, we must use different operations to retrieve the objects + $keywords_relation = [ + 'many' => [ + 'users', + 'assets', + 'rtd_assets', + 'consumables', + 'components', + 'accessories', + 'assignedAssets', + 'assignedAccessories' + ], + 'one' => [ + 'parent', + 'manager' + ]]; - // In case of a single location the children must be checked either, becuase we don't walk every location + // In case of a single location, the children must be checked as well, because we don't walk every location if ($location_id) { $keywords_relation['many'][] = 'children'; } @@ -1575,14 +1588,20 @@ class Helper foreach ($keywords_relation as $relation => $keywords) { foreach($keywords as $keyword) { if ($relation == 'many') { - $items = $location->$keyword->all(); + $items = $location->{$keyword}->all(); } else { $items = collect([])->push($location->$keyword); } foreach ($items as $item) { if ($item && $item->company_id != $location_company) { - $ret[] = 'type: ' . get_class($item) . ', id: ' . $item->id . ', company_id: ' . $item->company_id . ', location company_id: ' . $location_company; + $row[] = [ + $ret['type'] = class_basename(get_class($item)), + $ret['id'] = $item->id, + $ret['company_id'] = $item->company_id, + $ret['location_company_id'] = $location_company ?? 'null', + ]; + // when not called from artisan command we bail out on the first error if (!$artisan) { return $ret; @@ -1592,6 +1611,6 @@ class Helper } } } - return $ret; + return $row; } } From c42f53e846cc731379a5a261ce8fa3a2f4049305 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 8 Apr 2025 12:34:11 +0100 Subject: [PATCH 02/10] Use table layout Signed-off-by: snipe --- app/Console/Commands/TestLocationsFMCS.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/app/Console/Commands/TestLocationsFMCS.php b/app/Console/Commands/TestLocationsFMCS.php index fb606e6db4..566ebe2229 100644 --- a/app/Console/Commands/TestLocationsFMCS.php +++ b/app/Console/Commands/TestLocationsFMCS.php @@ -19,25 +19,27 @@ class TestLocationsFMCS extends Command * * @var string */ - protected $description = 'Test for inconsistencies if FullMultipleCompanySupport with scoped locations will be used'; + protected $description = 'Test for company ID inconsistencies if FullMultipleCompanySupport with scoped locations will be used.'; /** * Execute the console command. */ public function handle() { - $this->info('Test for inconsistencies if FullMultipleCompanySupport with scoped locations will be used'); - $this->info('Depending on the database size this will take a while, output will be displayed after the complete test is over'); + $this->info('This script checks for company ID inconsistencies if Full Multiple Company Support with scoped locations will be used.'); + $this->info('This could take few moments if have a very large dataset.'); + $this->newLine(); // if parameter location_id is set, only test this location $location_id = null; if ($this->option('location_id')) { $location_id = $this->option('location_id'); } - $ret = Helper::test_locations_fmcs(true, $location_id); - foreach($ret as $output) { - $this->info($output); - } + + $ret = Helper::test_locations_fmcs(true, $location_id); + $this->warn('There are '.count($ret).' items in the database that need your attention before you can enable location scoping.'); + $this->table(['Item Type', 'Item ID', 'Item Company ID', 'Location Company ID'], $ret); + } } From e0a04fe1ceaaa75d53b32273350cdc899e65d62c Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 8 Apr 2025 15:12:19 +0100 Subject: [PATCH 03/10] Localized message Signed-off-by: snipe --- resources/lang/en-US/admin/settings/message.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/resources/lang/en-US/admin/settings/message.php b/resources/lang/en-US/admin/settings/message.php index 9be2901747..9a0313f298 100644 --- a/resources/lang/en-US/admin/settings/message.php +++ b/resources/lang/en-US/admin/settings/message.php @@ -50,5 +50,10 @@ return [ 'error_misc' => 'Something went wrong. :( ', 'webhook_fail' => ' webhook notification failed: Check to make sure the URL is still valid.', 'webhook_channel_not_found' => ' webhook channel not found.' - ] + ], + + 'location_scoping' => [ + 'mismatch' => 'There is 1 item in the database that need your attention before you can enable location scoping. Your settings were not saved.| There are :count items in the database that need your attention before you can enable location scoping. Your settings were not saved.', + ], + ]; From 420278c63b5ea228e5c3de3d145f30d1e7a16abf Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 8 Apr 2025 15:12:36 +0100 Subject: [PATCH 04/10] Nicer layout for settings Signed-off-by: snipe --- resources/views/settings/general.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/settings/general.blade.php b/resources/views/settings/general.blade.php index 230702c841..481fa90921 100644 --- a/resources/views/settings/general.blade.php +++ b/resources/views/settings/general.blade.php @@ -36,7 +36,7 @@
-
+
From a96abeac5fe407b5362645a62fb2512503caf9a3 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 8 Apr 2025 15:12:58 +0100 Subject: [PATCH 05/10] Use localization w/trans_choice Signed-off-by: snipe --- app/Http/Controllers/SettingsController.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index bca72a911e..feca50ddd9 100644 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -325,9 +325,9 @@ class SettingsController extends Controller // check for inconsistencies when activating scoped locations if ($old_locations_fmcs == '0' && $setting->scope_locations_fmcs == '1') { - $ret = Helper::test_locations_fmcs(false); - if (count($ret) != 0) { - return redirect()->back()->withInput()->with('error', 'Inconsistencies with scoped locations found, please use php artisan snipeit:test-locations-fmcs for details'); + $mismatched = Helper::test_locations_fmcs(false); + if (count($mismatched) != 0) { + return redirect()->back()->withInput()->with('error', trans_choice('admin/settings/message.location_scoping.mismatch', count($mismatched))); } } From da2c76022787e3e3adcb264f60f26bd06445056b Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 8 Apr 2025 15:13:19 +0100 Subject: [PATCH 06/10] Renamed variables, skipped breaking out of loop Signed-off-by: snipe --- app/Helpers/Helper.php | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php index d37f4104dc..546d72735f 100644 --- a/app/Helpers/Helper.php +++ b/app/Helpers/Helper.php @@ -1544,7 +1544,7 @@ class Helper * @return string [] */ static public function test_locations_fmcs($artisan, $location_id = null, $new_company_id = null) { - $ret = []; + $mismatched = []; if ($location_id) { $location = Location::find($location_id); @@ -1566,18 +1566,18 @@ class Helper // depending on the relationship, we must use different operations to retrieve the objects $keywords_relation = [ 'many' => [ - 'users', - 'assets', - 'rtd_assets', - 'consumables', - 'components', 'accessories', + 'assets', + 'assignedAccessories', 'assignedAssets', - 'assignedAccessories' + 'components', + 'consumables', + 'rtd_assets', + 'users', ], 'one' => [ + 'manager', 'parent', - 'manager' ]]; // In case of a single location, the children must be checked as well, because we don't walk every location @@ -1595,22 +1595,18 @@ class Helper foreach ($items as $item) { if ($item && $item->company_id != $location_company) { - $row[] = [ - $ret['type'] = class_basename(get_class($item)), - $ret['id'] = $item->id, - $ret['company_id'] = $item->company_id, - $ret['location_company_id'] = $location_company ?? 'null', + $mismatched[] = [ + class_basename(get_class($item)), + $item->id, + $item->company_id, + $location_company ?? 'null', ]; - // when not called from artisan command we bail out on the first error - if (!$artisan) { - return $ret; - } } } } } } - return $row; + return $mismatched; } } From 34f8f50a4ae5726c33bd2f895c549acbf88a731a Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 8 Apr 2025 15:13:57 +0100 Subject: [PATCH 07/10] Use localization, turned display into table Signed-off-by: snipe --- app/Console/Commands/TestLocationsFMCS.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/Console/Commands/TestLocationsFMCS.php b/app/Console/Commands/TestLocationsFMCS.php index 566ebe2229..488cca011b 100644 --- a/app/Console/Commands/TestLocationsFMCS.php +++ b/app/Console/Commands/TestLocationsFMCS.php @@ -36,10 +36,14 @@ class TestLocationsFMCS extends Command $location_id = $this->option('location_id'); } + $mismatched = Helper::test_locations_fmcs(true, $location_id); + $this->warn(trans_choice('admin/settings/message.location_scoping.mismatch', count($mismatched))); - $ret = Helper::test_locations_fmcs(true, $location_id); - $this->warn('There are '.count($ret).' items in the database that need your attention before you can enable location scoping.'); - $this->table(['Item Type', 'Item ID', 'Item Company ID', 'Location Company ID'], $ret); + $header = ['Item Type', 'Item ID', 'Item Company ID', 'Location Company ID']; + sort($mismatched); + + $this->table($header, $mismatched); } + } From 548ae49c69ea81c93b58f1f86f950a9ca3f30416 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 8 Apr 2025 15:30:07 +0100 Subject: [PATCH 08/10] Small tweaks to table Signed-off-by: snipe --- app/Console/Commands/TestLocationsFMCS.php | 4 +++- app/Helpers/Helper.php | 7 +++++-- app/Http/Controllers/SettingsController.php | 2 +- resources/lang/en-US/admin/settings/message.php | 3 ++- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/Console/Commands/TestLocationsFMCS.php b/app/Console/Commands/TestLocationsFMCS.php index 488cca011b..d71c41f960 100644 --- a/app/Console/Commands/TestLocationsFMCS.php +++ b/app/Console/Commands/TestLocationsFMCS.php @@ -38,8 +38,10 @@ class TestLocationsFMCS extends Command $mismatched = Helper::test_locations_fmcs(true, $location_id); $this->warn(trans_choice('admin/settings/message.location_scoping.mismatch', count($mismatched))); + $this->newLine(); + $this->info('Edit your locations to associate them with the correct company.'); - $header = ['Item Type', 'Item ID', 'Item Company ID', 'Location Company ID']; + $header = ['Type', 'Name', 'ID', 'Item Company', 'Company ID', 'Item Location', 'Location Company ID']; sort($mismatched); $this->table($header, $mismatched); diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php index 546d72735f..e5857fe4a5 100644 --- a/app/Helpers/Helper.php +++ b/app/Helpers/Helper.php @@ -1597,9 +1597,12 @@ class Helper if ($item && $item->company_id != $location_company) { $mismatched[] = [ class_basename(get_class($item)), + $item->name, $item->id, - $item->company_id, - $location_company ?? 'null', + $item->company->name ?? null, + $item->company_id ?? null, + $item->location->name ?? null, + $location_company ?? null, ]; } diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index feca50ddd9..a6de4466dd 100644 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -327,7 +327,7 @@ class SettingsController extends Controller if ($old_locations_fmcs == '0' && $setting->scope_locations_fmcs == '1') { $mismatched = Helper::test_locations_fmcs(false); if (count($mismatched) != 0) { - return redirect()->back()->withInput()->with('error', trans_choice('admin/settings/message.location_scoping.mismatch', count($mismatched))); + return redirect()->back()->withInput()->with('error', trans_choice('admin/settings/message.location_scoping.mismatch', count($mismatched)).' '.trans('admin/settings/message.location_scoping.not_saved')); } } diff --git a/resources/lang/en-US/admin/settings/message.php b/resources/lang/en-US/admin/settings/message.php index 9a0313f298..8d9b40e956 100644 --- a/resources/lang/en-US/admin/settings/message.php +++ b/resources/lang/en-US/admin/settings/message.php @@ -53,7 +53,8 @@ return [ ], 'location_scoping' => [ - 'mismatch' => 'There is 1 item in the database that need your attention before you can enable location scoping. Your settings were not saved.| There are :count items in the database that need your attention before you can enable location scoping. Your settings were not saved.', + 'not_saved' => 'Your settings were not saved.', + 'mismatch' => 'There is 1 item in the database that need your attention before you can enable location scoping.|There are :count items in the database that need your attention before you can enable location scoping.', ], ]; From ea365e5645c2c29fee3dba754eed886615da3652 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 8 Apr 2025 16:17:57 +0100 Subject: [PATCH 09/10] More fields visible Signed-off-by: snipe --- app/Console/Commands/TestLocationsFMCS.php | 2 +- app/Helpers/Helper.php | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/Console/Commands/TestLocationsFMCS.php b/app/Console/Commands/TestLocationsFMCS.php index d71c41f960..908057565a 100644 --- a/app/Console/Commands/TestLocationsFMCS.php +++ b/app/Console/Commands/TestLocationsFMCS.php @@ -41,7 +41,7 @@ class TestLocationsFMCS extends Command $this->newLine(); $this->info('Edit your locations to associate them with the correct company.'); - $header = ['Type', 'Name', 'ID', 'Item Company', 'Company ID', 'Item Location', 'Location Company ID']; + $header = ['ID', 'Type', 'Name', 'Checkout Type', 'Company ID', 'Item Company','Item Location', 'Location Company', 'Location Company ID']; sort($mismatched); $this->table($header, $mismatched); diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php index e5857fe4a5..75bcc6d01d 100644 --- a/app/Helpers/Helper.php +++ b/app/Helpers/Helper.php @@ -1556,14 +1556,14 @@ class Helper } foreach($locations as $location) { - // in case of an update of a single location use the newly requested company_id + // in case of an update of a single location, use the newly requested company_id if ($new_company_id) { $location_company = $new_company_id; } else { $location_company = $location->company_id; } - // depending on the relationship, we must use different operations to retrieve the objects + // Depending on the relationship, we must use different operations to retrieve the objects $keywords_relation = [ 'many' => [ 'accessories', @@ -1594,14 +1594,17 @@ class Helper } foreach ($items as $item) { + if ($item && $item->company_id != $location_company) { $mismatched[] = [ - class_basename(get_class($item)), - $item->name, $item->id, - $item->company->name ?? null, + class_basename(get_class($item)), + $item->name ?? $item->asset_tag ?? $item->serial ?? $item->username, + str_replace('App\\Models\\', '', $item->assigned_type) ?? null, $item->company_id ?? null, + $item->company->name ?? null, $item->location->name ?? null, + $item->location->company->name ?? null, $location_company ?? null, ]; From 890702f66d009921e82cf25b47cfdc0cbcab3a6e Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 8 Apr 2025 17:47:39 +0100 Subject: [PATCH 10/10] A few more display tweaks Signed-off-by: snipe --- app/Console/Commands/TestLocationsFMCS.php | 2 +- app/Helpers/Helper.php | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/Console/Commands/TestLocationsFMCS.php b/app/Console/Commands/TestLocationsFMCS.php index 908057565a..f14c78063a 100644 --- a/app/Console/Commands/TestLocationsFMCS.php +++ b/app/Console/Commands/TestLocationsFMCS.php @@ -41,7 +41,7 @@ class TestLocationsFMCS extends Command $this->newLine(); $this->info('Edit your locations to associate them with the correct company.'); - $header = ['ID', 'Type', 'Name', 'Checkout Type', 'Company ID', 'Item Company','Item Location', 'Location Company', 'Location Company ID']; + $header = ['Type', 'ID', 'Name', 'Checkout Type', 'Company ID', 'Item Company', 'Item Location', 'Location Company', 'Location Company ID']; sort($mismatched); $this->table($header, $mismatched); diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php index 75bcc6d01d..27a1f99c01 100644 --- a/app/Helpers/Helper.php +++ b/app/Helpers/Helper.php @@ -1597,12 +1597,16 @@ class Helper if ($item && $item->company_id != $location_company) { $mismatched[] = [ - $item->id, class_basename(get_class($item)), + $item->id, $item->name ?? $item->asset_tag ?? $item->serial ?? $item->username, str_replace('App\\Models\\', '', $item->assigned_type) ?? null, $item->company_id ?? null, $item->company->name ?? null, +// $item->defaultLoc->id ?? null, +// $item->defaultLoc->name ?? null, +// $item->defaultLoc->company->id ?? null, +// $item->defaultLoc->company->name ?? null, $item->location->name ?? null, $item->location->company->name ?? null, $location_company ?? null,