Skip to content

A free open source IT asset/license management system

Tweaked dashboard counts

snipe5db5f96998e6
diff --git a/app/Http/Controllers/Api/DashboardController.php b/app/Http/Controllers/Api/DashboardController.php
index 0d09ab659c..e81306dff8 100644
--- a/app/Http/Controllers/Api/DashboardController.php
+++ b/app/Http/Controllers/Api/DashboardController.php
@@ -141,16 +141,22 @@ class DashboardController extends Controller
$query->withCount('showableAssets as assets_count');
$countSortColumns[] = 'assets_count';
}
+ // Accessory / Consumable / Component counts are sum(qty) over
+ // live non-archived children to match the top-box semantic on
+ // the main dashboard. SoftDeletes auto-excludes trashed rows,
+ // notArchived() excludes retired-but-live ones. License stays
+ // as a parent-row count because licensesController hasn't been
+ // audited for the same semantic yet.
if (Gate::allows('view', Accessory::class)) {
- $query->withCount('accessories as accessories_count');
+ $query->withSum(['accessories as accessories_count' => fn ($q) => $q->notArchived()], 'qty');
$countSortColumns[] = 'accessories_count';
}
if (Gate::allows('view', Consumable::class)) {
- $query->withCount('consumables as consumables_count');
+ $query->withSum(['consumables as consumables_count' => fn ($q) => $q->notArchived()], 'qty');
$countSortColumns[] = 'consumables_count';
}
if (Gate::allows('view', Component::class)) {
- $query->withCount('components as components_count');
+ $query->withSum(['components as components_count' => fn ($q) => $q->notArchived()], 'qty');
$countSortColumns[] = 'components_count';
}
if (Gate::allows('view', License::class)) {
@@ -221,16 +227,18 @@ class DashboardController extends Controller
$query->withCount(['assets as assets_count' => fn ($q) => $q->AssetsForShow()]);
$countSortColumns[] = 'assets_count';
}
+ // See categories() above for the sum(qty) rationale on the
+ // three checkoutable types.
if (Gate::allows('view', Accessory::class)) {
- $query->withCount('accessories as accessories_count');
+ $query->withSum(['accessories as accessories_count' => fn ($q) => $q->notArchived()], 'qty');
$countSortColumns[] = 'accessories_count';
}
if (Gate::allows('view', Consumable::class)) {
- $query->withCount('consumables as consumables_count');
+ $query->withSum(['consumables as consumables_count' => fn ($q) => $q->notArchived()], 'qty');
$countSortColumns[] = 'consumables_count';
}
if (Gate::allows('view', Component::class)) {
- $query->withCount('components as components_count');
+ $query->withSum(['components as components_count' => fn ($q) => $q->notArchived()], 'qty');
$countSortColumns[] = 'components_count';
}
if (Gate::allows('view', License::class)) {
@@ -287,16 +295,18 @@ class DashboardController extends Controller
$countSortColumns[] = 'assets_count';
$countSortColumns[] = 'assigned_assets_count';
}
+ // See categories() above for the sum(qty) rationale on the
+ // three checkoutable types.
if (Gate::allows('view', Accessory::class)) {
- $query->withCount('accessories as accessories_count');
+ $query->withSum(['accessories as accessories_count' => fn ($q) => $q->notArchived()], 'qty');
$countSortColumns[] = 'accessories_count';
}
if (Gate::allows('view', Consumable::class)) {
- $query->withCount('consumables as consumables_count');
+ $query->withSum(['consumables as consumables_count' => fn ($q) => $q->notArchived()], 'qty');
$countSortColumns[] = 'consumables_count';
}
if (Gate::allows('view', Component::class)) {
- $query->withCount('components as components_count');
+ $query->withSum(['components as components_count' => fn ($q) => $q->notArchived()], 'qty');
$countSortColumns[] = 'components_count';
}
if (Gate::allows('view', User::class)) {
@@ -315,8 +325,14 @@ class DashboardController extends Controller
'view' => Gate::allows('view', $location),
],
];
+ // array_key_exists on getAttributes() rather than isset
+ // because withSum sets the alias to null when the relation
+ // has no matching children, and isset would drop the key
+ // in that case (yielding a row with a missing column that
+ // the admin reads as "zero but hidden"). The key must stay
+ // visible as 0 so the dashboard column always renders.
foreach (['assets_count', 'assigned_assets_count', 'accessories_count', 'consumables_count', 'components_count', 'users_count'] as $key) {
- if (isset($location->{$key})) {
+ if (array_key_exists($key, $location->getAttributes())) {
$row[$key] = (int) $location->{$key};
}
}
diff --git a/app/Presenters/ConsumablePresenter.php b/app/Presenters/ConsumablePresenter.php
index 8744b4fea3..9478f7ebbf 100644
--- a/app/Presenters/ConsumablePresenter.php
+++ b/app/Presenters/ConsumablePresenter.php
@@ -294,7 +294,7 @@ class ConsumablePresenter extends Presenter
if ($hide_fields !== []) {
$layout = array_values(array_filter(
$layout,
- fn (array $col) => ! in_array($col['field'] ?? null, $hide_fields, true),
+ fn (array $col) => ! in_array($col['field'], $hide_fields, true),
));
}
diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon
index 8c81f19338..8cb74eef43 100644
--- a/phpstan-baseline.neon
+++ b/phpstan-baseline.neon
@@ -1092,6 +1092,18 @@ parameters:
count: 2
path: app/Http/Controllers/Api/ConsumablesController.php
+ -
+ message: '#^Property App\\Models\\IdeHelperActionlog\:\:\$created_at \(Illuminate\\Support\\Carbon\|null\) does not accept string\.$#'
+ identifier: assign.propertyType
+ count: 1
+ path: app/Http/Controllers/Api/ConsumablesController.php
+
+ -
+ message: '#^Relation ''consumable'' is not found in App\\Models\\ConsumableAssignment model\.$#'
+ identifier: larastan.relationExistence
+ count: 1
+ path: app/Http/Controllers/Api/ConsumablesController.php
+
-
message: '#^Access to an undefined property App\\Models\\CustomField\:\:\$pivot\.$#'
identifier: property.notFound
@@ -1890,6 +1902,18 @@ parameters:
count: 1
path: app/Http/Controllers/Consumables/ConsumableCheckoutController.php
+ -
+ message: '#^Property App\\Models\\IdeHelperActionlog\:\:\$created_at \(Illuminate\\Support\\Carbon\|null\) does not accept string\.$#'
+ identifier: assign.propertyType
+ count: 1
+ path: app/Http/Controllers/Consumables/ConsumableCheckoutController.php
+
+ -
+ message: '#^Relation ''consumable'' is not found in App\\Models\\ConsumableAssignment model\.$#'
+ identifier: larastan.relationExistence
+ count: 2
+ path: app/Http/Controllers/Consumables/ConsumableCheckoutController.php
+
-
message: '#^Using nullsafe property access "\?\-\>name" on left side of \?\? is unnecessary\. Use \-\> instead\.$#'
identifier: nullsafe.neverNull
@@ -4812,6 +4836,12 @@ parameters:
count: 1
path: app/Models/Consumable.php
+ -
+ message: '#^Call to an undefined method Illuminate\\Database\\Eloquent\\Relations\\Relation\:\:outstanding\(\)\.$#'
+ identifier: method.notFound
+ count: 1
+ path: app/Models/Consumable.php
+
-
message: '#^Call to an undefined method Illuminate\\Database\\Eloquent\\Relations\\Relation\:\:withTrashed\(\)\.$#'
identifier: method.notFound
diff --git a/tests/Feature/DashboardTest.php b/tests/Feature/DashboardTest.php
index 734162329d..123775a169 100644
--- a/tests/Feature/DashboardTest.php
+++ b/tests/Feature/DashboardTest.php
@@ -67,33 +67,44 @@ class DashboardTest extends TestCase
public function test_counts_are_loaded_correctly_for_admins(): void
{
+ // Accessory / Consumable / Component top boxes report
+ // sum(qty) over live non-archived rows, not the parent-row
+ // count, so the dashboard reflects actual inventory volume.
+ // Seed a mix of live + archived + trashed with explicit qty
+ // so the assertions pin the exclusions rather than relying
+ // on factory defaults.
Asset::factory()->count(2)->create();
- Accessory::factory()->count(2)->create();
+ Accessory::factory()->create(['qty' => 7]);
+ Accessory::factory()->create(['qty' => 5]);
+ Accessory::factory()->archived()->create(['qty' => 100]);
License::factory()->count(2)->create();
- Consumable::factory()->count(2)->create();
- Component::factory()->count(2)->create();
+ Consumable::factory()->create(['qty' => 10]);
+ Consumable::factory()->create(['qty' => 3]);
+ Consumable::factory()->archived()->create(['qty' => 500]);
+ $trashed = Consumable::factory()->create(['qty' => 999]);
+ $trashed->delete();
+ Component::factory()->create(['qty' => 4]);
+ Component::factory()->create(['qty' => 2]);
+ Component::factory()->archived()->create(['qty' => 50]);
$this->actingAs(User::factory()->admin()->create())
->get(route('home'))
->assertViewIs('dashboard')
->assertViewHas('counts', function ($value) {
- $accessoryCount = Accessory::count();
$assetCount = Asset::count();
- $componentCount = Component::count();
- $consumableCount = Consumable::count();
$licenseCount = License::assetcount();
$userCount = User::count();
- $this->assertEquals($value['accessory'], $accessoryCount, 'Accessory count incorrect.');
+ $this->assertSame(12, $value['accessory'], 'Accessory count should sum qty across live non-archived rows (7 + 5).');
+ $this->assertSame(13, $value['consumable'], 'Consumable count should sum qty across live non-archived rows (10 + 3).');
+ $this->assertSame(6, $value['component'], 'Component count should sum qty across live non-archived rows (4 + 2).');
$this->assertEquals($value['asset'], $assetCount, 'Asset count incorrect.');
$this->assertEquals($value['license'], $licenseCount, 'License count incorrect.');
- $this->assertEquals($value['consumable'], $consumableCount, 'Consumable count incorrect.');
- $this->assertEquals($value['component'], $componentCount, 'Component count incorrect.');
$this->assertEquals($value['user'], $userCount, 'User count incorrect.');
- $this->assertEquals(
+ $this->assertSame(
$value['grand_total'],
- $accessoryCount + $assetCount + $consumableCount + $licenseCount,
- 'Grand total count incorrect.'
+ $value['accessory'] + $value['asset'] + $value['consumable'] + $value['license'],
+ 'Grand total should sum asset + accessory + license + consumable using the same unit-based counts.',
);
return true;