Zeige im Zahlungsjournal die Zahlungen je gekauftem Angebot

Filter nach Angebot mit dem Anteil des Angebots an der Bestellung (Preis nach Rabatt), damit eine
Bestellung mit mehreren Angeboten den Umsatz des einzelnen nicht überzeichnet. Zusätzliche CSV-Spalte,
Link von der Angebotsseite.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-11-23 08:16:13 +01:00
1 parent 32272e8e40
commit dd3cc83057
5 files changed
+183 -16

No files matched your search

+39 -6
View File
@@ -123,10 +123,11 @@ public static function range(string $mode, int $year, int $index): array
* @param 'echt'|'test'|'alle' $operation
* @return Collection<int, LedgerEntry>
*/
public function entries(CarbonInterface $from, CarbonInterface $to, string $kind = 'alle', string $provider = 'alle', string $operation = 'echt'): Collection
public function entries(CarbonInterface $from, CarbonInterface $to, string $kind = 'alle', string $provider = 'alle', string $operation = 'echt', ?int $offerId = null): Collection
{
return LedgerEntry::query()
->whereBetween('occurred_at', [$this->utc($from), $this->utc($to)])
->when($offerId !== null, fn ($query) => $query->whereHas('order.lines.offerVersion', fn ($versions) => $versions->where('offer_id', $offerId)))
->when($kind !== 'alle', fn ($query) => $query->where('kind', $kind))
->when($provider !== 'alle', fn ($query) => $query->where('provider', $provider))
->when($operation === 'echt', fn ($query) => $query->where('live', true))
@@ -136,6 +137,33 @@ public function entries(CarbonInterface $from, CarbonInterface $to, string $kind
->get();
}
/**
* Anteil eines Angebots an jeder Bewegung: die Positionen dieses Angebots in der Bestellung (Preis nach Rabatt,
* wie bei der Bestellung festgehalten), bei einer Erstattung negativ. Eine Buchung trägt den Betrag der ganzen
* Bestellung; ohne diese Aufteilung würde eine Bestellung mit mehreren Angeboten den Umsatz des einzelnen
* Angebots überzeichnen. Es gibt nur Vollerstattungen, deshalb ist der Anteil der Erstattung der gespiegelte.
*
* @param Collection<int, LedgerEntry> $entries
* @return array<int, int> Buchungsnummer => Anteil in Cent (mit Vorzeichen der Buchung)
*/
public function shares(Collection $entries, int $offerId): array
{
$perOrder = OrderLine::query()
->whereIn('order_id', $entries->pluck('order_id')->unique()->all())
->whereHas('offerVersion', fn ($versions) => $versions->where('offer_id', $offerId))
->get()
->groupBy('order_id')
->map(fn (Collection $lines): int => (int) $lines->sum(fn (OrderLine $line): int => $line->payable()));
$shares = [];
foreach ($entries as $entry) {
$share = (int) ($perOrder[$entry->order_id] ?? 0);
$shares[$entry->id] = $entry->isRefund() ? -$share : $share;
}
return $shares;
}
/**
* Anzahl der Bewegungen außerhalb des Betriebsfilters, damit ein leerer Echtbetrieb nicht wie „keine Daten“ wirkt.
*/
@@ -148,17 +176,19 @@ public function countByOperation(CarbonInterface $from, CarbonInterface $to): ar
/**
* @param Collection<int, LedgerEntry> $entries
* @param array<int, int>|null $shares Anteile eines Angebots (shares), sonst die ganzen Buchungen
* @return array{eingang: int, erstattung: int, saldo: int, anzahl_eingang: int, anzahl_erstattung: int}
*/
public static function totals(Collection $entries): array
public static function totals(Collection $entries, ?array $shares = null): array
{
$in = $entries->where('kind', LedgerEntry::KIND_PAYMENT);
$out = $entries->where('kind', LedgerEntry::KIND_REFUND);
$amount = fn (LedgerEntry $entry): int => $shares === null ? $entry->amount_cents : (int) ($shares[$entry->id] ?? 0);
return [
'eingang' => (int) $in->sum('amount_cents'),
'erstattung' => (int) abs($out->sum('amount_cents')),
'saldo' => (int) $entries->sum('amount_cents'),
'eingang' => (int) $in->sum($amount),
'erstattung' => (int) abs($out->sum($amount)),
'saldo' => (int) $entries->sum($amount),
'anzahl_eingang' => $in->count(),
'anzahl_erstattung' => $out->count(),
];
@@ -189,13 +219,15 @@ public static function hasTime(LedgerEntry $entry): bool
* Semikolon-getrennt, Dezimalkomma, UTF-8 mit BOM: öffnet in Excel und bei der Steuerberatung ohne Umweg.
*
* @param Collection<int, LedgerEntry> $entries
* @param array<int, int>|null $shares Mit Angebotsfilter eine zusätzliche Spalte „Anteil des Angebots“
*/
public static function csv(Collection $entries): string
public static function csv(Collection $entries, ?array $shares = null): string
{
$header = [
'Buchungsnummer', 'Datum', 'Uhrzeit', 'Art', 'Betrag EUR', 'Währung', 'Bestellung', 'Zahlungsweg', 'Betrieb', 'Herkunft',
'Referenz', 'Zahlungsvorgang', 'Status beim Anbieter', 'Lizenzinhaber', 'Rechnungsempfänger', 'Zusatz', 'Straße', 'PLZ', 'Ort', 'Land',
'Besteller', 'Zwischensumme EUR', 'Rabatt EUR', 'Rabattcode', 'Positionen', 'Begründung', 'Gebucht von', 'Gebucht am',
...($shares === null ? [] : ['Anteil des Angebots EUR']),
];
$rows = [$header];
@@ -235,6 +267,7 @@ public static function csv(Collection $entries): string
$entry->reason,
$entry->actor_name,
self::local($entry->created_at)->format('d.m.Y H:i:s'),
...($shares === null ? [] : [self::decimal((int) ($shares[$entry->id] ?? 0))]),
];
}
+20 -6
View File
@@ -2,6 +2,7 @@
namespace App\Filament\Pages;
use App\Domain\Catalog\Offer;
use App\Domain\Commerce\LedgerEntry;
use App\Domain\Commerce\PaymentJournal;
use App\Filament\Support\ShopAccess;
@@ -50,6 +51,10 @@ class Zahlungsjournal extends Page
#[Url(as: 'betrieb')]
public string $betrieb = 'echt';
/** 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();
@@ -95,6 +100,7 @@ private function normalize(): void
$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 : 'echt';
$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)));
@@ -126,13 +132,18 @@ public function daten(): array
{
[$from, $to] = $this->range();
$journal = app(PaymentJournal::class);
$entries = $journal->entries($from, $to, $this->art, $this->weg, $this->betrieb);
$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(),
'summen' => PaymentJournal::totals($entries),
'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(),
@@ -144,17 +155,20 @@ public function exportieren(): StreamedResponse
abort_unless(ShopAccess::allowed(), 403);
[$from, $to] = $this->range();
$entries = app(PaymentJournal::class)->entries($from, $to, $this->art, $this->weg, $this->betrieb);
$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'
}.'.csv';
}.($offerId === null ? '' : '-angebot-'.$offerId).'.csv';
return response()->streamDownload(function () use ($entries): void {
echo PaymentJournal::csv($entries);
return response()->streamDownload(function () use ($entries, $shares): void {
echo PaymentJournal::csv($entries, $shares);
}, $name, ['Content-Type' => 'text/csv; charset=UTF-8']);
}
}
@@ -18,11 +18,13 @@
use App\Domain\Cms\Catalog\PublicCatalogReader;
use App\Domain\Commerce\Money;
use App\Domain\Commerce\ProductOptions;
use App\Filament\Pages\Zahlungsjournal;
use App\Filament\Resources\Offers\OfferResource;
use App\Filament\Support\CatalogLabels;
use App\Filament\Support\CatalogOptions;
use App\Filament\Support\PublicationReadiness;
use App\Filament\Support\ReportsDomainRules;
use App\Filament\Support\ShopAccess;
use Filament\Actions\Action;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Select;
@@ -366,6 +368,11 @@ private function angebotDaten(): array
'wire' => "mountAction('end')",
'art' => 'line',
]] : []),
...(ShopAccess::allowed() ? [[
'label' => 'Zahlungen zu diesem Angebot',
'href' => Zahlungsjournal::getUrl(['angebot' => $offer->id, 'zeitraum' => 'jahr', 'betrieb' => 'alle']),
'art' => 'quiet',
]] : []),
],
],
'voraussetzungen' => app(PublicationReadiness::class)->forOffer($offer),