diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index 96ec01354d..8cedb2c67e 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -77,6 +77,9 @@ class AssetsController extends Controller 'last_audit_date', 'next_audit_date', 'warranty_months', + 'checkouts_count', + 'checkins_count', + 'user_requests_count', ]; $filter = array(); @@ -92,7 +95,7 @@ class AssetsController extends Controller $assets = Company::scopeCompanyables(Asset::select('assets.*'),"company_id","assets") ->with('location', 'assetstatus', 'assetlog', 'company', 'defaultLoc','assignedTo', - 'model.category', 'model.manufacturer', 'model.fieldset','supplier'); + 'model.category', 'model.manufacturer', 'model.fieldset','supplier')->withCount('checkins', 'checkouts', 'userRequests'); // These are used by the API to query against specific ID numbers. @@ -116,7 +119,6 @@ class AssetsController extends Controller if ($request->has('location_id')) { $assets->where('assets.location_id', '=', $request->input('location_id')); - // dd($assets->toSql()); } if ($request->has('supplier_id')) { @@ -322,7 +324,7 @@ class AssetsController extends Controller */ public function show($id) { - if ($asset = Asset::with('assetstatus')->with('assignedTo')->withTrashed()->findOrFail($id)) { + if ($asset = Asset::with('assetstatus')->with('assignedTo')->withTrashed()->withCount('checkins', 'checkouts', 'userRequests')->findOrFail($id)) { $this->authorize('view', $asset); return (new AssetsTransformer)->transformAsset($asset); } diff --git a/app/Http/Transformers/AssetsTransformer.php b/app/Http/Transformers/AssetsTransformer.php index 3c1dd3bcd0..83448e4836 100644 --- a/app/Http/Transformers/AssetsTransformer.php +++ b/app/Http/Transformers/AssetsTransformer.php @@ -77,6 +77,9 @@ class AssetsTransformer 'last_checkout' => Helper::getFormattedDateObject($asset->last_checkout, 'datetime'), 'expected_checkin' => Helper::getFormattedDateObject($asset->expected_checkin, 'date'), 'purchase_cost' => Helper::formatCurrencyOutput($asset->purchase_cost), + 'checkins_count' => (int) $asset->checkins_count, + 'checkouts_count' => (int) $asset->checkouts_count, + 'user_requests_count' => (int) $asset->user_requests_count, 'user_can_checkout' => (bool) $asset->availableForCheckout(), ]; diff --git a/app/Models/Asset.php b/app/Models/Asset.php index d143ea63ab..d50024448d 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -350,6 +350,38 @@ class Asset extends Depreciable ->withTrashed(); } + /** + * Get checkouts + */ + public function checkouts() + { + return $this->assetlog()->where('action_type', '=', 'checkout') + ->orderBy('created_at', 'desc') + ->withTrashed(); + } + + /** + * Get checkins + */ + public function checkins() + { + return $this->assetlog() + ->where('action_type', '=', 'checkin from') + ->orderBy('created_at', 'desc') + ->withTrashed(); + } + + /** + * Get user requests + */ + public function userRequests() + { + return $this->assetlog() + ->where('action_type', '=', 'requested') + ->orderBy('created_at', 'desc') + ->withTrashed(); + } + /** * assetmaintenances diff --git a/app/Presenters/AssetPresenter.php b/app/Presenters/AssetPresenter.php index f87819a0cb..11f00e6904 100644 --- a/app/Presenters/AssetPresenter.php +++ b/app/Presenters/AssetPresenter.php @@ -181,6 +181,27 @@ class AssetPresenter extends Presenter "visible" => false, "title" => trans('general.notes'), + ], [ + "field" => "checkouts_count", + "searchable" => false, + "sortable" => true, + "visible" => false, + "title" => trans('general.checkouts_count') + + ],[ + "field" => "checkins_count", + "searchable" => false, + "sortable" => true, + "visible" => false, + "title" => trans('general.checkins_count') + + ], [ + "field" => "user_requests_count", + "searchable" => false, + "sortable" => true, + "visible" => false, + "title" => trans('general.user_requests_count') + ], [ "field" => "created_at", "searchable" => false, diff --git a/resources/lang/en/general.php b/resources/lang/en/general.php index ff78b8cfb9..ad3c27a08b 100644 --- a/resources/lang/en/general.php +++ b/resources/lang/en/general.php @@ -39,6 +39,9 @@ 'checkin' => 'Checkin', 'checkin_from' => 'Checkin from', 'checkout' => 'Checkout', + 'checkouts_count' => 'Checkouts', + 'checkins_count' => 'Checkins', + 'user_requests_count' => 'Requests', 'city' => 'City', 'click_here' => 'Click here', 'clear_selection' => 'Clear Selection', diff --git a/resources/views/hardware/view.blade.php b/resources/views/hardware/view.blade.php index 780a449533..9fd7c26571 100755 --- a/resources/views/hardware/view.blade.php +++ b/resources/views/hardware/view.blade.php @@ -465,6 +465,28 @@ @endif + +