From 4cf01e4f48da649c4917a40fd1cceee6cc7ebabe Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 8 Feb 2017 03:31:42 -0800 Subject: [PATCH] Added ability to search by company_id and location_id --- app/Http/Controllers/Api/AssetsController.php | 8 +++++ app/Http/Transformers/AssetsTransformer.php | 1 + app/Models/Asset.php | 35 ++++++++++++++++--- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index 8384a7185b..fb3839640e 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -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')); } diff --git a/app/Http/Transformers/AssetsTransformer.php b/app/Http/Transformers/AssetsTransformer.php index b2bf9dd206..0321a8be82 100644 --- a/app/Http/Transformers/AssetsTransformer.php +++ b/app/Http/Transformers/AssetsTransformer.php @@ -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, diff --git a/app/Models/Asset.php b/app/Models/Asset.php index 4aaf96f367..186777dc11 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -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); + }); + }); + }); + + } }