mivita/app/Console/Commands/DhlBackfillEmails.php
2026-01-23 17:35:23 +01:00

97 lines
2.5 KiB
PHP

<?php
namespace App\Console\Commands;
use Acme\Dhl\Models\DhlShipment;
use Illuminate\Console\Command;
class DhlBackfillEmails extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'dhl:backfill-emails
{--dry-run : Nur simulieren, keine Änderungen}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Füllt das E-Mail-Feld für bestehende DHL-Sendungen nach';
/**
* Execute the console command.
*/
public function handle(): int
{
$dryRun = $this->option('dry-run');
$this->info('DHL E-Mail Backfill gestartet');
$this->info('Modus: ' . ($dryRun ? 'DRY-RUN (keine Änderungen)' : 'LIVE'));
$this->newLine();
// Hole alle Sendungen ohne E-Mail
$shipments = DhlShipment::with('shoppingOrder.shopping_user')
->whereNull('email')
->orWhere('email', '')
->get();
$total = $shipments->count();
$updated = 0;
$skipped = 0;
$this->info("Gefundene Sendungen ohne E-Mail: {$total}");
$this->newLine();
$bar = $this->output->createProgressBar($total);
foreach ($shipments as $shipment) {
$bar->advance();
// Hole E-Mail aus Shopping User
$email = null;
if ($shipment->shoppingOrder && $shipment->shoppingOrder->shopping_user) {
$email = $shipment->shoppingOrder->shopping_user->email;
}
if (empty($email)) {
$skipped++;
continue;
}
if (! $dryRun) {
$shipment->email = $email;
$shipment->save();
}
$updated++;
}
$bar->finish();
$this->newLine(2);
// Statistik
$this->info('Backfill abgeschlossen!');
$this->newLine();
$this->table(
['Metrik', 'Anzahl'],
[
['Gesamt geprüft', $total],
['E-Mail gesetzt', $updated],
['Übersprungen (keine E-Mail)', $skipped],
]
);
if ($dryRun) {
$this->warn('DRY-RUN: Keine Änderungen wurden vorgenommen.');
$this->info('Führen Sie den Befehl ohne --dry-run aus, um die Änderungen zu speichern.');
} else {
$this->info("{$updated} Sendungen wurden aktualisiert.");
}
return Command::SUCCESS;
}
}