Skip to content

Commit 84a2937

Browse files
authored
Merge pull request #16295 from snipe/#16282_adds_accessories_tab_to_assets
Fixed #16282 - adds accessories tab to assets
2 parents 7db11dc + 0cd0abe commit 84a2937

File tree

8 files changed

+147
-7
lines changed

8 files changed

+147
-7
lines changed

app/Http/Controllers/Api/AssetsController.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,10 @@ public function assignedAccessories(Request $request, Asset $asset) : JsonRespon
12301230
{
12311231
$this->authorize('view', Asset::class);
12321232
$this->authorize('view', $asset);
1233-
$accessory_checkouts = AccessoryCheckout::AssetsAssigned()->with('adminuser')->with('accessories');
1233+
$accessory_checkouts = AccessoryCheckout::AssetsAssigned()
1234+
->where('assigned_to', $asset->id)
1235+
->with('adminuser')
1236+
->with('accessories');
12341237

12351238
$offset = ($request->input('offset') > $accessory_checkouts->count()) ? $accessory_checkouts->count() : app('api_offset_value');
12361239
$limit = app('api_limit_value');
@@ -1239,6 +1242,8 @@ public function assignedAccessories(Request $request, Asset $asset) : JsonRespon
12391242
$accessory_checkouts = $accessory_checkouts->skip($offset)->take($limit)->get();
12401243
return (new AssetsTransformer)->transformCheckedoutAccessories($accessory_checkouts, $total);
12411244
}
1245+
1246+
12421247
/**
12431248
* Generate asset labels by tag
12441249
*

app/Http/Transformers/AssetsTransformer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ public function transformCheckedoutAccessory(AccessoryCheckout $accessory_checko
309309
'id' => $accessory_checkout->accessory->id,
310310
'name' => $accessory_checkout->accessory->name,
311311
],
312+
'assigned_to' => $accessory_checkout->assigned_to,
312313
'image' => ($accessory_checkout->accessory->image) ? Storage::disk('public')->url('accessories/'.e($accessory_checkout->accessory->image)) : null,
313314
'note' => $accessory_checkout->note ? e($accessory_checkout->note) : null,
314315
'created_by' => $accessory_checkout->adminuser ? [

app/Http/Transformers/LocationsTransformer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public function transformCheckedoutAccessory(AccessoryCheckout $accessory_checko
100100

101101
$array = [
102102
'id' => $accessory_checkout->id,
103+
'assigned_to' => $accessory_checkout->assigned_to,
103104
'accessory' => [
104105
'id' => $accessory_checkout->accessory->id,
105106
'name' => $accessory_checkout->accessory->name,

app/Models/AccessoryCheckout.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function accessories()
5757
*/
5858
public function adminuser()
5959
{
60-
return $this->hasOne(\App\Models\User::class, 'created_by');
60+
return $this->hasOne(\App\Models\User::class, 'id', 'created_by');
6161
}
6262

6363
/**
@@ -118,7 +118,7 @@ public function scopeLocationAssigned(Builder $query): void
118118
$query->where('assigned_type', '=', Location::class);
119119
}
120120

121-
public function scopeAssetAssigned(Builder $query): void
121+
public function scopeAssetsAssigned(Builder $query): void
122122
{
123123
$query->where('assigned_type', '=', Asset::class);
124124
}

app/Models/Asset.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,18 @@ public function assignedAssets()
523523
return $this->morphMany(self::class, 'assigned', 'assigned_type', 'assigned_to')->withTrashed();
524524
}
525525

526+
/**
527+
* Establishes the accessory -> asset assignment relationship
528+
*
529+
* @author A. Gianotto <[email protected]>
530+
* @since [v3.0]
531+
* @return \Illuminate\Database\Eloquent\Relations\Relation
532+
*/
533+
public function assignedAccessories()
534+
{
535+
return $this->morphMany(\App\Models\AccessoryCheckout::class, 'assigned', 'assigned_type', 'assigned_to');
536+
}
537+
526538

