Added ability to search by company_id and location_id

This commit is contained in:
snipe 2017-02-08 03:31:42 -08:00
parent dd6a477355
commit 4cf01e4f48
3 changed files with 40 additions and 4 deletions

View file

@ -96,6 +96,14 @@ class AssetsController extends Controller
$assets->InCategory($request->input('category_id'));
}
if ($request->has('location_id')) {
$assets->ByLocationId($request->input('location_id'));
}
if ($request->has('company_id')) {
$assets->where('company_id','=',$request->input('company_id'));
}
if ($request->has('manufacturer_id')) {
$assets->ByManufacturer($request->input('manufacturer_id'));
}

View file

@ -35,6 +35,7 @@ class AssetsTransformer
'order_number' => $asset->order_number,
'company' => ($asset->company) ? ['id' => $asset->company->id,'name'=> e($asset->company->name)] : null,
'location' => ($asset->assetLoc) ? ['id' => $asset->assetLoc->id,'name'=> e($asset->assetLoc->name)] : null,
'rtd_location' => ($asset->defaultLoc) ? ['id' => $asset->defaultLoc->id,'name'=> e($asset->defaultLoc->name)] : null,
'image' => ($asset->getImageUrl()) ? $asset->getImageUrl() : null,
'assigned_to' => ($asset->assigneduser) ? (new UsersTransformer)->transformUser($asset->assigneduser) : null,
'warranty' => ($asset->warranty_months > 0) ? e($asset->warranty_months).' '.trans('admin/hardware/form.months') : null,

View file

@ -246,15 +246,17 @@ class Asset extends Depreciable
**/
public function assetLoc()
{
if($this->assignedTo) {
return $this->assignedTo->userLoc();
}
if(!empty($this->assignedType())) {
if ($this->assignedType() == self::ASSET) {
return $this->assignedTo->assetloc(); // Recurse until we have a final location
} elseif ($this->assignedType() == self::LOCATION) {
return $this->assignedTo();
}
if($this->assignedTo) {
return $this->assignedTo->userLoc();
}
}
return $this->defaultLoc();
}
@ -890,7 +892,7 @@ class Asset extends Depreciable
->orderBy('manufacturers.name', $order);
}
/**
/**
* Query builder scope to order on location
*
* @param \Illuminate\Database\Query\Builder $query Query builder instance
@ -902,4 +904,29 @@ class Asset extends Depreciable
{
return $query->join('locations', 'locations.id', '=', 'assets.rtd_location_id')->orderBy('locations.name', $order);
}
/**
* Query builder scope to search on location ID
*
* @param \Illuminate\Database\Query\Builder $query Query builder instance
* @param text $search Search term
*
* @return \Illuminate\Database\Query\Builder Modified query builder
*/
public function scopeByLocationId($query, $search)
{
return $query->where(function ($query) use ($search) {
$query->whereHas('defaultLoc', function ($query) use ($search) {
$query->where('locations.id', '=', $search);
})->whereNull('assigned_to');
})->orWhere(function ($query) use ($search) {
$query->whereHas('assigneduser', function ($query) use ($search) {
$query->whereHas('userloc', function ($query) use ($search) {
$query->where('locations.id', '=', $search);
});
});
});
}
}