A free open source IT asset/license management system
Tweaks to requestable status on checkin/checkout
snipea390b37400c0
diff --git a/app/Http/Controllers/Assets/AssetCheckinController.php b/app/Http/Controllers/Assets/AssetCheckinController.phpindex 6f5ba57cad..1929a99f62 100644--- a/app/Http/Controllers/Assets/AssetCheckinController.php+++ b/app/Http/Controllers/Assets/AssetCheckinController.php@@ -125,8 +125,12 @@ class AssetCheckinController extends Controller->where('deployable', 1)->exists();- if ($request->boolean('set_requestable') && $isDeployableStatus) {- $asset->requestable = true;+ // Requestable is only meaningful when the selected status is deployable+ // (the view hides the wrapper otherwise). Under a deployable status,+ // apply the checkbox value: checked = true, unchecked (or absent) =+ // false, per Laravel's boolean() default.+ if ($isDeployableStatus) {+ $asset->requestable = $request->boolean('requestable');}// Add any custom fields that should be included in the checkoutdiff --git a/app/Http/Controllers/Assets/AssetCheckoutController.php b/app/Http/Controllers/Assets/AssetCheckoutController.phpindex da8a1cd14f..73376c66a3 100644--- a/app/Http/Controllers/Assets/AssetCheckoutController.php+++ b/app/Http/Controllers/Assets/AssetCheckoutController.php@@ -103,9 +103,11 @@ class AssetCheckoutController extends Controller$asset->status_id = $request->input('status_id');}- if ($request->boolean('set_not_requestable')) {- $asset->requestable = false;- }+ // Two-way toggle: checked = requestable, unchecked (or absent) =+ // not. The form pre-populates the checkbox with the asset's current+ // state so users can flip either direction (e.g. mark "no longer+ // requestable" during checkout because the item is now assigned).+ $asset->requestable = $request->boolean('requestable');if (! empty($asset->licenseseats->all())) {if (request('checkout_to_type') == 'user') {diff --git a/app/Http/Requests/AssetCheckinRequest.php b/app/Http/Requests/AssetCheckinRequest.phpindex dc5179f49c..3763ee5864 100644--- a/app/Http/Requests/AssetCheckinRequest.php+++ b/app/Http/Requests/AssetCheckinRequest.php@@ -26,7 +26,7 @@ class AssetCheckinRequest extends Request$settings = Setting::getSettings();$rules = [- 'set_requestable' => 'nullable|boolean',+ 'requestable' => 'nullable|boolean',];if ($settings->require_checkinout_notes) {diff --git a/app/Http/Requests/AssetCheckoutRequest.php b/app/Http/Requests/AssetCheckoutRequest.phpindex 35e972d1c6..f1878077a6 100644--- a/app/Http/Requests/AssetCheckoutRequest.php+++ b/app/Http/Requests/AssetCheckoutRequest.php@@ -40,7 +40,6 @@ class AssetCheckoutRequest extends Request'date',],'requestable' => 'nullable|boolean',- 'set_not_requestable' => 'nullable|boolean',];if ($settings->require_checkinout_notes) {diff --git a/resources/views/hardware/checkin.blade.php b/resources/views/hardware/checkin.blade.phpindex 45e8aab4ea..02dcc6d4e8 100755--- a/resources/views/hardware/checkin.blade.php+++ b/resources/views/hardware/checkin.blade.php@@ -119,17 +119,17 @@<divclass="form-group"- id="set-requestable-wrapper"+ id="requestable-wrapper"@if (! $show_requestable_toggle) style="display: none;" @endif><div class="col-md-9 col-md-offset-3">- <label class="form-control" for="set_requestable">+ <label class="form-control" for="requestable"><inputtype="checkbox"value="1"- name="set_requestable"- id="set_requestable"- @checked((bool) old('set_requestable', false))+ name="requestable"+ id="requestable"+ @checked((bool) old('requestable', $asset->requestable))/>{{ trans('admin/hardware/general.requestable') }}</label>@@ -224,13 +224,18 @@const deployableStatusIds = @json($deployable_status_ids);const statusSelect = document.getElementById('modal-statuslabel_types')?? document.querySelector('select[name="status_id"]');- const requestableWrapper = document.getElementById('set-requestable-wrapper');- const requestableCheckbox = document.getElementById('set_requestable');+ const requestableWrapper = document.getElementById('requestable-wrapper');if (!statusSelect || !requestableWrapper) {return;}+ // Preserve the checkbox state when hiding: the server only applies the+ // checkbox value when the selected status is deployable, so a hidden+ // wrapper never lets us accidentally clear the asset's requestable+ // flag. Users who bounce the status between deployable and non-+ // deployable keep their original tick when they land back on a+ // deployable status.const toggleRequestable = function () {const selectedStatusValue = statusSelect.value;const selectedStatusId = Number.parseInt(selectedStatusValue, 10);@@ -239,10 +244,6 @@&& deployableStatusIds.includes(selectedStatusId);requestableWrapper.style.display = isDeployable ? '' : 'none';-- if (!isDeployable && requestableCheckbox) {- requestableCheckbox.checked = false;- }};statusSelect.addEventListener('change', toggleRequestable);@@ -254,10 +255,49 @@toggleRequestable();};+ // Per-user localStorage preference for the requestable default on+ // checkin. Namespaced by user id so a shared browser session 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 initializeRequestablePreference = function () {+ const storageKey = 'snipeit.checkin.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', initializeRequestableToggle);+ document.addEventListener('DOMContentLoaded', initializeRequestablePreference);} else {initializeRequestableToggle();+ initializeRequestablePreference();}</script>@stopdiff --git a/resources/views/hardware/checkout.blade.php b/resources/views/hardware/checkout.blade.phpindex 5d96a7b312..e56f80ca5f 100755--- a/resources/views/hardware/checkout.blade.php+++ b/resources/views/hardware/checkout.blade.php@@ -101,22 +101,20 @@</div></div>- @if ($asset->requestable)- <div class="form-group">- <div class="col-md-7 col-md-offset-3">- <label class="form-control" for="set_not_requestable">- <input- type="checkbox"- value="1"- name="set_not_requestable"- id="set_not_requestable"- @checked((bool) old('set_not_requestable', true))- >- {{ trans('admin/hardware/general.not_requestable') }}- </label>- </div>+ <div class="form-group">+ <div class="col-md-7 col-md-offset-3">+ <label class="form-control" for="requestable">+ <input+ type="checkbox"+ value="1"+ name="requestable"+ id="requestable"+ @checked((bool) old('requestable', $asset->requestable))+ />+ {{ trans('admin/hardware/general.requestable') }}+ </label></div>- @endif+ </div>@include ('partials.forms.checkout-selector', ['user_select' => 'true','asset_select' => 'true', 'location_select' => 'true'])@include ('partials.forms.edit.user-select', ['translated_name' => trans('general.user'), 'fieldname' => 'assigned_user', 'company_id' => $asset->company_id, 'style' => (session('checkout_to_type') ?: 'user') == 'user' ? '' : 'display: none;'])@@ -271,4 +269,49 @@@section('moar_scripts')@include('partials/assets-assigned')++ <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/Checkins/Ui/AssetCheckinTest.php b/tests/Feature/Checkins/Ui/AssetCheckinTest.phpindex debf869902..4263b2d28f 100644--- a/tests/Feature/Checkins/Ui/AssetCheckinTest.php+++ b/tests/Feature/Checkins/Ui/AssetCheckinTest.php@@ -62,9 +62,9 @@ class AssetCheckinTest extends TestCase$content = $response->getContent();- $this->assertStringContainsString('id="set-requestable-wrapper"', $content);+ $this->assertStringContainsString('id="requestable-wrapper"', $content);$this->assertMatchesRegularExpression(- '/id="set-requestable-wrapper"(?:(?!>).)*style="display:\s*none;"/s',+ '/id="requestable-wrapper"(?:(?!>).)*style="display:\s*none;"/s',$content);}@@ -83,7 +83,7 @@ class AssetCheckinTest extends TestCase$content = $response->getContent();$this->assertMatchesRegularExpression(- '/id="set-requestable-wrapper"(?:(?!>).)*style="display:\s*none;"/s',+ '/id="requestable-wrapper"(?:(?!>).)*style="display:\s*none;"/s',$content);}@@ -103,7 +103,7 @@ class AssetCheckinTest extends TestCase->assertOk();$this->assertDoesNotMatchRegularExpression(- '/id="set-requestable-wrapper"(?:(?!>).)*style="display:\s*none;"/s',+ '/id="requestable-wrapper"(?:(?!>).)*style="display:\s*none;"/s',$responseWithDeployableOldInput->getContent());@@ -117,7 +117,7 @@ class AssetCheckinTest extends TestCase->assertOk();$this->assertMatchesRegularExpression(- '/id="set-requestable-wrapper"(?:(?!>).)*style="display:\s*none;"/s',+ '/id="requestable-wrapper"(?:(?!>).)*style="display:\s*none;"/s',$responseWithNonDeployableOldInput->getContent());}@@ -327,7 +327,7 @@ class AssetCheckinTest extends TestCase$this->actingAs(User::factory()->checkinAssets()->create())->post(route('hardware.checkin.store', [$asset]), ['status_id' => $deployableStatus->id,- 'set_requestable' => 1,+ 'requestable' => 1,]);$this->assertTrue((bool) $asset->fresh()->requestable);@@ -358,7 +358,7 @@ class AssetCheckinTest extends TestCase$this->actingAs(User::factory()->checkinAssets()->create())->post(route('hardware.checkin.store', [$asset]), ['status_id' => $undeployableStatus->id,- 'set_requestable' => 1,+ 'requestable' => 1,]);$this->assertFalse((bool) $asset->fresh()->requestable);diff --git a/tests/Feature/Checkouts/Ui/AssetCheckoutTest.php b/tests/Feature/Checkouts/Ui/AssetCheckoutTest.phpindex 4e22ce0cd5..94153f056c 100644--- a/tests/Feature/Checkouts/Ui/AssetCheckoutTest.php+++ b/tests/Feature/Checkouts/Ui/AssetCheckoutTest.php@@ -336,7 +336,8 @@ class AssetCheckoutTest extends TestCase->post(route('hardware.checkout.store', $asset), ['checkout_to_type' => 'user','assigned_user' => $targetUser->id,- 'set_not_requestable' => 1,+ // Unchecked checkbox on the form → key absent → boolean()+ // returns false → asset flipped to not-requestable.]);$this->assertFalse((bool) $asset->fresh()->requestable);