mivita/app/Console/Commands/UserEnsureShops.php
Kevin 083d643220 Standard-Shop fuer jeden Berater: Kundenbestellung ohne Shop crashte nicht mehr
Berater ohne UserShop liefen bei Kundenbestellungen (abo-ot-customer) in
"Attempt to read property id on null". Shops werden jetzt bei Freischaltung
und bei Bedarf automatisch angelegt (Name aus Vor-/Nachname, bei Belegung
-2, -3, ...). Nachtrag fuer Bestandsberater: php artisan user:ensure-shops.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 08:13:29 +00:00

53 lines
1.5 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Services\UserShopService;
use App\User;
use Illuminate\Console\Command;
class UserEnsureShops extends Command
{
/**
* @var string
*/
protected $signature = 'user:ensure-shops {--dry-run : Nur anzeigen, keine Shops anlegen}';
/**
* @var string
*/
protected $description = 'Legt für aktive Berater ohne Shop einen Standard-Shop an';
public function handle(): int
{
$dryRun = (bool) $this->option('dry-run');
$users = User::query()
->where('active', 1)
->where('admin', 0)
->whereDoesntHave('shop')
->with('account')
->orderBy('id')
->get();
$rows = [];
foreach ($users as $user) {
$name = $dryRun
? UserShopService::generateDefaultName($user, array_column($rows, 2))
: UserShopService::ensureForUser($user)->name;
$rows[] = [$user->id, $user->email, $name];
}
$this->table(['User', 'E-Mail', 'Shop'], $rows);
$this->info(($dryRun ? '[DRY-RUN] ' : '').count($rows).' Berater ohne Shop'.($dryRun ? ' gefunden.' : ' mit Standard-Shop versorgt.'));
if (! $dryRun && count($rows)) {
\Log::channel('cleanup')->info('COMMAND [user:ensure-shops] created '.count($rows).' shops.', [
'user_ids' => array_column($rows, 0),
]);
}
return self::SUCCESS;
}
}