Skip to content

A free open source IT asset/license management system

Fixed #6013 - add accessory checkout notes to detail page

snipeb1b5eeecbada
diff --git a/app/Http/Controllers/Api/AccessoriesController.php b/app/Http/Controllers/Api/AccessoriesController.php
index 61cf61582e..e078999c0f 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 fd6832e00c..a4e935dab1 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 944304d806..772099418d 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 <[email protected]>
+ * @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 646a7f4483..94e2667422 100644
--- a/resources/views/accessories/view.blade.php
+++ b/resources/views/accessories/view.blade.php
@@ -74,6 +74,7 @@
<thead>
<tr>
<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>
</tr>
</thead>