A free open source IT asset/license management system
Fixed #19387 - added redirect preference to bulk checkout
snipe66fc6a5bea17
diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.phpindex 7c2e2289b3..b7457a196a 100644--- a/app/Http/Controllers/Assets/BulkAssetsController.php+++ b/app/Http/Controllers/Assets/BulkAssetsController.php@@ -754,9 +754,12 @@ class BulkAssetsController extends Controller$asset->status_id = $request->input('status_id');}- if ($request->boolean('set_not_requestable')) {- $asset->requestable = false;- }+ // The bulk-checkout form now carries a plain `requestable`+ // checkbox (matching the per-item hardware/checkout form+ // so the localStorage-backed remembered-default JS is+ // shared between the two). Always set the flag from the+ // request, so the operator's explicit choice sticks.+ $asset->requestable = $request->boolean('requestable');$checkout_success = $asset->checkOut($target, $admin, $checkout_at, $expected_checkin, e($request->input('note')), $asset->name, null);@@ -785,8 +788,17 @@ class BulkAssetsController extends Controllere($request->get('note')),);- // Redirect to the new asset page- return redirect()->to('hardware')->with('success', trans_choice('admin/hardware/message.multi-checkout.success', $asset_ids));+ // Honor the redirect_option select from the form. Choosing+ // 'bulk_checkout' bounces the operator right back to the+ // bulk-checkout screen so they can keep scanning without+ // navigating away — the workflow that Quick Scan Checkin+ // uses for the checkin side. Any other value (default is+ // 'index') falls back to the asset listing.+ $redirect = $request->input('redirect_option') === 'bulk_checkout'+ ? route('hardware.bulkcheckout.show')+ : route('hardware.index');++ return redirect()->to($redirect)->with('success', trans_choice('admin/hardware/message.multi-checkout.success', $asset_ids));}// Redirect to the asset management page with errordiff --git a/resources/lang/en-US/admin/hardware/form.php b/resources/lang/en-US/admin/hardware/form.phpindex fca53b1bb4..8e2b7e9046 100644--- a/resources/lang/en-US/admin/hardware/form.php+++ b/resources/lang/en-US/admin/hardware/form.php@@ -42,6 +42,7 @@ return ['redirect_to_all' => 'Return to all :type','redirect_to_type' => 'Go to :type','redirect_to_checked_out_to' => 'Go to Checked Out to',+ 'redirect_to_bulk_checkout' => 'Return to Bulk Checkout','select_statustype' => 'Select Status Type','serial' => 'Serial','serial_required' => 'Asset :number requires a serial number',diff --git a/resources/views/hardware/bulk-checkout.blade.php b/resources/views/hardware/bulk-checkout.blade.phpindex 70a1c64616..c76c75214a 100644--- a/resources/views/hardware/bulk-checkout.blade.php+++ b/resources/views/hardware/bulk-checkout.blade.php@@ -48,24 +48,12 @@</x-slot:input></x-form.row>- <x-form.row- :label="trans('admin/hardware/form.requestable')"- name="set_not_requestable"- >- <x-slot:input>- <x-input.select- name="set_not_requestable"- id="set_not_requestable"- :options="[- '' => trans('general.do_not_change'),- '1' => trans('admin/hardware/general.not_requestable'),- ]"- :selected="old('set_not_requestable', '')"- style="width: 100%;"- aria-label="set_not_requestable"- />- </x-slot:input>- </x-form.row>+ <x-form.checkbox-row+ name="requestable"+ :label="trans('admin/hardware/general.requestable')"+ data-user-preference-key="snipeit.checkout.requestable_default.{{ auth()->id() ?? 'guest' }}"+ data-had-old-input="{{ ((bool) old('requestable', false)) || session()->has('_old_input.requestable') ? '1' : '0' }}"+ />@include ('partials.forms.checkout-selector', ['user_select' => 'true', 'asset_select' => 'true', 'location_select' => 'true'])<x-input.user-select@@ -102,10 +90,14 @@</x-form.row><x-slot:customfooter>- <div class="box-footer">- <a class="btn btn-link" href="{{ URL::previous() }}">{{ trans('button.cancel') }}</a>- <button type="submit" class="btn btn-primary pull-right"><x-icon type="checkmark"/> {{ trans('general.checkout') }}</button>- </div>+ <x-redirect_submit_options+ index_route="hardware.index"+ :button_label="trans('general.checkout')"+ :options="[+ 'bulk_checkout' => trans('admin/hardware/form.redirect_to_bulk_checkout'),+ 'index' => trans('admin/hardware/form.redirect_to_all', ['type' => trans('general.assets')]),+ ]"+ /></x-slot:customfooter></x-box>diff --git a/resources/views/hardware/checkout.blade.php b/resources/views/hardware/checkout.blade.phpindex 9a01e4dbe5..722a699062 100755--- a/resources/views/hardware/checkout.blade.php+++ b/resources/views/hardware/checkout.blade.php@@ -71,6 +71,8 @@name="requestable":label="trans('admin/hardware/general.requestable')":item="$asset"+ data-user-preference-key="snipeit.checkout.requestable_default.{{ auth()->id() ?? 'guest' }}"+ data-had-old-input="{{ ((bool) old('requestable', false)) || session()->has('_old_input.requestable') ? '1' : '0' }}"/>@include ('partials.forms.checkout-selector', ['user_select' => 'true', 'asset_select' => 'true', 'location_select' => 'true'])@@ -194,51 +196,3 @@</x-container>@stop--@section('moar_scripts')-- <script nonce="{{ csrf_token() }}">- // Per-user localStorage preference for the requestable default on- // checkout. Namespaced by user id so a shared browser doesn't leak one- // user's habit into another user's default. Only takes over when the- // field wasn't repopulated from a validation-error redirect (old()- // beats the stored preference). On submit we save whatever the user- // actually chose, so the preference tracks their real habit.- const initializeCheckoutRequestablePreference = function () {- const storageKey = 'snipeit.checkout.requestable_default.' + @json(auth()->id() ?? 'guest');- const hadOldInput = @json((bool) old('requestable', false)) || @json(session()->has('_old_input.requestable'));- const checkbox = document.getElementById('requestable');- const form = checkbox ? checkbox.closest('form') : null;-- if (!checkbox || !form) {- return;- }-- if (!hadOldInput) {- let stored = null;- try {- stored = window.localStorage.getItem(storageKey);- } catch (e) {- // localStorage may be unavailable (private mode, disabled).- }- if (stored === '1' || stored === '0') {- checkbox.checked = stored === '1';- }- }-- form.addEventListener('submit', function () {- try {- window.localStorage.setItem(storageKey, checkbox.checked ? '1' : '0');- } catch (e) {- // Non-fatal: preference just won't persist this time.- }- });- };-- if (document.readyState === 'loading') {- document.addEventListener('DOMContentLoaded', initializeCheckoutRequestablePreference);- } else {- initializeCheckoutRequestablePreference();- }- </script>-@stopdiff --git a/tests/Feature/Checkouts/Ui/BulkAssetCheckoutTest.php b/tests/Feature/Checkouts/Ui/BulkAssetCheckoutTest.phpindex 289f2ace8c..d130c76757 100644--- a/tests/Feature/Checkouts/Ui/BulkAssetCheckoutTest.php+++ b/tests/Feature/Checkouts/Ui/BulkAssetCheckoutTest.php@@ -106,17 +106,21 @@ class BulkAssetCheckoutTest extends TestCase}}- public function test_bulk_checkout_can_set_assets_to_not_requestable()+ public function test_bulk_checkout_sets_requestable_to_false_when_checkbox_unchecked(){$assets = Asset::factory()->count(2)->create(['requestable' => 1]);$targetUser = User::factory()->create();+ // Bulk checkout now mirrors the per-item checkout: `requestable`+ // is a plain checkbox and its posted value (or absence) is+ // authoritative. Omitting the field means unchecked, so every+ // selected asset ends up requestable=false regardless of its+ // prior state.$this->actingAs(User::factory()->checkoutAssets()->create())->post(route('hardware.bulkcheckout.store'), ['selected_assets' => $assets->pluck('id')->toArray(),'checkout_to_type' => 'user','assigned_user' => $targetUser->id,- 'set_not_requestable' => 1,]);$assets->each(function (Asset $asset) {@@ -124,7 +128,7 @@ class BulkAssetCheckoutTest extends TestCase});}- public function test_bulk_checkout_leaves_requestable_unchanged_when_not_selected()+ public function test_bulk_checkout_sets_requestable_to_true_when_checkbox_checked(){$requestableAsset = Asset::factory()->create(['requestable' => 1]);$nonRequestableAsset = Asset::factory()->create(['requestable' => 0]);@@ -135,11 +139,11 @@ class BulkAssetCheckoutTest extends TestCase'selected_assets' => [$requestableAsset->id, $nonRequestableAsset->id],'checkout_to_type' => 'user','assigned_user' => $targetUser->id,- // Intentionally omitted: set_not_requestable+ 'requestable' => '1',]);$this->assertTrue((bool) $requestableAsset->fresh()->requestable);- $this->assertFalse((bool) $nonRequestableAsset->fresh()->requestable);+ $this->assertTrue((bool) $nonRequestableAsset->fresh()->requestable);}public static function checkoutTargets()@@ -246,6 +250,37 @@ class BulkAssetCheckoutTest extends TestCase** @see \App\Http\Controllers\Assets\BulkAssetsController::store*/+ public function test_bulk_checkout_defaults_to_asset_index_when_no_redirect_option()+ {+ Mail::fake();+ $assets = Asset::factory()->count(2)->create();+ $user = User::factory()->create();++ $this->actingAs(User::factory()->checkoutAssets()->viewAssets()->create())+ ->post(route('hardware.bulkcheckout.store'), [+ 'selected_assets' => $assets->pluck('id')->toArray(),+ 'checkout_to_type' => 'user',+ 'assigned_user' => $user->id,+ ])+ ->assertRedirect(route('hardware.index'));+ }++ public function test_bulk_checkout_returns_to_bulk_checkout_when_option_selected()+ {+ Mail::fake();+ $assets = Asset::factory()->count(2)->create();+ $user = User::factory()->create();++ $this->actingAs(User::factory()->checkoutAssets()->viewAssets()->create())+ ->post(route('hardware.bulkcheckout.store'), [+ 'selected_assets' => $assets->pluck('id')->toArray(),+ 'checkout_to_type' => 'user',+ 'assigned_user' => $user->id,+ 'redirect_option' => 'bulk_checkout',+ ])+ ->assertRedirect(route('hardware.bulkcheckout.show'));+ }+#[DataProvider('bulkCheckoutTargetTypesProvider')]public function test_bulk_checkout_stores_target_type_as_string_in_session(string $type){