Führe ein unveränderliches Zahlungsjournal mit Ansicht und CSV-Export ein
Jede Zahlung und jede Erstattung wird in derselben Transaktion wie der Bestellstatus unveränderlich gebucht: Zeitpunkt, Betrag, Beleg beim Anbieter (Session, pi_, re_), handelnde Person, Begründung und Zahler samt Rechnungsadresse zum Zeitpunkt. Testzahlungen zählen nie als Umsatz. Verwaltung: Verkauf, Einnahmen und Erstattungen mit Zeitraum, Summen und Export. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6518d1b80f
commit
fe39e1efb6
15 files changed
+1485
-22
No files matched your search
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
/**
|
||||
* Zahlungsjournal: eine unveränderliche Zeile je Geldbewegung (Zahlungseingang, Erstattung). Rein ergänzend.
|
||||
*
|
||||
* Die Tabelle `payments` hält nur den aktuellen Zustand einer Zahlung und überschreibt ihn bei der Erstattung. Für den
|
||||
* Nachweis gegenüber dem Finanzamt braucht es dagegen je Bewegung Datum, Betrag, Beleg beim Anbieter, handelnde
|
||||
* Person, Begründung und den Zahler zum Zeitpunkt der Bewegung. Zeilen werden nie geändert oder gelöscht; eine
|
||||
* Korrektur ist eine neue Gegenbuchung. Auf PostgreSQL erzwingt das ein Trigger auch gegen direkte SQL-Zugriffe.
|
||||
*
|
||||
* Bestehende bestätigte und erstattete Zahlungen werden nachgetragen und als „nachgetragen“ markiert: Ihr Datum ist
|
||||
* aus den Zeitstempeln der Zahlung abgeleitet und nicht der belegte Zahlungs- oder Erstattungszeitpunkt.
|
||||
*/
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('payment_ledger', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->string('kind', 16);
|
||||
$table->foreignId('order_id')->constrained('orders')->restrictOnDelete();
|
||||
$table->foreignId('payment_id')->nullable()->constrained('payments')->restrictOnDelete();
|
||||
$table->timestamp('occurred_at');
|
||||
$table->integer('amount_cents');
|
||||
$table->string('currency', 3)->default('EUR');
|
||||
$table->string('provider', 32);
|
||||
$table->string('provider_reference', 191);
|
||||
$table->string('provider_payment_ref', 191)->nullable();
|
||||
$table->string('provider_status', 32)->nullable();
|
||||
$table->boolean('live')->default(false);
|
||||
$table->boolean('derived')->default(false);
|
||||
$table->unsignedBigInteger('actor_id')->nullable();
|
||||
$table->string('actor_name', 160)->nullable();
|
||||
$table->text('reason')->nullable();
|
||||
$table->json('payer');
|
||||
$table->json('order_snapshot');
|
||||
$table->timestamp('created_at')->useCurrent();
|
||||
|
||||
$table->unique(['kind', 'provider', 'provider_reference'], 'payment_ledger_event_unique');
|
||||
$table->index(['occurred_at']);
|
||||
$table->index(['order_id']);
|
||||
});
|
||||
|
||||
$this->backfill();
|
||||
|
||||
if (DB::getDriverName() === 'pgsql') {
|
||||
DB::unprepared(<<<'SQL'
|
||||
CREATE FUNCTION payment_ledger_immutable() RETURNS trigger AS $$
|
||||
BEGIN
|
||||
RAISE EXCEPTION 'Das Zahlungsjournal ist unveränderlich. Korrekturen sind neue Gegenbuchungen.';
|
||||
END;
|
||||
$$ LANGUAGE plpgsql
|
||||
SQL);
|
||||
DB::unprepared('CREATE TRIGGER payment_ledger_no_change BEFORE UPDATE OR DELETE ON payment_ledger FOR EACH ROW EXECUTE PROCEDURE payment_ledger_immutable()');
|
||||
}
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
if (DB::getDriverName() === 'pgsql') {
|
||||
DB::unprepared('DROP TRIGGER IF EXISTS payment_ledger_no_change ON payment_ledger');
|
||||
DB::unprepared('DROP FUNCTION IF EXISTS payment_ledger_immutable()');
|
||||
}
|
||||
|
||||
Schema::dropIfExists('payment_ledger');
|
||||
}
|
||||
|
||||
private function backfill(): void
|
||||
{
|
||||
$payments = DB::table('payments')->whereIn('status', ['bestaetigt', 'erstattet'])->orderBy('id')->get();
|
||||
|
||||
foreach ($payments as $payment) {
|
||||
$order = DB::table('orders')->where('id', $payment->order_id)->first();
|
||||
|
||||
if ($order === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$amount = (int) ($payment->amount_cents ?? $order->total_cents ?? 0);
|
||||
$payer = json_encode($this->payer($order), JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
|
||||
$snapshot = json_encode($this->orderSnapshot($order), JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
|
||||
$base = [
|
||||
'order_id' => $order->id,
|
||||
'payment_id' => $payment->id,
|
||||
'currency' => $order->currency ?? 'EUR',
|
||||
'provider' => $payment->provider,
|
||||
'provider_payment_ref' => $payment->provider_payment_ref,
|
||||
'live' => false,
|
||||
'derived' => true,
|
||||
'actor_id' => $payment->confirmed_by,
|
||||
'payer' => $payer,
|
||||
'order_snapshot' => $snapshot,
|
||||
];
|
||||
|
||||
DB::table('payment_ledger')->insert($base + [
|
||||
'kind' => 'eingang',
|
||||
'occurred_at' => $payment->created_at ?? $order->created_at,
|
||||
'amount_cents' => $amount,
|
||||
'provider_reference' => $payment->provider_reference,
|
||||
]);
|
||||
|
||||
if ($payment->status === 'erstattet') {
|
||||
DB::table('payment_ledger')->insert($base + [
|
||||
'kind' => 'erstattung',
|
||||
'occurred_at' => $payment->updated_at ?? $payment->created_at ?? $order->created_at,
|
||||
'amount_cents' => -$amount,
|
||||
'provider_reference' => 'nachgetragen-'.$payment->id,
|
||||
'actor_id' => null,
|
||||
'reason' => 'Nachgetragen: Zeitpunkt, Referenz und Begründung der Erstattung wurden damals nicht festgehalten.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function payer(object $order): array
|
||||
{
|
||||
$party = $order->party_id === null ? null : DB::table('parties')->where('id', $order->party_id)->first();
|
||||
$user = $order->user_id === null ? null : DB::table('users')->where('id', $order->user_id)->first();
|
||||
$address = $order->party_id === null ? null : DB::table('billing_addresses')->where('party_id', $order->party_id)->first();
|
||||
|
||||
return [
|
||||
'holder' => $party?->display_name,
|
||||
'holder_kind' => $order->holder_kind,
|
||||
'buyer' => $user?->name,
|
||||
'buyer_email' => $user?->email,
|
||||
'billing' => $address === null ? null : [
|
||||
'recipient' => $address->recipient,
|
||||
'line2' => $address->line2,
|
||||
'street' => $address->street,
|
||||
'postal_code' => $address->postal_code,
|
||||
'city' => $address->city,
|
||||
'country' => $address->country,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function orderSnapshot(object $order): array
|
||||
{
|
||||
$lines = DB::table('order_lines')->where('order_id', $order->id)->orderBy('id')->get(['offer_name', 'price_cents', 'discount_cents']);
|
||||
|
||||
return [
|
||||
'subtotal_cents' => (int) ($order->subtotal_cents ?? $order->total_cents ?? 0),
|
||||
'discount_cents' => (int) ($order->discount_cents ?? 0),
|
||||
'discount_code' => json_decode((string) ($order->discount_snapshot ?? 'null'), true)['code'] ?? null,
|
||||
'lines' => $lines->map(fn (object $line): array => [
|
||||
'name' => $line->offer_name,
|
||||
'price_cents' => (int) $line->price_cents,
|
||||
'discount_cents' => (int) ($line->discount_cents ?? 0),
|
||||
])->all(),
|
||||
];
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user