← Code-Übersicht

LexwareInvoicePayloadFactory.php

Pfad: src/Infrastructure/Rechnung/LexwareInvoicePayloadFactory.php

Ext: php

Größe: 1662 Bytes

Geändert: 2026-07-16 17:41:12+02

<?php
declare(strict_types=1);

namespace Demo\Infrastructure\Rechnung;

final class LexwareInvoicePayloadFactory
{
    public function __construct(private readonly ?string $categoryId)
    {
    }

    public function voucherStatus(): string
    {
        return $this->categoryId === null ? 'unchecked' : 'open';
    }

    /**
     * @param array<string,mixed> $data
     * @return array<string,mixed>
     */
    public function create(array $data, string $contactId): array
    {
        $gross = LexwareMoney::format((string) $data['brutto']);
        $tax = LexwareMoney::format((string) $data['steuer']);
        $payload = [
            'type' => 'purchaseinvoice',
            'voucherStatus' => $this->voucherStatus(),
            'voucherNumber' => (string) $data['invoiceNumber'],
            'voucherDate' => (string) $data['invoiceDate'],
            'totalGrossAmount' => (float) $gross,
            'totalTaxAmount' => (float) $tax,
            'taxType' => 'gross',
            'useCollectiveContact' => false,
            'contactId' => $contactId,
            'remark' => 'Automatischer Import; fachlicher Schluessel: ' . (string) $data['invoiceNumber'],
        ];
        if ($this->categoryId !== null) {
            $net = (float) LexwareMoney::format((string) $data['netto']);
            $rate = $net > 0 ? round(((float) $tax / $net) * 100, 2) : 0.0;
            $payload['voucherItems'] = [[
                'amount' => (float) $gross,
                'taxAmount' => (float) $tax,
                'taxRatePercent' => $rate,
                'categoryId' => $this->categoryId,
            ]];
        }
        return $payload;
    }
}