527539
/**
528540
* Get the asset's location based on the assigned user

app/Presenters/AssetPresenter.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,74 @@ public static function dataTableLayout()
352352
return json_encode($layout);
353353
}
354354

355+
356+
public static function assignedAccessoriesDataTableLayout()
357+
{
358+
$layout = [
359+
[
360+
'field' => 'id',
361+
'searchable' => false,
362+
'sortable' => false,
363+
'switchable' => true,
364+
'title' => trans('general.id'),
365+
'visible' => false,
366+
],
367+
[
368+
'field' => 'accessory',
369+
'searchable' => false,
370+
'sortable' => false,
371+
'switchable' => true,
372+
'title' => trans('general.accessory'),
373+
'visible' => true,
374+
'formatter' => 'accessoriesLinkObjFormatter',
375+
],
376+
[
377+
'field' => 'image',
378+
'searchable' => false,
379+
'sortable' => false,
380+
'switchable' => true,
381+
'title' => trans('general.image'),
382+
'visible' => true,
383+
'formatter' => 'imageFormatter',
384+
],
385+
[
386+
'field' => 'note',
387+
'searchable' => false,
388+
'sortable' => false,
389+
'switchable' => true,
390+
'title' => trans('general.notes'),
391+
'visible' => true,
392+
],
393+
[
394+
'field' => 'created_at',
395+
'searchable' => false,
396+
'sortable' => false,
397+
'switchable' => true,
398+
'title' => trans('admin/hardware/table.checkout_date'),
399+
'visible' => true,
400+
'formatter' => 'dateDisplayFormatter',
401+
],
402+
[
403+
'field' => 'created_by',
404+
'searchable' => false,
405+
'sortable' => false,
406+
'title' => trans('general.created_by'),
407+
'visible' => false,
408+
'formatter' => 'usersLinkObjFormatter',
409+
],
410+
[
411+
'field' => 'available_actions',
412+
'searchable' => false,
413+
'sortable' => false,
414+
'switchable' => false,
415+
'title' => trans('table.actions'),
416+
'formatter' => 'accessoriesInOutFormatter',
417+
],
418+
];
419+
420+
return json_encode($layout);
421+
}
422+
355423
/**
356424
* Generate html link to this items name.
357425
* @return string

resources/views/hardware/view.blade.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,30 @@
7777
<span class="hidden-lg hidden-md">
7878
<x-icon type="assets" class="fa-2x" />
7979
</span>
80-
<span class="hidden-xs hidden-sm">{{ trans('general.assets') }}
80+
<span class="hidden-xs hidden-sm">
81+
{{ trans('general.assets') }}
8182
{!! ($asset->assignedAssets()->count() > 0 ) ? '<span class="badge badge-secondary">'.number_format($asset->assignedAssets()->count()).'</span>' : '' !!}
8283

8384
</span>
8485
</a>
8586
</li>
8687

88+
@if ($asset->assignedAccessories->count() > 0)
89+
<li>
90+
<a href="#accessories_assigned" data-toggle="tab" data-tooltip="true">
91+
92+
<span class="hidden-lg hidden-md">
93+
<i class="fas fa-keyboard fa-2x"></i>
94+
</span>
95+
<span class="hidden-xs hidden-sm">
96+
{{ trans('general.accessories_assigned') }}
97+
{!! ($asset->assignedAccessories()->count() > 0 ) ? '<span class="badge badge-secondary">'.number_format($asset->assignedAccessories()->count()).'</span>' : '' !!}
98+
99+
</span>
100+
</a>
101+
</li>
102+
@endif
103+
87104

88105
<li>
89106
<a href="#history" data-toggle="tab">
@@ -1294,6 +1311,40 @@ class="table table-striped snipe-table"
12941311
</div> <!-- /.tab-pane software -->
12951312

12961313

1314+
<div class="tab-pane" id="accessories_assigned">
1315+
1316+
1317+
<div class="table table-responsive">
1318+
1319+
<h2 class="box-title" style="float:left">
1320+
{{ trans('general.accessories_assigned') }}
1321+
</h2>
1322+
1323+
<table
1324+
data-columns="{{ \App\Presenters\AssetPresenter::assignedAccessoriesDataTableLayout() }}"
1325+
data-cookie-id-table="accessoriesAssignedListingTable"
1326+
data-pagination="true"
1327+
data-id-table="accessoriesAssignedListingTable"
1328+
data-search="true"
1329+
data-side-pagination="server"
1330+
data-show-columns="true"
1331+
data-show-export="true"
1332+
data-show-refresh="true"
1333+
data-sort-order="asc"
1334+
data-click-to-select="true"
1335+
id="accessoriesAssignedListingTable"
1336+
class="table table-striped snipe-table"
1337+
data-url="{{ route('api.assets.assigned_accessories', ['asset' => $asset]) }}"
1338+
data-export-options='{
1339+
"fileName": "export-locations-{{ str_slug($asset->name) }}-accessories-{{ date('Y-m-d') }}",
1340+
"ignoreColumn": ["actions","image","change","checkbox","checkincheckout","icon"]
1341+
}'>
1342+
</table>
1343+
1344+
</div><!-- /.table-responsive -->
1345+
</div><!-- /.tab-pane -->
1346+
1347+
12971348
<div class="tab-pane fade" id="maintenances">
12981349
<div class="row{{($asset->assetmaintenances->count() > 0 ) ? '' : ' hidden-print'}}">
12991350
<div class="col-md-12">

resources/views/locations/view.blade.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,13 @@ class="table table-striped snipe-table"
314314
</div><!-- /.tab-pane -->
315315

316316
<div class="tab-pane" id="accessories_assigned">
317-
<h2 class="box-title">
318-
Assigned Accessories
319-
</h2>
320317

321318
<div class="table table-responsive">
319+
320+
<h2 class="box-title" style="float:left">
321+
{{ trans('general.accessories_assigned') }}
322+
</h2>
323+
322324
<table
323325
data-columns="{{ \App\Presenters\LocationPresenter::assignedAccessoriesDataTableLayout() }}"
324326
data-cookie-id-table="accessoriesAssignedListingTable"

0 commit comments

Comments
 (0)