diff --git a/app/Http/Controllers/Api/ConsumablesController.php b/app/Http/Controllers/Api/ConsumablesController.php index 1665b7f4f0..7be4c3d2dd 100644 --- a/app/Http/Controllers/Api/ConsumablesController.php +++ b/app/Http/Controllers/Api/ConsumablesController.php @@ -86,6 +86,9 @@ class ConsumablesController extends Controller case 'company': $consumables = $consumables->OrderCompany($order); break; + case 'remaining': + $consumables = $consumables->OrderRemaining($order); + break; case 'supplier': $consumables = $consumables->OrderSupplier($order); break; diff --git a/app/Models/Consumable.php b/app/Models/Consumable.php index 944ac5bfde..3b33035b1e 100644 --- a/app/Models/Consumable.php +++ b/app/Models/Consumable.php @@ -425,6 +425,20 @@ class Consumable extends SnipeModel return $query->leftJoin('companies', 'consumables.company_id', '=', 'companies.id')->orderBy('companies.name', $order); } + /** + * Query builder scope to order on remaining + * + * @param \Illuminate\Database\Query\Builder $query Query builder instance + * @param string $order Order + * + * @return \Illuminate\Database\Query\Builder Modified query builder + */ + public function scopeOrderRemaining($query, $order) + { + $order_by = 'consumables.qty - consumables_users_count ' . $order; + return $query->orderByRaw($order_by); + } + /** * Query builder scope to order on supplier * diff --git a/app/Presenters/ConsumablePresenter.php b/app/Presenters/ConsumablePresenter.php index d3e73de1cf..dc22c69e24 100644 --- a/app/Presenters/ConsumablePresenter.php +++ b/app/Presenters/ConsumablePresenter.php @@ -75,13 +75,13 @@ class ConsumablePresenter extends Presenter ], [ 'field' => 'qty', 'searchable' => false, - 'sortable' => false, + 'sortable' => true, 'title' => trans('admin/components/general.total'), 'visible' => true, ], [ 'field' => 'remaining', 'searchable' => false, - 'sortable' => false, + 'sortable' => true, 'title' => trans('admin/components/general.remaining'), 'visible' => true, ], [