Cleans up the consumables index controller
Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
parent
498a443230
commit
82743abadc
1 changed files with 25 additions and 30 deletions
|
@ -27,27 +27,8 @@ class ConsumablesController extends Controller
|
||||||
{
|
{
|
||||||
$this->authorize('index', Consumable::class);
|
$this->authorize('index', Consumable::class);
|
||||||
|
|
||||||
// This array is what determines which fields should be allowed to be sorted on ON the table itself, no relations
|
$consumables = Consumable::with('company', 'location', 'category', 'manufacturer')
|
||||||
// Relations will be handled in query scopes a little further down.
|
->withCount('users as consumables_users_count');
|
||||||
$allowed_columns =
|
|
||||||
[
|
|
||||||
'id',
|
|
||||||
'name',
|
|
||||||
'order_number',
|
|
||||||
'min_amt',
|
|
||||||
'purchase_date',
|
|
||||||
'purchase_cost',
|
|
||||||
'company',
|
|
||||||
'category',
|
|
||||||
'model_number',
|
|
||||||
'item_no',
|
|
||||||
'qty',
|
|
||||||
'image',
|
|
||||||
'notes',
|
|
||||||
];
|
|
||||||
|
|
||||||
$consumables = Consumable::select('consumables.*')
|
|
||||||
->with('company', 'location', 'category', 'users', 'manufacturer');
|
|
||||||
|
|
||||||
if ($request->filled('search')) {
|
if ($request->filled('search')) {
|
||||||
$consumables = $consumables->TextSearch(e($request->input('search')));
|
$consumables = $consumables->TextSearch(e($request->input('search')));
|
||||||
|
@ -89,15 +70,9 @@ class ConsumablesController extends Controller
|
||||||
// Make sure the offset and limit are actually integers and do not exceed system limits
|
// Make sure the offset and limit are actually integers and do not exceed system limits
|
||||||
$offset = ($request->input('offset') > $consumables->count()) ? $consumables->count() : app('api_offset_value');
|
$offset = ($request->input('offset') > $consumables->count()) ? $consumables->count() : app('api_offset_value');
|
||||||
$limit = app('api_limit_value');
|
$limit = app('api_limit_value');
|
||||||
|
|
||||||
$allowed_columns = ['id', 'name', 'order_number', 'min_amt', 'purchase_date', 'purchase_cost', 'company', 'category', 'model_number', 'item_no', 'manufacturer', 'location', 'qty', 'image'];
|
|
||||||
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
|
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
|
||||||
|
|
||||||
$sort_override = $request->input('sort');
|
switch ($request->input('sort')) {
|
||||||
$column_sort = in_array($sort_override, $allowed_columns) ? $sort_override : 'created_at';
|
|
||||||
|
|
||||||
|
|
||||||
switch ($sort_override) {
|
|
||||||
case 'category':
|
case 'category':
|
||||||
$consumables = $consumables->OrderCategory($order);
|
$consumables = $consumables->OrderCategory($order);
|
||||||
break;
|
break;
|
||||||
|
@ -111,10 +86,30 @@ class ConsumablesController extends Controller
|
||||||
$consumables = $consumables->OrderCompany($order);
|
$consumables = $consumables->OrderCompany($order);
|
||||||
break;
|
break;
|
||||||
case 'supplier':
|
case 'supplier':
|
||||||
$components = $consumables->OrderSupplier($order);
|
$consumables = $consumables->OrderSupplier($order);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$consumables = $consumables->orderBy($column_sort, $order);
|
// This array is what determines which fields should be allowed to be sorted on ON the table itself.
|
||||||
|
// These must match a column on the consumables table directly.
|
||||||
|
$allowed_columns = [
|
||||||
|
'id',
|
||||||
|
'name',
|
||||||
|
'order_number',
|
||||||
|
'min_amt',
|
||||||
|
'purchase_date',
|
||||||
|
'purchase_cost',
|
||||||
|
'company',
|
||||||
|
'category',
|
||||||
|
'model_number',
|
||||||
|
'item_no',
|
||||||
|
'manufacturer',
|
||||||
|
'location',
|
||||||
|
'qty',
|
||||||
|
'image'
|
||||||
|
];
|
||||||
|
|
||||||
|
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';
|
||||||
|
$consumables = $consumables->orderBy($sort, $order);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue