custom/plugins/WbfkDigitalInvoice64/src/Subscriber/GenerateDocumentSubscriber.php line 48

Open in your IDE?
  1. <?php
  2. /**
  3.  * WBFK OG
  4.  * Copyright (c) 2024 WBFK OG
  5.  *
  6.  * @copyright  Copyright (c) 2024 WBFK OG (https://wbfk.at)
  7.  * @author     WBFK OG <info@wbfk.at>
  8.  */
  9. declare(strict_types=1);
  10. namespace Wbfk\DigitalInvoice\Subscriber;
  11. use Easybill\ZUGFeRD2\Builder;
  12. use Shopware\Core\Checkout\Document\Renderer\CreditNoteRenderer;
  13. use Shopware\Core\Checkout\Document\Renderer\InvoiceRenderer;
  14. use Shopware\Core\Checkout\Document\Renderer\StornoRenderer;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Wbfk\DigitalInvoice\Core\Document\DigitalDocumentConfigExtension;
  17. use Wbfk\DigitalInvoice\Core\Document\DigitalGeneratedDocumentExtension;
  18. use Wbfk\DigitalInvoice\Entity\Order\WbfkInvoiceDetailsOrderExtensionEntity;
  19. use Wbfk\DigitalInvoice\Event\GenerateDocumentEvent;
  20. use Wbfk\DigitalInvoice\Service\Document\DocumentService;
  21. use Wbfk\DigitalInvoice\Service\Document\ZugferdException;
  22. use Wbfk\DigitalInvoice\Service\Document\ZugferdService;
  23. use Wbfk\DigitalInvoice\Service\Utils\SystemConfigReader;
  24. class GenerateDocumentSubscriber implements EventSubscriberInterface
  25. {
  26.     public function __construct(
  27.         private readonly ZugferdService $zugferdService,
  28.         private readonly SystemConfigReader $systemConfigReader,
  29.     ) {
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             DocumentService::GENERATE_DOCUMENT_EVENT => 'generateDocumentEvent',
  35.         ];
  36.     }
  37.     /**
  38.      * @throws ZugferdException
  39.      */
  40.     public function generateDocumentEvent(GenerateDocumentEvent $event): void
  41.     {
  42.         $wbfkDocumentExtension DigitalDocumentConfigExtension::fromDocumentConfig($event->documentConfiguration);
  43.         if (!$wbfkDocumentExtension) {
  44.             return;
  45.         }
  46.         $defaultInvoiceType $this->systemConfigReader->stringOrNull('defaultInvoiceType');
  47.         $invoiceType $wbfkDocumentExtension?->getInvoiceType() ?: $defaultInvoiceType;
  48.         if ($invoiceType === WbfkInvoiceDetailsOrderExtensionEntity::$INVOICE_TYPE_ZUGFERD_EN16931) {
  49.             $this->zugferd(Builder::GUIDELINE_SPECIFIED_DOCUMENT_CONTEXT_ID_EN16931$wbfkDocumentExtension$event);
  50.         } elseif (
  51.             $invoiceType === WbfkInvoiceDetailsOrderExtensionEntity::$INVOICE_TYPE_X_INVOICE
  52.             || $invoiceType === WbfkInvoiceDetailsOrderExtensionEntity::$INVOICE_TYPE_ZUGFERD_X_INVOICE
  53.         ) {
  54.             $this->zugferd(Builder::GUIDELINE_SPECIFIED_DOCUMENT_CONTEXT_ID_XRECHNUNG$wbfkDocumentExtension$event);
  55.         }
  56.     }
  57.     /**
  58.      * @throws ZugferdException
  59.      */
  60.     private function zugferd(string $profileDigitalDocumentConfigExtension $wbfkDocumentExtensionGenerateDocumentEvent $event): void
  61.     {
  62.         $docExt = new DigitalGeneratedDocumentExtension();
  63.         $docExt->type = ($profile === Builder::GUIDELINE_SPECIFIED_DOCUMENT_CONTEXT_ID_XRECHNUNG) ?
  64.             DigitalGeneratedDocumentExtension::TYPE_XINVOICE DigitalGeneratedDocumentExtension::TYPE_ZUGFERD;
  65.         /** @noinspection PhpDeprecationInspection */
  66.         $event->generatedDocument->addExtension(DigitalGeneratedDocumentExtension::NAME$docExt);
  67.         $this->catchAndShowErrorInPreview(function () use ($profile$event$docExt$wbfkDocumentExtension) {
  68.             $docName $event->documentType->getTechnicalName();
  69.             if ($docName === InvoiceRenderer::TYPE) {
  70.                 $docExt->zugferdXml $this->zugferdService->invoice($profile$event->documentConfiguration$wbfkDocumentExtension$event->order);
  71.             } elseif ($docName === CreditNoteRenderer::TYPE) {
  72.                 $docExt->zugferdXml $this->zugferdService->creditNote($profile$event->documentConfiguration$wbfkDocumentExtension$event->order);
  73.             } elseif ($docName === StornoRenderer::TYPE) {
  74.                 $docExt->zugferdXml $this->zugferdService->cancellation($profile$event->documentConfiguration$wbfkDocumentExtension$event->order);
  75.             }
  76.         }, $event->isPreview$docExt);
  77.     }
  78.     /**
  79.      * @throws ZugferdException
  80.      */
  81.     private function catchAndShowErrorInPreview(callable $funcbool $isPreviewDigitalGeneratedDocumentExtension $docExt): void
  82.     {
  83.         if ($isPreview) {
  84.             try {
  85.                 $func();
  86.             } catch (ZugferdException $exception) {
  87.                 $docExt->errors[] = $exception;
  88.             }
  89.         } else {
  90.             $func();
  91.         }
  92.     }
  93. }