presseportale/app/Services/Customer/CustomerStatusSummary.php
Kevin Adametz af99189b23 Status-Widget: immer verfuegbarer Buchungsueberblick in der Topbar
Ein Header-Dropdown oben rechts (Desktop) bzw. im Mobile-Header zeigt
fuer alle Kunden-Panel-User den aktuellen Status: aktuelle Buchung/Tarif,
verbleibendes PM-Kontingent und Credit-Guthaben. Ohne aktive Buchung
weist es direkt auf die noetige Buchung hin und verlinkt die Buchungsseite.

CustomerStatusSummary buendelt die Anzeige-Werte ueber die oeffentliche
User-API (Gating bleibt Single Source of Truth im Modell); die anonyme
Blade-Komponente x-customer.status-menu rendert das Dropdown.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-18 13:03:25 +00:00

124 lines
3.8 KiB
PHP

<?php
namespace App\Services\Customer;
use App\Enums\Tier;
use App\Enums\UserPaymentOptionStatus;
use App\Models\Plan;
use App\Models\User;
/**
* Read-only Statusüberblick für das immer verfügbare Buchungs-Widget im
* Header (aktuelle Buchung, verbleibendes PM-Kontingent, Credit-Guthaben).
*
* Single Source of Truth für die Gating-Logik bleibt das User-Modell; dieses
* DTO bündelt nur die fürs UI nötigen Werte, damit die Blade-Komponente keine
* eigene Geschäftslogik trägt.
*/
class CustomerStatusSummary
{
public function __construct(
public readonly Tier $tier,
public readonly ?string $planName,
public readonly ?string $billingInterval,
public readonly bool $hasActiveBooking,
public readonly bool $hasSubscription,
public readonly bool $hasGrandfathered,
public readonly int $openSinglePurchases,
public readonly bool $unlimitedQuota,
public readonly ?int $quotaTotal,
public readonly ?int $quotaRemaining,
public readonly int $creditBalance,
) {}
public static function fromUser(User $user): self
{
$plan = $user->currentPlan();
$subscription = $user->subscription();
$hasSubscription = (bool) $subscription?->valid();
$billingInterval = null;
if ($plan instanceof Plan && $subscription?->stripe_price) {
$billingInterval = $subscription->stripe_price === $plan->stripe_price_id_yearly
? 'yearly'
: 'monthly';
}
$hasGrandfathered = $user->userPaymentOptions()
->whereIn('status', [
UserPaymentOptionStatus::Active->value,
UserPaymentOptionStatus::Grandfathered->value,
])
->exists();
return new self(
tier: $user->currentTier(),
planName: $plan?->name,
billingInterval: $billingInterval,
hasActiveBooking: $user->hasActiveBooking(),
hasSubscription: $hasSubscription,
hasGrandfathered: $hasGrandfathered,
openSinglePurchases: $user->singlePurchases()->grantingSubmission()->count(),
unlimitedQuota: $user->hasUnlimitedPressReleaseQuota(),
quotaTotal: $user->pressReleaseQuotaTotal(),
quotaRemaining: $user->pressReleaseQuotaRemaining(),
creditBalance: $user->creditBalance(),
);
}
public function hasCredits(): bool
{
return $this->creditBalance > 0;
}
/**
* Menschlich lesbares Etikett der aktuellen Buchung.
*/
public function bookingLabel(): string
{
if ($this->planName !== null) {
return $this->planName;
}
if ($this->hasGrandfathered) {
return __('Bestandstarif');
}
if ($this->openSinglePurchases > 0) {
return trans_choice(
'{1}:count Einzelbuchung|[2,*]:count Einzelbuchungen',
$this->openSinglePurchases,
['count' => $this->openSinglePurchases],
);
}
return __('Kein Abo');
}
public function billingIntervalLabel(): ?string
{
return match ($this->billingInterval) {
'monthly' => __('monatlich'),
'yearly' => __('jährlich'),
default => null,
};
}
/**
* Kompakter Text für das Trigger-Badge in der Topbar.
*/
public function badgeLabel(): string
{
$parts = [];
$parts[] = $this->unlimitedQuota
? __(':icon PM', ['icon' => '∞'])
: trans_choice('{0}:count PM|{1}:count PM|[2,*]:count PM', (int) $this->quotaRemaining, ['count' => (int) $this->quotaRemaining]);
if ($this->hasCredits()) {
$parts[] = __(':count Cr', ['count' => $this->creditBalance]);
}
return implode(' · ', $parts);
}
}