presseportale/resources/views/livewire/admin/payments/plans.blade.php
Kevin Adametz 036a53499f Responsive-Härtung: Seiten-Header, Kontextleiste, Stat-Cards
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 14:08:08 +00:00

288 lines
14 KiB
PHP

<?php
use App\Models\Plan;
use App\Services\Billing\StripePlanSyncService;
use Flux\Flux;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Volt\Component;
new #[Layout('components.layouts.app'), Title('Tarife & Pakete')] class extends Component
{
public ?int $editingPlanId = null;
public string $name = '';
public string $monthlyPrice = '';
public string $yearlyPrice = '';
public string $quota = '';
public string $dailyLimit = '';
public bool $isActive = true;
public string $sortOrder = '0';
public ?string $savedMessage = null;
public function edit(int $planId): void
{
$plan = Plan::query()->findOrFail($planId);
$this->editingPlanId = $plan->id;
$this->name = $plan->name;
$this->monthlyPrice = number_format($plan->monthly_price_cents / 100, 2, ',', '');
$this->yearlyPrice = number_format($plan->yearly_price_cents / 100, 2, ',', '');
$this->quota = (string) $plan->press_release_quota;
$this->dailyLimit = $plan->daily_limit === null ? '' : (string) $plan->daily_limit;
$this->isActive = $plan->is_active;
$this->sortOrder = (string) $plan->sort_order;
$this->resetValidation();
Flux::modal('plan-edit')->show();
}
public function save(StripePlanSyncService $stripeSync): void
{
// Deutsche Dezimal-Eingaben (49,00) für die numeric-Regel normalisieren.
$this->monthlyPrice = str_replace(',', '.', trim($this->monthlyPrice));
$this->yearlyPrice = str_replace(',', '.', trim($this->yearlyPrice));
$validated = $this->validate(
[
'name' => ['required', 'string', 'max:120'],
'monthlyPrice' => ['required', 'numeric', 'min:0'],
'yearlyPrice' => ['required', 'numeric', 'min:0'],
'quota' => ['required', 'integer', 'min:0'],
'dailyLimit' => ['nullable', 'integer', 'min:1'],
'sortOrder' => ['required', 'integer', 'min:0'],
],
attributes: [
'name' => __('Name'),
'monthlyPrice' => __('Monatspreis'),
'yearlyPrice' => __('Jahrespreis'),
'quota' => __('PM-Kontingent'),
'dailyLimit' => __('Tageslimit'),
'sortOrder' => __('Sortierung'),
],
);
$plan = Plan::query()->findOrFail($this->editingPlanId);
$plan->fill([
'name' => trim($validated['name']),
'monthly_price_cents' => $this->toCents($validated['monthlyPrice']),
'yearly_price_cents' => $this->toCents($validated['yearlyPrice']),
'press_release_quota' => (int) $validated['quota'],
'daily_limit' => $validated['dailyLimit'] === null || $validated['dailyLimit'] === '' ? null : (int) $validated['dailyLimit'],
'is_active' => $this->isActive,
'sort_order' => (int) $validated['sortOrder'],
]);
$priceChanged = $plan->isDirty(['monthly_price_cents', 'yearly_price_cents']);
$plan->save();
$stripeSync->syncAfterUpdate($plan, $plan->getChanges());
$this->savedMessage = $priceChanged
? __('Tarif „:name" gespeichert. Der neue Preis gilt sofort für neue Buchungen — Bestandsabos behalten ihren bisherigen Preis.', ['name' => $plan->name])
: __('Tarif „:name" gespeichert.', ['name' => $plan->name]);
Flux::modal('plan-edit')->close();
}
/**
* Wandelt eine Preiseingabe (deutsches oder englisches Dezimalformat)
* verlustfrei in Cent um.
*/
private function toCents(string $price): int
{
return (int) round(((float) str_replace(',', '.', $price)) * 100);
}
public function with(): array
{
return [
'plans' => Plan::query()->orderBy('sort_order')->orderBy('id')->get(),
'singlePmPriceCents' => (int) config('billing.single_pm_price_cents'),
'singlePmPriceId' => config('billing.single_pm_stripe_price_id'),
];
}
}; ?>
<div class="space-y-8">
{{-- ============== PAGE HEADER ============== --}}
<header class="page-header">
<div class="min-w-0">
<div class="flex items-center gap-3 mb-3 flex-wrap">
<span class="badge hub dot">{{ __('Admin Backend') }}</span>
<span class="eyebrow muted">{{ __('Administration · Finanzen') }}</span>
</div>
<h1 class="text-[30px] font-bold tracking-[-0.6px] leading-[1.15] m-0 text-[color:var(--color-ink)]">
{{ __('Tarife & Pakete') }}
</h1>
<p class="text-[13px] leading-[1.55] mt-2 m-0 max-w-[720px] text-[color:var(--color-ink-2)]">
{{ __('Preise, Kontingente und Limits der Tarife pflegen. Änderungen erscheinen sofort auf der Buchungs-Seite und werden direkt nach Stripe synchronisiert.') }}
</p>
</div>
<div class="flex items-center gap-2">
<flux:button size="sm" variant="filled" icon="credit-card" :href="route('admin.payments.index')" wire:navigate>
{{ __('Zahlungen') }}
</flux:button>
</div>
</header>
@if ($savedMessage)
<div class="px-4 py-3 rounded-[5px] border text-[12.5px] flex items-start gap-3
bg-[color:var(--color-ok-soft)] border-[color:var(--color-ok)]/30 text-[color:var(--color-ink-2)]">
<flux:icon.check-circle class="size-[16px] flex-shrink-0 mt-0.5 text-[color:var(--color-ok)]" />
<div class="flex-1">{{ $savedMessage }}</div>
</div>
@endif
{{-- ============== HINWEIS STRIPE-PREISLOGIK ============== --}}
<div class="px-4 py-3 rounded-[5px] border text-[12.5px] flex items-start gap-3
bg-[color:var(--color-hub-soft)] border-[color:var(--color-hub-soft-2)] text-[color:var(--color-ink-2)]">
<flux:icon.information-circle class="size-[16px] flex-shrink-0 mt-0.5 text-[color:var(--color-hub)]" />
<div class="flex-1">
{{ __('Stripe-Preise sind unveränderlich: Eine Preisänderung legt automatisch ein neues Preis-Objekt in Stripe an und deaktiviert das alte für neue Buchungen. Laufende Abos behalten ihren bisherigen Preis. Alle Preise sind Netto-Preise — die Umsatzsteuer ergänzt Stripe Tax im Checkout.') }}
</div>
</div>
{{-- ============== TARIF-TABELLE ============== --}}
<article class="panel overflow-hidden">
<div class="panel-head">
<span class="section-eyebrow">{{ __('Tarife') }}</span>
<span class="text-[11.5px] text-[color:var(--color-ink-3)]">
{{ __(':count Tarife', ['count' => $plans->count()]) }}
</span>
</div>
<flux:table>
<flux:table.columns>
<flux:table.column>{{ __('Tarif') }}</flux:table.column>
<flux:table.column>{{ __('Monatlich (netto)') }}</flux:table.column>
<flux:table.column>{{ __('Jährlich (netto)') }}</flux:table.column>
<flux:table.column>{{ __('PM-Kontingent') }}</flux:table.column>
<flux:table.column>{{ __('Tageslimit') }}</flux:table.column>
<flux:table.column>{{ __('Stripe') }}</flux:table.column>
<flux:table.column>{{ __('Status') }}</flux:table.column>
<flux:table.column></flux:table.column>
</flux:table.columns>
@forelse ($plans as $plan)
<flux:table.row wire:key="admin-plan-{{ $plan->id }}">
<flux:table.cell>
<div class="space-y-0.5">
<div class="text-[13px] font-semibold text-[color:var(--color-ink)]">{{ $plan->name }}</div>
<div class="text-[11px] text-[color:var(--color-ink-3)] font-mono">{{ $plan->slug }}</div>
</div>
</flux:table.cell>
<flux:table.cell>
<span class="text-[13px] font-semibold text-[color:var(--color-ink)] tabular-nums">{{ number_format($plan->monthly_price_cents / 100, 2, ',', '.') }} €</span>
</flux:table.cell>
<flux:table.cell>
<span class="text-[13px] font-semibold text-[color:var(--color-ink)] tabular-nums">{{ number_format($plan->yearly_price_cents / 100, 2, ',', '.') }} €</span>
</flux:table.cell>
<flux:table.cell>
<span class="text-[12.5px] text-[color:var(--color-ink-2)]">{{ __(':count PM / Monat', ['count' => $plan->press_release_quota]) }}</span>
</flux:table.cell>
<flux:table.cell>
<span class="text-[12.5px] text-[color:var(--color-ink-2)]">{{ $plan->daily_limit ? __('max. :count / Tag', ['count' => $plan->daily_limit]) : __('ohne') }}</span>
</flux:table.cell>
<flux:table.cell>
@if ($plan->stripe_product_id && $plan->stripe_price_id_monthly && $plan->stripe_price_id_yearly)
<span class="badge ok dot">{{ __('verknüpft') }}</span>
@else
<span class="badge warn dot">{{ __('nicht synchronisiert') }}</span>
@endif
</flux:table.cell>
<flux:table.cell>
@if ($plan->is_active)
<span class="badge ok">{{ __('Aktiv') }}</span>
@else
<span class="badge">{{ __('Inaktiv') }}</span>
@endif
</flux:table.cell>
<flux:table.cell>
<flux:button size="sm" variant="filled" icon="pencil-square" wire:click="edit({{ $plan->id }})">
{{ __('Bearbeiten') }}
</flux:button>
</flux:table.cell>
</flux:table.row>
@empty
<flux:table.row>
<flux:table.cell colspan="8">
<div class="px-4 py-10 text-center text-[13px] text-[color:var(--color-ink-3)]">
{{ __('Noch keine Tarife angelegt. Der Tarif-Katalog wird über den Seeder bzw. die Migrationen befüllt.') }}
</div>
</flux:table.cell>
</flux:table.row>
@endforelse
</flux:table>
</article>
{{-- ============== EINZEL-PM (KONFIGURATION) ============== --}}
<article class="panel">
<div class="panel-head">
<span class="section-eyebrow">{{ __('Einzel-Pressemitteilung') }}</span>
</div>
<div class="p-5 flex items-start gap-3">
<flux:icon.megaphone class="size-[16px] shrink-0 mt-0.5 text-[color:var(--color-hub)]" />
<div class="text-[12.5px] text-[color:var(--color-ink-2)] space-y-1">
<p class="m-0">
{{ __('Preis: :price € netto pro Veröffentlichung.', ['price' => number_format($singlePmPriceCents / 100, 2, ',', '.')]) }}
@if ($singlePmPriceId)
<span class="badge ok ml-2">{{ __('Stripe verknüpft') }}</span>
@else
<span class="badge warn ml-2">{{ __('STRIPE_PRICE_SINGLE_PM fehlt') }}</span>
@endif
</p>
<p class="m-0 text-[12px] text-[color:var(--color-ink-3)]">
{{ __('Der Einzel-PM-Preis wird in config/billing.php bzw. über die ENV-Variable STRIPE_PRICE_SINGLE_PM gepflegt (ein fester Preis, kein Tarif). Eine Änderung erfordert „billing:sync-stripe-plans" mit geleerter ENV-Variable.') }}
</p>
</div>
</div>
</article>
{{-- ============== EDIT-MODAL ============== --}}
<flux:modal name="plan-edit" class="max-w-xl">
<div class="space-y-6">
<div>
<flux:heading size="lg">{{ __('Tarif bearbeiten') }}</flux:heading>
<flux:text class="mt-1">
{{ __('Preisänderungen erzeugen ein neues Stripe-Preis-Objekt und gelten nur für neue Buchungen.') }}
</flux:text>
</div>
<div class="space-y-4">
<flux:input wire:model="name" :label="__('Name')" />
<div class="grid gap-4 sm:grid-cols-2">
<flux:input wire:model="monthlyPrice" :label="__('Monatspreis (netto, €)')" inputmode="decimal" />
<flux:input wire:model="yearlyPrice" :label="__('Jahrespreis (netto, €)')" inputmode="decimal"
:description="__('Konzept: 10 Monatsbeiträge („2 Monate gratis").')" />
</div>
<div class="grid gap-4 sm:grid-cols-3">
<flux:input wire:model="quota" type="number" min="0" :label="__('PM-Kontingent / Monat')" />
<flux:input wire:model="dailyLimit" type="number" min="1" :label="__('Tageslimit')"
:placeholder="__('ohne')" />
<flux:input wire:model="sortOrder" type="number" min="0" :label="__('Sortierung')" />
</div>
<flux:switch wire:model="isActive" :label="__('Tarif aktiv (buchbar und auf der Tarif-Seite sichtbar)')" />
</div>
<div class="flex justify-end gap-2">
<flux:modal.close>
<flux:button variant="filled">{{ __('Abbrechen') }}</flux:button>
</flux:modal.close>
<flux:button variant="primary" wire:click="save">
{{ __('Speichern & mit Stripe abgleichen') }}
</flux:button>
</div>
</div>
</flux:modal>
</div>