Abo (A3): Status und Grund eines Abos sichtbar machen
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>
This commit is contained in:
parent
e8057f9f7b
commit
d6efe56cd4
22 changed files with 977 additions and 15 deletions
96
app/Console/Commands/AboBackfillStatusReasons.php
Normal file
96
app/Console/Commands/AboBackfillStatusReasons.php
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
<?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);
|
||||
}
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@ use App\Cron\UserMakeOrder;
|
|||
use App\Mail\AboPaymentFailedMail;
|
||||
use App\Models\UserAbo;
|
||||
use App\Models\UserAboOrder;
|
||||
use App\Services\Abo\AboStatusExplainer;
|
||||
use App\Services\AboHelper;
|
||||
use App\Services\AboOneTimeService;
|
||||
use App\Services\Incentive\IncentiveTracker;
|
||||
|
|
@ -428,7 +429,13 @@ class UserMakeAboOrder extends Command
|
|||
$this->info("Aktualisiere Abo bei Fehler: {$userAbo->id} (Status: {$status})");
|
||||
|
||||
try {
|
||||
DB::transaction(function () use ($userAbo, $shoppingOrder, $status) {
|
||||
// Ticket A3: Fehlertext des Zahlungsanbieters (Karte abgelaufen, Deckung,
|
||||
// PayPal) an der Bestellung festhalten — sonst geht der Grund verloren.
|
||||
$errorMessage = is_numeric($status)
|
||||
? AboStatusExplainer::paymentErrorMessage(is_array($errorResponse) ? $errorResponse : null)
|
||||
: mb_substr((string) $status, 0, 255);
|
||||
|
||||
DB::transaction(function () use ($userAbo, $shoppingOrder, $status, $errorMessage) {
|
||||
// Nur last_date aktualisieren, next_date bleibt unverändert
|
||||
// Damit wird das Abo beim nächsten Cron-Lauf erneut versucht
|
||||
$updateData = [
|
||||
|
|
@ -448,6 +455,7 @@ class UserMakeAboOrder extends Command
|
|||
'user_abo_id' => $userAbo->id,
|
||||
'shopping_order_id' => $shoppingOrder->id,
|
||||
'status' => $orderStatus,
|
||||
'error_message' => $errorMessage,
|
||||
'paid' => false,
|
||||
]);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue