diff --git a/app/Http/Controllers/Assets/AssetsController.php b/app/Http/Controllers/Assets/AssetsController.php index 59b22b386d..5f944c386e 100755 --- a/app/Http/Controllers/Assets/AssetsController.php +++ b/app/Http/Controllers/Assets/AssetsController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Assets; +use App\Events\CheckoutableCheckedIn; use App\Helpers\Helper; use App\Http\Controllers\Controller; use App\Http\Requests\ImageUploadRequest; @@ -330,14 +331,21 @@ class AssetsController extends Controller $asset->expected_checkin = $request->input('expected_checkin', null); // If the box isn't checked, it's not in the request at all. - $asset->requestable = $request->filled('requestable'); + $asset->requestable = $request->filled('requestable', 0); $asset->rtd_location_id = $request->input('rtd_location_id', null); $asset->byod = $request->input('byod', 0); - $status = Statuslabel::find($asset->status_id); + $status = Statuslabel::find($request->input('status_id')); - if ($status && $status->archived) { + // This is a non-deployable status label - we should check the asset back in. + if (($status && $status->getStatuslabelType() != 'deployable') && ($target = $asset->assignedTo)) { + + $originalValues = $asset->getRawOriginal(); $asset->assigned_to = null; + $asset->assigned_type = null; + $asset->accepted = null; + + event(new CheckoutableCheckedIn($asset, $target, auth()->user(), 'Checkin on asset update', date('Y-m-d H:i:s'), $originalValues)); } if ($asset->assigned_to == '') { diff --git a/resources/lang/en-US/admin/hardware/form.php b/resources/lang/en-US/admin/hardware/form.php index edec543637..03b8f04add 100644 --- a/resources/lang/en-US/admin/hardware/form.php +++ b/resources/lang/en-US/admin/hardware/form.php @@ -55,6 +55,7 @@ return [ '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' => 'That asset status is not deployable. Using this status label will checkin the asset.', 'asset_deployable' => 'That status is deployable. This asset can be checked out.', 'processing_spinner' => 'Processing... (This might take a bit of time on large files)', 'optional_infos' => 'Optional Information', diff --git a/resources/views/hardware/edit.blade.php b/resources/views/hardware/edit.blade.php index bdbc3c3c2e..4c14b2ea02 100755 --- a/resources/views/hardware/edit.blade.php +++ b/resources/views/hardware/edit.blade.php @@ -263,7 +263,7 @@ $("#assignto_selector").hide(); $("#selected_status_status").removeClass('text-success'); $("#selected_status_status").addClass('text-danger'); - $("#selected_status_status").html(' {{ trans('admin/hardware/form.asset_not_deployable')}} '); + $("#selected_status_status").html(' {{ (($item->assigned_to!='') && ($item->assigned_type!='') && ($item->deleted_at == '')) ? trans('admin/hardware/form.asset_not_deployable_checkin') : trans('admin/hardware/form.asset_not_deployable') }} '); } } }); diff --git a/tests/Feature/Assets/Ui/EditAssetTest.php b/tests/Feature/Assets/Ui/EditAssetTest.php index befe64a864..27f00b5313 100644 --- a/tests/Feature/Assets/Ui/EditAssetTest.php +++ b/tests/Feature/Assets/Ui/EditAssetTest.php @@ -2,16 +2,20 @@ namespace Tests\Feature\Assets\Ui; +use App\Events\CheckoutableCheckedIn; use App\Models\Asset; use App\Models\AssetModel; +use App\Models\Location; use App\Models\StatusLabel; use App\Models\User; +use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\Event; use Tests\TestCase; class EditAssetTest extends TestCase { - public function testPermissionRequiredToViewLicense() + public function testPermissionRequiredToViewAsset() { $asset = Asset::factory()->create(); $this->actingAs(User::factory()->create()) @@ -64,4 +68,38 @@ class EditAssetTest extends TestCase $this->assertDatabaseHas('assets', ['asset_tag' => 'New Asset Tag']); } + public function testNewCheckinIsLoggedIfStatusChangedToUndeployable() + { + Event::fake([CheckoutableCheckedIn::class]); + + $user = User::factory()->create(); + $deployable_status = Statuslabel::factory()->rtd()->create(); + $achived_status = Statuslabel::factory()->archived()->create(); + $asset = Asset::factory()->assignedToUser($user)->create(['status_id' => $deployable_status->id]); + $this->assertTrue($asset->assignedTo->is($user)); + + $currentTimestamp = now(); + + $this->actingAs(User::factory()->viewAssets()->editAssets()->create()) + ->from(route('hardware.edit', $asset->id)) + ->put(route('hardware.update', $asset->id), [ + 'status_id' => $achived_status->id, + 'model_id' => $asset->model_id, + 'asset_tags' => $asset->asset_tag, + ], + ) + ->assertStatus(302); + //->assertRedirect(route('hardware.show', ['hardware' => $asset->id]));; + + // $asset->refresh(); + $asset = Asset::find($asset->id); + $this->assertNull($asset->assigned_to); + $this->assertNull($asset->assigned_type); + $this->assertEquals($achived_status->id, $asset->status_id); + + Event::assertDispatched(function (CheckoutableCheckedIn $event) use ($currentTimestamp) { + return Carbon::parse($event->action_date)->diffInSeconds($currentTimestamp) < 2; + }, 1); + } + }