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 */ 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']); } }