Skip to content

A free open source IT asset/license management system

Fixed #18905 - update location of child assets if parent asset is checked in

snipec22e4c00a566
diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php
index 234faba4b0..b3566573bd 100644
--- a/app/Http/Controllers/Api/AssetsController.php
+++ b/app/Http/Controllers/Api/AssetsController.php
@@ -1067,6 +1067,12 @@ class AssetsController extends Controller
});
if ($asset->save()) {
+
+ // Update the location of any child assets
+ Asset::where('assigned_type', Asset::class)
+ ->where('assigned_to', $asset->id)
+ ->update(['location_id' => $asset->location_id]);
+
event(new CheckoutableCheckedIn($asset, $target, auth()->user(), $request->input('note'), $checkin_at, $originalValues));
return response()->json(Helper::formatStandardApiResponse('success', [
diff --git a/app/Http/Controllers/Assets/AssetCheckinController.php b/app/Http/Controllers/Assets/AssetCheckinController.php
index 8ab4cc74e1..dd3f36e035 100644
--- a/app/Http/Controllers/Assets/AssetCheckinController.php
+++ b/app/Http/Controllers/Assets/AssetCheckinController.php
@@ -175,6 +175,10 @@ class AssetCheckinController extends Controller
$asset->customFieldsForCheckinCheckout('display_checkin');
if ($asset->save()) {
+ // Update the location of any child assets
+ Asset::where('assigned_type', Asset::class)
+ ->where('assigned_to', $asset->id)
+ ->update(['location_id' => $asset->location_id]);
event(new CheckoutableCheckedIn($asset, $target, auth()->user(), $request->input('note'), $checkin_at, $originalValues));
diff --git a/tests/Feature/Checkins/Api/AssetCheckinTest.php b/tests/Feature/Checkins/Api/AssetCheckinTest.php
index afb9c6eb11..5b6c87af47 100644
--- a/tests/Feature/Checkins/Api/AssetCheckinTest.php
+++ b/tests/Feature/Checkins/Api/AssetCheckinTest.php
@@ -120,6 +120,35 @@ class AssetCheckinTest extends TestCase
$this->assertNull($asset->refresh()->licenseseats->first()->assigned_to);
}
+ public function test_checking_in_asset_updates_location_of_assets_assigned_to_it()
+ {
+ $originalLocation = Location::factory()->create();
+ $checkedOutLocation = Location::factory()->create();
+
+ $parentAsset = Asset::factory()->assignedToLocation($checkedOutLocation)->create([
+ 'location_id' => $checkedOutLocation->id,
+ 'rtd_location_id' => $originalLocation->id,
+ ]);
+
+ $childAsset = Asset::factory()->create([
+ 'assigned_to' => $parentAsset->id,
+ 'assigned_type' => Asset::class,
+ 'location_id' => $checkedOutLocation->id,
+ 'rtd_location_id' => $originalLocation->id,
+ ]);
+
+ $this->actingAsForApi(User::factory()->checkinAssets()->create())
+ ->postJson(route('api.asset.checkin', $parentAsset), [
+ 'location_id' => $originalLocation->id,
+ ])
+ ->assertOk();
+
+ $this->assertEquals($originalLocation->id, $parentAsset->fresh()->location_id);
+ $this->assertEquals($originalLocation->id, $childAsset->fresh()->location_id);
+ $this->assertEquals($parentAsset->id, $childAsset->fresh()->assigned_to);
+ $this->assertEquals(Asset::class, $childAsset->fresh()->assigned_type);
+ }
+
public function test_legacy_location_values_set_to_zero_are_updated()
{
$asset = Asset::factory()->canBeInvalidUponCreation()->assignedToUser()->create([
diff --git a/tests/Feature/Checkins/Ui/AssetCheckinTest.php b/tests/Feature/Checkins/Ui/AssetCheckinTest.php
index 2ec6958d6a..1179aa721d 100644
--- a/tests/Feature/Checkins/Ui/AssetCheckinTest.php
+++ b/tests/Feature/Checkins/Ui/AssetCheckinTest.php
@@ -206,6 +206,34 @@ class AssetCheckinTest extends TestCase
$this->assertNull($asset->refresh()->licenseseats->first()->assigned_to);
}
+ public function test_checking_in_asset_updates_location_of_assets_assigned_to_it()
+ {
+ $originalLocation = Location::factory()->create();
+ $checkedOutLocation = Location::factory()->create();
+
+ $parentAsset = Asset::factory()->assignedToLocation($checkedOutLocation)->create([
+ 'location_id' => $checkedOutLocation->id,
+ 'rtd_location_id' => $originalLocation->id,
+ ]);
+
+ $childAsset = Asset::factory()->create([
+ 'assigned_to' => $parentAsset->id,
+ 'assigned_type' => Asset::class,
+ 'location_id' => $checkedOutLocation->id,
+ 'rtd_location_id' => $originalLocation->id,
+ ]);
+
+ $this->actingAs(User::factory()->checkinAssets()->create())
+ ->post(route('hardware.checkin.store', [$parentAsset]), [
+ 'location_id' => $originalLocation->id,
+ ]);
+
+ $this->assertEquals($originalLocation->id, $parentAsset->fresh()->location_id);
+ $this->assertEquals($originalLocation->id, $childAsset->fresh()->location_id);
+ $this->assertEquals($parentAsset->id, $childAsset->fresh()->assigned_to);
+ $this->assertEquals(Asset::class, $childAsset->fresh()->assigned_type);
+ }
+
public function test_legacy_location_values_set_to_zero_are_updated()
{
$asset = Asset::factory()->canBeInvalidUponCreation()->assignedToUser()->create([