diff --git a/app/Http/Controllers/Api/AssetModelsController.php b/app/Http/Controllers/Api/AssetModelsController.php index 835f4d22e0..9f78193420 100644 --- a/app/Http/Controllers/Api/AssetModelsController.php +++ b/app/Http/Controllers/Api/AssetModelsController.php @@ -78,6 +78,10 @@ class AssetModelsController extends Controller $assetmodels = $assetmodels->where('models.category_id', '=', $request->input('category_id')); } + if ($request->filled('depreciation_id')) { + $assetmodels = $assetmodels->where('models.depreciation_id', '=', $request->input('depreciation_id')); + } + if ($request->filled('search')) { $assetmodels->TextSearch($request->input('search')); } diff --git a/app/Http/Controllers/Api/DepreciationsController.php b/app/Http/Controllers/Api/DepreciationsController.php index 0209eae392..72e0f3a14a 100644 --- a/app/Http/Controllers/Api/DepreciationsController.php +++ b/app/Http/Controllers/Api/DepreciationsController.php @@ -20,9 +20,22 @@ class DepreciationsController extends Controller public function index(Request $request) : JsonResponse | array { $this->authorize('view', Depreciation::class); - $allowed_columns = ['id','name','months','depreciation_min', 'depreciation_type','created_at']; + $allowed_columns = [ + 'id', + 'name', + 'months', + 'depreciation_min', + 'depreciation_type', + 'created_at', + 'assets_count', + 'models_count', + 'licenses_count', + ]; - $depreciations = Depreciation::select('id','name','months','depreciation_min','depreciation_type','user_id','created_at','updated_at'); + $depreciations = Depreciation::select('id','name','months','depreciation_min','depreciation_type','user_id','created_at','updated_at') + ->withCount('assets as assets_count') + ->withCount('models as models_count') + ->withCount('licenses as licenses_count'); if ($request->filled('search')) { $depreciations = $depreciations->TextSearch($request->input('search')); diff --git a/app/Http/Controllers/DepreciationsController.php b/app/Http/Controllers/DepreciationsController.php index c564cc98f7..888f7a7e77 100755 --- a/app/Http/Controllers/DepreciationsController.php +++ b/app/Http/Controllers/DepreciationsController.php @@ -193,13 +193,20 @@ class DepreciationsController extends Controller */ public function show($id) : View | RedirectResponse { - if (is_null($depreciation = Depreciation::find($id))) { - // Redirect to the blogs management page - return redirect()->route('depreciations.index')->with('error', trans('admin/depreciations/message.does_not_exist')); - } + $depreciation = Depreciation::withCount('assets as assets_count') + ->withCount('models as models_count') + ->withCount('licenses as licenses_count') + ->find($id); $this->authorize('view', $depreciation); - return view('depreciations/view', compact('depreciation')); + if ($depreciation) { + return view('depreciations/view', compact('depreciation')); + + } + + return redirect()->route('depreciations.index')->with('error', trans('admin/depreciations/message.does_not_exist')); + + } } diff --git a/app/Http/Transformers/DepreciationsTransformer.php b/app/Http/Transformers/DepreciationsTransformer.php index 87e2ddaca4..b3dc8c5aae 100644 --- a/app/Http/Transformers/DepreciationsTransformer.php +++ b/app/Http/Transformers/DepreciationsTransformer.php @@ -28,6 +28,9 @@ class DepreciationsTransformer 'name' => e($depreciation->name), 'months' => $depreciation->months.' '.trans('general.months'), 'depreciation_min' => $depreciation->depreciation_type === 'percent' ? $depreciation->depreciation_min.'%' : $depreciation->depreciation_min, + 'assets_count' => $depreciation->assets_count, + 'models_count' => $depreciation->models_count, + 'licenses_count' => $depreciation->licenses_count, 'created_at' => Helper::getFormattedDateObject($depreciation->created_at, 'datetime'), 'updated_at' => Helper::getFormattedDateObject($depreciation->updated_at, 'datetime') ]; diff --git a/app/Models/AssetModel.php b/app/Models/AssetModel.php index 3c023507db..e9b859e128 100755 --- a/app/Models/AssetModel.php +++ b/app/Models/AssetModel.php @@ -79,7 +79,12 @@ class AssetModel extends SnipeModel * * @var array */ - protected $searchableAttributes = ['name', 'model_number', 'notes', 'eol']; + protected $searchableAttributes = [ + 'name', + 'model_number', + 'notes', + 'eol' + ]; /** * The relations and their attributes that should be included when searching the model. diff --git a/app/Models/Depreciation.php b/app/Models/Depreciation.php index 9faa1b86e2..7aceddf7c4 100755 --- a/app/Models/Depreciation.php +++ b/app/Models/Depreciation.php @@ -75,4 +75,17 @@ class Depreciation extends SnipeModel { return $this->hasMany(\App\Models\License::class, 'depreciation_id'); } + + /** + * Establishes the depreciation -> assets relationship + * + * @author A. Gianotto + * @since [v5.0] + * @return \Illuminate\Database\Eloquent\Relations\Relation + */ + public function assets() + { + return $this->hasManyThrough(\App\Models\Asset::class, \App\Models\AssetModel::class, 'depreciation_id', 'model_id'); + } + } diff --git a/app/Presenters/DepreciationPresenter.php b/app/Presenters/DepreciationPresenter.php index 9df1fe1322..cfba531623 100644 --- a/app/Presenters/DepreciationPresenter.php +++ b/app/Presenters/DepreciationPresenter.php @@ -46,6 +46,26 @@ class DepreciationPresenter extends Presenter "title" => trans('admin/depreciations/table.depreciation_min'), "visible" => true, ], + [ + 'field' => 'assets_count', + 'searchable' => false, + 'sortable' => true, + 'title' => trans('general.assets'), + 'visible' => true, + ], + [ + 'field' => 'models_count', + 'searchable' => false, + 'sortable' => true, + 'title' => trans('general.asset_models'), + 'visible' => true, + ], [ + 'field' => 'licenses_count', + 'searchable' => false, + 'sortable' => true, + 'title' => trans('general.licenses'), + 'visible' => true, + ], [ 'field' => 'actions', 'searchable' => false, diff --git a/resources/views/depreciations/view.blade.php b/resources/views/depreciations/view.blade.php index d075a31e95..b49bf0b9e1 100644 --- a/resources/views/depreciations/view.blade.php +++ b/resources/views/depreciations/view.blade.php @@ -30,15 +30,38 @@