Mein Konto, Zahlungen: eigene Zahlungen und Rückzahlungen mit Beleg als PDF, auch von der Bestellseite verlinkt. Der Beleg entsteht aus den bei der Buchung festgehaltenen Angaben und ist byte-gleich; er heißt nie Rechnung und trägt weder Rechnungsnummer noch Steuerangaben. Kleiner PDF-Schreiber ohne neue Abhängigkeit. Aussteller und Hinweis nur bei Pflege (SHOP_RECEIPT_*). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
174 lines
7.0 KiB
PHP
174 lines
7.0 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Commerce\Receipt;
|
|
|
|
use App\Domain\Commerce\LedgerEntry;
|
|
use App\Domain\Commerce\Money;
|
|
use App\Domain\Commerce\PaymentJournal;
|
|
use App\Domain\Identity\BillingAddress;
|
|
|
|
/**
|
|
* Zahlungs- oder Erstattungsbeleg zu einer Buchung des Zahlungsjournals als PDF. Gebaut wird ausschließlich aus den
|
|
* bei der Buchung festgehaltenen Angaben (Zahler, Positionen, Beträge, Zeitpunkt, Beleg beim Anbieter), nie aus dem
|
|
* heutigen Profil oder der heutigen Bestellung: Spätere Änderungen der Adresse ändern den Beleg nicht, und jeder Abruf
|
|
* liefert dieselben Bytes. Der Beleg bestätigt die Zahlung. Er enthält weder Rechnungsnummer noch Steuerangaben; ob eine
|
|
* Rechnung zu stellen ist, ist rechtlich noch nicht geklärt (OPEN-06). Aussteller und Zusatzhinweis kommen aus
|
|
* `config('shop.receipt')` und werden nur gedruckt, wenn sie gepflegt sind. Zu nachgetragenen Buchungen (abgeleitetes
|
|
* Datum) gibt es keinen Beleg.
|
|
*/
|
|
final class PaymentReceipt
|
|
{
|
|
private const LEFT = 50;
|
|
|
|
private const RIGHT = 545;
|
|
|
|
public static function available(LedgerEntry $entry): bool
|
|
{
|
|
return ! $entry->derived;
|
|
}
|
|
|
|
public static function filename(LedgerEntry $entry): string
|
|
{
|
|
return ($entry->isRefund() ? 'Erstattungsbeleg' : 'Zahlungsbeleg').'-Bestellung-'.$entry->order_id.'-Buchung-'.$entry->id.'.pdf';
|
|
}
|
|
|
|
public static function render(LedgerEntry $entry): string
|
|
{
|
|
$refund = $entry->isRefund();
|
|
$payer = (array) $entry->payer;
|
|
$billing = (array) ($payer['billing'] ?? []);
|
|
$snapshot = (array) $entry->order_snapshot;
|
|
$money = fn (int $cents): string => Money::format($cents);
|
|
$when = PaymentJournal::local($entry->occurred_at);
|
|
$title = $refund ? 'Erstattungsbeleg' : 'Zahlungsbeleg';
|
|
$pdf = new PdfDocument;
|
|
$pdf->addPage();
|
|
$y = 790.0;
|
|
|
|
$pdf->text(self::LEFT, $y, $title, 22, true);
|
|
$issuer = array_values(array_filter(array_map('trim', explode('|', (string) config('shop.receipt.issuer')))));
|
|
$yIssuer = $y + 4;
|
|
|
|
foreach ($issuer as $i => $line) {
|
|
$pdf->textRight(self::RIGHT, $yIssuer - $i * 12, $line, 9, $i === 0);
|
|
}
|
|
|
|
$y = min($y - 26, $yIssuer - 12 * count($issuer) - 8);
|
|
|
|
if (! $entry->live) {
|
|
$pdf->text(self::LEFT, $y, 'TESTZAHLUNG - kein Umsatz, kein Beleg für eine echte Zahlung', 10, true);
|
|
$y -= 16;
|
|
}
|
|
|
|
$y -= 6;
|
|
$meta = [
|
|
['Bestellung', (string) $entry->order_id],
|
|
['Buchung', (string) $entry->id],
|
|
[$refund ? 'Rückzahlung am' : 'Zahlung am', $when->format('d.m.Y').(PaymentJournal::hasTime($entry) ? ', '.$when->format('H:i').' Uhr' : '')],
|
|
['Zahlungsweg', PaymentJournal::providerLabel($entry->provider)],
|
|
['Referenz', (string) $entry->provider_reference],
|
|
];
|
|
|
|
foreach ($meta as [$label, $value]) {
|
|
$pdf->text(self::LEFT, $y, $label, 9);
|
|
|
|
foreach (PdfDocument::wrap($value, 330, 10) as $i => $part) {
|
|
$pdf->text(160, $y - $i * 12, $part, 10);
|
|
}
|
|
|
|
$y -= 14 * max(1, count(PdfDocument::wrap($value, 330, 10)));
|
|
}
|
|
|
|
$y -= 10;
|
|
$pdf->text(self::LEFT, $y, $refund ? 'Erstattung an' : 'Zahler', 9);
|
|
$recipient = $billing !== [] ? array_values(array_filter([
|
|
$billing['recipient'] ?? null,
|
|
$billing['line2'] ?? null,
|
|
$billing['street'] ?? null,
|
|
trim(($billing['postal_code'] ?? '').' '.($billing['city'] ?? '')),
|
|
($billing['country'] ?? 'DE') !== 'DE' ? (BillingAddress::COUNTRIES[$billing['country']] ?? $billing['country']) : null,
|
|
])) : array_values(array_filter([$payer['holder'] ?? null, 'Keine Rechnungsadresse hinterlegt']));
|
|
|
|
foreach ($recipient as $i => $line) {
|
|
$pdf->text(160, $y - $i * 13, (string) $line, 10, $i === 0);
|
|
}
|
|
|
|
$y -= 13 * count($recipient);
|
|
|
|
if (filled($payer['buyer'] ?? null) && ($payer['buyer'] ?? '') !== ($billing['recipient'] ?? $payer['holder'] ?? '')) {
|
|
$pdf->text(160, $y, 'Bestellt von '.$payer['buyer'], 9);
|
|
$y -= 13;
|
|
}
|
|
|
|
$y -= 16;
|
|
$pdf->line(self::LEFT, $y + 12, self::RIGHT, $y + 12, 0.8);
|
|
$pdf->text(self::LEFT, $y, 'Position', 9, true);
|
|
$pdf->textRight(400, $y, 'Preis', 9, true);
|
|
$pdf->textRight(472, $y, 'Rabatt', 9, true);
|
|
$pdf->textRight(self::RIGHT, $y, 'Betrag', 9, true);
|
|
$y -= 6;
|
|
$pdf->line(self::LEFT, $y, self::RIGHT, $y, 0.4);
|
|
$y -= 14;
|
|
|
|
foreach ((array) ($snapshot['lines'] ?? []) as $line) {
|
|
$lines = PdfDocument::wrap((string) ($line['name'] ?? 'Angebot'), 270, 10);
|
|
|
|
if ($y - 13 * count($lines) < 140) {
|
|
$pdf->addPage();
|
|
$y = 790.0;
|
|
}
|
|
|
|
$price = (int) ($line['price_cents'] ?? 0);
|
|
$discount = (int) ($line['discount_cents'] ?? 0);
|
|
|
|
foreach ($lines as $i => $part) {
|
|
$pdf->text(self::LEFT, $y - $i * 13, $part, 10);
|
|
}
|
|
|
|
$pdf->textRight(400, $y, $money($price), 10);
|
|
$pdf->textRight(472, $y, $discount > 0 ? '-'.$money($discount) : '', 10);
|
|
$pdf->textRight(self::RIGHT, $y, $money($price - $discount), 10);
|
|
$y -= 13 * count($lines) + 4;
|
|
}
|
|
|
|
$y -= 6;
|
|
$pdf->line(self::LEFT, $y + 10, self::RIGHT, $y + 10, 0.4);
|
|
$y -= 6;
|
|
|
|
if ((int) ($snapshot['discount_cents'] ?? 0) > 0) {
|
|
$pdf->text(300, $y, 'Zwischensumme', 10);
|
|
$pdf->textRight(self::RIGHT, $y, $money((int) ($snapshot['subtotal_cents'] ?? 0)), 10);
|
|
$y -= 14;
|
|
$pdf->text(300, $y, 'Rabatt'.(filled($snapshot['discount_code'] ?? null) ? ' (Code '.$snapshot['discount_code'].')' : ''), 10);
|
|
$pdf->textRight(self::RIGHT, $y, '-'.$money((int) $snapshot['discount_cents']), 10);
|
|
$y -= 14;
|
|
}
|
|
|
|
$pdf->text(300, $y, $refund ? 'Erstatteter Betrag' : 'Gezahlter Betrag', 11, true);
|
|
$pdf->textRight(self::RIGHT, $y, $money(abs($entry->amount_cents)), 11, true);
|
|
$y -= 26;
|
|
|
|
$notes = [
|
|
$refund
|
|
? 'Dieser Beleg bestätigt die Rückzahlung zur genannten Bestellung. Die Berechtigungen aus dieser Bestellung sind widerrufen.'
|
|
: 'Dieser Beleg bestätigt die Zahlung zur genannten Bestellung.',
|
|
'Er enthält keine Rechnungsnummer und keine steuerlichen Angaben und wurde aus den bei der Zahlung festgehaltenen Angaben erstellt.',
|
|
];
|
|
|
|
if (filled(config('shop.receipt.note'))) {
|
|
$notes[] = (string) config('shop.receipt.note');
|
|
}
|
|
|
|
foreach ($notes as $note) {
|
|
foreach (PdfDocument::wrap($note, self::RIGHT - self::LEFT, 9) as $part) {
|
|
$pdf->text(self::LEFT, $y, $part, 9);
|
|
$y -= 12;
|
|
}
|
|
|
|
$y -= 3;
|
|
}
|
|
|
|
return $pdf->output($title.' Bestellung '.$entry->order_id);
|
|
}
|
|
}
|