Skip to content

Commit 04c3481

Browse files
authored
Merge pull request #15973 from uberbrady/next_try_accessories_to_locations_rebased
Next try accessories to locations rebased
2 parents edacc4e + 582b462 commit 04c3481

File tree

79 files changed

+918
-277046
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+918
-277046
lines changed

app/Http/Controllers/Accessories/AccessoryCheckoutController.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,23 @@ public function store(AccessoryCheckoutRequest $request, Accessory $accessory) :
7575
$accessory->checkout_qty = $request->input('checkout_qty', 1);
7676

7777
for ($i = 0; $i < $accessory->checkout_qty; $i++) {
78-
AccessoryCheckout::create([
78+
79+
$accessory_checkout = new AccessoryCheckout([
7980
'accessory_id' => $accessory->id,
8081
'created_at' => Carbon::now(),
81-
'created_by' => auth()->id(),
8282
'assigned_to' => $target->id,
8383
'assigned_type' => $target::class,
8484
'note' => $request->input('note'),
8585
]);
86+
87+
$accessory_checkout->created_by = auth()->id();
88+
$accessory_checkout->save();
8689
}
90+
8791
event(new CheckoutableCheckedOut($accessory, $target, auth()->user(), $request->input('note')));
8892

89-
// Set this as user since we only allow checkout to user for this item type
9093
$request->request->add(['checkout_to_type' => request('checkout_to_type')]);
91-
$request->request->add(['assigned_user' => $target->id]);
94+
$request->request->add(['assigned_to' => $target->id]);
9295

9396
session()->put(['redirect_option' => $request->get('redirect_option'), 'checkout_to_type' => $request->get('checkout_to_type')]);
9497

app/Http/Controllers/Api/AccessoriesController.php

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use App\Models\Accessory;
1414
use App\Models\Company;
1515
use App\Models\User;
16+
use Illuminate\Http\JsonResponse;
1617
use Illuminate\Support\Facades\Auth;
1718
use Carbon\Carbon;
1819
use Illuminate\Support\Facades\DB;
@@ -184,39 +185,33 @@ public function accessory_detail($id)
184185

185186

186187
/**
187-
* Display the specified resource.
188+
* Get the list of checkouts for a specific accessory
188189
*
189190
* @author [A. Gianotto] [<[email protected]>]
190191
* @since [v4.0]
191192
* @param int $id
192-
* @return \Illuminate\Http\Response
193+
* @return | array
193194
*/
194-
public function checkedout($id, Request $request)
195+
public function checkedout(Request $request, $id)
195196
{
196197
$this->authorize('view', Accessory::class);
197198

198199
$accessory = Accessory::with('lastCheckout')->findOrFail($id);
199-
200200
$offset = request('offset', 0);
201201
$limit = request('limit', 50);
202202

203-
$accessory_checkouts = $accessory->checkouts;
204-
$total = $accessory_checkouts->count();
205-
206-
if ($total < $offset) {
207-
$offset = 0;
208-
}
209-
210-
$accessory_checkouts = $accessory->checkouts()->skip($offset)->take($limit)->get();
203+
// Total count of all checkouts for this asset
204+
$accessory_checkouts = $accessory->checkouts();
211205

206+
// Check for search text in the request
212207
if ($request->filled('search')) {
213-
214-
$accessory_checkouts = $accessory->checkouts()->TextSearch($request->input('search'))
215-
->get();
216-
$total = $accessory_checkouts->count();
208+
$accessory_checkouts = $accessory_checkouts->TextSearch($request->input('search'));
217209
}
218210

219-
return (new AccessoriesTransformer)->transformCheckedoutAccessory($accessory, $accessory_checkouts, $total);
211+
$total = $accessory_checkouts->count();
212+
$accessory_checkouts = $accessory_checkouts->skip($offset)->take($limit)->get();
213+
214+
return (new AccessoriesTransformer)->transformCheckedoutAccessory($accessory_checkouts, $total);
220215
}
221216

222217

@@ -227,7 +222,7 @@ public function checkedout($id, Request $request)
227222
* @since [v4.0]
228223
* @param \App\Http\Requests\ImageUploadRequest $request
229224
* @param int $id
230-
* @return \Illuminate\Http\Response
225+
* @return \Illuminate\Http\JsonResponse
231226
*/
232227
public function update(ImageUploadRequest $request, $id)
233228
{
@@ -249,7 +244,7 @@ public function update(ImageUploadRequest $request, $id)
249244
* @author [A. Gianotto] [<[email protected]>]
250245
* @since [v4.0]
251246
* @param int $id
252-
* @return \Illuminate\Http\Response
247+
* @return \Illuminate\Http\JsonResponse
253248
*/
254249
public function destroy($id)
255250
{
@@ -284,14 +279,17 @@ public function checkout(AccessoryCheckoutRequest $request, Accessory $accessory
284279
$accessory->checkout_qty = $request->input('checkout_qty', 1);
285280

286281
for ($i = 0; $i < $accessory->checkout_qty; $i++) {
287-
AccessoryCheckout::create([
282+
283+
$accessory_checkout = new AccessoryCheckout([
288284
'accessory_id' => $accessory->id,
289285
'created_at' => Carbon::now(),
290-
'created_by' => auth()->id(),
291286
'assigned_to' => $target->id,
292287
'assigned_type' => $target::class,
293288
'note' => $request->input('note'),
294289
]);
290+
291+
$accessory_checkout->created_by = auth()->id();
292+
$accessory_checkout->save();
295293
}
296294

297295
// Set this value to be able to pass the qty through to the event

app/Http/Controllers/Api/AssetsController.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Http\Requests\StoreAssetRequest;
77
use App\Http\Requests\UpdateAssetRequest;
88
use App\Http\Traits\MigratesLegacyAssetLocations;
9+
use App\Models\AccessoryCheckout;
910
use App\Models\CheckoutAcceptance;
1011
use App\Models\LicenseSeat;
1112
use Illuminate\Database\Eloquent\Builder;
@@ -26,11 +27,9 @@
2627
use App\Models\Location;
2728
use App\Models\Setting;
2829
use App\Models\User;
29-
use Illuminate\Support\Facades\Auth;
3030
use Carbon\Carbon;
3131
use Illuminate\Support\Facades\DB;
3232
use Illuminate\Http\Request;
33-
use App\Http\Requests\ImageUploadRequest;
3433
use Illuminate\Support\Facades\Log;
3534
use Illuminate\Support\Facades\Route;
3635
use App\View\Label;
@@ -129,6 +128,7 @@ public function index(Request $request, $action = null, $upcoming_status = null)
129128

130129
$assets = Asset::select('assets.*')
131130
->with(
131+
'model',
132132
'location',
133133
'assetstatus',
134134
'company',
@@ -140,7 +140,7 @@ public function index(Request $request, $action = null, $upcoming_status = null)
140140
'model.manufacturer',
141141
'model.fieldset',
142142
'supplier'
143-
); //it might be tempting to add 'assetlog' here, but don't. It blows up update-heavy users.
143+
); // it might be tempting to add 'assetlog' here, but don't. It blows up update-heavy users.
144144

145145

146146
if ($filter_non_deprecable_assets) {
@@ -1214,6 +1214,27 @@ public function requestable(Request $request): JsonResponse | array
12141214
return (new AssetsTransformer)->transformRequestedAssets($assets, $total);
12151215
}
12161216

1217+
1218+
public function assignedAssets(Request $request, Asset $asset) : JsonResponse | array
1219+
{
1220+
1221+
return [];
1222+
// to do
1223+
}
1224+
1225+
public function assignedAccessories(Request $request, Asset $asset) : JsonResponse | array
1226+
{
1227+
$this->authorize('view', Asset::class);
1228+
$this->authorize('view', $asset);
1229+
$accessory_checkouts = AccessoryCheckout::AssetsAssigned()->with('adminuser')->with('accessories');
1230+
1231+
$offset = ($request->input('offset') > $accessory_checkouts->count()) ? $accessory_checkouts->count() : app('api_offset_value');
1232+
$limit = app('api_limit_value');
1233+
1234+
$total = $accessory_checkouts->count();
1235+
$accessory_checkouts = $accessory_checkouts->skip($offset)->take($limit)->get();
1236+
return (new AssetsTransformer)->transformCheckedoutAccessories($accessory_checkouts, $total);
1237+
}
12171238
/**
12181239
* Generate asset labels by tag
12191240
*

app/Http/Controllers/Api/LocationsController.php

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
namespace App\Http\Controllers\Api;
44

55
use App\Helpers\Helper;
6-
use App\Http\Requests\ImageUploadRequest;
76
use App\Http\Controllers\Controller;
7+
use App\Http\Requests\ImageUploadRequest;
8+
use App\Http\Transformers\AccessoriesTransformer;
89
use App\Http\Transformers\AssetsTransformer;
910
use App\Http\Transformers\LocationsTransformer;
1011
use App\Http\Transformers\SelectlistTransformer;
12+
use App\Models\Accessory;
13+
use App\Models\AccessoryCheckout;
1114
use App\Models\Asset;
1215
use App\Models\Location;
16+
use Illuminate\Http\JsonResponse;
1317
use Illuminate\Http\Request;
1418
use Illuminate\Pagination\LengthAwarePaginator;
1519
use Illuminate\Support\Collection;
16-
use Illuminate\Http\JsonResponse;
1720

1821
class LocationsController extends Controller
1922
{
@@ -28,26 +31,28 @@ public function index(Request $request) : JsonResponse | array
2831
{
2932
$this->authorize('view', Location::class);
3033
$allowed_columns = [
31-
'id',
32-
'name',
34+
'accessories_count',
3335
'address',
3436
'address2',
37+
'assets_count',
38+
'assets_count',
39+
'assigned_accessories_count',
40+
'assigned_assets_count',
41+
'assigned_assets_count',
3542
'city',
36-
'state',
3743
'country',
38-
'zip',
3944
'created_at',
40-
'updated_at',
41-
'manager_id',
42-
'image',
43-
'assigned_assets_count',
44-
'users_count',
45-
'assets_count',
46-
'assigned_assets_count',
47-
'assets_count',
48-
'rtd_assets_count',
4945
'currency',
46+
'id',
47+
'image',
5048
'ldap_ou',
49+
'manager_id',
50+
'name',
51+
'rtd_assets_count',
52+
'state',
53+
'updated_at',
54+
'users_count',
55+
'zip',
5156
];
5257

5358
$locations = Location::with('parent', 'manager', 'children')->select([
@@ -68,8 +73,11 @@ public function index(Request $request) : JsonResponse | array
6873
'locations.image',
6974
'locations.ldap_ou',
7075
'locations.currency',
71-
])->withCount('assignedAssets as assigned_assets_count')
76+
])
77+
->withCount('assignedAssets as assigned_assets_count')
7278
->withCount('assets as assets_count')
79+
->withCount('assignedAccessories as assigned_accessories_count')
80+
->withCount('accessories as accessories_count')
7381
->withCount('rtd_assets as rtd_assets_count')
7482
->withCount('children as children_count')
7583
->withCount('users as users_count');
@@ -224,7 +232,17 @@ public function update(ImageUploadRequest $request, $id) : JsonResponse
224232
return response()->json(Helper::formatStandardApiResponse('error', null, $location->getErrors()));
225233
}
226234

235+
227236
public function assets(Request $request, Location $location) : JsonResponse | array
237+
{
238+
$this->authorize('view', Asset::class);
239+
$this->authorize('view', $location);
240+
$assets = Asset::where('location_id', '=', $location->id)->with('model', 'model.category', 'assetstatus', 'location', 'company', 'defaultLoc');
241+
$assets = $assets->get();
242+
return (new AssetsTransformer)->transformAssets($assets, $assets->count(), $request);
243+
}
244+
245+
public function assignedAssets(Request $request, Location $location) : JsonResponse | array
228246
{
229247
$this->authorize('view', Asset::class);
230248
$this->authorize('view', $location);
@@ -233,6 +251,20 @@ public function assets(Request $request, Location $location) : JsonResponse | ar
233251
return (new AssetsTransformer)->transformAssets($assets, $assets->count(), $request);
234252
}
235253

254+
public function assignedAccessories(Request $request, Location $location) : JsonResponse | array
255+
{
256+
$this->authorize('view', Accessory::class);
257+
$this->authorize('view', $location);
258+
$accessory_checkouts = AccessoryCheckout::LocationAssigned()->with('adminuser')->with('accessories');
259+
260+
$offset = ($request->input('offset') > $accessory_checkouts->count()) ? $accessory_checkouts->count() : app('api_offset_value');
261+
$limit = app('api_limit_value');
262+
263+
$total = $accessory_checkouts->count();
264+
$accessory_checkouts = $accessory_checkouts->skip($offset)->take($limit)->get();
265+
return (new LocationsTransformer)->transformCheckedoutAccessories($accessory_checkouts, $total);
266+
}
267+
236268
/**
237269
* Remove the specified resource from storage.
238270
*

app/Http/Controllers/CustomFieldsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function store(CustomFieldRequest $request) : RedirectResponse
104104
"auto_add_to_fieldsets" => $request->get("auto_add_to_fieldsets", 0),
105105
"show_in_listview" => $request->get("show_in_listview", 0),
106106
"show_in_requestable_list" => $request->get("show_in_requestable_list", 0),
107-
"user_id" => auth()->id()
107+
"created_by" => auth()->id()
108108
]);
109109

110110

app/Http/Transformers/AccessoriesTransformer.php

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,21 @@ public function transformAccessory(Accessory $accessory)
6969
return $array;
7070
}
7171

72-
public function transformCheckedoutAccessory($accessory, $accessory_checkouts, $total)
72+
public function transformCheckedoutAccessory($accessory_checkouts, $total)
7373
{
7474
$array = [];
7575

7676
foreach ($accessory_checkouts as $checkout) {
7777
$array[] = [
7878
'id' => $checkout->id,
7979
'assigned_to' => $this->transformAssignedTo($checkout),
80-
'checkout_notes' => e($checkout->note),
81-
'last_checkout' => Helper::getFormattedDateObject($checkout->created_at, 'datetime'),
82-
'available_actions' => ['checkin' => true],
80+
'note' => $checkout->note ? e($checkout->note) : null,
81+
'created_by' => $checkout->adminuser ? [
82+
'id' => (int) $checkout->adminuser->id,
83+
'name'=> e($checkout->adminuser->present()->fullName),
84+
]: null,
85+
'created_at' => Helper::getFormattedDateObject($checkout->created_at, 'datetime'),
86+
'available_actions' => Gate::allows('checkout', Accessory::class) ? ['checkin' => true] : ['checkin' => false],
8387
];
8488
}
8589

@@ -89,22 +93,11 @@ public function transformCheckedoutAccessory($accessory, $accessory_checkouts, $
8993
public function transformAssignedTo($accessoryCheckout)
9094
{
9195
if ($accessoryCheckout->checkedOutToUser()) {
92-
return [
93-
'id' => (int) $accessoryCheckout->assigned->id,
94-
'username' => e($accessoryCheckout->assigned->username),
95-
'name' => e($accessoryCheckout->assigned->getFullNameAttribute()),
96-
'first_name'=> e($accessoryCheckout->assigned->first_name),
97-
'last_name'=> ($accessoryCheckout->assigned->last_name) ? e($accessoryCheckout->assigned->last_name) : null,
98-
'email'=> ($accessoryCheckout->assigned->email) ? e($accessoryCheckout->assigned->email) : null,
99-
'employee_number' => ($accessoryCheckout->assigned->employee_num) ? e($accessoryCheckout->assigned->employee_num) : null,
100-
'type' => 'user',
101-
];
96+
return (new UsersTransformer)->transformUserCompact($accessoryCheckout->assigned);
97+
} elseif ($accessoryCheckout->checkedOutToLocation()) {
98+
return (new LocationsTransformer())->transformLocationCompact($accessoryCheckout->assigned);
99+
} elseif ($accessoryCheckout->checkedOutToAsset()) {
100+
return (new AssetsTransformer())->transformAssetCompact($accessoryCheckout->assigned);
102101
}
103-
104-
return $accessoryCheckout->assigned ? [
105-
'id' => $accessoryCheckout->assigned->id,
106-
'name' => e($accessoryCheckout->assigned->display_name),
107-
'type' => $accessoryCheckout->assignedType(),
108-
] : null;
109102
}
110103
}

0 commit comments

Comments
 (0)