Skip to content

A free open source IT asset/license management system

Added delete cli prompt tool

snipeb0a6cdc29f86
diff --git a/app/Console/Commands/BulkDelete.php b/app/Console/Commands/BulkDelete.php
new file mode 100644
index 0000000000..751eacf39c
--- /dev/null
+++ b/app/Console/Commands/BulkDelete.php
@@ -0,0 +1,832 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Events\CheckoutableCheckedIn;
+use App\Models\Accessory;
+use App\Models\AccessoryCheckout;
+use App\Models\Actionlog;
+use App\Models\Asset;
+use App\Models\CheckoutAcceptance;
+use App\Models\Company;
+use App\Models\Component;
+use App\Models\Consumable;
+use App\Models\License;
+use App\Models\LicenseSeat;
+use App\Models\Maintenance;
+use App\Models\User;
+use Illuminate\Console\Command;
+use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Storage;
+use Symfony\Component\Console\Helper\ProgressBar;
+
+use function Laravel\Prompts\confirm;
+use function Laravel\Prompts\error;
+use function Laravel\Prompts\info;
+use function Laravel\Prompts\multiselect;
+use function Laravel\Prompts\search;
+use function Laravel\Prompts\select;
+use function Laravel\Prompts\warning;
+
+class BulkDelete extends Command
+{
+ protected $signature = 'snipeit:checkin-delete-items';
+
+ protected $description = 'Interactively check in and/or delete items by company and type';
+
+ private const CHECKIN_NOTE = 'Checked in via bulk CLI operation';
+
+ public function handle(): int
+ {
+ // Step 1: Dry run?
+ $dryRun = confirm(
+ label: 'Is this a dry run?',
+ default: true,
+ yes: 'Yes — preview only, no changes will be made',
+ no: 'No — actions will be performed',
+ );
+
+ // Step 2: Who are you?
+ $adminId = search(
+ label: 'Who are you? Search by username, first or last name.',
+ placeholder: 'Type to search users...',
+ options: function (string $value): array {
+ if (strlen($value) < 1) {
+ return [];
+ }
+
+ return User::where('activated', 1)
+ ->whereNull('deleted_at')
+ ->where(function ($query) use ($value) {
+ $query->where('username', 'like', "%{$value}%")
+ ->orWhere('first_name', 'like', "%{$value}%")
+ ->orWhere('last_name', 'like', "%{$value}%")
+ ->orWhereRaw("CONCAT(first_name, ' ', last_name) LIKE ?", ["%{$value}%"]);
+ })
+ ->get()
+ ->mapWithKeys(fn (User $u) => [$u->id => "{$u->first_name} {$u->last_name} ({$u->username})"])
+ ->toArray();
+ },
+ validate: fn (mixed $value) => ! $value ? 'A valid active user is required.' : null,
+ );
+
+ /** @var User $admin */
+ $admin = User::findOrFail((int) $adminId);
+
+ // Step 3: Which companies?
+ $allCompanyOptions = Company::orderBy('name')
+ ->get()
+ ->mapWithKeys(fn (Company $c) => [$c->id => "{$c->name} (ID: {$c->id})"])
+ ->toArray();
+
+ if (empty($allCompanyOptions)) {
+ error('No companies found. Please create at least one company before using this command.');
+
+ return 1;
+ }
+
+ // Add a virtual "No Company" option for items with a null company_id
+ $companyChoices = ['__null__' => '(No Company / Unassigned)'] + $allCompanyOptions;
+
+ $selectedCompanyKeys = multiselect(
+ label: 'Which companies would you like to check in and delete items for?',
+ options: $companyChoices,
+ hint: 'Space to toggle, arrow keys to navigate, enter to confirm.',
+ required: 'Please select at least one company.',
+ );
+
+ $includeNullCompany = in_array('__null__', $selectedCompanyKeys);
+ $selectedCompanyIds = array_values(array_filter(
+ $selectedCompanyKeys,
+ fn ($k) => $k !== '__null__'
+ ));
+
+ $selectedCompanyNames = array_map(fn ($id) => $allCompanyOptions[$id] ?? '(No Company)', $selectedCompanyKeys);
+
+ // Step 4: Which item types?
+ $rawTypeSelection = multiselect(
+ label: 'What item types would you like to check in and delete?',
+ options: [
+ 'all' => 'All Items (assets, licenses, accessories, components, consumables, users)',
+ 'assets' => 'Assets',
+ 'licenses' => 'Licenses',
+ 'accessories' => 'Accessories',
+ 'components' => 'Components',
+ 'consumables' => 'Consumables',
+ 'users' => 'Users',
+ ],
+ required: 'Please select at least one item type.',
+ hint: 'Select "All Items" to process every supported type.',
+ );
+
+ $allSubTypes = ['assets', 'licenses', 'accessories', 'components', 'consumables', 'users'];
+ $selectedTypes = in_array('all', $rawTypeSelection)
+ ? $allSubTypes
+ : array_values(array_intersect($allSubTypes, $rawTypeSelection));
+
+ // Compute and display counts now so the user can see what will be affected
+ $counts = $this->getCounts($selectedTypes, $selectedCompanyIds, $includeNullCompany);
+
+ $skipAdminUser = false;
+
+ $this->line('');
+ $this->line(' Items that would be affected:');
+ foreach ($counts as $type => $count) {
+ $this->line(sprintf(' %-14s %d', ucfirst($type).':', $count));
+ }
+
+ if (in_array('users', $selectedTypes)) {
+ $userInScope = $this->buildUserQuery($selectedCompanyIds, $includeNullCompany)
+ ->where('users.id', $admin->id)
+ ->exists();
+
+ if ($userInScope) {
+ $skipAdminUser = true;
+ $counts['users'] = max(0, ($counts['users'] ?? 0) - 1);
+ warning(" Your user ({$admin->username}) is within the selected scope and will be skipped during user deletion.");
+ }
+ }
+
+ $this->line('');
+
+ // Step 5: Hard delete, soft delete, or no delete?
+ $deleteType = select(
+ label: 'How should items be deleted?',
+ options: [
+ 'soft' => 'Soft delete — items moved to trash (recoverable)',
+ 'hard' => 'Hard delete — permanently removed (cannot be recovered)',
+ 'none' => 'No delete — check in only, items remain in inventory',
+ ],
+ default: 'soft',
+ );
+
+ // Step 6: Send checkin notifications? (not applicable to users or consumables)
+ $notifiableTypes = array_intersect($selectedTypes, ['assets', 'licenses', 'accessories', 'components']);
+ $sendNotifications = false;
+
+ if (! empty($notifiableTypes)) {
+ $sendNotifications = confirm(
+ label: 'Should we send checkin notifications?',
+ default: true,
+ hint: 'Applies to: '.implode(', ', $notifiableTypes).'. Users and consumables are excluded.',
+ );
+ }
+
+ // Step 7: Clear related action_logs?
+ $clearLogs = confirm(
+ label: 'Should we clear related action logs?',
+ default: false,
+ hint: 'This removes all history for affected items, as if the data never existed.',
+ );
+
+ // Step 8: Delete associated files?
+ $deleteFiles = false;
+ if ($deleteType !== 'none') {
+ $deleteFiles = confirm(
+ label: 'Should we also delete associated image and upload files?',
+ default: false,
+ hint: 'Permanently removes images, avatars, signatures, EULAs, and action log uploads from disk.',
+ );
+ }
+
+ // Step 9: Backup first?
+ $doBackup = confirm(
+ label: 'Should we run a backup before proceeding?',
+ default: true,
+ hint: 'Strongly recommended. Saved as backup-before-bulk-delete-cli-[datetime].zip',
+ );
+
+ // Step 10: Summary + final confirmation
+ $this->line('');
+ $this->line(' ════════════════════════════════════════════════════');
+ $this->line(' SUMMARY OF ACTIONS');
+ $this->line(' ════════════════════════════════════════════════════');
+ $this->line(" Admin user: {$admin->first_name} {$admin->last_name} ({$admin->username})");
+ $this->line(' Companies: '.implode(', ', $selectedCompanyNames));
+ $this->line(' Item types: '.implode(', ', $selectedTypes));
+ $this->line(" Delete mode: {$deleteType}");
+ $this->line(' Notifications: '.($sendNotifications ? 'Yes' : 'No'));
+ $this->line(' Clear logs: '.($clearLogs ? 'Yes' : 'No'));
+ $this->line(' Delete files: '.($deleteFiles ? 'Yes' : 'No'));
+ $this->line(' Backup first: '.($doBackup ? 'Yes' : 'No'));
+ $this->line(' Dry run: '.($dryRun ? 'Yes' : 'No'));
+ $this->line('');
+ $this->line(' Items to be processed:');
+ foreach ($counts as $type => $count) {
+ $this->line(sprintf(' %-14s %d', ucfirst($type).':', $count));
+ }
+ if ($skipAdminUser) {
+ $this->line(' * Your user account will be skipped during user deletion.');
+ }
+ $this->line(' ════════════════════════════════════════════════════');
+ $this->line('');
+
+ $confirmed = confirm(
+ label: $dryRun
+ ? 'Proceed with dry run?'
+ : 'Are you sure you want to proceed? This cannot be undone.',
+ default: false,
+ );
+
+ if (! $confirmed) {
+ info('Aborted. No changes were made.');
+
+ return 0;
+ }
+
+ // Run backup if requested
+ if ($doBackup && ! $dryRun) {
+ $backupFilename = 'backup-before-bulk-delete-cli-'.now()->format('Y-m-d-H-i-s');
+ info("Running backup ({$backupFilename}.zip)...");
+ $this->call('snipeit:backup', ['--filename' => $backupFilename]);
+ }
+
+ // Step 11: Execute with progress bar
+ $totalItems = array_sum($counts);
+ $bar = $this->output->createProgressBar($totalItems > 0 ? $totalItems : 1);
+ $bar->setFormat(' %current%/%max% [%bar%] %percent:3s%% %message%');
+ $bar->setMessage('Starting...');
+ $bar->start();
+
+ foreach ($selectedTypes as $type) {
+ match ($type) {
+ 'assets' => $this->processAssets($selectedCompanyIds, $includeNullCompany, $sendNotifications, $admin, $dryRun, $deleteType, $clearLogs, $deleteFiles, $bar),
+ 'licenses' => $this->processLicenses($selectedCompanyIds, $includeNullCompany, $sendNotifications, $admin, $dryRun, $deleteType, $clearLogs, $deleteFiles, $bar),
+ 'accessories' => $this->processAccessories($selectedCompanyIds, $includeNullCompany, $sendNotifications, $admin, $dryRun, $deleteType, $clearLogs, $deleteFiles, $bar),
+ 'components' => $this->processComponents($selectedCompanyIds, $includeNullCompany, $sendNotifications, $admin, $dryRun, $deleteType, $clearLogs, $deleteFiles, $bar),
+ 'consumables' => $this->processConsumables($selectedCompanyIds, $includeNullCompany, $dryRun, $deleteType, $clearLogs, $deleteFiles, $bar),
+ 'users' => $this->processUsers($selectedCompanyIds, $includeNullCompany, $admin, $skipAdminUser, $dryRun, $deleteType, $clearLogs, $deleteFiles, $bar),
+ };
+ }
+
+ $bar->setMessage('Done.');
+ $bar->finish();
+ $this->line('');
+ $this->line('');
+
+ if ($dryRun) {
+ warning('Dry run complete — no changes were made.');
+ } else {
+ info('All actions completed successfully.');
+ }
+
+ return 0;
+ }
+
+ private function getCounts(array $types, array $companyIds, bool $includeNull): array
+ {
+ $counts = [];
+
+ if (in_array('assets', $types)) {
+ $counts['assets'] = $this->buildCompanyQuery(Asset::query(), $companyIds, $includeNull)->count();
+ }
+ if (in_array('licenses', $types)) {
+ $counts['licenses'] = $this->buildCompanyQuery(License::query(), $companyIds, $includeNull)->count();
+ }
+ if (in_array('accessories', $types)) {
+ $counts['accessories'] = $this->buildCompanyQuery(Accessory::query(), $companyIds, $includeNull)->count();
+ }
+ if (in_array('components', $types)) {
+ $counts['components'] = $this->buildCompanyQuery(Component::query(), $companyIds, $includeNull)->count();
+ }
+ if (in_array('consumables', $types)) {
+ $counts['consumables'] = $this->buildCompanyQuery(Consumable::query(), $companyIds, $includeNull)->count();
+ }
+ if (in_array('users', $types)) {
+ $counts['users'] = $this->buildUserQuery($companyIds, $includeNull)->count();
+ }
+
+ return $counts;
+ }
+
+ private function buildCompanyQuery(Builder $query, array $companyIds, bool $includeNull): Builder
+ {
+ return $query->where(function (Builder $q) use ($companyIds, $includeNull) {
+ if (! empty($companyIds)) {
+ $q->whereIn('company_id', $companyIds);
+ }
+ if ($includeNull) {
+ $method = ! empty($companyIds) ? 'orWhereNull' : 'whereNull';
+ $q->{$method}('company_id');
+ }
+ });
+ }
+
+ private function buildUserQuery(array $companyIds, bool $includeNull): Builder
+ {
+ return User::query()
+ ->where('activated', 1)
+ ->where(function (Builder $q) use ($companyIds, $includeNull) {
+ if (! empty($companyIds)) {
+ $q->whereIn('company_id', $companyIds);
+ }
+ if ($includeNull) {
+ $method = ! empty($companyIds) ? 'orWhereNull' : 'whereNull';
+ $q->{$method}('company_id');
+ }
+ });
+ }
+
+ private function processAssets(
+ array $companyIds,
+ bool $includeNull,
+ bool $sendNotifications,
+ User $admin,
+ bool $dryRun,
+ string $deleteType,
+ bool $clearLogs,
+ bool $deleteFiles,
+ ProgressBar $bar,
+ ): void {
+ $assets = $this->buildCompanyQuery(Asset::query(), $companyIds, $includeNull)->get();
+
+ foreach ($assets as $asset) {
+ $bar->setMessage("Assets: {$asset->asset_tag}");
+
+ if ($asset->assignedTo) {
+ if ($dryRun) {
+ $this->line("\n [dry-run] Would check in asset {$asset->asset_tag} from {$asset->assignedTo->name}");
+ } else {
+ $target = $asset->assignedTo;
+ $checkinAt = now()->format('Y-m-d H:i:s');
+ $originalValues = $asset->getRawOriginal();
+
+ if ($sendNotifications) {
+ event(new CheckoutableCheckedIn($asset, $target, $admin, self::CHECKIN_NOTE, $checkinAt, $originalValues));
+ DB::table('assets')->where('id', $asset->id)->update(['assigned_to' => null, 'assigned_type' => null]);
+ } else {
+ DB::table('assets')->where('id', $asset->id)->update(['assigned_to' => null, 'assigned_type' => null]);
+ $asset->logCheckin($target, self::CHECKIN_NOTE, $checkinAt, $originalValues);
+ }
+
+ $asset->licenseseats()->update(['assigned_to' => null]);
+
+ CheckoutAcceptance::where('checkoutable_type', Asset::class)
+ ->where('checkoutable_id', $asset->id)
+ ->whereNull('accepted_at')
+ ->whereNull('declined_at')
+ ->forceDelete();
+ }
+ }
+
+ if (! $dryRun) {
+ // Collect action log file paths before logs may be cleared
+ $actionLogPaths = $deleteFiles
+ ? $asset->assetlog()->whereNotNull('filename')->get()
+ ->map(fn (Actionlog $log) => $log->uploads_file_path())
+ ->filter()
+ ->values()
+ ->toArray()
+ : [];
+
+ // Delete checkout acceptance files, then hard-remove all acceptances
+ if ($deleteFiles) {
+ CheckoutAcceptance::where('checkoutable_type', Asset::class)
+ ->where('checkoutable_id', $asset->id)
+ ->get()
+ ->each(fn (CheckoutAcceptance $ca) => $this->deleteAcceptanceFiles($ca));
+ }
+ CheckoutAcceptance::where('checkoutable_type', Asset::class)
+ ->where('checkoutable_id', $asset->id)
+ ->forceDelete();
+
+ // Hard-delete-only cleanup: maintenance records, accessory checkouts to this
+ // asset, and any other assets that were assigned to this one
+ $maintenanceImages = [];
+ if ($deleteType === 'hard') {
+ if ($deleteFiles) {
+ $maintenanceImages = $asset->maintenances()
+ ->whereNotNull('image')
+ ->pluck('image')
+ ->toArray();
+ }
+ $asset->maintenances()->forceDelete();
+ AccessoryCheckout::where('assigned_to', $asset->id)
+ ->where('assigned_type', Asset::class)
+ ->delete();
+ DB::table('assets')
+ ->where('assigned_to', $asset->id)
+ ->where('assigned_type', Asset::class)
+ ->update(['assigned_to' => null, 'assigned_type' => null]);
+ }
+
+ match ($deleteType) {
+ 'soft' => $asset->delete(),
+ 'hard' => $asset->forceDelete(),
+ default => null,
+ };
+
+ if ($clearLogs) {
+ $asset->assetlog()->forceDelete();
+ }
+
+ if ($deleteFiles) {
+ if ($asset->image) {
+ $this->deleteStorageFile('public', app('assets_upload_path').$asset->image);
+ }
+ foreach ($maintenanceImages as $img) {
+ $this->deleteStorageFile('public', app('maintenances_upload_path').$img);
+ }
+ foreach ($actionLogPaths as $path) {
+ $this->deleteStorageFile('local', $path);
+ }
+ }
+ } elseif ($deleteType !== 'none') {
+ $this->line("\n [dry-run] Would {$deleteType}-delete asset {$asset->asset_tag}");
+ }
+
+ $bar->advance();
+ }
+ }
+
+ private function processLicenses(
+ array $companyIds,
+ bool $includeNull,
+ bool $sendNotifications,
+ User $admin,
+ bool $dryRun,
+ string $deleteType,
+ bool $clearLogs,
+ bool $deleteFiles,
+ ProgressBar $bar,
+ ): void {
+ $licenses = $this->buildCompanyQuery(License::query(), $companyIds, $includeNull)->get();
+
+ foreach ($licenses as $license) {
+ $bar->setMessage("Licenses: {$license->name}");
+
+ $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("\n [dry-run] Would check in license seat for {$license->name} from ".($target?->name ?? $target?->asset_tag ?? 'unknown'));
+ } else {
+ $seat->assigned_to = null;
+ $seat->asset_id = null;
+ $seat->save();
+
+ if ($target) {
+ if ($sendNotifications) {
+ event(new CheckoutableCheckedIn($seat, $target, $admin, self::CHECKIN_NOTE));
+ } else {
+ $seat->logCheckin($target, self::CHECKIN_NOTE);
+ }
+ }
+ }
+ }
+
+ if (! $dryRun) {
+ // Collect action log file paths before logs may be cleared
+ $actionLogPaths = $deleteFiles
+ ? $license->assetlog()->whereNotNull('filename')->get()
+ ->map(fn (Actionlog $log) => $log->uploads_file_path())
+ ->filter()
+ ->values()
+ ->toArray()
+ : [];
+
+ if ($deleteType === 'soft') {
+ $license->delete();
+ } elseif ($deleteType === 'hard') {
+ $seatIds = $license->licenseseats()->pluck('id');
+ if ($deleteFiles) {
+ CheckoutAcceptance::where('checkoutable_type', LicenseSeat::class)
+ ->whereIn('checkoutable_id', $seatIds)
+ ->get()
+ ->each(fn (CheckoutAcceptance $ca) => $this->deleteAcceptanceFiles($ca));
+ }
+ CheckoutAcceptance::where('checkoutable_type', LicenseSeat::class)
+ ->whereIn('checkoutable_id', $seatIds)
+ ->forceDelete();
+ $license->licenseseats()->forceDelete();
+ DB::table('kits_licenses')->where('license_id', $license->id)->delete();
+ $license->forceDelete();
+ }
+
+ if ($clearLogs) {
+ $license->assetlog()->forceDelete();
+ }
+
+ if ($deleteFiles) {
+ foreach ($actionLogPaths as $path) {
+ $this->deleteStorageFile('local', $path);
+ }
+ }
+ } elseif ($deleteType !== 'none') {
+ $this->line("\n [dry-run] Would {$deleteType}-delete license {$license->name}");
+ }
+
+ $bar->advance();
+ }
+ }
+
+ private function processAccessories(
+ array $companyIds,
+ bool $includeNull,
+ bool $sendNotifications,
+ User $admin,
+ bool $dryRun,
+ string $deleteType,
+ bool $clearLogs,
+ bool $deleteFiles,
+ ProgressBar $bar,
+ ): void {
+ $accessories = $this->buildCompanyQuery(Accessory::query(), $companyIds, $includeNull)->get();
+
+ foreach ($accessories as $accessory) {
+ $bar->setMessage("Accessories: {$accessory->name}");
+
+ $checkouts = AccessoryCheckout::where('accessory_id', $accessory->id)->get();
+
+ foreach ($checkouts as $checkout) {
+ $target = $checkout->assignedTo;
+
+ if ($dryRun) {
+ $this->line("\n [dry-run] Would check in accessory {$accessory->name} from ".($target?->name ?? 'unknown'));
+ } else {
+ $checkinAt = now()->format('Y-m-d H:i:s');
+ $checkout->delete();
+
+ if ($target) {
+ if ($sendNotifications) {
+ event(new CheckoutableCheckedIn($accessory, $target, $admin, self::CHECKIN_NOTE, $checkinAt));
+ } else {
+ $accessory->logCheckin($target, self::CHECKIN_NOTE, $checkinAt);
+ }
+ }
+ }
+ }
+
+ if (! $dryRun) {
+ // Collect action log file paths before logs may be cleared
+ $actionLogPaths = $deleteFiles
+ ? $accessory->assetlog()->whereNotNull('filename')->get()
+ ->map(fn (Actionlog $log) => $log->uploads_file_path())
+ ->filter()
+ ->values()
+ ->toArray()
+ : [];
+
+ if ($clearLogs) {
+ $accessory->assetlog()->forceDelete();
+ }
+
+ if ($deleteType === 'hard') {
+ DB::table('kits_accessories')->where('accessory_id', $accessory->id)->delete();
+ }
+
+ match ($deleteType) {
+ 'soft' => $accessory->delete(),
+ 'hard' => $accessory->forceDelete(),
+ default => null,
+ };
+
+ if ($deleteFiles) {
+ if ($accessory->image) {
+ $this->deleteStorageFile('public', app('accessories_upload_path').$accessory->image);
+ }
+ foreach ($actionLogPaths as $path) {
+ $this->deleteStorageFile('local', $path);
+ }
+ }
+ } elseif ($deleteType !== 'none') {
+ $this->line("\n [dry-run] Would {$deleteType}-delete accessory {$accessory->name}");
+ }
+
+ $bar->advance();
+ }
+ }
+
+ private function processComponents(
+ array $companyIds,
+ bool $includeNull,
+ bool $sendNotifications,
+ User $admin,
+ bool $dryRun,
+ string $deleteType,
+ bool $clearLogs,
+ bool $deleteFiles,
+ ProgressBar $bar,
+ ): void {
+ $components = $this->buildCompanyQuery(Component::query(), $companyIds, $includeNull)->get();
+
+ foreach ($components as $component) {
+ $bar->setMessage("Components: {$component->name}");
+
+ $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("\n [dry-run] Would check in component {$component->name} from asset ".($asset?->asset_tag ?? 'unknown'));
+ } else {
+ $checkinAt = now()->format('Y-m-d H:i:s');
+ DB::table('components_assets')->where('id', $assignment->id)->delete();
+
+ if ($asset) {
+ if ($sendNotifications) {
+ event(new CheckoutableCheckedIn($component, $asset, $admin, self::CHECKIN_NOTE, $checkinAt));
+ } else {
+ $component->logCheckin($asset, self::CHECKIN_NOTE, $checkinAt);
+ }
+ }
+ }
+ }
+
+ if (! $dryRun) {
+ // Collect action log file paths before logs may be cleared
+ $actionLogPaths = $deleteFiles
+ ? $component->assetlog()->whereNotNull('filename')->get()
+ ->map(fn (Actionlog $log) => $log->uploads_file_path())
+ ->filter()
+ ->values()
+ ->toArray()
+ : [];
+
+ if ($clearLogs) {
+ $component->assetlog()->forceDelete();
+ }
+
+ match ($deleteType) {
+ 'soft' => $component->delete(),
+ 'hard' => $component->forceDelete(),
+ default => null,
+ };
+
+ if ($deleteFiles) {
+ if ($component->image) {
+ $this->deleteStorageFile('public', app('components_upload_path').$component->image);
+ }
+ foreach ($actionLogPaths as $path) {
+ $this->deleteStorageFile('local', $path);
+ }
+ }
+ } elseif ($deleteType !== 'none') {
+ $this->line("\n [dry-run] Would {$deleteType}-delete component {$component->name}");
+ }
+
+ $bar->advance();
+ }
+ }
+
+ private function processConsumables(
+ array $companyIds,
+ bool $includeNull,
+ bool $dryRun,
+ string $deleteType,
+ bool $clearLogs,
+ bool $deleteFiles,
+ ProgressBar $bar,
+ ): void {
+ $consumables = $this->buildCompanyQuery(Consumable::query(), $companyIds, $includeNull)->get();
+
+ foreach ($consumables as $consumable) {
+ $bar->setMessage("Consumables: {$consumable->name}");
+
+ if (! $dryRun) {
+ // Collect action log file paths before logs may be cleared
+ $actionLogPaths = $deleteFiles
+ ? $consumable->assetlog()->whereNotNull('filename')->get()
+ ->map(fn (Actionlog $log) => $log->uploads_file_path())
+ ->filter()
+ ->values()
+ ->toArray()
+ : [];
+
+ if ($clearLogs) {
+ $consumable->assetlog()->forceDelete();
+ }
+
+ if ($deleteType === 'hard') {
+ DB::table('kits_consumables')->where('consumable_id', $consumable->id)->delete();
+ }
+
+ match ($deleteType) {
+ 'soft' => $consumable->delete(),
+ 'hard' => $consumable->forceDelete(),
+ default => null,
+ };
+
+ if ($deleteFiles) {
+ if ($consumable->image) {
+ $this->deleteStorageFile('public', app('consumables_upload_path').$consumable->image);
+ }
+ foreach ($actionLogPaths as $path) {
+ $this->deleteStorageFile('local', $path);
+ }
+ }
+ } elseif ($deleteType !== 'none') {
+ $this->line("\n [dry-run] Would {$deleteType}-delete consumable {$consumable->name}");
+ }
+
+ $bar->advance();
+ }
+ }
+
+ private function processUsers(
+ array $companyIds,
+ bool $includeNull,
+ User $admin,
+ bool $skipAdminUser,
+ bool $dryRun,
+ string $deleteType,
+ bool $clearLogs,
+ bool $deleteFiles,
+ ProgressBar $bar,
+ ): void {
+ $users = $this->buildUserQuery($companyIds, $includeNull)->get();
+
+ foreach ($users as $user) {
+ if ($skipAdminUser && $user->id === $admin->id) {
+ continue;
+ }
+
+ $bar->setMessage("Users: {$user->username}");
+
+ if (! $dryRun) {
+ // Collect file paths and acceptance records before deleting pivot data
+ $acceptancesToDelete = $deleteFiles
+ ? CheckoutAcceptance::where('assigned_to_id', $user->id)->get()
+ : collect();
+
+ $actionLogPaths = $deleteFiles
+ ? Actionlog::where('item_type', User::class)
+ ->where('item_id', $user->id)
+ ->where('action_type', 'uploaded')
+ ->whereNotNull('filename')
+ ->get()
+ ->map(fn (Actionlog $log) => $log->uploads_file_path())
+ ->filter()
+ ->values()
+ ->toArray()
+ : [];
+
+ // Clear pivot/assignment data that will orphan on deletion
+ LicenseSeat::where('assigned_to', $user->id)->update(['assigned_to' => null]);
+ AccessoryCheckout::where('assigned_to', $user->id)
+ ->where('assigned_type', User::class)
+ ->delete();
+ DB::table('consumables_users')->where('assigned_to', $user->id)->delete();
+ CheckoutAcceptance::where('assigned_to_id', $user->id)->forceDelete();
+
+ if ($clearLogs) {
+ $user->userlog()->forceDelete();
+ }
+
+ match ($deleteType) {
+ 'soft' => $user->delete(),
+ 'hard' => $user->forceDelete(),
+ default => null,
+ };
+
+ if ($deleteFiles) {
+ if ($user->avatar) {
+ $this->deleteStorageFile('public', app('users_upload_path').$user->avatar);
+ }
+ $acceptancesToDelete->each(fn (CheckoutAcceptance $ca) => $this->deleteAcceptanceFiles($ca));
+ foreach ($actionLogPaths as $path) {
+ $this->deleteStorageFile('local', $path);
+ }
+ }
+ } elseif ($deleteType !== 'none') {
+ $this->line("\n [dry-run] Would {$deleteType}-delete user {$user->username}");
+ }
+
+ $bar->advance();
+ }
+ }
+
+ private function deleteStorageFile(string $disk, ?string $path): void
+ {
+ if (! $path) {
+ return;
+ }
+ try {
+ $storage = Storage::disk($disk);
+ if ($storage->exists($path)) {
+ $storage->delete($path);
+ }
+ } catch (\Exception $e) {
+ Log::warning("Could not delete file {$path}: ".$e->getMessage());
+ }
+ }
+
+ private function deleteAcceptanceFiles(CheckoutAcceptance $acceptance): void
+ {
+ if ($acceptance->signature_filename) {
+ $this->deleteStorageFile('local', 'private_uploads/signatures/'.$acceptance->signature_filename);
+ }
+ if ($acceptance->stored_eula_file) {
+ $this->deleteStorageFile('local', 'private_uploads/eula-pdfs/'.$acceptance->stored_eula_file);
+ }
+ }
+}
diff --git a/tests/Feature/Console/BulkDeleteTest.php b/tests/Feature/Console/BulkDeleteTest.php
new file mode 100644
index 0000000000..e03d2ff54b
--- /dev/null
+++ b/tests/Feature/Console/BulkDeleteTest.php
@@ -0,0 +1,619 @@
+<?php
+
+namespace Tests\Feature\Console;
+
+use App\Events\CheckoutableCheckedIn;
+use App\Models\Accessory;
+use App\Models\Asset;
+use App\Models\CheckoutAcceptance;
+use App\Models\Company;
+use App\Models\Component;
+use App\Models\Consumable;
+use App\Models\License;
+use App\Models\LicenseSeat;
+use App\Models\Maintenance;
+use App\Models\User;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Event;
+use Illuminate\Testing\PendingCommand;
+use Tests\TestCase;
+
+class BulkDeleteTest extends TestCase
+{
+ // ---------------------------------------------------------------------------
+ // Prompt sequence helpers
+ // ---------------------------------------------------------------------------
+
+ /**
+ * Builds a PendingCommand with the full prompt sequence pre-answered.
+ *
+ * Search() falls back as two steps: ask() for the search term, then
+ * choice() to select from matching results. Multiselect() and select()
+ * each fall back as a single choice() call.
+ */
+ private function runCommand(
+ User $admin,
+ array $companyIds,
+ array $types,
+ string $deleteType = 'soft',
+ bool $dryRun = false,
+ bool $sendNotifications = false,
+ bool $clearLogs = false,
+ bool $deleteFiles = false,
+ bool $doBackup = false,
+ bool $confirm = true,
+ ): PendingCommand {
+ $hasNotifiableTypes = ! empty(array_intersect($types, ['assets', 'licenses', 'accessories', 'components']));
+
+ $searchLabel = 'Who are you? Search by username, first or last name.';
+ $companiesLabel = 'Which companies would you like to check in and delete items for?';
+ $typesLabel = 'What item types would you like to check in and delete?';
+ $deleteLabel = 'How should items be deleted?';
+
+ $cmd = $this->artisan('snipeit:checkin-delete-items')
+ // Step 1: dry run confirm
+ ->expectsConfirmation('Is this a dry run?', $dryRun ? 'yes' : 'no')
+ // Step 2: search() — ask() for the search term, then choice() to pick from results
+ // Using expectsQuestion for both to avoid triggering choices validation
+ ->expectsQuestion($searchLabel, $admin->username)
+ ->expectsQuestion($searchLabel, (string) $admin->id)
+ // Step 3: multiselect for companies
+ ->expectsQuestion($companiesLabel, $companyIds)
+ // Step 4: multiselect for item types
+ ->expectsQuestion($typesLabel, $types)
+ // Step 5: select for delete mode
+ ->expectsQuestion($deleteLabel, $deleteType);
+
+ // Step 6: notification confirm only shown for checkiable types
+ if ($hasNotifiableTypes) {
+ $cmd->expectsConfirmation('Should we send checkin notifications?', $sendNotifications ? 'yes' : 'no');
+ }
+
+ // Steps 7–10 and final confirm
+ $cmd->expectsConfirmation('Should we clear related action logs?', $clearLogs ? 'yes' : 'no');
+
+ // Step 8: file deletion prompt — only shown when deleteType !== 'none'
+ if ($deleteType !== 'none') {
+ $cmd->expectsConfirmation('Should we also delete associated image and upload files?', $deleteFiles ? 'yes' : 'no');
+ }
+
+ $cmd->expectsConfirmation('Should we run a backup before proceeding?', $doBackup ? 'yes' : 'no')
+ ->expectsConfirmation(
+ $dryRun ? 'Proceed with dry run?' : 'Are you sure you want to proceed? This cannot be undone.',
+ $confirm ? 'yes' : 'no',
+ );
+
+ return $cmd;
+ }
+
+ // ---------------------------------------------------------------------------
+ // Assets
+ // ---------------------------------------------------------------------------
+
+ public function test_assigned_asset_is_checked_in_and_soft_deleted(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $user = User::factory()->create(['company_id' => $company->id]);
+ $asset = Asset::factory()->for($company)->assignedToUser($user)->create();
+
+ $this->assertNotNull($asset->assigned_to);
+
+ $this->runCommand($admin, [$company->id], ['assets'])
+ ->assertExitCode(0);
+
+ $this->assertSoftDeleted($asset);
+ $this->assertNull($asset->fresh()->assigned_to);
+ }
+
+ public function test_unassigned_asset_is_soft_deleted(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $asset = Asset::factory()->for($company)->create();
+
+ $this->runCommand($admin, [$company->id], ['assets'])
+ ->assertExitCode(0);
+
+ $this->assertSoftDeleted($asset);
+ }
+
+ public function test_asset_is_hard_deleted(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $asset = Asset::factory()->for($company)->assignedToUser()->create();
+
+ $this->runCommand($admin, [$company->id], ['assets'], deleteType: 'hard')
+ ->assertExitCode(0);
+
+ $this->assertDatabaseMissing('assets', ['id' => $asset->id]);
+ }
+
+ public function test_asset_checkin_event_fired_when_notifications_enabled(): void
+ {
+ Event::fake([CheckoutableCheckedIn::class]);
+
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $asset = Asset::factory()->for($company)->assignedToUser()->create();
+
+ $this->runCommand($admin, [$company->id], ['assets'], sendNotifications: true)
+ ->assertExitCode(0);
+
+ Event::assertDispatched(CheckoutableCheckedIn::class, fn ($e) => $e->checkoutable->is($asset));
+ }
+
+ public function test_asset_checkin_event_not_fired_when_notifications_suppressed(): void
+ {
+ Event::fake([CheckoutableCheckedIn::class]);
+
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ Asset::factory()->for($company)->assignedToUser()->create();
+
+ $this->runCommand($admin, [$company->id], ['assets'], sendNotifications: false)
+ ->assertExitCode(0);
+
+ Event::assertNotDispatched(CheckoutableCheckedIn::class);
+ }
+
+ public function test_asset_scoped_to_correct_company(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $assetA = Asset::factory()->for($companyA)->create();
+ $assetB = Asset::factory()->for($companyB)->create();
+
+ $this->runCommand($admin, [$companyA->id], ['assets'])
+ ->assertExitCode(0);
+
+ $this->assertSoftDeleted($assetA);
+ $this->assertNotSoftDeleted($assetB);
+ }
+
+ public function test_asset_delete_clears_all_checkout_acceptances(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $asset = Asset::factory()->for($company)->create();
+
+ CheckoutAcceptance::factory()->create([
+ 'checkoutable_type' => Asset::class,
+ 'checkoutable_id' => $asset->id,
+ ]);
+
+ $this->runCommand($admin, [$company->id], ['assets'])
+ ->assertExitCode(0);
+
+ $this->assertDatabaseMissing('checkout_acceptances', [
+ 'checkoutable_type' => Asset::class,
+ 'checkoutable_id' => $asset->id,
+ ]);
+ }
+
+ public function test_asset_checkin_clears_linked_license_seats(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $user = User::factory()->create(['company_id' => $company->id]);
+ $asset = Asset::factory()->for($company)->assignedToUser($user)->create();
+ $seat = LicenseSeat::factory()->create(['asset_id' => $asset->id, 'assigned_to' => $user->id]);
+
+ $this->runCommand($admin, [$company->id], ['assets'])
+ ->assertExitCode(0);
+
+ $this->assertNull($seat->fresh()->assigned_to);
+ }
+
+ // ---------------------------------------------------------------------------
+ // Licenses
+ // ---------------------------------------------------------------------------
+
+ public function test_assigned_license_seat_is_checked_in_and_license_soft_deleted(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $license = License::factory()->for($company)->create();
+ $seat = LicenseSeat::factory()->for($license)->assignedToUser()->create();
+
+ $this->assertNotNull($seat->assigned_to);
+
+ $this->runCommand($admin, [$company->id], ['licenses'])
+ ->assertExitCode(0);
+
+ $this->assertSoftDeleted($license);
+ $this->assertNull($seat->fresh()->assigned_to);
+ $this->assertNull($seat->fresh()->asset_id);
+ }
+
+ public function test_license_hard_delete_removes_seats_and_their_checkout_acceptances(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $license = License::factory()->for($company)->create();
+ $seat = LicenseSeat::factory()->for($license)->assignedToUser()->create();
+
+ CheckoutAcceptance::factory()->create([
+ 'checkoutable_type' => LicenseSeat::class,
+ 'checkoutable_id' => $seat->id,
+ ]);
+
+ $this->runCommand($admin, [$company->id], ['licenses'], deleteType: 'hard')
+ ->assertExitCode(0);
+
+ $this->assertDatabaseMissing('licenses', ['id' => $license->id]);
+ $this->assertDatabaseMissing('license_seats', ['id' => $seat->id]);
+ $this->assertDatabaseMissing('checkout_acceptances', [
+ 'checkoutable_type' => LicenseSeat::class,
+ 'checkoutable_id' => $seat->id,
+ ]);
+ }
+
+ public function test_license_scoped_to_correct_company(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $licenseA = License::factory()->for($companyA)->create();
+ $licenseB = License::factory()->for($companyB)->create();
+
+ $this->runCommand($admin, [$companyA->id], ['licenses'])
+ ->assertExitCode(0);
+
+ $this->assertSoftDeleted($licenseA);
+ $this->assertNotSoftDeleted($licenseB);
+ }
+
+ // ---------------------------------------------------------------------------
+ // Accessories
+ // ---------------------------------------------------------------------------
+
+ public function test_checked_out_accessory_is_checked_in_and_soft_deleted(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $accessory = Accessory::factory()->for($company)->checkedOutToUser()->create();
+
+ $this->assertEquals(1, $accessory->checkouts->count());
+
+ $this->runCommand($admin, [$company->id], ['accessories'])
+ ->assertExitCode(0);
+
+ $this->assertSoftDeleted($accessory);
+ $this->assertEquals(0, $accessory->fresh()->checkouts->count());
+ }
+
+ public function test_accessory_scoped_to_correct_company(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $accessoryA = Accessory::factory()->for($companyA)->checkedOutToUser()->create();
+ $accessoryB = Accessory::factory()->for($companyB)->checkedOutToUser()->create();
+
+ $this->runCommand($admin, [$companyA->id], ['accessories'])
+ ->assertExitCode(0);
+
+ $this->assertSoftDeleted($accessoryA);
+ $this->assertNotSoftDeleted($accessoryB);
+ }
+
+ // ---------------------------------------------------------------------------
+ // Components
+ // ---------------------------------------------------------------------------
+
+ public function test_checked_out_component_is_checked_in_and_soft_deleted(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $component = Component::factory()->for($company)->checkedOutToAsset()->create();
+
+ $this->assertDatabaseHas('components_assets', ['component_id' => $component->id]);
+
+ $this->runCommand($admin, [$company->id], ['components'])
+ ->assertExitCode(0);
+
+ $this->assertSoftDeleted($component);
+ $this->assertDatabaseMissing('components_assets', ['component_id' => $component->id]);
+ }
+
+ // ---------------------------------------------------------------------------
+ // Consumables
+ // ---------------------------------------------------------------------------
+
+ public function test_consumable_is_soft_deleted(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $consumable = Consumable::factory()->for($company)->create();
+
+ $this->runCommand($admin, [$company->id], ['consumables'])
+ ->assertExitCode(0);
+
+ $this->assertSoftDeleted($consumable);
+ }
+
+ public function test_consumable_is_hard_deleted(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $consumable = Consumable::factory()->for($company)->create();
+
+ $this->runCommand($admin, [$company->id], ['consumables'], deleteType: 'hard')
+ ->assertExitCode(0);
+
+ $this->assertDatabaseMissing('consumables', ['id' => $consumable->id]);
+ }
+
+ // ---------------------------------------------------------------------------
+ // Users
+ // ---------------------------------------------------------------------------
+
+ public function test_user_is_soft_deleted(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $user = User::factory()->create(['company_id' => $company->id, 'activated' => 1]);
+
+ $this->runCommand($admin, [$company->id], ['users'])
+ ->assertExitCode(0);
+
+ $this->assertSoftDeleted($user);
+ }
+
+ public function test_user_delete_nulls_license_seat_assignments(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $user = User::factory()->create(['company_id' => $company->id, 'activated' => 1]);
+ $seat = LicenseSeat::factory()->create(['assigned_to' => $user->id]);
+
+ $this->runCommand($admin, [$company->id], ['users'])
+ ->assertExitCode(0);
+
+ $this->assertNull($seat->fresh()->assigned_to);
+ }
+
+ public function test_user_delete_removes_accessory_checkout_records(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $user = User::factory()->create(['company_id' => $company->id, 'activated' => 1]);
+ Accessory::factory()->for($company)->checkedOutToUser($user)->create();
+
+ $this->assertDatabaseHas('accessories_checkout', ['assigned_to' => $user->id]);
+
+ $this->runCommand($admin, [$company->id], ['users'])
+ ->assertExitCode(0);
+
+ $this->assertDatabaseMissing('accessories_checkout', ['assigned_to' => $user->id]);
+ }
+
+ public function test_user_delete_removes_consumable_assignments(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $user = User::factory()->create(['company_id' => $company->id, 'activated' => 1]);
+ Consumable::factory()->for($company)->checkedOutToUser($user)->create();
+
+ $this->assertDatabaseHas('consumables_users', ['assigned_to' => $user->id]);
+
+ $this->runCommand($admin, [$company->id], ['users'])
+ ->assertExitCode(0);
+
+ $this->assertDatabaseMissing('consumables_users', ['assigned_to' => $user->id]);
+ }
+
+ public function test_user_delete_removes_checkout_acceptances(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $user = User::factory()->create(['company_id' => $company->id, 'activated' => 1]);
+
+ CheckoutAcceptance::factory()->create(['assigned_to_id' => $user->id]);
+
+ $this->runCommand($admin, [$company->id], ['users'])
+ ->assertExitCode(0);
+
+ $this->assertDatabaseMissing('checkout_acceptances', ['assigned_to_id' => $user->id]);
+ }
+
+ public function test_admin_user_is_skipped_during_user_deletion(): void
+ {
+ $company = Company::factory()->create();
+ $admin = User::factory()->superuser()->create(['company_id' => $company->id, 'activated' => 1]);
+
+ $this->runCommand($admin, [$company->id], ['users'])
+ ->assertExitCode(0);
+
+ $this->assertDatabaseHas('users', ['id' => $admin->id, 'deleted_at' => null]);
+ }
+
+ // ---------------------------------------------------------------------------
+ // Action log clearing
+ // ---------------------------------------------------------------------------
+
+ public function test_clear_logs_removes_asset_action_logs(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $user = User::factory()->create();
+ $asset = Asset::factory()->for($company)->assignedToUser($user)->create();
+
+ $this->runCommand($admin, [$company->id], ['assets'], clearLogs: true)
+ ->assertExitCode(0);
+
+ $this->assertDatabaseMissing('action_logs', [
+ 'item_type' => Asset::class,
+ 'item_id' => $asset->id,
+ ]);
+ }
+
+ // ---------------------------------------------------------------------------
+ // Maintenance / related table cleanup
+ // ---------------------------------------------------------------------------
+
+ public function test_asset_hard_delete_removes_maintenance_records(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $asset = Asset::factory()->for($company)->create();
+ $maintenance = Maintenance::factory()->create(['asset_id' => $asset->id]);
+
+ $this->runCommand($admin, [$company->id], ['assets'], deleteType: 'hard')
+ ->assertExitCode(0);
+
+ $this->assertDatabaseMissing('maintenances', ['id' => $maintenance->id]);
+ }
+
+ public function test_asset_hard_delete_removes_accessory_checkouts_to_asset(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $asset = Asset::factory()->for($company)->create();
+ $accessory = Accessory::factory()->for($company)->create();
+ $accessory->checkouts()->create([
+ 'accessory_id' => $accessory->id,
+ 'assigned_to' => $asset->id,
+ 'assigned_type' => Asset::class,
+ 'created_by' => $admin->id,
+ ]);
+
+ $this->assertDatabaseHas('accessories_checkout', [
+ 'assigned_to' => $asset->id,
+ 'assigned_type' => Asset::class,
+ ]);
+
+ $this->runCommand($admin, [$company->id], ['assets'], deleteType: 'hard')
+ ->assertExitCode(0);
+
+ $this->assertDatabaseMissing('accessories_checkout', [
+ 'assigned_to' => $asset->id,
+ 'assigned_type' => Asset::class,
+ ]);
+ }
+
+ public function test_asset_hard_delete_nulls_child_asset_assignments(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $target = Asset::factory()->for($company)->create();
+ $child = Asset::factory()->create(['assigned_to' => $target->id, 'assigned_type' => Asset::class]);
+
+ $this->runCommand($admin, [$company->id], ['assets'], deleteType: 'hard')
+ ->assertExitCode(0);
+
+ $this->assertNull($child->fresh()->assigned_to);
+ $this->assertNull($child->fresh()->assigned_type);
+ }
+
+ public function test_license_hard_delete_removes_kit_entries(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $license = License::factory()->for($company)->create();
+ DB::table('kits_licenses')->insert(['kit_id' => 1, 'license_id' => $license->id, 'quantity' => 1]);
+
+ $this->runCommand($admin, [$company->id], ['licenses'], deleteType: 'hard')
+ ->assertExitCode(0);
+
+ $this->assertDatabaseMissing('kits_licenses', ['license_id' => $license->id]);
+ }
+
+ public function test_accessory_hard_delete_removes_kit_entries(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $accessory = Accessory::factory()->for($company)->create();
+ DB::table('kits_accessories')->insert(['kit_id' => 1, 'accessory_id' => $accessory->id, 'quantity' => 1]);
+
+ $this->runCommand($admin, [$company->id], ['accessories'], deleteType: 'hard')
+ ->assertExitCode(0);
+
+ $this->assertDatabaseMissing('kits_accessories', ['accessory_id' => $accessory->id]);
+ }
+
+ public function test_consumable_hard_delete_removes_kit_entries(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $consumable = Consumable::factory()->for($company)->create();
+ DB::table('kits_consumables')->insert(['kit_id' => 1, 'consumable_id' => $consumable->id, 'quantity' => 1]);
+
+ $this->runCommand($admin, [$company->id], ['consumables'], deleteType: 'hard')
+ ->assertExitCode(0);
+
+ $this->assertDatabaseMissing('kits_consumables', ['consumable_id' => $consumable->id]);
+ }
+
+ // ---------------------------------------------------------------------------
+ // Dry run
+ // ---------------------------------------------------------------------------
+
+ public function test_dry_run_does_not_delete_or_checkin_assets(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $user = User::factory()->create(['company_id' => $company->id]);
+ $asset = Asset::factory()->for($company)->assignedToUser($user)->create();
+
+ $this->runCommand($admin, [$company->id], ['assets'], dryRun: true)
+ ->assertExitCode(0);
+
+ $this->assertNotSoftDeleted($asset);
+ $this->assertNotNull($asset->fresh()->assigned_to);
+ }
+
+ // ---------------------------------------------------------------------------
+ // Final confirm / abort
+ // ---------------------------------------------------------------------------
+
+ public function test_declining_final_confirm_makes_no_changes(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $asset = Asset::factory()->for($company)->create();
+
+ $this->runCommand($admin, [$company->id], ['assets'], confirm: false)
+ ->assertExitCode(0);
+
+ $this->assertNotSoftDeleted($asset);
+ }
+
+ // ---------------------------------------------------------------------------
+ // Multi-company / checkin-only mode
+ // ---------------------------------------------------------------------------
+
+ public function test_multiple_companies_are_all_processed(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $assetA = Asset::factory()->for($companyA)->create();
+ $assetB = Asset::factory()->for($companyB)->create();
+ $assetC = Asset::factory()->create(); // no company — should be untouched
+
+ $this->runCommand($admin, [$companyA->id, $companyB->id], ['assets'])
+ ->assertExitCode(0);
+
+ $this->assertSoftDeleted($assetA);
+ $this->assertSoftDeleted($assetB);
+ $this->assertNotSoftDeleted($assetC);
+ }
+
+ public function test_checkin_only_mode_checks_in_without_deleting(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ $company = Company::factory()->create();
+ $user = User::factory()->create(['company_id' => $company->id]);
+ $asset = Asset::factory()->for($company)->assignedToUser($user)->create();
+
+ $this->runCommand($admin, [$company->id], ['assets'], deleteType: 'none')
+ ->assertExitCode(0);
+
+ $this->assertNotSoftDeleted($asset);
+ $this->assertNull($asset->fresh()->assigned_to);
+ }
+}