Files
laraundfelix/app/Filament/Pages/Zahlungsjournal.php
T
LarsandClaude Sonnet 5.5 3f9e463dfa
CI Feature / test (push) Successful in 3m41s
Deploy Test / test (push) Successful in 3m39s
Deploy Test / deploy (push) Successful in 40s
Zeige im Zahlungsjournal ausserhalb von Prod standardmaessig die Testzahlungen
Auf Prod bleibt die Ansicht bei den echten Zahlungen. Die Markierung der Buchung
(live) folgt weiter dem Modus des Zahlungsanbieters und nicht der Umgebung.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-23 10:43:18 +01:00

183 lines
7.0 KiB
PHP

<?php
namespace App\Filament\Pages;
use App\Domain\Catalog\Offer;
use App\Domain\Commerce\LedgerEntry;
use App\Domain\Commerce\PaymentJournal;
use App\Filament\Support\ShopAccess;
use Carbon\CarbonImmutable;
use Filament\Pages\Page;
use Filament\Support\Enums\Width;
use Illuminate\Contracts\Support\Htmlable;
use Livewire\Attributes\Url;
use Symfony\Component\HttpFoundation\StreamedResponse;
/**
* Einnahmen und Erstattungen (Zahlungsjournal): Jede Geldbewegung steht unveränderlich mit Datum, Betrag, Beleg beim
* Anbieter, handelnder Person, Begründung und Zahler da. Die Seite liest nur; es gibt nichts zu bearbeiten oder zu
* löschen. Standardmäßig zählen nur echte Zahlungen; Testzahlungen sind ein eigener Filter und fließen nie in die
* Summen echter Einnahmen. Der Export (CSV) enthält alle Zeilen des gewählten Zeitraums und Filters.
*/
class Zahlungsjournal extends Page
{
protected static string|\UnitEnum|null $navigationGroup = 'Verkauf';
protected static ?string $navigationLabel = 'Einnahmen und Erstattungen';
protected static ?int $navigationSort = 2;
protected static ?string $slug = 'zahlungsjournal';
protected static ?string $title = 'Einnahmen und Erstattungen';
protected string $view = 'filament.pages.zahlungsjournal';
#[Url(as: 'zeitraum')]
public string $modus = 'monat';
#[Url(as: 'jahr')]
public ?int $jahr = null;
#[Url(as: 'nr')]
public ?int $nr = null;
#[Url(as: 'art')]
public string $art = 'alle';
#[Url(as: 'weg')]
public string $weg = 'alle';
/** Ohne Angabe: auf Prod die echten Zahlungen, auf jeder anderen Instanz (Test, lokal) die Testzahlungen. */
#[Url(as: 'betrieb')]
public ?string $betrieb = null;
/** Kennung eines Angebots oder leer für alle (Zeichenkette, damit die Auswahl „Alle“ leer sein kann). */
#[Url(as: 'angebot')]
public string $angebot = '';
public static function canAccess(): bool
{
return ShopAccess::allowed();
}
public function getHeading(): string|Htmlable|null
{
return null;
}
public function getMaxContentWidth(): Width|string|null
{
return Width::Full;
}
public function mount(): void
{
$now = CarbonImmutable::now(PaymentJournal::TIMEZONE);
$this->jahr ??= $now->year;
$this->nr ??= $this->modus === 'quartal' ? $now->quarter : $now->month;
$this->betrieb ??= self::defaultBetrieb();
$this->normalize();
}
/** Nur Prod zeigt standardmäßig echte Zahlungen; auf anderen Instanzen entstehen Buchungen nur aus der Sandbox. */
public static function defaultBetrieb(): string
{
return app()->isProduction() ? 'echt' : 'test';
}
/** Beim Wechsel zwischen Monat und Quartal bleibt der gewählte Zeitraum erhalten. */
public function updatingModus(string $value): void
{
if ($this->modus === 'monat' && $value === 'quartal') {
$this->nr = (int) ceil(((int) $this->nr) / 3);
} elseif ($this->modus === 'quartal' && $value === 'monat') {
$this->nr = (((int) $this->nr) - 1) * 3 + 1;
}
}
public function updated(): void
{
$this->normalize();
}
/** Ungültige Eingaben aus der Adresszeile fallen auf zulässige Werte zurück. */
private function normalize(): void
{
$this->modus = in_array($this->modus, ['monat', 'quartal', 'jahr'], true) ? $this->modus : 'monat';
$this->art = in_array($this->art, ['alle', LedgerEntry::KIND_PAYMENT, LedgerEntry::KIND_REFUND], true) ? $this->art : 'alle';
$this->weg = in_array($this->weg, ['alle', 'stripe', 'ueberweisung', 'test'], true) ? $this->weg : 'alle';
$this->betrieb = in_array($this->betrieb, ['echt', 'test', 'alle'], true) ? $this->betrieb : self::defaultBetrieb();
$this->angebot = ctype_digit($this->angebot) && Offer::query()->whereKey((int) $this->angebot)->exists() ? $this->angebot : '';
$this->jahr = max(2020, min((int) CarbonImmutable::now(PaymentJournal::TIMEZONE)->year + 1, (int) ($this->jahr ?? 2026)));
$max = $this->modus === 'quartal' ? 4 : 12;
$this->nr = max(1, min($max, (int) ($this->nr ?? 1)));
}
/**
* @return array{0: CarbonImmutable, 1: CarbonImmutable}
*/
private function range(): array
{
return PaymentJournal::range($this->modus, (int) $this->jahr, (int) $this->nr);
}
public function zeitraumName(): string
{
$months = [1 => 'Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'];
return match ($this->modus) {
'monat' => $months[(int) $this->nr].' '.$this->jahr,
'quartal' => $this->nr.'. Quartal '.$this->jahr,
default => 'Jahr '.$this->jahr,
};
}
/**
* @return array<string, mixed>
*/
public function daten(): array
{
[$from, $to] = $this->range();
$journal = app(PaymentJournal::class);
$offerId = $this->angebot === '' ? null : (int) $this->angebot;
$entries = $journal->entries($from, $to, $this->art, $this->weg, $this->betrieb, $offerId);
$shares = $offerId === null ? null : $journal->shares($entries, $offerId);
return [
'zeitraum' => $this->zeitraumName(),
'eintraege' => $entries->take(PaymentJournal::DISPLAY_LIMIT),
'anzahl' => $entries->count(),
'angebote' => Offer::query()->orderBy('name')->pluck('name', 'id')->all(),
'angebotName' => $offerId === null ? null : Offer::query()->find($offerId)?->name,
'anteile' => $shares,
'summen' => PaymentJournal::totals($entries, $shares),
'nachBetrieb' => $journal->countByOperation($from, $to),
'ohneAdresse' => $entries->filter(fn (LedgerEntry $entry): bool => $entry->kind === LedgerEntry::KIND_PAYMENT && empty(($entry->payer ?? [])['billing']))->count(),
'nachgetragen' => $entries->where('derived', true)->count(),
];
}
public function exportieren(): StreamedResponse
{
abort_unless(ShopAccess::allowed(), 403);
[$from, $to] = $this->range();
$journal = app(PaymentJournal::class);
$offerId = $this->angebot === '' ? null : (int) $this->angebot;
$entries = $journal->entries($from, $to, $this->art, $this->weg, $this->betrieb, $offerId);
$shares = $offerId === null ? null : $journal->shares($entries, $offerId);
$name = 'zahlungsjournal-'.match ($this->modus) {
'monat' => $this->jahr.'-'.str_pad((string) $this->nr, 2, '0', STR_PAD_LEFT),
'quartal' => $this->jahr.'-Q'.$this->nr,
default => (string) $this->jahr,
}.'-'.match ($this->betrieb) {
'echt' => 'echt', 'test' => 'test', default => 'alle'
}.($offerId === null ? '' : '-angebot-'.$offerId).'.csv';
return response()->streamDownload(function () use ($entries, $shares): void {
echo PaymentJournal::csv($entries, $shares);
}, $name, ['Content-Type' => 'text/csv; charset=UTF-8']);
}
}