Skip to content

A free open source IT asset/license management system

Added CheckinAndDeleteItems command

snipe14c5cff42938
diff --git a/app/Console/Commands/CheckinAndDeleteItems.php b/app/Console/Commands/CheckinAndDeleteItems.php
new file mode 100644
index 0000000000..4b39f8919e
--- /dev/null
+++ b/app/Console/Commands/CheckinAndDeleteItems.php
@@ -0,0 +1,308 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Events\CheckoutableCheckedIn;
+use App\Models\Accessory;
+use App\Models\AccessoryCheckout;
+use App\Models\Asset;
+use App\Models\CheckoutAcceptance;
+use App\Models\Component;
+use App\Models\License;
+use App\Models\LicenseSeat;
+use App\Models\User;
+use Illuminate\Console\Command;
+use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Support\Facades\DB;
+
+class CheckinAndDeleteItems extends Command
+{
+ protected $signature = 'snipeit:checkin-delete-all
+ {--company-id= : Only process items belonging to this company ID}
+ {--admin-id= : ID of the user credited for the checkins (defaults to first superadmin)}
+ {--no-notifications : Suppress email and webhook notifications}
+ {--type=all : Comma-separated types to process: assets, licenses, accessories, components, or all}
+ {--note= : Note recorded on each checkin action log entry}
+ {--dry-run : Preview what would be processed without making any changes}
+ {--force : Skip the confirmation prompt}';
+
+ protected $description = 'Check in all assigned items and soft-delete them, optionally scoped to a company';
+
+ public function handle(): int
+ {
+ $companyId = $this->option('company-id');
+ $noNotifications = $this->option('no-notifications');
+ $dryRun = $this->option('dry-run');
+ $typeOption = $this->option('type') ?? 'all';
+ $note = $this->option('note') ?: 'Checked in and deleted via CLI';
+
+ $allTypes = ['assets', 'licenses', 'accessories', 'components'];
+ $typesToProcess = $typeOption === 'all'
+ ? $allTypes
+ : array_intersect(array_map('trim', explode(',', $typeOption)), $allTypes);
+
+ if (empty($typesToProcess)) {
+ $this->error('Invalid --type value. Use: assets, licenses, accessories, components, or all.');
+
+ return 1;
+ }
+
+ $admin = null;
+ if (! $dryRun && ! $noNotifications) {
+ if ($this->option('admin-id')) {
+ $admin = User::find($this->option('admin-id'));
+ if (! $admin) {
+ $this->error('No user found with admin-id '.$this->option('admin-id').'.');
+
+ return 1;
+ }
+ } else {
+ $admin = User::onlySuperAdmins()->first();
+ }
+
+ if (! $admin) {
+ $this->warn('No admin user found — notifications will be suppressed.');
+ $noNotifications = true;
+ }
+ }
+
+ $scopeMsg = $companyId ? "company ID {$companyId}" : 'all companies';
+ $typesMsg = implode(', ', $typesToProcess);
+
+ if ($dryRun) {
+ $this->warn('DRY RUN — no changes will be made.');
+ } elseif (! $this->option('force')) {
+ if (! $this->confirm("This will check in and soft-delete all [{$typesMsg}] for [{$scopeMsg}]. Continue?")) {
+ $this->info('Aborted.');
+
+ return 0;
+ }
+ }
+
+ if (in_array('assets', $typesToProcess)) {
+ $this->processAssets($companyId, $noNotifications, $note, $admin, $dryRun);
+ }
+
+ if (in_array('licenses', $typesToProcess)) {
+ $this->processLicenses($companyId, $noNotifications, $note, $admin, $dryRun);
+ }
+
+ if (in_array('accessories', $typesToProcess)) {
+ $this->processAccessories($companyId, $noNotifications, $note, $admin, $dryRun);
+ }
+
+ if (in_array('components', $typesToProcess)) {
+ $this->processComponents($companyId, $noNotifications, $note, $admin, $dryRun);
+ }
+
+ if ($dryRun) {
+ $this->warn('Dry run complete — no changes were made.');
+ }
+
+ return 0;
+ }
+
+ private function processAssets(?string $companyId, bool $noNotifications, string $note, ?User $admin, bool $dryRun): void
+ {
+ $query = Asset::query();
+ if ($companyId) {
+ $query->where('company_id', $companyId);
+ }
+
+ $assets = $query->get();
+ $checkedIn = 0;
+ $deleted = 0;
+
+ foreach ($assets as $asset) {
+ if ($asset->assignedTo) {
+ if ($dryRun) {
+ $this->line(' Would check in asset: '.$asset->asset_tag.' (assigned to '.$asset->assignedTo->name.')');
+ } else {
+ $target = $asset->assignedTo;
+ $checkin_at = now()->format('Y-m-d H:i:s');
+ $originalValues = $asset->getRawOriginal();
+
+ if ($noNotifications) {
+ DB::table('assets')->where('id', $asset->id)
+ ->update(['assigned_to' => null, 'assigned_type' => null]);
+ $asset->logCheckin($target, $note, $checkin_at, $originalValues);
+ } else {
+ // Fire event before clearing so the log captures the original state
+ event(new CheckoutableCheckedIn($asset, $target, $admin, $note, $checkin_at, $originalValues));
+ DB::table('assets')->where('id', $asset->id)
+ ->update(['assigned_to' => null, 'assigned_type' => null]);
+ }
+
+ $asset->licenseseats()->update(['assigned_to' => null]);
+
+ CheckoutAcceptance::pending()
+ ->whereHasMorph('checkoutable', [Asset::class], fn (Builder $q) => $q->where('id', $asset->id))
+ ->delete();
+ }
+
+ $checkedIn++;
+ }
+
+ if ($dryRun) {
+ $this->line(' Would delete asset: '.$asset->asset_tag);
+ } else {
+ $asset->delete();
+ }
+
+ $deleted++;
+ }
+
+ $action = $dryRun ? 'would be' : 'were';
+ $this->info("Assets: {$checkedIn} {$action} checked in, {$deleted} {$action} deleted.");
+ }
+
+ private function processLicenses(?string $companyId, bool $noNotifications, string $note, ?User $admin, bool $dryRun): void
+ {
+ $query = License::query();
+ if ($companyId) {
+ $query->where('company_id', $companyId);
+ }
+
+ $licenses = $query->get();
+ $seatsCheckedIn = 0;
+ $deleted = 0;
+
+ foreach ($licenses as $license) {
+ $seats = LicenseSeat::where('license_id', $license->id)
+ ->where(fn ($q) => $q->whereNotNull('assigned_to')->orWhereNotNull('asset_id'))
+ ->get();
+
+ foreach ($seats as $seat) {
+ $target = $seat->assigned_to ? $seat->user : $seat->asset;
+
+ if ($dryRun) {
+ $this->line(' Would check in license seat for: '.$license->name.' (assigned to '.($target?->name ?? $target?->asset_tag ?? 'unknown').')');
+ } else {
+ $seat->assigned_to = null;
+ $seat->asset_id = null;
+ $seat->save();
+
+ if ($target) {
+ if ($noNotifications) {
+ $seat->logCheckin($target, $note);
+ } else {
+ event(new CheckoutableCheckedIn($seat, $target, $admin, $note));
+ }
+ }
+ }
+
+ $seatsCheckedIn++;
+ }
+
+ if ($dryRun) {
+ $this->line(' Would delete license: '.$license->name);
+ } else {
+ $license->licenseseats()->delete();
+ $license->delete();
+ }
+
+ $deleted++;
+ }
+
+ $action = $dryRun ? 'would be' : 'were';
+ $this->info("Licenses: {$seatsCheckedIn} seats {$action} checked in, {$deleted} licenses {$action} deleted.");
+ }
+
+ private function processAccessories(?string $companyId, bool $noNotifications, string $note, ?User $admin, bool $dryRun): void
+ {
+ $query = Accessory::query();
+ if ($companyId) {
+ $query->where('company_id', $companyId);
+ }
+
+ $accessories = $query->get();
+ $checkedIn = 0;
+ $deleted = 0;
+
+ foreach ($accessories as $accessory) {
+ $checkouts = AccessoryCheckout::where('accessory_id', $accessory->id)->get();
+
+ foreach ($checkouts as $checkout) {
+ $target = $checkout->assignedTo;
+
+ if ($dryRun) {
+ $this->line(' Would check in accessory: '.$accessory->name.' (assigned to '.($target?->name ?? $target?->asset_tag ?? 'unknown').')');
+ } else {
+ $checkin_at = now()->format('Y-m-d H:i:s');
+ $checkout->delete();
+
+ if ($target) {
+ if ($noNotifications) {
+ $accessory->logCheckin($target, $note, $checkin_at);
+ } else {
+ event(new CheckoutableCheckedIn($accessory, $target, $admin, $note, $checkin_at));
+ }
+ }
+ }
+
+ $checkedIn++;
+ }
+
+ if ($dryRun) {
+ $this->line(' Would delete accessory: '.$accessory->name);
+ } else {
+ $accessory->delete();
+ }
+
+ $deleted++;
+ }
+
+ $action = $dryRun ? 'would be' : 'were';
+ $this->info("Accessories: {$checkedIn} {$action} checked in, {$deleted} {$action} deleted.");
+ }
+
+ private function processComponents(?string $companyId, bool $noNotifications, string $note, ?User $admin, bool $dryRun): void
+ {
+ $query = Component::query();
+ if ($companyId) {
+ $query->where('company_id', $companyId);
+ }
+
+ $components = $query->get();
+ $checkedIn = 0;
+ $deleted = 0;
+
+ foreach ($components as $component) {
+ $assignments = DB::table('components_assets')
+ ->where('component_id', $component->id)
+ ->get();
+
+ foreach ($assignments as $assignment) {
+ $asset = Asset::find($assignment->asset_id);
+
+ if ($dryRun) {
+ $this->line(' Would check in component: '.$component->name.' (assigned to '.($asset?->asset_tag ?? 'unknown').')');
+ } else {
+ $checkin_at = now()->format('Y-m-d H:i:s');
+ DB::table('components_assets')->where('id', $assignment->id)->delete();
+
+ if ($asset) {
+ if ($noNotifications) {
+ $component->logCheckin($asset, $note, $checkin_at);
+ } else {
+ event(new CheckoutableCheckedIn($component, $asset, $admin, $note, $checkin_at));
+ }
+ }
+ }
+
+ $checkedIn++;
+ }
+
+ if ($dryRun) {
+ $this->line(' Would delete component: '.$component->name);
+ } else {
+ $component->delete();
+ }
+
+ $deleted++;
+ }
+
+ $action = $dryRun ? 'would be' : 'were';
+ $this->info("Components: {$checkedIn} {$action} checked in, {$deleted} {$action} deleted.");
+ }
+}
diff --git a/tests/Feature/Console/CheckinAndDeleteItemsTest.php b/tests/Feature/Console/CheckinAndDeleteItemsTest.php
new file mode 100644
index 0000000000..e2b1506959
--- /dev/null
+++ b/tests/Feature/Console/CheckinAndDeleteItemsTest.php
@@ -0,0 +1,347 @@
+<?php
+
+namespace Tests\Feature\Console;
+
+use App\Events\CheckoutableCheckedIn;
+use App\Models\Accessory;
+use App\Models\Asset;
+use App\Models\Company;
+use App\Models\Component;
+use App\Models\License;
+use App\Models\LicenseSeat;
+use App\Models\User;
+use Illuminate\Support\Facades\Event;
+use Tests\TestCase;
+
+class CheckinAndDeleteItemsTest extends TestCase
+{
+ // ---------------------------------------------------------------------------
+ // Assets
+ // ---------------------------------------------------------------------------
+
+ public function test_checked_out_asset_is_checked_in_and_soft_deleted()
+ {
+ $user = User::factory()->create();
+ $asset = Asset::factory()->assignedToUser($user)->create();
+
+ $this->assertNotNull($asset->assigned_to);
+
+ $this->artisan('snipeit:checkin-delete-all', ['--type' => 'assets', '--force' => true])
+ ->assertExitCode(0);
+
+ $this->assertSoftDeleted($asset);
+ $this->assertNull($asset->fresh()->assigned_to);
+ }
+
+ public function test_unassigned_asset_is_soft_deleted_without_checkin_log()
+ {
+ $asset = Asset::factory()->create();
+
+ $this->assertNull($asset->assigned_to);
+
+ $this->artisan('snipeit:checkin-delete-all', ['--type' => 'assets', '--force' => true])
+ ->assertExitCode(0);
+
+ $this->assertSoftDeleted($asset);
+ }
+
+ public function test_asset_checkin_creates_action_log()
+ {
+ $user = User::factory()->create();
+ $asset = Asset::factory()->assignedToUser($user)->create();
+
+ $this->artisan('snipeit:checkin-delete-all', ['--type' => 'assets', '--force' => true, '--no-notifications' => true])
+ ->assertExitCode(0);
+
+ $this->assertHasTheseActionLogs($asset, ['create', 'checkin from', 'delete']);
+ }
+
+ public function test_event_is_fired_for_checked_out_asset_when_notifications_enabled()
+ {
+ Event::fake([CheckoutableCheckedIn::class]);
+
+ User::factory()->superuser()->create();
+ $asset = Asset::factory()->assignedToUser()->create();
+
+ $this->artisan('snipeit:checkin-delete-all', ['--type' => 'assets', '--force' => true])
+ ->assertExitCode(0);
+
+ Event::assertDispatched(CheckoutableCheckedIn::class, function ($event) use ($asset) {
+ return $event->checkoutable->is($asset);
+ });
+ }
+
+ public function test_admin_id_option_is_used_as_checked_in_by_user()
+ {
+ Event::fake([CheckoutableCheckedIn::class]);
+
+ $admin = User::factory()->superuser()->create();
+ $asset = Asset::factory()->assignedToUser()->create();
+
+ $this->artisan('snipeit:checkin-delete-all', ['--type' => 'assets', '--force' => true, '--admin-id' => $admin->id])
+ ->assertExitCode(0);
+
+ Event::assertDispatched(CheckoutableCheckedIn::class, function ($event) use ($admin) {
+ return $event->checkedInBy->is($admin);
+ });
+ }
+
+ public function test_invalid_admin_id_returns_exit_code_1()
+ {
+ $this->artisan('snipeit:checkin-delete-all', ['--type' => 'assets', '--force' => true, '--admin-id' => 999999])
+ ->assertExitCode(1);
+ }
+
+ public function test_event_is_not_fired_when_no_notifications_option_set()
+ {
+ Event::fake([CheckoutableCheckedIn::class]);
+
+ $asset = Asset::factory()->assignedToUser()->create();
+
+ $this->artisan('snipeit:checkin-delete-all', ['--type' => 'assets', '--force' => true, '--no-notifications' => true])
+ ->assertExitCode(0);
+
+ Event::assertNotDispatched(CheckoutableCheckedIn::class);
+ }
+
+ public function test_company_id_option_scopes_assets()
+ {
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $assetA = Asset::factory()->for($companyA)->assignedToUser()->create();
+ $assetB = Asset::factory()->for($companyB)->assignedToUser()->create();
+
+ $this->artisan('snipeit:checkin-delete-all', [
+ '--type' => 'assets',
+ '--company-id' => $companyA->id,
+ '--force' => true,
+ '--no-notifications' => true,
+ ])->assertExitCode(0);
+
+ $this->assertSoftDeleted($assetA);
+ $this->assertNotSoftDeleted($assetB);
+ }
+
+ public function test_asset_license_seats_are_freed_on_checkin()
+ {
+ $user = User::factory()->create();
+ $asset = Asset::factory()->assignedToUser($user)->create();
+ $seat = LicenseSeat::factory()->create(['asset_id' => $asset->id, 'assigned_to' => $user->id]);
+
+ $this->artisan('snipeit:checkin-delete-all', ['--type' => 'assets', '--force' => true, '--no-notifications' => true])
+ ->assertExitCode(0);
+
+ $this->assertNull($seat->fresh()->assigned_to);
+ }
+
+ // ---------------------------------------------------------------------------
+ // Licenses
+ // ---------------------------------------------------------------------------
+
+ public function test_assigned_license_seat_is_checked_in_and_license_soft_deleted()
+ {
+ $license = License::factory()->create();
+ $seat = LicenseSeat::factory()->for($license)->assignedToUser()->create();
+
+ $this->assertNotNull($seat->assigned_to);
+
+ $this->artisan('snipeit:checkin-delete-all', ['--type' => 'licenses', '--force' => true, '--no-notifications' => true])
+ ->assertExitCode(0);
+
+ $this->assertSoftDeleted($license);
+ $this->assertNull($seat->fresh()->assigned_to);
+ $this->assertNull($seat->fresh()->asset_id);
+ }
+
+ public function test_unassigned_license_is_soft_deleted()
+ {
+ $license = License::factory()->create();
+
+ $this->artisan('snipeit:checkin-delete-all', ['--type' => 'licenses', '--force' => true, '--no-notifications' => true])
+ ->assertExitCode(0);
+
+ $this->assertSoftDeleted($license);
+ }
+
+ public function test_company_id_option_scopes_licenses()
+ {
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $licenseA = License::factory()->for($companyA)->create();
+ $licenseB = License::factory()->for($companyB)->create();
+
+ $this->artisan('snipeit:checkin-delete-all', [
+ '--type' => 'licenses',
+ '--company-id' => $companyA->id,
+ '--force' => true,
+ '--no-notifications' => true,
+ ])->assertExitCode(0);
+
+ $this->assertSoftDeleted($licenseA);
+ $this->assertNotSoftDeleted($licenseB);
+ }
+
+ // ---------------------------------------------------------------------------
+ // Accessories
+ // ---------------------------------------------------------------------------
+
+ public function test_checked_out_accessory_is_checked_in_and_soft_deleted()
+ {
+ $accessory = Accessory::factory()->checkedOutToUser()->create();
+
+ $this->assertEquals(1, $accessory->checkouts->count());
+
+ $this->artisan('snipeit:checkin-delete-all', ['--type' => 'accessories', '--force' => true, '--no-notifications' => true])
+ ->assertExitCode(0);
+
+ $this->assertSoftDeleted($accessory);
+ $this->assertEquals(0, $accessory->fresh()->checkouts->count());
+ }
+
+ public function test_company_id_option_scopes_accessories()
+ {
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $accessoryA = Accessory::factory()->for($companyA)->checkedOutToUser()->create();
+ $accessoryB = Accessory::factory()->for($companyB)->checkedOutToUser()->create();
+
+ $this->artisan('snipeit:checkin-delete-all', [
+ '--type' => 'accessories',
+ '--company-id' => $companyA->id,
+ '--force' => true,
+ '--no-notifications' => true,
+ ])->assertExitCode(0);
+
+ $this->assertSoftDeleted($accessoryA);
+ $this->assertNotSoftDeleted($accessoryB);
+ }
+
+ // ---------------------------------------------------------------------------
+ // Components
+ // ---------------------------------------------------------------------------
+
+ public function test_checked_out_component_is_checked_in_and_soft_deleted()
+ {
+ $component = Component::factory()->checkedOutToAsset()->create();
+
+ $this->assertDatabaseHas('components_assets', ['component_id' => $component->id]);
+
+ $this->artisan('snipeit:checkin-delete-all', ['--type' => 'components', '--force' => true, '--no-notifications' => true])
+ ->assertExitCode(0);
+
+ $this->assertSoftDeleted($component);
+ $this->assertDatabaseMissing('components_assets', ['component_id' => $component->id]);
+ }
+
+ public function test_company_id_option_scopes_components()
+ {
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $componentA = Component::factory()->for($companyA)->checkedOutToAsset()->create();
+ $componentB = Component::factory()->for($companyB)->checkedOutToAsset()->create();
+
+ $this->artisan('snipeit:checkin-delete-all', [
+ '--type' => 'components',
+ '--company-id' => $companyA->id,
+ '--force' => true,
+ '--no-notifications' => true,
+ ])->assertExitCode(0);
+
+ $this->assertSoftDeleted($componentA);
+ $this->assertNotSoftDeleted($componentB);
+ }
+
+ // ---------------------------------------------------------------------------
+ // Type filtering and general behaviour
+ // ---------------------------------------------------------------------------
+
+ public function test_type_option_limits_processing_to_specified_type()
+ {
+ $asset = Asset::factory()->assignedToUser()->create();
+ $accessory = Accessory::factory()->checkedOutToUser()->create();
+
+ $this->artisan('snipeit:checkin-delete-all', ['--type' => 'assets', '--force' => true, '--no-notifications' => true])
+ ->assertExitCode(0);
+
+ $this->assertSoftDeleted($asset);
+ $this->assertNotSoftDeleted($accessory);
+ }
+
+ public function test_invalid_type_returns_exit_code_1()
+ {
+ $this->artisan('snipeit:checkin-delete-all', ['--type' => 'invalid', '--force' => true])
+ ->assertExitCode(1);
+ }
+
+ public function test_confirmation_is_required_without_force()
+ {
+ Asset::factory()->create();
+
+ $this->artisan('snipeit:checkin-delete-all', ['--type' => 'assets'])
+ ->expectsConfirmation('This will check in and soft-delete all [assets] for [all companies]. Continue?', 'no')
+ ->assertExitCode(0);
+
+ $this->assertDatabaseCount('assets', 1);
+ }
+
+ public function test_force_option_skips_confirmation()
+ {
+ $asset = Asset::factory()->create();
+
+ $this->artisan('snipeit:checkin-delete-all', ['--type' => 'assets', '--force' => true, '--no-notifications' => true])
+ ->assertExitCode(0);
+
+ $this->assertSoftDeleted($asset);
+ }
+
+ public function test_dry_run_does_not_delete_or_checkin_assets()
+ {
+ $user = User::factory()->create();
+ $asset = Asset::factory()->assignedToUser($user)->create();
+
+ $this->artisan('snipeit:checkin-delete-all', ['--type' => 'assets', '--dry-run' => true])
+ ->assertExitCode(0);
+
+ $this->assertNotSoftDeleted($asset);
+ $this->assertNotNull($asset->fresh()->assigned_to);
+ }
+
+ public function test_dry_run_skips_confirmation_prompt()
+ {
+ Asset::factory()->create();
+
+ // Would hang waiting for input if confirmation were shown
+ $this->artisan('snipeit:checkin-delete-all', ['--type' => 'assets', '--dry-run' => true])
+ ->assertExitCode(0);
+ }
+
+ public function test_dry_run_outputs_would_be_messaging()
+ {
+ $asset = Asset::factory()->assignedToUser()->create();
+
+ $this->artisan('snipeit:checkin-delete-all', ['--type' => 'assets', '--dry-run' => true])
+ ->expectsOutputToContain('DRY RUN')
+ ->expectsOutputToContain('Would check in asset')
+ ->expectsOutputToContain('Would delete asset')
+ ->expectsOutputToContain('Dry run complete')
+ ->assertExitCode(0);
+ }
+
+ public function test_custom_note_is_used_in_action_log()
+ {
+ $asset = Asset::factory()->assignedToUser()->create();
+
+ $this->artisan('snipeit:checkin-delete-all', [
+ '--type' => 'assets',
+ '--force' => true,
+ '--no-notifications' => true,
+ '--note' => 'Bulk removal for decommission',
+ ])->assertExitCode(0);
+
+ $this->assertDatabaseHas('action_logs', [
+ 'item_id' => $asset->id,
+ 'action_type' => 'checkin from',
+ 'note' => 'Bulk removal for decommission',
+ ]);
+ }
+}