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>
546 lines
28 KiB
PHP
546 lines
28 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Domain\Catalog\DomainRuleException;
|
|
use App\Domain\Commerce\Checkout;
|
|
use App\Domain\Commerce\LedgerEntry;
|
|
use App\Domain\Commerce\Order;
|
|
use App\Domain\Commerce\Payment;
|
|
use App\Domain\Commerce\PaymentJournal;
|
|
use App\Domain\Entitlement\Grant;
|
|
use App\Domain\Entitlement\GrantStatus;
|
|
use App\Domain\Identity\BillingAddress;
|
|
use App\Filament\Pages\Zahlungsjournal;
|
|
use App\Filament\Resources\Offers\Pages\ViewOffer;
|
|
use App\Filament\Resources\Orders\Pages\ViewOrder;
|
|
use App\Models\User;
|
|
use Carbon\CarbonImmutable;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Testing\TestResponse;
|
|
use Livewire\Livewire;
|
|
use LogicException;
|
|
use Tests\Support\BuildsShop;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* Zahlungsjournal: unveränderlicher Nachweis aller Einnahmen und Erstattungen für die Verwaltung und das Finanzamt.
|
|
* Alle Aufrufe an Stripe sind gefälscht (Http::fake).
|
|
*/
|
|
class PaymentJournalTest extends TestCase
|
|
{
|
|
use BuildsShop;
|
|
use RefreshDatabase;
|
|
|
|
private const WEBHOOK_SECRET = 'whsec_testgeheimnis_nur_fuer_tests';
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->seedShop();
|
|
config([
|
|
'services.payment.driver' => 'stripe',
|
|
'services.stripe.secret' => 'sk_test_nur_fuer_tests',
|
|
'services.stripe.webhook_secret' => self::WEBHOOK_SECRET,
|
|
'services.stripe.api_base' => 'https://stripe.test',
|
|
]);
|
|
$this->travelTo('2026-11-24 10:00:00');
|
|
}
|
|
|
|
private function staff(string $state = 'katalogverwalter'): User
|
|
{
|
|
$user = User::factory()->{$state}()->create();
|
|
$user->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
|
|
|
|
return $user;
|
|
}
|
|
|
|
private function address(User $user, string $recipient = 'Tina Trainerin'): void
|
|
{
|
|
BillingAddress::query()->updateOrCreate(['party_id' => $user->party_id], [
|
|
'recipient' => $recipient, 'street' => 'Hauptstraße 1', 'postal_code' => '50667', 'city' => 'Köln', 'country' => 'DE',
|
|
]);
|
|
}
|
|
|
|
private function paidEvent(Order $order, string $session, int $amount, bool $live, int $created, string $intent = 'pi_test_1'): string
|
|
{
|
|
return json_encode([
|
|
'id' => 'evt_'.$session, 'object' => 'event', 'type' => 'checkout.session.completed', 'livemode' => $live, 'created' => $created,
|
|
'data' => ['object' => [
|
|
'id' => $session, 'object' => 'checkout.session', 'client_reference_id' => (string) $order->id,
|
|
'payment_status' => 'paid', 'amount_total' => $amount, 'currency' => 'eur', 'payment_intent' => $intent,
|
|
'metadata' => ['order_id' => (string) $order->id],
|
|
]],
|
|
], JSON_THROW_ON_ERROR);
|
|
}
|
|
|
|
private function webhook(string $payload): TestResponse
|
|
{
|
|
$timestamp = now()->getTimestamp();
|
|
$signature = 't='.$timestamp.',v1='.hash_hmac('sha256', $timestamp.'.'.$payload, self::WEBHOOK_SECRET);
|
|
|
|
return $this->call('POST', route('stripe.webhook'), [], [], [], ['CONTENT_TYPE' => 'application/json', 'HTTP_STRIPE_SIGNATURE' => $signature], $payload);
|
|
}
|
|
|
|
/** Eine bezahlte Stripe-Bestellung (Standard: Tina, 49 €) mit Rechnungsadresse. */
|
|
private function paidStripeOrder(bool $live = true, string $session = 'cs_test_1', ?User $buyer = null, ?string $offer = null, int $amount = 4900): Order
|
|
{
|
|
$buyer ??= $this->tina();
|
|
$this->address($buyer, $buyer->name);
|
|
$order = $this->orderOf($buyer, [$offer === 'grenzen' ? $this->privateGrenzen() : $this->privateKonflikte()]);
|
|
$this->webhook($this->paidEvent($order, $session, $amount, $live, CarbonImmutable::parse('2026-11-24 09:30:00', 'UTC')->getTimestamp()))->assertOk();
|
|
|
|
return $order->fresh();
|
|
}
|
|
|
|
public function test_a_stripe_payment_creates_one_entry_with_provider_time_references_and_payer_snapshot(): void
|
|
{
|
|
$order = $this->paidStripeOrder();
|
|
|
|
$entry = LedgerEntry::query()->sole();
|
|
$this->assertSame(LedgerEntry::KIND_PAYMENT, $entry->kind);
|
|
$this->assertSame(4900, $entry->amount_cents);
|
|
$this->assertTrue($entry->live, 'Stripe meldet livemode je Ereignis.');
|
|
$this->assertSame('stripe', $entry->provider);
|
|
$this->assertSame('cs_test_1', $entry->provider_reference);
|
|
$this->assertSame('pi_test_1', $entry->provider_payment_ref);
|
|
$this->assertSame('2026-11-24 09:30:00', $entry->occurred_at->utc()->format('Y-m-d H:i:s'), 'Es gilt der Zeitpunkt von Stripe, nicht der der Verarbeitung.');
|
|
$this->assertSame($order->id, $entry->order_id);
|
|
$this->assertSame($this->tina()->name, $entry->payer['billing']['recipient']);
|
|
$this->assertSame(4900, $entry->order_snapshot['subtotal_cents']);
|
|
$this->assertFalse($entry->derived);
|
|
}
|
|
|
|
public function test_a_repeated_webhook_adds_no_second_entry(): void
|
|
{
|
|
$order = $this->paidStripeOrder();
|
|
$payload = $this->paidEvent($order, 'cs_test_1', 4900, true, now()->getTimestamp());
|
|
|
|
$this->webhook($payload)->assertOk();
|
|
$this->webhook($payload)->assertOk();
|
|
|
|
$this->assertSame(1, LedgerEntry::query()->count());
|
|
}
|
|
|
|
public function test_the_database_refuses_a_second_entry_for_the_same_payment(): void
|
|
{
|
|
$this->paidStripeOrder();
|
|
$entry = LedgerEntry::query()->sole();
|
|
|
|
$this->expectException(QueryException::class);
|
|
DB::table('payment_ledger')->insert($entry->only(['kind', 'order_id', 'provider', 'provider_reference', 'occurred_at', 'amount_cents']) + ['payer' => '{}', 'order_snapshot' => '{}', 'occurred_at' => now()]);
|
|
}
|
|
|
|
public function test_a_later_change_of_the_billing_address_does_not_change_the_entry(): void
|
|
{
|
|
$order = $this->paidStripeOrder();
|
|
|
|
$order->party->billingAddress->update(['recipient' => 'Ganz Andere GmbH', 'street' => 'Neuweg 9']);
|
|
|
|
$entry = LedgerEntry::query()->sole();
|
|
$this->assertSame($this->tina()->name, $entry->payer['billing']['recipient']);
|
|
$this->assertSame('Hauptstraße 1', $entry->payer['billing']['street']);
|
|
}
|
|
|
|
public function test_an_invoice_confirmation_stores_the_given_date_and_refuses_impossible_ones(): void
|
|
{
|
|
$vera = $this->vera();
|
|
BillingAddress::query()->create([
|
|
'party_id' => $this->club()->id, 'recipient' => 'Testverein Musterstadt e. V.', 'street' => 'Vereinsweg 2',
|
|
'postal_code' => '80331', 'city' => 'München', 'country' => 'DE',
|
|
]);
|
|
$order = $this->orderOf($vera, [$this->clubKonflikte()], $this->club()->id);
|
|
$order->update(['payment_method' => 'rechnung']);
|
|
$operator = $this->tina();
|
|
$this->travelTo('2026-12-02 08:00:00');
|
|
|
|
foreach (['2026-12-03' => 'in der Zukunft', '2026-11-20' => 'vor dem Bestelldatum'] as $date => $why) {
|
|
try {
|
|
app(Checkout::class)->confirmInvoicePayment($order, 'RE-1', $operator, $date);
|
|
$this->fail('Ein Datum '.$why.' muss abgewiesen werden.');
|
|
} catch (DomainRuleException) {
|
|
$this->assertSame('offen', $order->fresh()->status);
|
|
$this->assertSame(0, LedgerEntry::query()->count());
|
|
}
|
|
}
|
|
|
|
app(Checkout::class)->confirmInvoicePayment($order, 'RE-2026-014', $operator, '2026-11-30');
|
|
|
|
$entry = LedgerEntry::query()->sole();
|
|
$this->assertSame('ueberweisung', $entry->provider);
|
|
$this->assertSame('rechnung-RE-2026-014', $entry->provider_reference);
|
|
$this->assertSame('2026-11-30', PaymentJournal::local($entry->occurred_at)->format('Y-m-d'), 'Der Tag des Geldeingangs, nicht der der Bestätigung.');
|
|
$this->assertFalse(PaymentJournal::hasTime($entry));
|
|
$this->assertSame($operator->id, $entry->actor_id);
|
|
$this->assertSame($operator->name, $entry->actor_name);
|
|
$this->assertSame('Testverein Musterstadt e. V.', $entry->payer['billing']['recipient']);
|
|
$this->assertFalse($entry->live, 'Ohne Produktivfreigabe ist es kein echtes Geld.');
|
|
|
|
[$novFrom, $novTo] = PaymentJournal::range('monat', 2026, 11);
|
|
[$decFrom, $decTo] = PaymentJournal::range('monat', 2026, 12);
|
|
$journal = app(PaymentJournal::class);
|
|
$this->assertCount(1, $journal->entries($novFrom, $novTo, 'alle', 'alle', 'alle'), 'Die Einnahme zählt im November, nicht im Dezember der Bestätigung.');
|
|
$this->assertCount(0, $journal->entries($decFrom, $decTo, 'alle', 'alle', 'alle'));
|
|
}
|
|
|
|
public function test_a_stripe_refund_is_booked_with_refund_id_status_actor_and_reason(): void
|
|
{
|
|
$order = $this->paidStripeOrder();
|
|
$actor = $this->staff('kundenbetreuung');
|
|
$refundedAt = CarbonImmutable::parse('2026-11-25 14:05:00', 'UTC');
|
|
Http::fake(['stripe.test/v1/refunds' => Http::response(['id' => 're_test_9', 'status' => 'pending', 'created' => $refundedAt->getTimestamp()])]);
|
|
|
|
app(Checkout::class)->refund($order, 'Widerruf geprüft, Frist gewahrt', null, $actor);
|
|
|
|
$this->assertSame(2, LedgerEntry::query()->count());
|
|
$refund = LedgerEntry::query()->where('kind', LedgerEntry::KIND_REFUND)->sole();
|
|
$this->assertSame(-4900, $refund->amount_cents);
|
|
$this->assertSame('re_test_9', $refund->provider_reference);
|
|
$this->assertSame('pending', $refund->provider_status, 'Der Status wird so festgehalten, wie Stripe ihn meldet.');
|
|
$this->assertSame('pi_test_1', $refund->provider_payment_ref);
|
|
$this->assertSame('2026-11-25 14:05:00', $refund->occurred_at->utc()->format('Y-m-d H:i:s'));
|
|
$this->assertSame($actor->id, $refund->actor_id);
|
|
$this->assertSame('Widerruf geprüft, Frist gewahrt', $refund->reason);
|
|
$this->assertTrue($refund->live, 'Die Erstattung folgt dem Zahlungseingang.');
|
|
$this->assertSame($this->tina()->name, $refund->payer['billing']['recipient']);
|
|
$this->assertSame(0, PaymentJournal::totals(LedgerEntry::query()->get())['saldo']);
|
|
$this->assertSame('erstattet', $order->fresh()->status);
|
|
}
|
|
|
|
public function test_a_failed_stripe_refund_books_nothing_and_changes_nothing(): void
|
|
{
|
|
$order = $this->paidStripeOrder();
|
|
|
|
foreach ([
|
|
Http::response(['error' => ['type' => 'invalid_request_error', 'message' => 'x']], 400),
|
|
Http::response(['id' => 're_x', 'status' => 'failed']),
|
|
] as $answer) {
|
|
Http::fake(['stripe.test/v1/refunds' => $answer]);
|
|
|
|
try {
|
|
app(Checkout::class)->refund($order, 'Widerruf geprüft');
|
|
$this->fail('Ohne wirksame Rückzahlung bei Stripe darf nichts gebucht werden.');
|
|
} catch (DomainRuleException) {
|
|
$this->assertSame(1, LedgerEntry::query()->count());
|
|
$this->assertSame('bezahlt', $order->fresh()->status);
|
|
$this->assertSame('bestaetigt', $order->payments()->firstOrFail()->status);
|
|
$this->assertSame(1, Grant::query()->where('status', GrantStatus::Aktiv)->count());
|
|
}
|
|
}
|
|
}
|
|
|
|
public function test_a_manual_refund_books_the_given_day_and_reference(): void
|
|
{
|
|
$vera = $this->vera();
|
|
$order = $this->orderOf($vera, [$this->clubKonflikte()], $this->club()->id);
|
|
$order->update(['payment_method' => 'rechnung']);
|
|
app(Checkout::class)->confirmInvoicePayment($order, 'RE-9', $this->tina());
|
|
$this->travelTo('2026-12-05 08:00:00');
|
|
Http::fake();
|
|
|
|
app(Checkout::class)->refund($order, 'Storno nach Rücksprache', null, $this->tina(), '2026-12-04', 'GS-2026-02');
|
|
|
|
Http::assertNothingSent();
|
|
$refund = LedgerEntry::query()->where('kind', LedgerEntry::KIND_REFUND)->sole();
|
|
$this->assertSame('erstattung-GS-2026-02', $refund->provider_reference);
|
|
$this->assertSame('2026-12-04', PaymentJournal::local($refund->occurred_at)->format('Y-m-d'));
|
|
$this->assertSame('Storno nach Rücksprache', $refund->reason);
|
|
$this->assertSame(-$order->payable(), $refund->amount_cents);
|
|
}
|
|
|
|
public function test_the_operator_books_invoice_receipt_and_refund_through_the_order_page_with_date_and_reference(): void
|
|
{
|
|
$vera = $this->vera();
|
|
$order = $this->orderOf($vera, [$this->clubKonflikte()], $this->club()->id);
|
|
$order->update(['payment_method' => 'rechnung']);
|
|
$staff = $this->staff('kundenbetreuung');
|
|
$this->travelTo('2026-12-02 08:00:00');
|
|
|
|
Livewire::actingAs($staff, 'staff')->test(ViewOrder::class, ['record' => $order->getKey()])
|
|
->mountAction('confirmInvoice')
|
|
->fillForm(['reference' => 'RE-2026-020', 'received_on' => '2026-11-27'])
|
|
->callMountedAction()->assertHasNoActionErrors();
|
|
|
|
Livewire::actingAs($staff, 'staff')->test(ViewOrder::class, ['record' => $order->getKey()])
|
|
->mountAction('refund')
|
|
->fillForm(['reason' => 'Storno', 'refunded_on' => '2026-12-01', 'refund_reference' => 'GS-1'])
|
|
->callMountedAction()->assertHasNoActionErrors();
|
|
|
|
$in = LedgerEntry::query()->where('kind', LedgerEntry::KIND_PAYMENT)->sole();
|
|
$out = LedgerEntry::query()->where('kind', LedgerEntry::KIND_REFUND)->sole();
|
|
$this->assertSame('2026-11-27', PaymentJournal::local($in->occurred_at)->format('Y-m-d'));
|
|
$this->assertSame('2026-12-01', PaymentJournal::local($out->occurred_at)->format('Y-m-d'));
|
|
$this->assertSame($staff->id, $out->actor_id);
|
|
}
|
|
|
|
public function test_test_payments_are_not_revenue_in_totals_or_the_default_view(): void
|
|
{
|
|
$this->paidStripeOrder(true, 'cs_live_1');
|
|
config(['services.payment.driver' => 'test']);
|
|
$test = $this->orderOf($this->tina(), [$this->privateGrenzen()]);
|
|
app(Checkout::class)->confirmTestPayment($test, 'test-grenzen');
|
|
config(['services.payment.driver' => 'stripe']);
|
|
$this->paidStripeOrder(false, 'cs_test_2', $this->vera());
|
|
[$from, $to] = PaymentJournal::range('monat', 2026, 11);
|
|
$journal = app(PaymentJournal::class);
|
|
|
|
$this->assertSame(3, LedgerEntry::query()->count());
|
|
$this->assertSame(4900, PaymentJournal::totals($journal->entries($from, $to))['eingang'], 'Standard: nur echte Zahlungen.');
|
|
$this->assertSame(2900 + 4900, PaymentJournal::totals($journal->entries($from, $to, 'alle', 'alle', 'test'))['eingang']);
|
|
$this->assertSame(4900 + 2900 + 4900, PaymentJournal::totals($journal->entries($from, $to, 'alle', 'alle', 'alle'))['eingang']);
|
|
$this->assertCount(1, $journal->entries($from, $to, 'alle', 'test', 'test'), 'Filter nach Zahlungsweg.');
|
|
$this->assertSame(['echt' => 1, 'test' => 2], $journal->countByOperation($from, $to));
|
|
}
|
|
|
|
public function test_period_boundaries_follow_german_time(): void
|
|
{
|
|
$journal = app(PaymentJournal::class);
|
|
$order = $this->orderOf($this->tina(), [$this->privateKonflikte()]);
|
|
$payment = $order->payments()->create(['provider' => 'stripe', 'status' => 'bestaetigt', 'provider_reference' => 'cs_grenze']);
|
|
|
|
// 30.11.2026 23:30 UTC ist in Deutschland bereits der 1.12.2026, 00:30 Uhr.
|
|
$journal->recordPayment($order, $payment, CarbonImmutable::parse('2026-11-30 23:30:00', 'UTC'), null, true);
|
|
|
|
[$novFrom, $novTo] = PaymentJournal::range('monat', 2026, 11);
|
|
[$decFrom, $decTo] = PaymentJournal::range('monat', 2026, 12);
|
|
[$q4From, $q4To] = PaymentJournal::range('quartal', 2026, 4);
|
|
[$yFrom, $yTo] = PaymentJournal::range('jahr', 2026, 1);
|
|
$this->assertCount(0, $journal->entries($novFrom, $novTo));
|
|
$this->assertCount(1, $journal->entries($decFrom, $decTo));
|
|
$this->assertCount(1, $journal->entries($q4From, $q4To));
|
|
$this->assertCount(1, $journal->entries($yFrom, $yTo));
|
|
$this->assertSame('2026-12-01 00:00:00', PaymentJournal::local($decFrom)->format('Y-m-d H:i:s'));
|
|
}
|
|
|
|
public function test_the_csv_has_german_format_references_recipient_and_guards_against_formulas(): void
|
|
{
|
|
$tina = $this->tina();
|
|
$tina->party->update(['display_name' => '=HYPERLINK("http://x")']);
|
|
$this->address($tina, '+Verein; "Quoted"');
|
|
$order = $this->orderOf($tina, [$this->privateKonflikte()]);
|
|
$this->webhook($this->paidEvent($order, 'cs_csv', 4900, true, CarbonImmutable::parse('2026-11-24 09:30:00', 'UTC')->getTimestamp(), 'pi_csv'))->assertOk();
|
|
Http::fake(['stripe.test/v1/refunds' => Http::response(['id' => 're_csv', 'status' => 'succeeded', 'created' => CarbonImmutable::parse('2026-11-26 10:00:00', 'UTC')->getTimestamp()])]);
|
|
app(Checkout::class)->refund($order->fresh(), 'Widerruf geprüft', null, $this->staff('kundenbetreuung'));
|
|
[$from, $to] = PaymentJournal::range('monat', 2026, 11);
|
|
|
|
$csv = PaymentJournal::csv(app(PaymentJournal::class)->entries($from, $to));
|
|
|
|
$this->assertStringStartsWith("\xEF\xBB\xBFBuchungsnummer;Datum;Uhrzeit;Art;\"Betrag EUR\";", $csv);
|
|
$lines = array_values(array_filter(explode("\r\n", $csv)));
|
|
$this->assertCount(3, $lines, 'Kopfzeile, Zahlung, Erstattung.');
|
|
$this->assertStringContainsString(';24.11.2026;10:30:00;Zahlungseingang;49,00;EUR;'.$order->id.';Stripe;echt;belegt;cs_csv;pi_csv;', $lines[1]);
|
|
$this->assertStringContainsString(';26.11.2026;11:00:00;Erstattung;-49,00;EUR;', $lines[2]);
|
|
$this->assertStringContainsString(';re_csv;pi_csv;succeeded;', $lines[2]);
|
|
$this->assertStringContainsString("'=HYPERLINK", $lines[1], 'Eine Zelle, die mit = beginnt, wird nicht zur Formel.');
|
|
$this->assertStringContainsString('"\'+Verein; ""Quoted"""', $lines[1], 'Semikolon und Anführungszeichen im Empfänger bleiben in einer Zelle.');
|
|
$this->assertStringContainsString('Widerruf geprüft', $lines[2]);
|
|
}
|
|
|
|
public function test_entries_cannot_be_changed_or_deleted(): void
|
|
{
|
|
$this->paidStripeOrder();
|
|
$entry = LedgerEntry::query()->sole();
|
|
|
|
try {
|
|
$entry->update(['amount_cents' => 1]);
|
|
$this->fail('Ein Eintrag darf nicht geändert werden.');
|
|
} catch (LogicException) {
|
|
}
|
|
|
|
try {
|
|
$entry->delete();
|
|
$this->fail('Ein Eintrag darf nicht gelöscht werden.');
|
|
} catch (LogicException) {
|
|
}
|
|
|
|
$this->assertSame(4900, $entry->fresh()->amount_cents);
|
|
$this->assertSame(1, LedgerEntry::query()->count());
|
|
}
|
|
|
|
public function test_an_order_with_journal_entries_cannot_be_deleted(): void
|
|
{
|
|
$order = $this->paidStripeOrder();
|
|
|
|
$this->expectException(QueryException::class);
|
|
DB::table('orders')->where('id', $order->id)->delete();
|
|
}
|
|
|
|
public function test_the_page_shows_totals_and_entries_and_hides_test_money_by_default(): void
|
|
{
|
|
$this->paidStripeOrder(true, 'cs_live_1');
|
|
$this->paidStripeOrder(false, 'cs_test_2', $this->vera(), 'grenzen', 2900);
|
|
$staff = $this->staff('kundenbetreuung');
|
|
|
|
Livewire::actingAs($staff, 'staff')->test(Zahlungsjournal::class)
|
|
->assertSee('Einnahmen und Erstattungen')
|
|
->assertSee('November 2026')
|
|
->assertSee('49,00 €')
|
|
->assertSee('cs_live_1')
|
|
->assertDontSee('cs_test_2')
|
|
->assertSee('In diesem Zeitraum gibt es zusätzlich 1 Testbewegung')
|
|
->assertSee($this->tina()->name)
|
|
->set('betrieb', 'test')
|
|
->assertSee('cs_test_2')
|
|
->assertDontSee('cs_live_1')
|
|
->assertSee('Test, kein Umsatz')
|
|
->assertSee('Testzahlungen sind kein Umsatz')
|
|
->set('betrieb', 'echt')
|
|
->set('modus', 'jahr')
|
|
->assertSee('Jahr 2026')
|
|
->set('modus', 'monat')
|
|
->set('nr', 10)
|
|
->assertSee('Keine Bewegungen');
|
|
}
|
|
|
|
/** Bestellung mit zwei Angeboten (49 € mit 4 € Rabatt und 29 €), bezahlt über Stripe. */
|
|
private function paidTwoLineOrder(string $session = 'cs_two'): Order
|
|
{
|
|
$tina = $this->tina();
|
|
$this->address($tina, $tina->name);
|
|
$order = $this->orderOf($tina, [$this->privateKonflikte(), $this->privateGrenzen()]);
|
|
$order->lines()->orderBy('id')->firstOrFail()->update(['discount_cents' => 400]);
|
|
$order->update(['total_cents' => 7400, 'discount_cents' => 400]);
|
|
$this->webhook($this->paidEvent($order, $session, 7400, true, CarbonImmutable::parse('2026-11-24 09:30:00', 'UTC')->getTimestamp()))->assertOk();
|
|
|
|
return $order->fresh();
|
|
}
|
|
|
|
public function test_a_product_filter_shows_only_orders_with_that_offer_and_only_its_share(): void
|
|
{
|
|
$this->paidTwoLineOrder();
|
|
$this->paidStripeOrder(true, 'cs_other', $this->vera(), 'grenzen', 2900);
|
|
[$from, $to] = PaymentJournal::range('monat', 2026, 11);
|
|
$journal = app(PaymentJournal::class);
|
|
$konflikte = $this->privateKonflikte()->id;
|
|
$grenzen = $this->privateGrenzen()->id;
|
|
|
|
$this->assertCount(2, $journal->entries($from, $to), 'Ohne Filter beide Bestellungen.');
|
|
$this->assertCount(1, $journal->entries($from, $to, offerId: $konflikte), 'Nur die Bestellung mit dem Angebot.');
|
|
$this->assertCount(2, $journal->entries($from, $to, offerId: $grenzen));
|
|
|
|
$entries = $journal->entries($from, $to, offerId: $grenzen);
|
|
$shares = $journal->shares($entries, $grenzen);
|
|
$this->assertSame(2900 + 2900, array_sum($shares));
|
|
$this->assertSame(7400, PaymentJournal::totals($journal->entries($from, $to, offerId: $konflikte))['eingang'], 'Ohne Aufteilung stünde hier die ganze Bestellung.');
|
|
|
|
$entries = $journal->entries($from, $to, offerId: $konflikte);
|
|
$shares = $journal->shares($entries, $konflikte);
|
|
$this->assertSame(4500, array_sum($shares), 'Anteil: 49,00 € minus 4,00 € Rabatt, nicht die 74,00 € der Bestellung.');
|
|
$this->assertSame(4500, PaymentJournal::totals($entries, $shares)['eingang']);
|
|
}
|
|
|
|
public function test_a_refund_mirrors_the_share_and_the_csv_names_it(): void
|
|
{
|
|
$order = $this->paidTwoLineOrder();
|
|
Http::fake(['stripe.test/v1/refunds' => Http::response(['id' => 're_two', 'status' => 'succeeded', 'created' => CarbonImmutable::parse('2026-11-26 10:00:00', 'UTC')->getTimestamp()])]);
|
|
app(Checkout::class)->refund($order, 'Widerruf geprüft');
|
|
[$from, $to] = PaymentJournal::range('monat', 2026, 11);
|
|
$journal = app(PaymentJournal::class);
|
|
$grenzen = $this->privateGrenzen()->id;
|
|
$entries = $journal->entries($from, $to, offerId: $grenzen);
|
|
$shares = $journal->shares($entries, $grenzen);
|
|
$totals = PaymentJournal::totals($entries, $shares);
|
|
|
|
$this->assertSame(2900, $totals['eingang']);
|
|
$this->assertSame(2900, $totals['erstattung']);
|
|
$this->assertSame(0, $totals['saldo']);
|
|
|
|
$csv = PaymentJournal::csv($entries, $shares);
|
|
$lines = array_values(array_filter(explode("\r\n", $csv)));
|
|
$this->assertStringEndsWith(';"Anteil des Angebots EUR"', $lines[0]);
|
|
$this->assertStringEndsWith(';29,00', $lines[1]);
|
|
$this->assertStringEndsWith(';-29,00', $lines[2]);
|
|
$this->assertStringContainsString(';74,00;', $lines[1], 'Die Buchung zeigt weiter den Betrag der ganzen Bestellung.');
|
|
}
|
|
|
|
public function test_the_page_filters_by_offer_validates_the_parameter_and_exports_with_the_offer_in_the_name(): void
|
|
{
|
|
$this->paidTwoLineOrder();
|
|
$konflikte = $this->privateKonflikte();
|
|
$staff = $this->staff('kundenbetreuung');
|
|
|
|
Livewire::actingAs($staff, 'staff')->test(Zahlungsjournal::class, ['angebot' => (string) $konflikte->id])
|
|
->assertSee('Gekauftes Angebot')
|
|
->assertSee('Anteil dieses Angebots')
|
|
->assertSee('45,00 €')
|
|
->assertSee('Buchung gesamt 74,00 €')
|
|
->call('exportieren')
|
|
->assertFileDownloaded('zahlungsjournal-2026-11-echt-angebot-'.$konflikte->id.'.csv')
|
|
->set('angebot', '')
|
|
->assertSee('74,00 €')
|
|
->assertDontSee('Anteil dieses Angebots');
|
|
|
|
Livewire::actingAs($staff, 'staff')->test(Zahlungsjournal::class, ['angebot' => '99999'])->assertSet('angebot', '');
|
|
Livewire::actingAs($staff, 'staff')->test(Zahlungsjournal::class, ['angebot' => 'x; DROP'])->assertSet('angebot', '');
|
|
}
|
|
|
|
public function test_the_offer_page_links_to_its_payments_for_those_who_may_see_them(): void
|
|
{
|
|
$offer = $this->privateKonflikte();
|
|
|
|
Livewire::actingAs($this->staff('katalogverwalter'), 'staff')->test(ViewOffer::class, ['record' => $offer->getKey()])
|
|
->assertSee('Zahlungen zu diesem Angebot')
|
|
->assertSee('angebot='.$offer->id, false);
|
|
}
|
|
|
|
public function test_the_page_exports_the_filtered_period_as_a_csv_download(): void
|
|
{
|
|
$this->paidStripeOrder(true, 'cs_live_1');
|
|
|
|
Livewire::actingAs($this->staff('katalogverwalter'), 'staff')->test(Zahlungsjournal::class)
|
|
->call('exportieren')
|
|
->assertFileDownloaded('zahlungsjournal-2026-11-echt.csv');
|
|
}
|
|
|
|
public function test_invalid_url_values_fall_back_to_valid_filters(): void
|
|
{
|
|
Livewire::actingAs($this->staff(), 'staff')->test(Zahlungsjournal::class, ['modus' => 'quatsch', 'jahr' => 1999, 'nr' => 99, 'art' => 'x', 'betrieb' => 'y'])
|
|
->assertSet('modus', 'monat')
|
|
->assertSet('jahr', 2020)
|
|
->assertSet('nr', 12)
|
|
->assertSet('art', 'alle')
|
|
->assertSet('betrieb', 'echt');
|
|
}
|
|
|
|
public function test_only_catalog_managers_and_customer_service_may_open_the_journal(): void
|
|
{
|
|
foreach (['katalogverwalter', 'kundenbetreuung'] as $role) {
|
|
$this->actingAs($this->staff($role), 'staff')->get('/verwaltung/zahlungsjournal')->assertOk()->assertSee('Einnahmen und Erstattungen');
|
|
}
|
|
|
|
foreach (['redakteur', 'systemverwalter'] as $role) {
|
|
$this->actingAs($this->staff($role), 'staff')->get('/verwaltung/zahlungsjournal')->assertForbidden();
|
|
}
|
|
|
|
}
|
|
|
|
public function test_a_member_is_sent_to_the_staff_login(): void
|
|
{
|
|
$this->actingAs($this->tina())->get('/verwaltung/zahlungsjournal')->assertRedirect('/verwaltung/login');
|
|
}
|
|
|
|
public function test_existing_payments_are_carried_over_marked_as_derived(): void
|
|
{
|
|
$paid = $this->orderOf($this->tina(), [$this->privateKonflikte()]);
|
|
$refunded = $this->orderOf($this->vera(), [$this->privateGrenzen()]);
|
|
Payment::query()->create(['order_id' => $paid->id, 'provider' => 'stripe', 'status' => 'bestaetigt', 'provider_reference' => 'cs_alt_1', 'provider_payment_ref' => 'pi_alt_1', 'amount_cents' => 4900]);
|
|
Payment::query()->create(['order_id' => $refunded->id, 'provider' => 'test', 'status' => 'erstattet', 'provider_reference' => 'test-alt', 'amount_cents' => 2900]);
|
|
Payment::query()->create(['order_id' => $paid->id, 'provider' => 'stripe', 'status' => 'offen', 'provider_reference' => 'cs_offen']);
|
|
$this->assertSame(0, LedgerEntry::query()->count());
|
|
|
|
$migration = require base_path('database/migrations/2026_11_25_100000_create_payment_ledger.php');
|
|
$method = new \ReflectionMethod($migration, 'backfill');
|
|
$method->invoke($migration);
|
|
|
|
$this->assertSame(3, LedgerEntry::query()->count(), 'Zwei Eingänge und eine Erstattung; die offene Zahlung zählt nicht.');
|
|
$this->assertSame(3, LedgerEntry::query()->where('derived', true)->count());
|
|
$this->assertSame(0, LedgerEntry::query()->where('live', true)->count(), 'Nachgetragenes gilt nie als echter Umsatz.');
|
|
$this->assertSame(-2900, LedgerEntry::query()->where('kind', LedgerEntry::KIND_REFUND)->sole()->amount_cents);
|
|
$this->assertStringContainsString('Nachgetragen', (string) LedgerEntry::query()->where('kind', LedgerEntry::KIND_REFUND)->sole()->reason);
|
|
$this->assertSame('pi_alt_1', LedgerEntry::query()->where('provider_reference', 'cs_alt_1')->sole()->provider_payment_ref);
|
|
}
|
|
}
|