Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #15392 - filter by depreciation when showing models #15403

Merged
merged 10 commits into from
Aug 28, 2024
4 changes: 4 additions & 0 deletions app/Http/Controllers/Api/AssetModelsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public function index(Request $request) : JsonResponse | array
$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'));
}
Expand Down
19 changes: 16 additions & 3 deletions app/Http/Controllers/Api/DepreciationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

$depreciations = Depreciation::select('id','name','months','depreciation_min','depreciation_type','user_id','created_at','updated_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')
->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'));
Expand Down
17 changes: 12 additions & 5 deletions app/Http/Controllers/DepreciationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,20 @@ public function destroy($depreciationId) : RedirectResponse
*/
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'));


}
}
3 changes: 3 additions & 0 deletions app/Http/Transformers/DepreciationsTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public function transformDepreciation(Depreciation $depreciation)
'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')
];
Expand Down
7 changes: 6 additions & 1 deletion app/Models/AssetModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions app/Models/Depreciation.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,17 @@ public function licenses()
{
return $this->hasMany(\App\Models\License::class, 'depreciation_id');
}

/**
* Establishes the depreciation -> assets relationship
*
* @author A. Gianotto <[email protected]>
* @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');
}

}
20 changes: 20 additions & 0 deletions app/Presenters/DepreciationPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ public static function dataTableLayout()
"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,
Expand Down
42 changes: 36 additions & 6 deletions resources/views/depreciations/view.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,38 @@
<!-- Custom Tabs -->
<div class="nav-tabs-custom">
<ul class="nav nav-tabs">
<li class="active"><a href="#assets" data-toggle="tab">{{ trans('general.assets') }}</a></li>
<li><a href="#licenses" data-toggle="tab">{{ trans('general.licenses') }}</a></li>
<li><a href="#models" data-toggle="tab">{{ trans('general.asset_models') }}</a></li>
</ul>
<li class="active">
<a href="#assets" data-toggle="tab">
{{ trans('general.assets') }}

{!! ($depreciation->assets()->AssetsForShow()->count() > 0 ) ? '<badge class="badge badge-secondary">'.number_format($depreciation->assets()->AssetsForShow()->count()).'</badge>' : '' !!}
</a>
</li>
<li>
<a href="#licenses" data-toggle="tab">
{{ trans('general.licenses') }}

{!! ($depreciation->licenses_count > 0 ) ? '<badge class="badge badge-secondary">'.number_format($depreciation->licenses_count).'</badge>' : '' !!}
</a>
</li>
<li>
<a href="#models" data-toggle="tab">
{{ trans('general.asset_models') }}

{!! ($depreciation->models_count > 0 ) ? '<badge class="badge badge-secondary">'.number_format($depreciation->models_count).'</badge>' : '' !!}
</a>
</li>
</ul>

<div class="tab-content">

<div class="tab-pane active" id="assets">

@include('partials.asset-bulk-actions', [
'id_divname' => 'assetsBulkEditToolbar',
'id_formname' => 'assetsBulkForm',
'id_button' => 'assetEditButton'
])

<table
data-columns="{{ \App\Presenters\AssetPresenter::dataTableLayout() }}"
Expand All @@ -53,6 +76,10 @@
data-show-refresh="true"
data-sort-order="asc"
data-sort-name="name"
data-toolbar="#assetsBulkEditToolbar"
data-bulk-button-id="#assetEditButton"
data-bulk-form-id="#assetsBulkForm"
data-click-to-select="true"
class="table table-striped snipe-table"
data-url="{{ route('api.assets.index',['depreciation_id'=> $depreciation->id]) }}"
data-export-options='{
Expand Down Expand Up @@ -107,7 +134,7 @@ class="table table-striped snipe-table"
'method' => 'POST',
'route' => ['models.bulkedit.index'],
'class' => 'form-inline',
'id' => 'bulkForm']
'id' => 'bulkForm']
) }}
<div class="col-md-12">
<div id="toolbar">
Expand All @@ -116,7 +143,7 @@ class="table table-striped snipe-table"
<option value="edit">{{ trans('general.bulk_edit') }}</option>
<option value="delete">{{ trans('general.bulk_delete') }}</option>
</select>
<button class="btn btn-primary" id="bulkEdit" disabled>{{ trans('button.go') }}</button>
<button class="btn btn-primary" id="AssetModelsBulkEditButton" disabled>{{ trans('button.go') }}</button>
</div>

<div class="table-responsive">
Expand All @@ -134,6 +161,9 @@ class="table table-striped snipe-table"
data-show-refresh="true"
data-sort-order="asc"
data-sort-name="name"
data-bulk-button-id="#AssetModelsBulkEditButton"
data-bulk-form-id="#bulkForm"
data-click-to-select="true"
class="table table-striped snipe-table"
data-url="{{ route('api.models.index',['depreciation_id'=> $depreciation->id]) }}"
data-export-options='{
Expand Down
17 changes: 17 additions & 0 deletions tests/Feature/Depreciations/Api/DepreciationsIndexTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Tests\Feature\Depreciations\Api;

use App\Models\User;
use Tests\TestCase;

class DepreciationsIndexTest extends TestCase
{
public function testViewingDepreciationIndexRequiresPermission()
{
$this->actingAsForApi(User::factory()->create())
->getJson(route('api.departments.index'))
->assertForbidden();
}

}
23 changes: 23 additions & 0 deletions tests/Feature/Depreciations/Ui/DepreciationsIndexTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Tests\Feature\Categories\Ui;

use App\Models\User;
use Tests\TestCase;

class DepreciationsIndexTest extends TestCase
{
public function testPermissionRequiredToViewDepreciationsList()
{
$this->actingAs(User::factory()->create())
->get(route('depreciations.index'))
->assertForbidden();
}

public function testUserCanListDepreciations()
{
$this->actingAs(User::factory()->admin()->create())
->get(route('depreciations.index'))
->assertOk();
}
}
Loading