diff --git a/app/Http/Controllers/LicensesController.php b/app/Http/Controllers/LicensesController.php index b89d6cad68..9c04518e7f 100755 --- a/app/Http/Controllers/LicensesController.php +++ b/app/Http/Controllers/LicensesController.php @@ -954,11 +954,22 @@ class LicensesController extends Controller $licenses = $licenses->TextSearch(Input::get('search')); } - $allowed_columns = ['id','name','purchase_cost','expiration_date','purchase_order','order_number','notes','purchase_date','serial']; + $allowed_columns = ['id','name','purchase_cost','expiration_date','purchase_order','order_number','notes','purchase_date','serial','manufacturer','company']; $order = Input::get('order') === 'asc' ? 'asc' : 'desc'; $sort = in_array(Input::get('sort'), $allowed_columns) ? e(Input::get('sort')) : 'created_at'; - $licenses = $licenses->orderBy($sort, $order); + switch ($sort) { + case 'manufacturer': + $licenses = $licenses->OrderManufacturer($order); + break; + case 'company': + $licenses = $licenses->OrderCompany($order); + break; + default: + $licenses = $licenses->orderBy($sort, $order); + break; + } + $licenseCount = $licenses->count(); $licenses = $licenses->skip(Input::get('offset'))->take(Input::get('limit'))->get(); @@ -1003,7 +1014,7 @@ class LicensesController extends Controller 'order_number' => ($license->order_number) ? e($license->order_number) : '', 'notes' => ($license->notes) ? e($license->notes) : '', 'actions' => $actions, - 'companyName' => is_null($license->company) ? '' : e($license->company->name), + 'company' => is_null($license->company) ? '' : e($license->company->name), 'manufacturer' => $license->manufacturer ? (string) link_to('/admin/settings/manufacturers/'.$license->manufacturer_id.'/view', $license->manufacturer->name) : '' ); } diff --git a/app/Models/License.php b/app/Models/License.php index b802c2dade..29146631b5 100755 --- a/app/Models/License.php +++ b/app/Models/License.php @@ -234,13 +234,51 @@ class License extends Depreciable return $query->where(function ($query) use ($search) { - $query->where('name', 'LIKE', '%'.$search.'%') - ->orWhere('serial', 'LIKE', '%'.$search.'%') - ->orWhere('notes', 'LIKE', '%'.$search.'%') - ->orWhere('order_number', 'LIKE', '%'.$search.'%') - ->orWhere('purchase_order', 'LIKE', '%'.$search.'%') - ->orWhere('purchase_date', 'LIKE', '%'.$search.'%') - ->orWhere('purchase_cost', 'LIKE', '%'.$search.'%'); + $query->where('licenses.name', 'LIKE', '%'.$search.'%') + ->orWhere('licenses.serial', 'LIKE', '%'.$search.'%') + ->orWhere('licenses.notes', 'LIKE', '%'.$search.'%') + ->orWhere('licenses.order_number', 'LIKE', '%'.$search.'%') + ->orWhere('licenses.purchase_order', 'LIKE', '%'.$search.'%') + ->orWhere('licenses.purchase_date', 'LIKE', '%'.$search.'%') + ->orWhere('licenses.purchase_cost', 'LIKE', '%'.$search.'%') + ->orWhereHas('manufacturer', function ($query) use ($search) { + $query->where(function ($query) use ($search) { + $query->where('manufacturers.name', 'LIKE', '%'.$search.'%'); + }); + }) + ->orWhereHas('company', function ($query) use ($search) { + $query->where(function ($query) use ($search) { + $query->where('companies.name', 'LIKE', '%'.$search.'%'); + }); + }); }); } + + /** + * 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', 'licenses.manufacturer_id', '=', 'manufacturers.id')->select('licenses.*') + ->orderBy('manufacturers.name', $order); + } + + /** + * Query builder scope to order on company + * + * @param Illuminate\Database\Query\Builder $query Query builder instance + * @param text $order Order + * + * @return Illuminate\Database\Query\Builder Modified query builder + */ + public function scopeOrderCompany($query, $order) + { + return $query->leftJoin('companies as companies', 'licenses.company_id', '=', 'companies.id')->select('licenses.*') + ->orderBy('companies.name', $order); + } }