F1: partner_paid_at wird beim Rechnungskauf (paymentStatusPaidAction mit paid=false) nicht mehr gesetzt, sondern erst bei der Rechnungszahlung; der Backfill nimmt das Aktivierungsdatum nur noch ohne jede Registrierungsbestellung (offene Rechnung bleibt leer). F2: Snapshots speichern je Linie new_partner_ids, die Neupartner-Liste zeigt genau diese; aeltere Snapshots ohne IDs rechnen mit der damaligen Regel (Registrierungsmonat, scopeNewPartnerInByActiveDate), damit Kachel und Liste uebereinstimmen. Keine Payload-Versionsaenderung, kein Neuaufbau noetig. F3: Liefertag-/Termin-Aenderung waehrend einer Pause zieht paused_until mit; eine Ausfuehrung loescht auch den paused_by_*-Grund. F4: expiredCount der Firstlines-Seite (Controller, View, Sprachen) zum bereits committeten Test nachgezogen. F5: Backfills schreiben ohne updated_at (toBase()->update, timestamps=false). Kuendigungsbestaetigung abo_cancel_confirm wird im Selfservice serverseitig verlangt. AboStatusExplainer laedt die Historie je Abo und Aktion nur einmal. Stellungnahme und Deploy-Folgen im Audit-Dokument, Regressionstests je Befund. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
97 lines
3.7 KiB
PHP
97 lines
3.7 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 den reinen Query-Builder (`toBase()`), damit weder Model-Events
|
|
* noch `updated_at` angefasst werden — der Eloquent-Builder wuerde den
|
|
* Zeitstempel mitschreiben (Audit 18.09., F5).
|
|
*/
|
|
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)->toBase()->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)->toBase()->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);
|
|
}
|
|
}
|