mirror of
https://github.com/snipe/snipe-it.git
synced 2025-03-05 20:52:15 -08:00
Load components in the assets API if components=true in API request
Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
parent
6e270c0ed2
commit
3b7ce0091c
|
@ -118,7 +118,7 @@ class AssetsController extends Controller
|
||||||
->with('location', 'assetstatus', 'assetlog', 'company', 'defaultLoc','assignedTo',
|
->with('location', 'assetstatus', 'assetlog', 'company', 'defaultLoc','assignedTo',
|
||||||
'model.category', 'model.manufacturer', 'model.fieldset','supplier');
|
'model.category', 'model.manufacturer', 'model.fieldset','supplier');
|
||||||
|
|
||||||
|
|
||||||
// These are used by the API to query against specific ID numbers.
|
// These are used by the API to query against specific ID numbers.
|
||||||
// They are also used by the individual searches on detail pages like
|
// They are also used by the individual searches on detail pages like
|
||||||
// locations, etc.
|
// locations, etc.
|
||||||
|
@ -270,7 +270,7 @@ class AssetsController extends Controller
|
||||||
$assets->TextSearch($request->input('search'));
|
$assets->TextSearch($request->input('search'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// This is kinda gross, but we need to do this because the Bootstrap Tables
|
// This is kinda gross, but we need to do this because the Bootstrap Tables
|
||||||
// API passes custom field ordering as custom_fields.fieldname, and we have to strip
|
// API passes custom field ordering as custom_fields.fieldname, and we have to strip
|
||||||
// that out to let the default sorter below order them correctly on the assets table.
|
// that out to let the default sorter below order them correctly on the assets table.
|
||||||
|
@ -319,12 +319,25 @@ class AssetsController extends Controller
|
||||||
|
|
||||||
$total = $assets->count();
|
$total = $assets->count();
|
||||||
$assets = $assets->skip($offset)->take($limit)->get();
|
$assets = $assets->skip($offset)->take($limit)->get();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Include additional associated relationships
|
||||||
|
*/
|
||||||
|
if ($request->input('components')) {
|
||||||
|
$assets->load(['components' => function ($query) {
|
||||||
|
$query->orderBy('created_at', 'desc');
|
||||||
|
}]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Here we're just determining which Transformer (via $transformer) to use based on the
|
* Here we're just determining which Transformer (via $transformer) to use based on the
|
||||||
* variables we set earlier on in this method - we default to AssetsTransformer.
|
* variables we set earlier on in this method - we default to AssetsTransformer.
|
||||||
*/
|
*/
|
||||||
return (new $transformer)->transformAssets($assets, $total);
|
return (new $transformer)->transformAssets($assets, $total, $request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -336,11 +349,11 @@ class AssetsController extends Controller
|
||||||
* @since [v4.2.1]
|
* @since [v4.2.1]
|
||||||
* @return JsonResponse
|
* @return JsonResponse
|
||||||
*/
|
*/
|
||||||
public function showByTag($tag)
|
public function showByTag(Request $request, $tag)
|
||||||
{
|
{
|
||||||
if ($asset = Asset::with('assetstatus')->with('assignedTo')->where('asset_tag',$tag)->first()) {
|
if ($asset = Asset::with('assetstatus')->with('assignedTo')->where('asset_tag',$tag)->first()) {
|
||||||
$this->authorize('view', $asset);
|
$this->authorize('view', $asset);
|
||||||
return (new AssetsTransformer)->transformAsset($asset);
|
return (new AssetsTransformer)->transformAsset($asset, $request);
|
||||||
}
|
}
|
||||||
return response()->json(Helper::formatStandardApiResponse('error', null, 'Asset not found'), 200);
|
return response()->json(Helper::formatStandardApiResponse('error', null, 'Asset not found'), 200);
|
||||||
|
|
||||||
|
@ -354,7 +367,7 @@ class AssetsController extends Controller
|
||||||
* @since [v4.2.1]
|
* @since [v4.2.1]
|
||||||
* @return JsonResponse
|
* @return JsonResponse
|
||||||
*/
|
*/
|
||||||
public function showBySerial($serial)
|
public function showBySerial(Request $request, $serial)
|
||||||
{
|
{
|
||||||
$this->authorize('index', Asset::class);
|
$this->authorize('index', Asset::class);
|
||||||
if ($assets = Asset::with('assetstatus')->with('assignedTo')
|
if ($assets = Asset::with('assetstatus')->with('assignedTo')
|
||||||
|
@ -374,17 +387,17 @@ class AssetsController extends Controller
|
||||||
* @since [v4.0]
|
* @since [v4.0]
|
||||||
* @return JsonResponse
|
* @return JsonResponse
|
||||||
*/
|
*/
|
||||||
public function show($id)
|
public function show(Request $request, $id)
|
||||||
{
|
{
|
||||||
if ($asset = Asset::with('assetstatus')->with('assignedTo')->withTrashed()
|
if ($asset = Asset::with('assetstatus')->with('assignedTo')->withTrashed()
|
||||||
->withCount('checkins as checkins_count', 'checkouts as checkouts_count', 'userRequests as user_requests_count')->findOrFail($id)) {
|
->withCount('checkins as checkins_count', 'checkouts as checkouts_count', 'userRequests as user_requests_count')->findOrFail($id)) {
|
||||||
$this->authorize('view', $asset);
|
$this->authorize('view', $asset);
|
||||||
return (new AssetsTransformer)->transformAsset($asset);
|
return (new AssetsTransformer)->transformAsset($asset, $request->input('components') );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
public function licenses($id)
|
public function licenses(Request $request, $id)
|
||||||
{
|
{
|
||||||
$this->authorize('view', Asset::class);
|
$this->authorize('view', Asset::class);
|
||||||
$this->authorize('view', License::class);
|
$this->authorize('view', License::class);
|
||||||
|
|
|
@ -440,12 +440,12 @@ class UsersController extends Controller
|
||||||
* @param $userId
|
* @param $userId
|
||||||
* @return string JSON
|
* @return string JSON
|
||||||
*/
|
*/
|
||||||
public function assets($id)
|
public function assets(Request $request, $id)
|
||||||
{
|
{
|
||||||
$this->authorize('view', User::class);
|
$this->authorize('view', User::class);
|
||||||
$this->authorize('view', Asset::class);
|
$this->authorize('view', Asset::class);
|
||||||
$assets = Asset::where('assigned_to', '=', $id)->where('assigned_type', '=', User::class)->with('model')->get();
|
$assets = Asset::where('assigned_to', '=', $id)->where('assigned_type', '=', User::class)->with('model')->get();
|
||||||
return (new AssetsTransformer)->transformAssets($assets, $assets->count());
|
return (new AssetsTransformer)->transformAssets($assets, $assets->count(), $request);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -134,6 +134,28 @@ class AssetsTransformer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (request('components')=='true') {
|
||||||
|
|
||||||
|
if ($asset->components) {
|
||||||
|
$array['components'] = [];
|
||||||
|
|
||||||
|
foreach ($asset->components as $component) {
|
||||||
|
$array['components'][] = [
|
||||||
|
[
|
||||||
|
'id' => $component->id,
|
||||||
|
'name' => $component->name,
|
||||||
|
'qty' => $component->pivot->assigned_qty,
|
||||||
|
'price_cost' => $component->purchase_cost,
|
||||||
|
'purchase_total' => $component->purchase_cost * $component->pivot->assigned_qty,
|
||||||
|
'checkout_date' => Helper::getFormattedDateObject($component->pivot->created_at, 'datetime') ,
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
$array += $permissions_array;
|
$array += $permissions_array;
|
||||||
return $array;
|
return $array;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue