Skip to content

A free open source IT asset/license management system

Fixed order by % remaing

snipeecdf7e895cf1
diff --git a/app/Http/Controllers/Api/AccessoriesController.php b/app/Http/Controllers/Api/AccessoriesController.php
index d8947e1e45..505c2e00cb 100644
--- a/app/Http/Controllers/Api/AccessoriesController.php
+++ b/app/Http/Controllers/Api/AccessoriesController.php
@@ -146,6 +146,9 @@ class AccessoriesController extends Controller
case 'total_cost':
$accessories = $accessories->orderByRaw('COALESCE(purchase_cost, 0) * qty '.$order);
break;
+ case 'percent_remaining':
+ $accessories = $accessories->OrderPercentRemaining($order);
+ break;
default:
$accessories = $accessories->orderBy($column_sort, $order);
break;
diff --git a/app/Http/Controllers/Api/AssetModelsController.php b/app/Http/Controllers/Api/AssetModelsController.php
index 4acc3b67cf..821340fc86 100644
--- a/app/Http/Controllers/Api/AssetModelsController.php
+++ b/app/Http/Controllers/Api/AssetModelsController.php
@@ -155,6 +155,9 @@ class AssetModelsController extends Controller
case 'depreciation':
$assetmodels->leftJoin('depreciations as depreciation_sort', 'models.depreciation_id', '=', 'depreciation_sort.id')->orderBy('depreciation_sort.name', $order);
break;
+ case 'percent_remaining':
+ $assetmodels->OrderPercentRemaining($order);
+ break;
default:
$assetmodels->orderBy($sort, $order);
break;
diff --git a/app/Http/Controllers/Api/ComponentsController.php b/app/Http/Controllers/Api/ComponentsController.php
index 0bfe7bc114..7f8af87abe 100644
--- a/app/Http/Controllers/Api/ComponentsController.php
+++ b/app/Http/Controllers/Api/ComponentsController.php
@@ -151,6 +151,9 @@ class ComponentsController extends Controller
case 'created_by':
$components = $components->OrderByCreatedBy($order);
break;
+ case 'percent_remaining':
+ $components = $components->OrderPercentRemaining($order);
+ break;
default:
$components = $components->orderBy($column_sort, $order);
break;
diff --git a/app/Http/Controllers/Api/ConsumablesController.php b/app/Http/Controllers/Api/ConsumablesController.php
index 17128748f6..6d75cc93f1 100644
--- a/app/Http/Controllers/Api/ConsumablesController.php
+++ b/app/Http/Controllers/Api/ConsumablesController.php
@@ -131,6 +131,9 @@ class ConsumablesController extends Controller
case 'remaining':
$consumables = $consumables->OrderRemaining($order);
break;
+ case 'percent_remaining':
+ $consumables = $consumables->OrderPercentRemaining($order);
+ break;
case 'supplier':
$consumables = $consumables->OrderSupplier($order);
break;
diff --git a/app/Http/Controllers/Api/LicensesController.php b/app/Http/Controllers/Api/LicensesController.php
index 60be0cf719..06168fc955 100644
--- a/app/Http/Controllers/Api/LicensesController.php
+++ b/app/Http/Controllers/Api/LicensesController.php
@@ -154,6 +154,9 @@ class LicensesController extends Controller
case 'product_key':
$licenses = $licenses->orderBy('licenses.serial', $order);
break;
+ case 'percent_remaining':
+ $licenses = $licenses->OrderPercentRemaining($order);
+ break;
default:
$allowed_columns =
[
diff --git a/app/Models/Accessory.php b/app/Models/Accessory.php
index b9b5804f2d..addd3cf9e6 100755
--- a/app/Models/Accessory.php
+++ b/app/Models/Accessory.php
@@ -535,4 +535,25 @@ class Accessory extends SnipeModel
{
return $query->leftJoin('suppliers', 'accessories.supplier_id', '=', 'suppliers.id')->orderBy('suppliers.name', $order);
}
+
+ /**
+ * Query builder scope to sort by the calculated `% remaining` column.
+ *
+ * Mirrors Accessory::percentRemaining(): (qty - checkouts_count) / qty * 100.
+ * checkouts_count is added by withCount() in the API index() before
+ * this scope runs. Guards against division by zero for accessories
+ * with qty of 0.
+ *
+ * PostgreSQL note: references a SELECT-list alias inside a compound
+ * ORDER BY expression, which PostgreSQL rejects per SQL standard.
+ * Snipe-IT officially supports MySQL/MariaDB and tests on SQLite
+ * (both allow this); moving to PostgreSQL would require inlining
+ * the subquery or wrapping the query in an outer SELECT.
+ */
+ public function scopeOrderPercentRemaining($query, $order)
+ {
+ $direction = strtolower($order) === 'asc' ? 'asc' : 'desc';
+
+ return $query->orderByRaw('CASE WHEN accessories.qty = 0 THEN 0 ELSE ((accessories.qty - checkouts_count) * 100.0 / accessories.qty) END '.$direction);
+ }
}
diff --git a/app/Models/AssetModel.php b/app/Models/AssetModel.php
index ad03a5b068..13822079f2 100755
--- a/app/Models/AssetModel.php
+++ b/app/Models/AssetModel.php
@@ -408,4 +408,29 @@ class AssetModel extends SnipeModel
{
return $query->leftJoin('users as admin_sort', 'models.created_by', '=', 'admin_sort.id')->select('models.*')->orderBy('admin_sort.first_name', $order)->orderBy('admin_sort.last_name', $order);
}
+
+ /**
+ * Query builder scope to sort by the calculated `% remaining` column.
+ *
+ * `% remaining` is (available / total) * 100 — see percentRemaining().
+ * The caller (Api\AssetModelsController::index) already adds the
+ * `remaining` and `assets_count` withCount aliases before applying
+ * this scope, so we reference them directly in ORDER BY. Guarded
+ * against division by zero for models with no assets. Uses
+ * orderByRaw because Laravel's query builder has no arithmetic API
+ * for ORDER BY expressions.
+ *
+ * PostgreSQL note: this expression references SELECT-list aliases
+ * inside a compound ORDER BY expression, which PostgreSQL rejects
+ * per SQL standard. Snipe-IT officially supports MySQL/MariaDB and
+ * tests on SQLite (both allow this); moving to PostgreSQL would
+ * require inlining the subqueries or wrapping the query in an
+ * outer SELECT.
+ */
+ public function scopeOrderPercentRemaining($query, $order)
+ {
+ $direction = strtolower($order) === 'asc' ? 'asc' : 'desc';
+
+ return $query->orderByRaw('CASE WHEN assets_count = 0 THEN 0 ELSE (remaining * 100.0 / assets_count) END '.$direction);
+ }
}
diff --git a/app/Models/Component.php b/app/Models/Component.php
index cb3201bb3a..e5a7017095 100644
--- a/app/Models/Component.php
+++ b/app/Models/Component.php
@@ -480,4 +480,25 @@ class Component extends SnipeModel
{
return $query->leftJoin('users as admin_sort', 'components.created_by', '=', 'admin_sort.id')->select('components.*')->orderBy('admin_sort.first_name', $order)->orderBy('admin_sort.last_name', $order);
}
+
+ /**
+ * Query builder scope to sort by the calculated `% remaining` column.
+ *
+ * Mirrors Component::percentRemaining(): (qty - numCheckedOut) / qty * 100.
+ * sum_unconstrained_assets is added by withSum() in the API index()
+ * as the total checked-out quantity. Guards against division by zero
+ * for components with qty of 0.
+ *
+ * PostgreSQL note: references a SELECT-list alias inside a compound
+ * ORDER BY expression, which PostgreSQL rejects per SQL standard.
+ * Snipe-IT officially supports MySQL/MariaDB and tests on SQLite
+ * (both allow this); moving to PostgreSQL would require inlining
+ * the subquery or wrapping the query in an outer SELECT.
+ */
+ public function scopeOrderPercentRemaining($query, $order)
+ {
+ $direction = strtolower($order) === 'asc' ? 'asc' : 'desc';
+
+ return $query->orderByRaw('CASE WHEN components.qty = 0 THEN 0 ELSE ((components.qty - COALESCE(sum_unconstrained_assets, 0)) * 100.0 / components.qty) END '.$direction);
+ }
}
diff --git a/app/Models/Consumable.php b/app/Models/Consumable.php
index 9b3df72b01..bc0c63e6ab 100644
--- a/app/Models/Consumable.php
+++ b/app/Models/Consumable.php
@@ -495,4 +495,25 @@ class Consumable extends SnipeModel
{
return $query->leftJoin('users as users_sort', 'consumables.created_by', '=', 'users_sort.id')->select('consumables.*')->orderBy('users_sort.first_name', $order)->orderBy('users_sort.last_name', $order);
}
+
+ /**
+ * Query builder scope to sort by the calculated `% remaining` column.
+ *
+ * Mirrors Consumable::percentRemaining(): (qty - consumables_users_count) / qty * 100.
+ * consumables_users_count is added by withCount() in the API index()
+ * before this scope runs. Guards against division by zero for
+ * consumables with qty of 0.
+ *
+ * PostgreSQL note: references a SELECT-list alias inside a compound
+ * ORDER BY expression, which PostgreSQL rejects per SQL standard.
+ * Snipe-IT officially supports MySQL/MariaDB and tests on SQLite
+ * (both allow this); moving to PostgreSQL would require inlining
+ * the subquery or wrapping the query in an outer SELECT.
+ */
+ public function scopeOrderPercentRemaining($query, $order)
+ {
+ $direction = strtolower($order) === 'asc' ? 'asc' : 'desc';
+
+ return $query->orderByRaw('CASE WHEN consumables.qty = 0 THEN 0 ELSE ((consumables.qty - consumables_users_count) * 100.0 / consumables.qty) END '.$direction);
+ }
}
diff --git a/app/Models/License.php b/app/Models/License.php
index 6f78fba239..5ff57201c5 100755
--- a/app/Models/License.php
+++ b/app/Models/License.php
@@ -955,4 +955,25 @@ class License extends Depreciable
{
return $query->leftJoin('users as admin_sort', 'licenses.created_by', '=', 'admin_sort.id')->select('licenses.*')->orderBy('admin_sort.first_name', $order)->orderBy('admin_sort.last_name', $order);
}
+
+ /**
+ * Query builder scope to sort by the calculated `% remaining` column.
+ *
+ * Mirrors License::percentRemaining(): available_seats / total_seats * 100.
+ * free_seats_count is added by withCount() in the API index() as the
+ * count of unassigned seats; seats is a real column on licenses.
+ * Guards against division by zero for licenses with no seats.
+ *
+ * PostgreSQL note: references a SELECT-list alias inside a compound
+ * ORDER BY expression, which PostgreSQL rejects per SQL standard.
+ * Snipe-IT officially supports MySQL/MariaDB and tests on SQLite
+ * (both allow this); moving to PostgreSQL would require inlining
+ * the subquery or wrapping the query in an outer SELECT.
+ */
+ public function scopeOrderPercentRemaining($query, $order)
+ {
+ $direction = strtolower($order) === 'asc' ? 'asc' : 'desc';
+
+ return $query->orderByRaw('CASE WHEN licenses.seats = 0 THEN 0 ELSE (free_seats_count * 100.0 / licenses.seats) END '.$direction);
+ }
}
diff --git a/app/Presenters/AccessoryPresenter.php b/app/Presenters/AccessoryPresenter.php
index df12bb1f35..40f8414313 100644
--- a/app/Presenters/AccessoryPresenter.php
+++ b/app/Presenters/AccessoryPresenter.php
@@ -135,7 +135,7 @@ class AccessoryPresenter extends Presenter
'field' => 'percent_remaining',
'scope' => 'col',
'searchable' => false,
- 'sortable' => false,
+ 'sortable' => true,
'switchable' => true,
'title' => '% '.trans('general.remaining'),
'visible' => true,
diff --git a/app/Presenters/ComponentPresenter.php b/app/Presenters/ComponentPresenter.php
index f3b205fae0..afeaf3f644 100644
--- a/app/Presenters/ComponentPresenter.php
+++ b/app/Presenters/ComponentPresenter.php
@@ -142,7 +142,7 @@ class ComponentPresenter extends Presenter
'field' => 'percent_remaining',
'scope' => 'col',
'searchable' => false,
- 'sortable' => false,
+ 'sortable' => true,
'switchable' => true,
'title' => '% '.trans('general.remaining'),
'visible' => true,
diff --git a/app/Presenters/ConsumablePresenter.php b/app/Presenters/ConsumablePresenter.php
index 6ceace1f21..a00d285a39 100644
--- a/app/Presenters/ConsumablePresenter.php
+++ b/app/Presenters/ConsumablePresenter.php
@@ -142,7 +142,7 @@ class ConsumablePresenter extends Presenter
'field' => 'percent_remaining',
'scope' => 'col',
'searchable' => false,
- 'sortable' => false,
+ 'sortable' => true,
'switchable' => true,
'title' => '% '.trans('general.remaining'),
'visible' => true,
diff --git a/app/Presenters/LicensePresenter.php b/app/Presenters/LicensePresenter.php
index bcc9012aef..53510a1e2d 100644
--- a/app/Presenters/LicensePresenter.php
+++ b/app/Presenters/LicensePresenter.php
@@ -136,7 +136,7 @@ class LicensePresenter extends Presenter
'field' => 'percent_remaining',
'scope' => 'col',
'searchable' => false,
- 'sortable' => false,
+ 'sortable' => true,
'switchable' => true,
'title' => '% '.trans('general.remaining'),
'visible' => true,