find($this->argument('from_abo')); $to = UserAbo::find($this->argument('to_abo')); if (! $from || ! $to) { $this->error('Abo nicht gefunden (from: '.($from ? 'ok' : 'fehlt').', to: '.($to ? 'ok' : 'fehlt').').'); return self::FAILURE; } if ($from->id === $to->id) { $this->error('from_abo und to_abo sind identisch.'); return self::FAILURE; } $sameCustomer = strcasecmp(trim((string) $from->email), trim((string) $to->email)) === 0 && (int) $from->member_id === (int) $to->member_id && $from->is_for === $to->is_for; if (! $sameCustomer) { $this->error("Abos gehören nicht zum selben Kunden (from: {$from->email} / Berater {$from->member_id}, to: {$to->email} / Berater {$to->member_id}). Abbruch."); return self::FAILURE; } $orders = UserAboOrder::where('user_abo_id', $from->id)->orderBy('id')->get(); if ($orders->isEmpty()) { $this->info("Keine user_abo_orders bei Abo {$from->id} - nichts zu tun."); return self::SUCCESS; } $this->info("Abo {$from->id} ({$from->email}".($from->trashed() ? ', gelöscht' : '').") → Abo {$to->id}: {$orders->count()} Lieferung(en)"); $this->table( ['user_abo_order', 'shopping_order', 'Status', 'bezahlt'], $orders->map(fn (UserAboOrder $o) => [$o->id, $o->shopping_order_id, $o->status, $o->paid ? 'ja' : 'nein'])->all() ); if ($this->option('dry-run')) { $this->info('DRY-RUN - nichts geändert.'); return self::SUCCESS; } UserAboOrder::where('user_abo_id', $from->id)->update(['user_abo_id' => $to->id]); $to->refresh(); $this->info("Umgehängt. Abo {$to->id} zählt jetzt {$to->getCountPaidOrders()} ausgeführte Lieferung(en)."); return self::SUCCESS; } }