From 56b02df7aa9d4d58934e0d61ec5e49be1b24a241 Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 14 Aug 2024 19:33:23 +0100 Subject: [PATCH] Fixed transformer Signed-off-by: snipe --- .../Transformers/LocationsTransformer.php | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/app/Http/Transformers/LocationsTransformer.php b/app/Http/Transformers/LocationsTransformer.php index 0bbcbc161b..c5e25f136b 100644 --- a/app/Http/Transformers/LocationsTransformer.php +++ b/app/Http/Transformers/LocationsTransformer.php @@ -3,6 +3,7 @@ namespace App\Http\Transformers; use App\Helpers\Helper; +use App\Models\Accessory; use App\Models\Location; use Illuminate\Support\Facades\Gate; use Illuminate\Database\Eloquent\Collection; @@ -45,9 +46,9 @@ class LocationsTransformer 'zip' => ($location->zip) ? e($location->zip) : null, 'phone' => ($location->phone!='') ? e($location->phone): null, 'fax' => ($location->fax!='') ? e($location->fax): null, - 'assigned_assets_count' => (int) $location->assigned_assets_count, - 'accessories_count' => (int) $location->assigned_accessories, + 'accessories_count' => (int) $location->accessories_count, 'assigned_accessories_count' => (int) $location->assigned_accessories_count, + 'assigned_assets_count' => (int) $location->assigned_assets_count, 'assets_count' => (int) $location->assets_count, 'rtd_assets_count' => (int) $location->rtd_assets_count, 'users_count' => (int) $location->users_count, @@ -79,6 +80,37 @@ class LocationsTransformer } } + public function transformCheckedoutAccessories($accessory_checkouts, $total) + { + + $array = []; + foreach ($accessory_checkouts as $checkout) { + $array = [ + 'id' => $checkout->id, + 'accessory' => [ + 'id' => $checkout->accessory->id, + 'name' => $checkout->accessory->name, + ], + 'image' => ($checkout->accessory->image) ? Storage::disk('public')->url('accessories/'.e($checkout->accessory->image)) : null, + 'note' => $checkout->note ? e($checkout->note) : null, + 'created_by' => $checkout->admin ? [ + 'id' => (int) $checkout->admin->id, + 'name'=> e($checkout->admin->present()->fullName), + ]: null, + 'created_at' => Helper::getFormattedDateObject($checkout->created_at, 'datetime'), + ]; + + $permissions_array['available_actions'] = [ + 'checkout' => Gate::allows('checkout', Accessory::class), + 'checkin' => Gate::allows('checkin', Accessory::class), + ]; + + $array += $permissions_array; + } + + return (new DatatablesTransformer)->transformDatatables($array, $total); + } + /** * This gives a compact view of the location data without any additional relational queries, * allowing us to 1) deliver a smaller payload and 2) avoid additional queries on relations that