A free open source IT asset/license management system
Fixed #11779 - adds nullability to default location and location in bulk edit
snipec14274770ace
diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.phpindex 468ef65bf6..ac7d57a064 100644--- a/app/Http/Controllers/Assets/BulkAssetsController.php+++ b/app/Http/Controllers/Assets/BulkAssetsController.php@@ -305,6 +305,9 @@ class BulkAssetsController extends Controller|| ($request->filled('supplier_id'))|| ($request->filled('warranty_months'))|| ($request->filled('rtd_location_id'))+ || ($request->filled('location_id'))+ || ($request->filled('null_rtd_location_id'))+ || ($request->filled('null_location_id'))|| ($request->filled('requestable'))|| ($request->filled('company_id'))|| ($request->filled('status_id'))@@ -458,31 +461,22 @@ class BulkAssetsController extends Controller}- /**- * We're changing the location ID - figure out which location we should apply- * this change to:- *- * 0 - RTD location only- * 1 - location ID and RTD location ID- * 2 - location ID only- *- * Note: this is kinda dumb and we should just use human-readable values IMHO. - snipe- */+ // Default location + current location are two+ // independent writes now. Each can be set to a new+ // location or cleared via the null_* checkbox. Clear+ // wins if both the picker and the checkbox are sent+ // for the same field.if ($request->filled('rtd_location_id')) {-- if (($request->filled('update_real_loc')) && (($request->input('update_real_loc')) == '0')) {- $this->update_array['rtd_location_id'] = $request->input('rtd_location_id');- }-- if (($request->filled('update_real_loc')) && (($request->input('update_real_loc')) == '1')) {- $this->update_array['location_id'] = $request->input('rtd_location_id');- $this->update_array['rtd_location_id'] = $request->input('rtd_location_id');- }-- if (($request->filled('update_real_loc')) && (($request->input('update_real_loc')) == '2')) {- $this->update_array['location_id'] = $request->input('rtd_location_id');- }-+ $this->update_array['rtd_location_id'] = $request->input('rtd_location_id');+ }+ if ($request->input('null_rtd_location_id') == '1') {+ $this->update_array['rtd_location_id'] = null;+ }+ if ($request->filled('location_id')) {+ $this->update_array['location_id'] = $request->input('location_id');+ }+ if ($request->input('null_location_id') == '1') {+ $this->update_array['location_id'] = null;}/**diff --git a/resources/lang/en-US/admin/hardware/form.php b/resources/lang/en-US/admin/hardware/form.phpindex d4a01b2633..1bfc6592b2 100644--- a/resources/lang/en-US/admin/hardware/form.php+++ b/resources/lang/en-US/admin/hardware/form.php@@ -56,8 +56,6 @@ return ['years' => 'years','asset_location' => 'Update Asset Location','asset_location_update_default_current' => 'Update default location AND actual location',- 'asset_location_update_default' => 'Update only default location',- 'asset_location_update_actual' => 'Update only actual location','asset_not_deployable' => 'That asset status is not deployable. This asset cannot be checked out.','asset_not_deployable_checkin' => '{1} That asset status is not deployable. Using this status label will check in the asset.|[2,*] That asset status in not deployable. Using this status label will result in no change.','asset_deployable' => '{1} This asset can be checked out.|[2,*] These assets can be checked out.',diff --git a/resources/views/hardware/bulk.blade.php b/resources/views/hardware/bulk.blade.phpindex 47b53835dd..eebdccb118 100755--- a/resources/views/hardware/bulk.blade.php+++ b/resources/views/hardware/bulk.blade.php@@ -1,3 +1,5 @@+@use('App\Models\Location', 'Location')+@extends('layouts/default'){{-- Page title --}}@@ -149,23 +151,68 @@:selected="old('model_id')"/>- <x-input.location-select- :label="trans('admin/hardware/form.default_location')"- name="rtd_location_id"- :selected="old('rtd_location_id')"- />+ <div class="form-group {{ $errors->has('rtd_location_id') ? ' has-error' : '' }}">+ <label for="rtd_location_id" class="col-md-3 control-label">+ {{ trans('admin/hardware/form.default_location') }}+ </label>+ <div class="col-md-4">+ <select+ class="js-data-ajax"+ data-endpoint="locations"+ data-placeholder="{{ trans('general.select_location') }}"+ name="rtd_location_id"+ id="rtd_location_id"+ style="width:100%"+ aria-label="rtd_location_id"+ >+ <option value=""></option>+ @if (old('rtd_location_id'))+ <option value="{{ old('rtd_location_id') }}" selected="selected">+ {{ Location::find(old('rtd_location_id'))?->name }}+ </option>+ @endif+ </select>+ <x-form.error name="rtd_location_id"/>+ </div>+ <div class="col-md-5">+ <x-form.checkbox-inline+ name="null_rtd_location_id"+ :label="trans_choice('general.set_to_null', count($assets), ['selection_count' => count($assets)])"+ />+ </div>+ </div>- {{-- Actual-location update rule (3-way radio) --}}- <x-form.radio-row- name="update_real_loc"- :options="[- '1' => trans('admin/hardware/form.asset_location_update_default_current'),- '0' => trans('admin/hardware/form.asset_location_update_default'),- '2' => trans('admin/hardware/form.asset_location_update_actual'),- ]"- selected="1"- input_div_class="col-md-9 col-md-offset-3"- />+ {{-- Current location + nullable checkbox. --}}+ <div class="form-group {{ $errors->has('location_id') ? ' has-error' : '' }}">+ <label for="location_id" class="col-md-3 control-label">+ {{ trans('general.location') }}+ </label>+ <div class="col-md-4">+ <select+ class="js-data-ajax"+ data-endpoint="locations"+ data-placeholder="{{ trans('general.select_location') }}"+ name="location_id"+ id="location_id"+ style="width:100%"+ aria-label="location_id"+ >+ <option value=""></option>+ @if (old('location_id'))+ <option value="{{ old('location_id') }}" selected="selected">+ {{ Location::find(old('location_id'))?->name }}+ </option>+ @endif+ </select>+ <x-form.error name="location_id"/>+ </div>+ <div class="col-md-5">+ <x-form.checkbox-inline+ name="null_location_id"+ :label="trans_choice('general.set_to_null', count($assets), ['selection_count' => count($assets)])"+ />+ </div>+ </div>{{-- Purchase cost + Order number bulk edits. Assetshave no per-row currency column, so if thediff --git a/tests/Feature/Assets/Ui/BulkEditAssetsTest.php b/tests/Feature/Assets/Ui/BulkEditAssetsTest.phpindex fedf0815ac..7fa7672916 100644--- a/tests/Feature/Assets/Ui/BulkEditAssetsTest.php+++ b/tests/Feature/Assets/Ui/BulkEditAssetsTest.php@@ -6,6 +6,7 @@ use App\Models\Asset;use App\Models\AssetModel;use App\Models\Company;use App\Models\CustomField;+use App\Models\Location;use App\Models\Statuslabel;use App\Models\Supplier;use App\Models\User;@@ -189,6 +190,79 @@ class BulkEditAssetsTest extends TestCase});}+ public function test_bulk_edit_writes_default_and_current_location_independently()+ {+ $originalDefault = Location::factory()->create();+ $originalCurrent = Location::factory()->create();+ $newDefault = Location::factory()->create();+ $newCurrent = Location::factory()->create();++ $assets = Asset::factory()->count(3)->create([+ 'rtd_location_id' => $originalDefault->id,+ 'location_id' => $originalCurrent->id,+ ]);++ $this->actingAs(User::factory()->editAssets()->create())->post(route('hardware/bulksave'), [+ 'ids' => $assets->pluck('id')->toArray(),+ 'rtd_location_id' => $newDefault->id,+ 'location_id' => $newCurrent->id,+ ])+ ->assertStatus(302)+ ->assertSessionHasNoErrors();++ Asset::findMany($assets->pluck('id'))->each(function (Asset $asset) use ($newDefault, $newCurrent) {+ $this->assertEquals($newDefault->id, $asset->rtd_location_id);+ $this->assertEquals($newCurrent->id, $asset->location_id);+ });+ }++ public function test_bulk_edit_clears_default_and_current_location_when_null_checkboxes_set()+ {+ // Regression test for #11779. Pre-fix the bulk edit form had+ // no way to null out rtd_location_id or location_id, only the+ // single-asset edit path cleared them.+ $default = Location::factory()->create();+ $current = Location::factory()->create();+ $otherDefault = Location::factory()->create();++ $assets = Asset::factory()->count(3)->create([+ 'rtd_location_id' => $default->id,+ 'location_id' => $current->id,+ ]);++ $this->actingAs(User::factory()->editAssets()->create())->post(route('hardware/bulksave'), [+ 'ids' => $assets->pluck('id')->toArray(),+ 'null_rtd_location_id' => '1',+ 'null_location_id' => '1',+ ])+ ->assertStatus(302)+ ->assertSessionHasNoErrors();++ Asset::findMany($assets->pluck('id'))->each(function (Asset $asset) {+ $this->assertNull($asset->rtd_location_id);+ $this->assertNull($asset->location_id);+ });++ // Mixed path: set default, clear current, independently.+ $moreAssets = Asset::factory()->count(2)->create([+ 'rtd_location_id' => $default->id,+ 'location_id' => $current->id,+ ]);++ $this->actingAs(User::factory()->editAssets()->create())->post(route('hardware/bulksave'), [+ 'ids' => $moreAssets->pluck('id')->toArray(),+ 'rtd_location_id' => $otherDefault->id,+ 'null_location_id' => '1',+ ])+ ->assertStatus(302)+ ->assertSessionHasNoErrors();++ Asset::findMany($moreAssets->pluck('id'))->each(function (Asset $asset) use ($otherDefault) {+ $this->assertEquals($otherDefault->id, $asset->rtd_location_id);+ $this->assertNull($asset->location_id);+ });+ }+public function test_bulk_edit_assets_accepts_and_updates_unencrypted_custom_fields(){$this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL');