From b1b5eeecbada607ff976f30fa3e38219eae4ade2 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 31 Jul 2018 16:00:38 -0700 Subject: [PATCH] Fixed #6013 - add accessory checkout notes to detail page --- .../Controllers/Api/AccessoriesController.php | 6 ++-- .../Transformers/AccessoriesTransformer.php | 3 +- app/Models/Accessory.php | 33 +++++++++++++++++++ resources/views/accessories/view.blade.php | 1 + 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Api/AccessoriesController.php b/app/Http/Controllers/Api/AccessoriesController.php index 61cf61582..e078999c0 100644 --- a/app/Http/Controllers/Api/AccessoriesController.php +++ b/app/Http/Controllers/Api/AccessoriesController.php @@ -136,14 +136,16 @@ class AccessoriesController extends Controller { $this->authorize('view', Accessory::class); - $accessory = Accessory::findOrFail($id); + $accessory = Accessory::with('lastCheckout')->findOrFail($id); if (!Company::isCurrentUserHasAccess($accessory)) { return ['total' => 0, 'rows' => []]; } + + $accessory->lastCheckoutArray = $accessory->lastCheckout->toArray(); $accessory_users = $accessory->users; $total = $accessory_users->count(); - return (new AccessoriesTransformer)->transformCheckedoutAccessory($accessory_users, $total); + return (new AccessoriesTransformer)->transformCheckedoutAccessory($accessory, $accessory_users, $total); } diff --git a/app/Http/Transformers/AccessoriesTransformer.php b/app/Http/Transformers/AccessoriesTransformer.php index fd6832e00..a4e935dab 100644 --- a/app/Http/Transformers/AccessoriesTransformer.php +++ b/app/Http/Transformers/AccessoriesTransformer.php @@ -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), 'last_name'=> e($user->last_name), 'employee_number' => e($user->employee_num), + 'checkout_notes' => $accessory->lastCheckoutArray[0]['note'], 'type' => 'user', 'available_actions' => ['checkin' => true] ]; diff --git a/app/Models/Accessory.php b/app/Models/Accessory.php index 944304d80..772099418 100755 --- a/app/Models/Accessory.php +++ b/app/Models/Accessory.php @@ -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(); } + /** + * 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 + * @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() { if ($this->image) { return url('/').'/uploads/accessories/'.$this->image; diff --git a/resources/views/accessories/view.blade.php b/resources/views/accessories/view.blade.php index 646a7f448..94e266742 100644 --- a/resources/views/accessories/view.blade.php +++ b/resources/views/accessories/view.blade.php @@ -74,6 +74,7 @@ {{ trans('general.user') }} + {{ trans('general.notes') }} {{ trans('table.actions') }}