← Code-Übersicht

PostInvoiceUseCase.php

Pfad: src/Application/Rechnung/PostInvoiceUseCase.php

Ext: php

Größe: 1886 Bytes

Geändert: 2026-07-16 14:13:13+02

<?php
declare(strict_types=1);

namespace Demo\Application\Rechnung;

use Demo\Domain\Rechnung\ExtractedInvoiceData;
use Demo\Domain\Rechnung\InvoiceErrorCode;
use Demo\Domain\Rechnung\InvoiceLoggerPort;
use Demo\Domain\Rechnung\InvoiceRecord;
use Demo\Domain\Rechnung\LexofficePort;
use Demo\Domain\Rechnung\PostingResult;

/**
 * Use Case: Buha-Upload + Zuordnung/Buchung.
 */
final class PostInvoiceUseCase
{
    /**
     * @param LexofficePort    $lexoffice Buha-Adapter.
     * @param InvoiceLoggerPort $logger   Logging.
     */
    public function __construct(
        private LexofficePort $lexoffice,
        private InvoiceLoggerPort $logger
    ) {
    }

    /**
     * @param string         $runId      Lauf-ID.
     * @param InvoiceRecord  $record     Rechnungsdatensatz.
     * @param ExtractedInvoiceData $data  Extrahierte Werte.
     * @param string         $contactId   Kontakt-ID.
     * @return PostingResult Buha-Ergebnis.
     */
    public function execute(string $runId, InvoiceRecord $record, ExtractedInvoiceData $data, string $contactId): PostingResult
    {
        if ($record->originalPfad === null || !is_file($record->originalPfad)) {
            throw new InvoiceProcessException(
                InvoiceErrorCode::POSTING,
                'posting',
                'Original-PDF fehlt fuer Upload'
            );
        }

        if (trim($contactId) === '') {
            throw new InvoiceProcessException(
                InvoiceErrorCode::POSTING,
                'posting',
                'Kontakt-ID fehlt'
            );
        }

        $this->logger->logStep($runId, 'post', 'START', ['sourceId' => $record->quelleId]);
        $result = $this->lexoffice->postInvoice($data, $contactId, $record->originalPfad);
        $this->logger->logStep($runId, 'post', 'DONE', ['voucherId' => $result->voucherId]);

        return $result;
    }
}