Skip to content

A free open source IT asset/license management system

Fixed #17565 -

snipe62fde2ac3eb6
diff --git a/app/Http/Controllers/Api/LocationsController.php b/app/Http/Controllers/Api/LocationsController.php
index 9c18bf4abb..97c4c52903 100644
--- a/app/Http/Controllers/Api/LocationsController.php
+++ b/app/Http/Controllers/Api/LocationsController.php
@@ -104,11 +104,17 @@ class LocationsController extends Controller
'locations.created_by',
'locations.deleted_at',
])
- ->withCount('assignedAssets as assigned_assets_count')
- ->withCount('assets as assets_count')
+ // Asset counts thread through AssetsForShow so the API totals
+ // agree with the tab counts in resources/views/locations/view.blade.php,
+ // which use ->assets()->AssetsForShow() etc. Without the scope,
+ // rtd_assets_count in particular ignores the show_archived_in_list
+ // setting because the rtd_assets() relation has no built-in status
+ // filter. See #17565.
+ ->withCount(['assignedAssets as assigned_assets_count' => fn ($q) => $q->AssetsForShow()])
+ ->withCount(['assets as assets_count' => fn ($q) => $q->AssetsForShow()])
->withCount('assignedAccessories as assigned_accessories_count')
->withCount('accessories as accessories_count')
- ->withCount('rtd_assets as rtd_assets_count')
+ ->withCount(['rtd_assets as rtd_assets_count' => fn ($q) => $q->AssetsForShow()])
->withCount('children as children_count')
->withCount('users as users_count')
->withCount('consumables as consumables_count')
@@ -279,11 +285,12 @@ class LocationsController extends Controller
'locations.notes',
'locations.tag_color',
])
- ->withCount('assignedAssets as assigned_assets_count')
- ->withCount('assets as assets_count')
+ // See index() for why the asset counts thread AssetsForShow. #17565.
+ ->withCount(['assignedAssets as assigned_assets_count' => fn ($q) => $q->AssetsForShow()])
+ ->withCount(['assets as assets_count' => fn ($q) => $q->AssetsForShow()])
->withCount('assignedAccessories as assigned_accessories_count')
->withCount('accessories as accessories_count')
- ->withCount('rtd_assets as rtd_assets_count')
+ ->withCount(['rtd_assets as rtd_assets_count' => fn ($q) => $q->AssetsForShow()])
->withCount('children as children_count')
->withCount('users as users_count')
->withCount('consumables as consumables_count')
diff --git a/tests/Feature/Locations/Api/LocationsViewTest.php b/tests/Feature/Locations/Api/LocationsViewTest.php
index 3768d16d56..d326d96e36 100644
--- a/tests/Feature/Locations/Api/LocationsViewTest.php
+++ b/tests/Feature/Locations/Api/LocationsViewTest.php
@@ -4,6 +4,7 @@ namespace Tests\Feature\Locations\Api;
use App\Models\Asset;
use App\Models\Location;
+use App\Models\Statuslabel;
use App\Models\User;
use Tests\TestCase;
@@ -58,4 +59,61 @@ class LocationsViewTest extends TestCase
'total' => 3,
]);
}
+
+ /**
+ * Regression for #17565. rtd_assets_count in the API used to ignore
+ * archived assets while the tab count on the location detail view
+ * excluded them, producing mismatched numbers between UI and API.
+ * The show endpoint now threads AssetsForShow into the withCount
+ * closure so both surfaces agree.
+ */
+ public function test_show_rtd_assets_count_excludes_archived_when_setting_off()
+ {
+ $this->settings->set(['show_archived_in_list' => 0]);
+
+ $location = Location::factory()->create();
+ $deployableStatus = Statuslabel::factory()->rtd()->create();
+ $archivedStatus = Statuslabel::factory()->archived()->create();
+
+ Asset::factory()->count(4)->create([
+ 'rtd_location_id' => $location->id,
+ 'status_id' => $deployableStatus->id,
+ ]);
+ Asset::factory()->count(1)->create([
+ 'rtd_location_id' => $location->id,
+ 'status_id' => $archivedStatus->id,
+ ]);
+
+ Statuslabel::clearIdCache();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.locations.show', $location->id))
+ ->assertOk()
+ ->assertJson(['rtd_assets_count' => 4]);
+ }
+
+ public function test_show_rtd_assets_count_includes_archived_when_setting_on()
+ {
+ $this->settings->set(['show_archived_in_list' => 1]);
+
+ $location = Location::factory()->create();
+ $deployableStatus = Statuslabel::factory()->rtd()->create();
+ $archivedStatus = Statuslabel::factory()->archived()->create();
+
+ Asset::factory()->count(4)->create([
+ 'rtd_location_id' => $location->id,
+ 'status_id' => $deployableStatus->id,
+ ]);
+ Asset::factory()->count(1)->create([
+ 'rtd_location_id' => $location->id,
+ 'status_id' => $archivedStatus->id,
+ ]);
+
+ Statuslabel::clearIdCache();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.locations.show', $location->id))
+ ->assertOk()
+ ->assertJson(['rtd_assets_count' => 5]);
+ }
}