Fixed #6013 - add accessory checkout notes to detail page

This commit is contained in:
snipe 2018-07-31 16:00:38 -07:00
parent 400913631c
commit b1b5eeecba
4 changed files with 40 additions and 3 deletions

View file

@ -136,14 +136,16 @@ class AccessoriesController extends Controller
{ {
$this->authorize('view', Accessory::class); $this->authorize('view', Accessory::class);
$accessory = Accessory::findOrFail($id); $accessory = Accessory::with('lastCheckout')->findOrFail($id);
if (!Company::isCurrentUserHasAccess($accessory)) { if (!Company::isCurrentUserHasAccess($accessory)) {
return ['total' => 0, 'rows' => []]; return ['total' => 0, 'rows' => []];
} }
$accessory->lastCheckoutArray = $accessory->lastCheckout->toArray();
$accessory_users = $accessory->users; $accessory_users = $accessory->users;
$total = $accessory_users->count(); $total = $accessory_users->count();
return (new AccessoriesTransformer)->transformCheckedoutAccessory($accessory_users, $total); return (new AccessoriesTransformer)->transformCheckedoutAccessory($accessory, $accessory_users, $total);
} }

View file

@ -61,7 +61,7 @@ class AccessoriesTransformer
} }
public function transformCheckedoutAccessory ($accessory_users, $total) public function transformCheckedoutAccessory ($accessory, $accessory_users, $total)
{ {
@ -75,6 +75,7 @@ class AccessoriesTransformer
'first_name'=> e($user->first_name), 'first_name'=> e($user->first_name),
'last_name'=> e($user->last_name), 'last_name'=> e($user->last_name),
'employee_number' => e($user->employee_num), 'employee_number' => e($user->employee_num),
'checkout_notes' => $accessory->lastCheckoutArray[0]['note'],
'type' => 'user', 'type' => 'user',
'available_actions' => ['checkin' => true] 'available_actions' => ['checkin' => true]
]; ];

View file

@ -140,6 +140,39 @@ class Accessory extends SnipeModel
return $this->hasMany('\App\Models\Actionlog', 'item_id')->where('item_type', Accessory::class)->orderBy('created_at', 'desc')->withTrashed(); return $this->hasMany('\App\Models\Actionlog', 'item_id')->where('item_type', Accessory::class)->orderBy('created_at', 'desc')->withTrashed();
} }
/**
* Get the LAST checkout for this accessory.
*
* This is kinda gross, but is necessary for how the accessory
* pivot stuff works for now.
*
* It looks like we should be able to use ->first() here and
* return an object instead of a collection, but we actually
* cannot.
*
* In short, you cannot execute the query defined when you're eager loading.
* and in order to avoid 1001 query problems when displaying the most
* recent checkout note, we have to eager load this.
*
* This means we technically return a collection of one here, and then
* in the controller, we convert that collection to an array, so we can
* use it in the transformer to display only the notes of the LAST
* checkout.
*
* It's super-mega-assy, but it's the best I could do for now.
*
* @author A. Gianotto <snipe@snipe.net>
* @since v5.0.0
*
* @see \App\Http\Controllers\Api\AccessoriesController\checkedout()
*
*/
public function lastCheckout()
{
return $this->assetlog()->where('action_type','=','checkout')->take(1);
}
public function getImageUrl() { public function getImageUrl() {
if ($this->image) { if ($this->image) {
return url('/').'/uploads/accessories/'.$this->image; return url('/').'/uploads/accessories/'.$this->image;

View file

@ -74,6 +74,7 @@
<thead> <thead>
<tr> <tr>
<th data-searchable="false" data-formatter="usersLinkFormatter" data-sortable="false" data-field="name">{{ trans('general.user') }}</th> <th data-searchable="false" data-formatter="usersLinkFormatter" data-sortable="false" data-field="name">{{ trans('general.user') }}</th>
<th data-searchable="false" data-sortable="false" data-field="checkout_notes">{{ trans('general.notes') }}</th>
<th data-searchable="false" data-sortable="false" data-field="actions" data-formatter="accessoriesInOutFormatter">{{ trans('table.actions') }}</th> <th data-searchable="false" data-sortable="false" data-field="actions" data-formatter="accessoriesInOutFormatter">{{ trans('table.actions') }}</th>
</tr> </tr>
</thead> </thead>