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); } }