A free open source IT asset/license management system
Fixed #18505 - make total and remaining sortable on accessories and components
snipee746ef1af3c8
diff --git a/app/Http/Controllers/Api/AccessoriesController.php b/app/Http/Controllers/Api/AccessoriesController.phpindex 5c80059974..fbedeee922 100644--- a/app/Http/Controllers/Api/AccessoriesController.php+++ b/app/Http/Controllers/Api/AccessoriesController.php@@ -171,6 +171,9 @@ class AccessoriesController extends Controllercase 'percent_remaining':$accessories = $accessories->OrderPercentRemaining($order);break;+ case 'remaining':+ $accessories = $accessories->OrderRemaining($order);+ break;default:$accessories = $accessories->orderBy($column_sort, $order);break;diff --git a/app/Http/Controllers/Api/ComponentsController.php b/app/Http/Controllers/Api/ComponentsController.phpindex a6c01aa64a..640a9aa787 100644--- a/app/Http/Controllers/Api/ComponentsController.php+++ b/app/Http/Controllers/Api/ComponentsController.php@@ -167,6 +167,9 @@ class ComponentsController extends Controllercase 'percent_remaining':$components = $components->OrderPercentRemaining($order);break;+ case 'remaining':+ $components = $components->OrderRemaining($order);+ break;case 'purchase_cost':// See AccessoriesController for the rationale — these// three sorts walk order_items rather than removeddiff --git a/app/Models/Accessory.php b/app/Models/Accessory.phpindex 74f37d57e4..b2e0995f5f 100755--- a/app/Models/Accessory.php+++ b/app/Models/Accessory.php@@ -585,4 +585,19 @@ class Accessory extends SnipeModelreturn $query->orderByRaw('CASE WHEN accessories.qty = 0 THEN 0 ELSE ((accessories.qty - checkouts_count) * 100.0 / accessories.qty) END '.$direction);}++ /**+ * Query builder scope to sort by the raw `remaining` column+ * (qty minus current checkouts). Same withCount-added alias as+ * scopeOrderPercentRemaining above; the difference is that this+ * one sorts by absolute count rather than percentage, so items+ * with the same absolute stock left group together regardless+ * of their total qty.+ */+ public function scopeOrderRemaining($query, $order)+ {+ $direction = strtolower($order) === 'asc' ? 'asc' : 'desc';++ return $query->orderByRaw('(accessories.qty - checkouts_count) ' . $direction);+ }}diff --git a/app/Models/Component.php b/app/Models/Component.phpindex 1b2616cfd6..223a782db7 100644--- a/app/Models/Component.php+++ b/app/Models/Component.php@@ -532,4 +532,19 @@ class Component extends SnipeModelreturn $query->orderByRaw('CASE WHEN components.qty = 0 THEN 0 ELSE ((components.qty - COALESCE(sum_unconstrained_assets, 0)) * 100.0 / components.qty) END '.$direction);}++ /**+ * Query builder scope to sort by the raw `remaining` column+ * (qty minus current checkouts). Same sum_unconstrained_assets+ * alias as scopeOrderPercentRemaining above; the difference is+ * that this one sorts by absolute count rather than percentage,+ * so items with the same absolute stock left group together+ * regardless of their total qty.+ */+ public function scopeOrderRemaining($query, $order)+ {+ $direction = strtolower($order) === 'asc' ? 'asc' : 'desc';++ return $query->orderByRaw('(components.qty - COALESCE(sum_unconstrained_assets, 0)) ' . $direction);+ }}diff --git a/app/Presenters/AccessoryPresenter.php b/app/Presenters/AccessoryPresenter.phpindex 79b0125d2c..7e11f1b56c 100644--- a/app/Presenters/AccessoryPresenter.php+++ b/app/Presenters/AccessoryPresenter.php@@ -117,7 +117,11 @@ class AccessoryPresenter extends Presenter'field' => 'remaining','scope' => 'col','searchable' => false,- 'sortable' => false,+ 'sortable' => true,+ // Kept hidden by default so the primary "% Remaining"+ // progress bar column stays the headline signal; users+ // who want raw-count sorting can toggle this on via+ // the bootstrap-table column picker (issue #18505).'visible' => false,'title' => trans('admin/accessories/general.remaining'),'footerFormatter' => 'qtySumFormatter',diff --git a/app/Presenters/ComponentPresenter.php b/app/Presenters/ComponentPresenter.phpindex 1100f8cbf5..f60306f0ea 100644--- a/app/Presenters/ComponentPresenter.php+++ b/app/Presenters/ComponentPresenter.php@@ -129,7 +129,7 @@ class ComponentPresenter extends Presenter'field' => 'remaining','scope' => 'col','searchable' => false,- 'sortable' => false,+ 'sortable' => true,'title' => trans('admin/components/general.remaining'),'visible' => true,'class' => 'text-right text-padding-number-cell',diff --git a/tests/Feature/Accessories/Api/IndexAccessoryTest.php b/tests/Feature/Accessories/Api/IndexAccessoryTest.phpindex afdf6e0dda..59a1c0a952 100644--- a/tests/Feature/Accessories/Api/IndexAccessoryTest.php+++ b/tests/Feature/Accessories/Api/IndexAccessoryTest.php@@ -156,4 +156,40 @@ class IndexAccessoryTest extends TestCase implements TestsFullMultipleCompaniesS->assertResponseDoesNotContainInRows($otherAccessory);}}++ public function test_can_sort_accessories_by_raw_remaining_count()+ {+ // Requested in issue #18505 alongside the percent_remaining sort:+ // sort by absolute stock left (qty - active checkouts). The scope+ // references the withCount-added `checkouts_count` alias, so a+ // regression in the API's eager-load order would surface here as+ // a SQL error or a null-count sort.+ $user = User::factory()->viewAccessories()->create();++ $lots = Accessory::factory()->create(['name' => 'Lots left', 'qty' => 10]);+ $some = Accessory::factory()->create(['name' => 'Some left', 'qty' => 10]);+ $none = Accessory::factory()->create(['name' => 'None left', 'qty' => 10]);++ AccessoryCheckout::factory()->count(1)->create(['accessory_id' => $lots->id]); // remaining = 9+ AccessoryCheckout::factory()->count(5)->create(['accessory_id' => $some->id]); // remaining = 5+ AccessoryCheckout::factory()->count(10)->create(['accessory_id' => $none->id]); // remaining = 0++ $descRows = $this->actingAsForApi($user)+ ->getJson(route('api.accessories.index', ['sort' => 'remaining', 'order' => 'desc']))+ ->assertOk()+ ->json('rows');++ $descNames = array_column($descRows, 'name');+ $descRelevant = array_values(array_intersect($descNames, ['Lots left', 'Some left', 'None left']));+ $this->assertSame(['Lots left', 'Some left', 'None left'], $descRelevant);++ $ascRows = $this->actingAsForApi($user)+ ->getJson(route('api.accessories.index', ['sort' => 'remaining', 'order' => 'asc']))+ ->assertOk()+ ->json('rows');++ $ascNames = array_column($ascRows, 'name');+ $ascRelevant = array_values(array_intersect($ascNames, ['Lots left', 'Some left', 'None left']));+ $this->assertSame(['None left', 'Some left', 'Lots left'], $ascRelevant);+ }}diff --git a/tests/Feature/Components/Api/ComponentIndexTest.php b/tests/Feature/Components/Api/ComponentIndexTest.phpindex f428eddac5..50da652318 100644--- a/tests/Feature/Components/Api/ComponentIndexTest.php+++ b/tests/Feature/Components/Api/ComponentIndexTest.php@@ -2,6 +2,7 @@namespace Tests\Feature\Components\Api;+use App\Models\Asset;use App\Models\Category;use App\Models\Company;use App\Models\Component;@@ -118,4 +119,45 @@ class ComponentIndexTest extends TestCase->assertResponseDoesNotContainInRows($otherComponent);}}++ public function test_can_sort_components_by_raw_remaining_count()+ {+ // Requested in issue #18505 alongside the percent_remaining sort:+ // sort by absolute stock left (qty - active assignments). The+ // scope references the withSum-added `sum_unconstrained_assets`+ // alias, so a regression in the API's eager-load order would+ // surface here as a SQL error or a null-count sort.+ $user = User::factory()->viewComponents()->create();++ $lots = Component::factory()->create(['name' => 'Lots left', 'qty' => 10]);+ $some = Component::factory()->create(['name' => 'Some left', 'qty' => 10]);+ $none = Component::factory()->create(['name' => 'None left', 'qty' => 10]);++ $asset = Asset::factory()->create();++ // assigned_qty on the pivot is what withSum('unconstrainedAssets')+ // sums into sum_unconstrained_assets, so one pivot row per+ // component with a distinct qty gives distinct remaining counts.+ $lots->assets()->attach($asset->id, ['assigned_qty' => 1, 'created_at' => now(), 'created_by' => $user->id]); // remaining = 9+ $some->assets()->attach($asset->id, ['assigned_qty' => 5, 'created_at' => now(), 'created_by' => $user->id]); // remaining = 5+ $none->assets()->attach($asset->id, ['assigned_qty' => 10, 'created_at' => now(), 'created_by' => $user->id]); // remaining = 0++ $descRows = $this->actingAsForApi($user)+ ->getJson(route('api.components.index', ['sort' => 'remaining', 'order' => 'desc']))+ ->assertOk()+ ->json('rows');++ $descNames = array_column($descRows, 'name');+ $descRelevant = array_values(array_intersect($descNames, ['Lots left', 'Some left', 'None left']));+ $this->assertSame(['Lots left', 'Some left', 'None left'], $descRelevant);++ $ascRows = $this->actingAsForApi($user)+ ->getJson(route('api.components.index', ['sort' => 'remaining', 'order' => 'asc']))+ ->assertOk()+ ->json('rows');++ $ascNames = array_column($ascRows, 'name');+ $ascRelevant = array_values(array_intersect($ascNames, ['Lots left', 'Some left', 'None left']));+ $this->assertSame(['None left', 'Some left', 'Lots left'], $ascRelevant);+ }}