Neue Spalten user_abos.status_reason / paused_until und
user_abo_orders.error_message. Der Grund wird zentral am Model gepflegt
(saving-Event): Zahlungsfehler, Kuendigung/Deaktivierung mit Akteur aus dem
Formular-Kanal, Loeschen beim Zurueckkehren in den Lauf; die Pause setzt
paused_until und paused_by_*. Cron- und Retry-Zahlung schreiben den Fehlertext
des Anbieters an die Abo-Bestellung.
AboStatusExplainer uebersetzt das in einen Satz ("Pausiert bis ... vom Kunden
am ...", "Zahlung am ... fehlgeschlagen - Grund: ...", "Gekuendigt am ... vom
Berater"), Partial _status_explain in Abo-Detail (CRM, Portal, Admin),
Abo-Uebersicht, Kundenabo-Liste, Team-Kundenabos und Admin-Datatable; dort
neuer Filter "Zustand" (pausiert / Zahlung fehlgeschlagen). Backfill-Kommando
abo:backfill-status-reasons fuer Bestandsdaten, Fallback ueber das
Statusprotokoll und die Pausenfelder.
Sprachdateien de/en/es/fr, Tests in AboStatusExplainTest, Plan-Status.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
96 lines
3.6 KiB
PHP
96 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\UserAbo;
|
|
use App\Models\UserAboStatusHistory;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Console\Command;
|
|
|
|
/**
|
|
* Einmaliger Nachtrag fuer Bestandsdaten (Ticket A3): fuellt `status_reason`
|
|
* und `paused_until` aus Status, Pausenfeldern und dem Statusprotokoll
|
|
* (`user_abo_status_histories`). Laesst bereits gefuellte Werte unberuehrt und
|
|
* schreibt ueber Query-Builder, damit weder Model-Events noch `updated_at`
|
|
* angefasst werden.
|
|
*/
|
|
class AboBackfillStatusReasons extends Command
|
|
{
|
|
protected $signature = 'abo:backfill-status-reasons {--dry-run : Nur zaehlen, nichts schreiben}';
|
|
|
|
protected $description = 'status_reason / paused_until fuer bestehende Abos aus Status und Statusprotokoll nachtragen';
|
|
|
|
public function handle(): int
|
|
{
|
|
$dryRun = (bool) $this->option('dry-run');
|
|
$counts = ['payment_failed' => 0, 'cancelled' => 0, 'deactivated' => 0, 'paused' => 0];
|
|
|
|
UserAbo::withTrashed()
|
|
->whereNull('status_reason')
|
|
->whereIn('status', [3, 4, 6])
|
|
->orderBy('id')
|
|
->chunkById(500, function ($abos) use (&$counts, $dryRun): void {
|
|
foreach ($abos as $abo) {
|
|
[$reason, $bucket] = match ((int) $abo->status) {
|
|
3 => [UserAbo::REASON_PAYMENT_FAILED, 'payment_failed'],
|
|
4 => [UserAbo::REASON_PREFIX_CANCELLED.$this->actorFromHistory($abo, UserAboStatusHistory::ACTION_CANCELLED), 'cancelled'],
|
|
default => [UserAbo::REASON_PREFIX_DEACTIVATED.$this->actorFromHistory($abo, UserAboStatusHistory::ACTION_DEACTIVATED), 'deactivated'],
|
|
};
|
|
|
|
$counts[$bucket]++;
|
|
|
|
if (! $dryRun) {
|
|
UserAbo::withTrashed()->whereKey($abo->id)->update(['status_reason' => $reason]);
|
|
}
|
|
}
|
|
});
|
|
|
|
UserAbo::query()
|
|
->whereNull('paused_until')
|
|
->where('status', 2)
|
|
->where('active', true)
|
|
->where('pause_count', '>', 0)
|
|
->whereNotNull('last_pause_date')
|
|
->orderBy('id')
|
|
->chunkById(500, function ($abos) use (&$counts, $dryRun): void {
|
|
foreach ($abos as $abo) {
|
|
$until = $abo->pausedUntil();
|
|
|
|
if ($until === null || $until->lt(Carbon::today())) {
|
|
continue;
|
|
}
|
|
|
|
$counts['paused']++;
|
|
|
|
if (! $dryRun) {
|
|
UserAbo::whereKey($abo->id)->update([
|
|
'paused_until' => $until->toDateString(),
|
|
'status_reason' => $abo->status_reason ?: UserAbo::REASON_PREFIX_PAUSED.$this->actorFromHistory($abo, UserAboStatusHistory::ACTION_PAUSED),
|
|
]);
|
|
}
|
|
}
|
|
});
|
|
|
|
$this->table(['Zustand', 'Abos'], [
|
|
['Zahlung fehlgeschlagen', $counts['payment_failed']],
|
|
['Gekuendigt', $counts['cancelled']],
|
|
['Deaktiviert', $counts['deactivated']],
|
|
['Pausiert (laufend)', $counts['paused']],
|
|
]);
|
|
|
|
$this->info($dryRun ? 'DRY-RUN, nichts geschrieben.' : 'Nachtrag abgeschlossen.');
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
private function actorFromHistory(UserAbo $abo, string $action): string
|
|
{
|
|
$entry = UserAboStatusHistory::query()
|
|
->where('user_abo_id', $abo->id)
|
|
->where('action', $action)
|
|
->orderByDesc('id')
|
|
->first();
|
|
|
|
return UserAbo::reasonActorFromView($entry?->view);
|
|
}
|
|
}
|