A free open source IT asset/license management system
Quickscan checkin: Fixed #19899 - don’t error when asset wasn’t checked in but fields have changes
snipe1bc01b7f9e2a
diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.phpindex 3bd7b605d8..b12e485f54 100644--- a/app/Http/Controllers/Api/AssetsController.php+++ b/app/Http/Controllers/Api/AssetsController.php@@ -1486,15 +1486,35 @@ class AssetsController extends Controller}$target = $locked->assignedTo;- if (is_null($target)) {+ $alreadyCheckedIn = is_null($target);++ // The Quickscan Checkin form carries status, location, name,+ // notes, and clear_name alongside the scan input. When the+ // operator scans an asset that was never checked out, they+ // still expect those form fields to apply (issue #19899:+ // scan a box of returned assets, set them all to Retired in+ // one pass). Only short-circuit on "already checked in" when+ // the form carried nothing to update, otherwise fall through+ // to the field-apply branch so the status / location /+ // name / notes still land.+ $hasFieldUpdates = $request->filled('status_id')+ || $request->filled('location_id')+ || $request->has('name')+ || $request->input('clear_name') == '1';++ if ($alreadyCheckedIn && ! $hasFieldUpdates) {return ['status' => 'already_checked_in', 'asset' => $locked];}- $locked->expected_checkin = null;- // $locked->last_checkout = null;- $locked->last_checkin = now();- $locked->assignedTo()->disassociate($locked);- $locked->accepted = null;+ // Checkout-clearing housekeeping only when there is actually+ // a checkout to clear. The status / location / name / notes+ // writes below run in both branches.+ if (! $alreadyCheckedIn) {+ $locked->expected_checkin = null;+ $locked->last_checkin = now();+ $locked->assignedTo()->disassociate($locked);+ $locked->accepted = null;+ }if ($request->input('clear_name') == '1') {$locked->name = null;@@ -1504,7 +1524,14 @@ class AssetsController extends Controller$this->migrateLegacyLocations($locked);- $locked->location_id = $locked->rtd_location_id;+ // Reset to the default location only on a real checkin.+ // Overwriting the location on an already-checked-in asset+ // with its rtd_location_id would be surprising when the+ // operator only asked for a status change. If they supplied+ // a location_id on the form, that still applies below.+ if (! $alreadyCheckedIn) {+ $locked->location_id = $locked->rtd_location_id;+ }if ($request->filled('location_id')) {$locked->location_id = $request->input('location_id');@@ -1525,36 +1552,46 @@ class AssetsController extends Controller$originalValues['action_date'] = $checkin_at;}- $locked->licenseseats->each(function (LicenseSeat $seat) {- $seat->update(['assigned_to' => null]);- });-- // Get all pending Acceptances for this asset and delete them- CheckoutAcceptance::pending()- ->whereHasMorph(- 'checkoutable',- [Asset::class],- function (Builder $query) use ($locked) {- $query->where('id', $locked->id);- }- )- ->get()- ->map(function ($acceptance) {- $acceptance->delete();+ // License-seat release and pending-Acceptance cleanup only+ // on a real checkin. An already-checked-in asset has no+ // seat to free and nothing is pending acceptance for it.+ if (! $alreadyCheckedIn) {+ $locked->licenseseats->each(function (LicenseSeat $seat) {+ $seat->update(['assigned_to' => null]);});+ CheckoutAcceptance::pending()+ ->whereHasMorph(+ 'checkoutable',+ [Asset::class],+ function (Builder $query) use ($locked) {+ $query->where('id', $locked->id);+ }+ )+ ->get()+ ->map(function ($acceptance) {+ $acceptance->delete();+ });+ }+if (! $locked->save()) {return ['status' => 'save_failed', 'asset' => $locked];}- // Update the location of any child assets- Asset::where('assigned_type', Asset::class)- ->where('assigned_to', $locked->id)- ->update(['location_id' => $locked->location_id]);+ if (! $alreadyCheckedIn) {+ // Child-asset location propagation only fires on a real+ // checkin so a status-only update on an already-checked-in+ // parent does not drag children to its rtd_location.+ Asset::where('assigned_type', Asset::class)+ ->where('assigned_to', $locked->id)+ ->update(['location_id' => $locked->location_id]);- event(new CheckoutableCheckedIn($locked, $target, auth()->user(), $request->input('note'), $checkin_at, $originalValues));+ event(new CheckoutableCheckedIn($locked, $target, auth()->user(), $request->input('note'), $checkin_at, $originalValues));- return ['status' => 'success', 'asset' => $locked];+ return ['status' => 'success', 'asset' => $locked];+ }++ return ['status' => 'updated_without_checkin', 'asset' => $locked];});$asset = $outcome['asset'];@@ -1567,6 +1604,14 @@ class AssetsController extends Controller], trans('admin/hardware/message.checkin.already_checked_in')));}+ if ($outcome['status'] === 'updated_without_checkin') {+ return response()->json(Helper::formatStandardApiResponse('success', [+ 'asset_tag' => e($asset->asset_tag),+ 'model' => e($asset->model->name),+ 'model_number' => e($asset->model->model_number),+ ], trans('admin/hardware/message.checkin.updated_without_checkin')));+ }+if ($outcome['status'] === 'success') {return response()->json(Helper::formatStandardApiResponse('success', ['asset_tag' => e($asset->asset_tag),diff --git a/resources/lang/en-US/admin/hardware/message.php b/resources/lang/en-US/admin/hardware/message.phpindex 7518b484cb..03cc88cc1a 100644--- a/resources/lang/en-US/admin/hardware/message.php+++ b/resources/lang/en-US/admin/hardware/message.php@@ -163,6 +163,7 @@ return ['checkin' => ['error' => 'Asset was not checked in, please try again','success' => 'Asset checked in successfully.',+ 'updated_without_checkin' => 'Asset was already checked in, but the form fields you set (status, location, name, notes) were applied anyway.','user_does_not_exist' => 'That user is invalid. Please try again.','already_checked_in' => 'That asset is already checked in.','force_checkin_orphaned_success' => 'Invalid assignment cleared successfully.',diff --git a/tests/Feature/Checkins/Api/AssetCheckinTest.php b/tests/Feature/Checkins/Api/AssetCheckinTest.phpindex cd061f5a61..cba17d222e 100644--- a/tests/Feature/Checkins/Api/AssetCheckinTest.php+++ b/tests/Feature/Checkins/Api/AssetCheckinTest.php@@ -215,4 +215,102 @@ class AssetCheckinTest extends TestCase$this->assertEquals('My Asset Name', $asset->refresh()->name);}++ public function test_status_update_on_not_checked_out_asset_still_applies()+ {+ // Issue #19899: operator in Quickscan Checkin scans a box of+ // returned assets to retire them. Scanning an asset that was+ // never checked out used to short-circuit, dropping the status+ // change the form carried. Now the status / location / name /+ // notes still apply when the asset was already checked in.+ Event::fake([CheckoutableCheckedIn::class]);++ $retired = Statuslabel::factory()->create();+ $asset = Asset::factory()->create(['status_id' => Statuslabel::factory()->readyToDeploy()->create()->id]);++ $this->actingAsForApi(User::factory()->checkinAssets()->create())+ ->postJson(route('api.asset.checkin', $asset), [+ 'status_id' => $retired->id,+ ])+ ->assertOk()+ ->assertStatusMessageIs('success');++ $this->assertSame($retired->id, $asset->refresh()->status_id);+ // No real checkin happened, so CheckoutableCheckedIn must not+ // fire. That event drives expiration-alert cascades, notification+ // mail, and action-log 'checkin from' writes, none of which make+ // sense when there was never a checkout.+ Event::assertNotDispatched(CheckoutableCheckedIn::class);+ }++ public function test_location_update_on_not_checked_out_asset_still_applies()+ {+ $newLocation = Location::factory()->create();+ $originalLocation = Location::factory()->create();+ $asset = Asset::factory()->create([+ 'location_id' => $originalLocation->id,+ 'rtd_location_id' => $originalLocation->id,+ ]);++ $this->actingAsForApi(User::factory()->checkinAssets()->create())+ ->postJson(route('api.asset.checkin', $asset), [+ 'location_id' => $newLocation->id,+ ])+ ->assertOk()+ ->assertStatusMessageIs('success');++ $this->assertSame($newLocation->id, $asset->refresh()->location_id);+ }++ public function test_name_update_on_not_checked_out_asset_still_applies()+ {+ $asset = Asset::factory()->create(['name' => 'Old Name']);++ $this->actingAsForApi(User::factory()->checkinAssets()->create())+ ->postJson(route('api.asset.checkin', $asset), [+ 'name' => 'New Name',+ ])+ ->assertOk()+ ->assertStatusMessageIs('success');++ $this->assertSame('New Name', $asset->refresh()->name);+ }++ public function test_empty_body_on_not_checked_out_asset_still_errors()+ {+ // Preserves the pre-19899 behavior for the no-op case: if the+ // operator scanned an already-checked-in asset with no form+ // fields set, we still want to surface the "already checked in"+ // error rather than silently save-with-no-changes.+ $asset = Asset::factory()->create(['name' => 'Untouched']);++ $this->actingAsForApi(User::factory()->checkinAssets()->create())+ ->postJson(route('api.asset.checkin', $asset))+ ->assertStatusMessageIs('error');++ $this->assertSame('Untouched', $asset->refresh()->name);+ }++ public function test_location_is_not_reset_to_rtd_when_only_status_changes_on_not_checked_out_asset()+ {+ // Operator changing a not-checked-out asset's status should not+ // trigger the "reset location to RTD" behavior that only makes+ // sense for a real checkin. Current location stays put unless+ // the operator supplies location_id explicitly.+ $rtd = Location::factory()->create();+ $current = Location::factory()->create();+ $retired = Statuslabel::factory()->create();+ $asset = Asset::factory()->create([+ 'rtd_location_id' => $rtd->id,+ 'location_id' => $current->id,+ ]);++ $this->actingAsForApi(User::factory()->checkinAssets()->create())+ ->postJson(route('api.asset.checkin', $asset), [+ 'status_id' => $retired->id,+ ])+ ->assertOk();++ $this->assertSame($current->id, $asset->refresh()->location_id);+ }}