diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php index b1e2fac746..75c0658bb3 100644 --- a/app/Helpers/Helper.php +++ b/app/Helpers/Helper.php @@ -1463,4 +1463,38 @@ class Helper return $new_locale; // better that you have some weird locale that doesn't fit into our mappings anywhere than 'void' } + + static public function getRedirectOption($request, $id, $table, $asset_id = null) + { + + $redirect_option = Session::get('redirect_option'); + $checkout_to_type = Session::get('checkout_to_type'); + + //return to index + if ($redirect_option == '0') { + switch ($table) { + case "Assets": + return redirect()->route('hardware.index')->with('success', trans('admin/hardware/message.checkout.success')); + } + } + //return to thing being assigned + if ($redirect_option == '1') { + switch ($table) { + case "Assets": + return redirect()->route('hardware.show', $id ? $id : $asset_id)->with('success', trans('admin/hardware/message.checkout.success')); + } + } + //return to thing being assigned to + if ($redirect_option == '2') { + switch ($checkout_to_type) { + case 'user': + return redirect()->route('users.show', $request->assigned_user)->with('success', trans('admin/hardware/message.checkout.success')); + case 'location': + return redirect()->route('locations.show', $request->assigned_location)->with('success', trans('admin/hardware/message.checkout.success')); + case 'asset': + return redirect()->route('hardware.show', $request->assigned_asset)->with('success', trans('admin/hardware/message.checkout.success')); + } + } + return redirect()->back()->with('error', trans('admin/hardware/message.checkout.error')); + } } diff --git a/app/Http/Controllers/Assets/AssetCheckinController.php b/app/Http/Controllers/Assets/AssetCheckinController.php index 8c5709c99a..d05822a55f 100644 --- a/app/Http/Controllers/Assets/AssetCheckinController.php +++ b/app/Http/Controllers/Assets/AssetCheckinController.php @@ -13,6 +13,7 @@ use App\Models\LicenseSeat; use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Redirect; +use Illuminate\Support\Facades\Session; use Illuminate\Support\Facades\View; use Illuminate\Support\Facades\Log; @@ -46,7 +47,7 @@ class AssetCheckinController extends Controller return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.checkin.already_checked_in')); } - return view('hardware/checkin', compact('asset'))->with('statusLabel_list', Helper::statusLabelList())->with('backto', $backto); + return view('hardware/checkin', compact('asset'))->with('statusLabel_list', Helper::statusLabelList())->with('backto', $backto)->with('table_name', 'Assets'); } /** @@ -123,15 +124,12 @@ class AssetCheckinController extends Controller $acceptance->delete(); }); + Session::put('redirect_option', $request->get('redirect_option')); // Was the asset updated? if ($asset->save()) { + event(new CheckoutableCheckedIn($asset, $target, Auth::user(), $request->input('note'), $checkin_at, $originalValues)); - - if ((isset($user)) && ($backto == 'user')) { - return redirect()->route('users.show', $user->id)->with('success', trans('admin/hardware/message.checkin.success')); - } - - return redirect()->route('hardware.index')->with('success', trans('admin/hardware/message.checkin.success')); + return Helper::getRedirectOption($asset, $assetId, 'Assets'); } // Redirect to the asset management page with error return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.checkin.error').$asset->getErrors()); diff --git a/app/Http/Controllers/Assets/AssetCheckoutController.php b/app/Http/Controllers/Assets/AssetCheckoutController.php index 71c28c24af..0cbbb79b6c 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\Support\Facades\Session; class AssetCheckoutController extends Controller { @@ -35,7 +36,8 @@ class AssetCheckoutController extends Controller if ($asset->availableForCheckout()) { return view('hardware/checkout', compact('asset')) - ->with('statusLabel_list', Helper::deployableStatusLabelList()); + ->with('statusLabel_list', Helper::deployableStatusLabelList()) + ->with('table_name', 'Assets'); } return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.checkout.not_available')); @@ -97,11 +99,12 @@ class AssetCheckoutController extends Controller return redirect()->to("hardware/$assetId/checkout")->with('error', trans('general.error_user_company')); } } - - if ($asset->checkOut($target, $admin, $checkout_at, $expected_checkin, $request->get('note'), $request->get('name'))) { - return redirect()->route('hardware.index')->with('success', trans('admin/hardware/message.checkout.success')); - } + Session::put(['redirect_option' => $request->get('redirect_option'), 'checkout_to_type' => $request->get('checkout_to_type')]); + + if ($asset->checkOut($target, $admin, $checkout_at, $expected_checkin, $request->get('note'), $request->get('name'))) { + return Helper::getRedirectOption($request, $assetId, 'Assets'); + } // Redirect to the asset management page with error return redirect()->to("hardware/$assetId/checkout")->with('error', trans('admin/hardware/message.checkout.error').$asset->getErrors()); } catch (ModelNotFoundException $e) { diff --git a/resources/lang/en-US/admin/hardware/form.php b/resources/lang/en-US/admin/hardware/form.php index a7aba0813c..edec543637 100644 --- a/resources/lang/en-US/admin/hardware/form.php +++ b/resources/lang/en-US/admin/hardware/form.php @@ -39,6 +39,9 @@ return [ 'order' => 'Order Number', 'qr' => 'QR Code', 'requestable' => 'Users may request this asset', + 'redirect_to_all' => 'Return to all :type', + 'redirect_to_type' => 'Go to :type', + 'redirect_to_checked_out_to' => 'Go to Checked Out to', 'select_statustype' => 'Select Status Type', 'serial' => 'Serial', 'status' => 'Status', diff --git a/resources/views/hardware/checkin.blade.php b/resources/views/hardware/checkin.blade.php index c41ef474db..debfdef91c 100755 --- a/resources/views/hardware/checkin.blade.php +++ b/resources/views/hardware/checkin.blade.php @@ -101,10 +101,7 @@ {!! $errors->first('note', '') !!} - + @include ('partials.forms.redirect_submit_options', ['route' => 'hardware.index', 'table_name' => $table_name, 'type'=> $asset->model->name, 'checkin' => true]) diff --git a/resources/views/hardware/checkout.blade.php b/resources/views/hardware/checkout.blade.php index 9f91dace17..bb02992819 100755 --- a/resources/views/hardware/checkout.blade.php +++ b/resources/views/hardware/checkout.blade.php @@ -141,10 +141,7 @@ @endif - + @include ('partials.forms.redirect_submit_options', ['route' => 'hardware.index', 'table_name' => $table_name, 'type'=> $asset->model->name, 'checkin' => false]) diff --git a/resources/views/partials/forms/redirect_submit_options.blade.php b/resources/views/partials/forms/redirect_submit_options.blade.php new file mode 100644 index 0000000000..dd5134e7ad --- /dev/null +++ b/resources/views/partials/forms/redirect_submit_options.blade.php @@ -0,0 +1,11 @@ + \ No newline at end of file diff --git a/tests/Feature/Checkins/AssetCheckinTest.php b/tests/Feature/Checkins/AssetCheckinTest.php index 1e6d2b995b..7734d4831d 100644 --- a/tests/Feature/Checkins/AssetCheckinTest.php +++ b/tests/Feature/Checkins/AssetCheckinTest.php @@ -51,14 +51,13 @@ class AssetCheckinTest extends TestCase $this->actingAs(User::factory()->checkinAssets()->create()) ->post( - route('hardware.checkin.store', ['assetId' => $asset->id, 'backto' => 'user']), + route('hardware.checkin.store', ['assetId' => $asset->id]), [ 'name' => 'Changed Name', 'status_id' => $status->id, 'location_id' => $location->id, ], - ) - ->assertRedirect(route('users.show', $user)); + ); $this->assertNull($asset->refresh()->assignedTo); $this->assertNull($asset->expected_checkin); diff --git a/tests/Unit/Helpers/HelperTest.php b/tests/Unit/Helpers/HelperTest.php index cfc8c7fac2..3ef114b798 100644 --- a/tests/Unit/Helpers/HelperTest.php +++ b/tests/Unit/Helpers/HelperTest.php @@ -3,6 +3,9 @@ namespace Tests\Unit\Helpers; use App\Helpers\Helper; +use Illuminate\Http\RedirectResponse; +use Illuminate\Support\Facades\Redirect; +use Illuminate\Support\Facades\Session; use Tests\TestCase; class HelperTest extends TestCase @@ -25,4 +28,60 @@ class HelperTest extends TestCase $this->settings->set(['digit_separator' => '1.234,56']); $this->assertSame(12.34, Helper::ParseCurrency('12,34')); } + public function testGetRedirectOptionMethod() + { + $test_data = [ + 'Option 2: redirect for user assigned to ' => [ + 'request' =>(object) ['assigned_user' => 22], + 'id' => 1, + 'checkout_to_type' => 'user', + 'redirect_option' => 2, + 'table' => 'Assets', + 'route' => route('users.show', 22), + ], + 'Option 2: redirect location assigned to ' => [ + 'request' =>(object) ['assigned_location' => 10], + 'id' => 2, + 'checkout_to_type' => 'location', + 'redirect_option' => 2, + 'table' => 'Locations', + 'route' => route('locations.show', 10), + ], + 'Option 2: redirect back to asset assigned to ' => [ + 'request' =>(object) ['assigned_asset' => 101], + 'id' => 3, + 'checkout_to_type' => 'asset', + 'redirect_option' => 2, + 'table' => 'Assets', + 'route' => route('hardware.show', 101), + ], + 'Option 1: redirect back to asset ' => [ + 'request' =>(object) ['assigned_asset' => null], + 'id' => 999, + 'checkout_to_type' => null, + 'redirect_option' => 1, + 'table' => 'Assets', + 'route' => route('hardware.show', 999), + ], + 'Option 0: redirect back to index ' => [ + 'request' =>(object) ['assigned_asset' => null], + 'id' => null, + 'checkout_to_type' => null, + 'redirect_option' => 0, + 'table' => 'Assets', + 'route' => route('hardware.index'), + ], + ]; + + foreach ($test_data as $scenario => $data ) { + + Session::put('redirect_option', $data['redirect_option']); + Session::put('checkout_to_type', $data['checkout_to_type']); + + $redirect = Helper::getRedirectOption($data['request'],$data['id'], $data['table']); + + $this->assertInstanceOf(RedirectResponse::class, $redirect); + $this->assertEquals($data['route'], $redirect->getTargetUrl(), $scenario.'failed.'); + } + } }