diff --git a/app/Http/Controllers/Assets/AssetCheckoutController.php b/app/Http/Controllers/Assets/AssetCheckoutController.php index 64eebcd211..9c33b15802 100644 --- a/app/Http/Controllers/Assets/AssetCheckoutController.php +++ b/app/Http/Controllers/Assets/AssetCheckoutController.php @@ -10,6 +10,7 @@ use App\Http\Requests\AssetCheckoutRequest; use App\Models\Asset; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Support\Facades\Auth; +use Illuminate\Http\Request; class AssetCheckoutController extends Controller { @@ -24,8 +25,9 @@ class AssetCheckoutController extends Controller * @since [v1.0] * @return View */ - public function create($assetId) + public function create(Request $request, $assetId) { + // Check if the asset exists if (is_null($asset = Asset::find(e($assetId)))) { return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist')); @@ -34,10 +36,28 @@ class AssetCheckoutController extends Controller $this->authorize('checkout', $asset); if ($asset->availableForCheckout()) { + + /** + * Set a session variable here for the possible return url - this may or may not be used + * in the store() method below, but we need to set it here, so we know what "back" means. + * We set this here so that we don't end up with UX weirdness if our referrer isn't what we're + * expecting (for example a checkout that happens from a place we didn't plan for yet. + * - @snipe - 2022-12-22 + */ + $request->session()->forget('backto_item_type'); + $request->session()->forget('backto_item_id'); + \Log::debug('dropping session data for backto_item_type and backto_item_id'); + + // Hard-coding this for now, since we're only workiing with assets, not other first-class items yet + $request->session()->put('backto_item_type', '\App\Models\Asset'); + $request->session()->put('backto_item_id', $assetId); + \Log::debug('setting new session data for asset '. $assetId . ' via backto_item_type and backto_item_id'); + return view('hardware/checkout', compact('asset')) ->with('statusLabel_list', Helper::deployableStatusLabelList()); } + return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.checkout.not_available')); } @@ -53,13 +73,11 @@ class AssetCheckoutController extends Controller public function store(AssetCheckoutRequest $request, $assetId) { - $bulk_back_url = request()->headers->get('referer'); - session(['return_to' => $request->input('return-to')]); - $asset = Asset::find($assetId); $this->authorize('checkout', $asset); + try { // Check if the asset exists if (!$asset) { @@ -67,19 +85,19 @@ class AssetCheckoutController extends Controller } elseif (!$asset->availableForCheckout()) { return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.checkout.not_available')); } + $this->authorize('checkout', $asset); + $admin = Auth::user(); - $target = $this->determineCheckoutTarget($asset); - $asset = $this->updateAssetLocation($asset, $target); - + $expected_checkin = ''; $checkout_at = date('Y-m-d H:i:s'); + if (($request->filled('checkout_at')) && ($request->get('checkout_at') != date('Y-m-d'))) { $checkout_at = $request->get('checkout_at'); } - $expected_checkin = ''; if ($request->filled('expected_checkin')) { $expected_checkin = $request->get('expected_checkin'); } @@ -88,20 +106,32 @@ class AssetCheckoutController extends Controller $asset->status_id = $request->get('status_id'); } - if(!empty($asset->licenseseats->all())){ - if(request('checkout_to_type') == 'user') { + if ($request->filled('next_action')) { + $back_to_route = $request->get('next_action'); + } + + if(!empty($asset->licenseseats->all())) { + + if (request('checkout_to_type') == 'user') { foreach ($asset->licenseseats as $seat){ $seat->assigned_to = $target->id; $seat->save(); } } + } if ($asset->checkOut($target, $admin, $checkout_at, $expected_checkin, e($request->get('note')), $request->get('name'))) { - return redirect()->route('hardware.index')->with('success', trans('admin/hardware/message.checkout.success')); + + if ($request->get('accept_in_person')=='1') { + return view('hardware/checkout', compact('asset')) + ->with('statusLabel_list', Helper::deployableStatusLabelList()); + } + + return redirect()->route($back_to_route)->with('success', trans('admin/hardware/message.checkout.success')); } - // Redirect to the asset management page with error + // Redirect to the asset page with error return redirect()->to("hardware/$assetId/checkout")->with('error', trans('admin/hardware/message.checkout.error').$asset->getErrors()); } catch (ModelNotFoundException $e) { return redirect()->back()->with('error', trans('admin/hardware/message.checkout.error'))->withErrors($asset->getErrors()); diff --git a/resources/views/hardware/checkout.blade.php b/resources/views/hardware/checkout.blade.php index 21cf21a045..3cf76221f9 100755 --- a/resources/views/hardware/checkout.blade.php +++ b/resources/views/hardware/checkout.blade.php @@ -161,9 +161,11 @@
Return to: - + + + + @@ -175,11 +177,6 @@
- - - - -