Fix some accessory sorting (#3108)

Fixes at least some of #3045
This commit is contained in:
Daniel Meltzer 2016-12-29 18:50:18 -05:00 committed by snipe
parent 51ceaedfaf
commit ce8eda1325
2 changed files with 28 additions and 6 deletions

View file

@ -419,7 +419,7 @@ class AccessoriesController extends Controller
$offset = request('offset', 0); $offset = request('offset', 0);
$limit = request('limit', 50); $limit = request('limit', 50);
$allowed_columns = ['name','min_amt','order_number','purchase_date','purchase_cost','companyName','category','model_number']; $allowed_columns = ['name','min_amt','order_number','purchase_date','purchase_cost','companyName','category','model_number', 'manufacturer', 'location'];
$order = Input::get('order') === 'asc' ? 'asc' : 'desc'; $order = Input::get('order') === 'asc' ? 'asc' : 'desc';
$sort = in_array(Input::get('sort'), $allowed_columns) ? e(Input::get('sort')) : 'created_at'; $sort = in_array(Input::get('sort'), $allowed_columns) ? e(Input::get('sort')) : 'created_at';
@ -430,6 +430,12 @@ class AccessoriesController extends Controller
case 'companyName': case 'companyName':
$accessories = $accessories->OrderCompany($order); $accessories = $accessories->OrderCompany($order);
break; break;
case 'location':
$accessories = $accessories->OrderLocation($order);
break;
case 'manufacturer':
$accessories = $accessories->OrderManufacturer($order);
break;
default: default:
$accessories = $accessories->orderBy($sort, $order); $accessories = $accessories->orderBy($sort, $order);
break; break;

View file

@ -168,11 +168,12 @@ class Accessory extends SnipeModel
*/ */
public function scopeOrderCompany($query, $order) public function scopeOrderCompany($query, $order)
{ {
return $query->leftJoin('companies', 'accessories.company_id', '=', 'companies.id')->orderBy('companies.name', $order); return $query->leftJoin('companies', 'accessories.company_id', '=', 'companies.id')
->orderBy('companies.name', $order);
} }
/** /**
* Query builder scope to order on company * Query builder scope to order on category
* *
* @param Illuminate\Database\Query\Builder $query Query builder instance * @param Illuminate\Database\Query\Builder $query Query builder instance
* @param text $order Order * @param text $order Order
@ -181,11 +182,12 @@ class Accessory extends SnipeModel
*/ */
public function scopeOrderCategory($query, $order) public function scopeOrderCategory($query, $order)
{ {
return $query->leftJoin('categories', 'accessories.category_id', '=', 'categories.id')->orderBy('categories.name', $order); return $query->leftJoin('categories', 'accessories.category_id', '=', 'categories.id')
->orderBy('categories.name', $order);
} }
/** /**
* Query builder scope to order on company * Query builder scope to order on location
* *
* @param Illuminate\Database\Query\Builder $query Query builder instance * @param Illuminate\Database\Query\Builder $query Query builder instance
* @param text $order Order * @param text $order Order
@ -194,6 +196,20 @@ class Accessory extends SnipeModel
*/ */
public function scopeOrderLocation($query, $order) public function scopeOrderLocation($query, $order)
{ {
return $query->leftJoin('locations', 'consumables.location_id', '=', 'locations.id')->orderBy('locations.name', $order); return $query->leftJoin('locations', 'accessories.location_id', '=', 'locations.id')
->orderBy('locations.name', $order);
}
/**
* Query builder scope to order on manufacturer
*
* @param Illuminate\Database\Query\Builder $query Query builder instance
* @param text $order Order
*
* @return Illuminate\Database\Query\Builder Modified query builder
*/
public function scopeOrderManufacturer($query, $order)
{
return $query->leftJoin('manufacturers', 'accessories.manufacturer_id', '=', 'manufacturers.id')->orderBy('manufacturers.name', $order);
} }
} }