custom/plugins/WbfkDigitalInvoice64/src/Subscriber/DocumentTypeSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * WBFK OG
  5.  * Copyright (c) 2024 WBFK OG
  6.  *
  7.  * @copyright  Copyright (c) 2024 WBFK OG (https://wbfk.at)
  8.  * @author     WBFK OG <info@wbfk.at>
  9.  */
  10. namespace Wbfk\DigitalInvoice\Subscriber;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Wbfk\DigitalInvoice\Entity\Order\WbfkInvoiceDetailsOrderExtensionEntity;
  16. use Wbfk\DigitalInvoice\Event\DocumentTypeEvent;
  17. use Wbfk\DigitalInvoice\Service\Document\DocumentService;
  18. class DocumentTypeSubscriber implements EventSubscriberInterface
  19. {
  20.     public function __construct(
  21.         private readonly EntityRepositoryInterface $wbfkInvoiceDetailsOrderExtensionRepository,
  22.     ) {
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             DocumentService::DOCUMENT_TYPE_EVENT => 'DocumentTypeEvent',
  28.         ];
  29.     }
  30.     public function DocumentTypeEvent(DocumentTypeEvent $event): void
  31.     {
  32.         $docType $event->documentTypeName;
  33.         if (in_array($docType, ['storno''invoice''credit_note'])) {
  34.             $criteria = new Criteria();
  35.             $criteria->addFilter(new EqualsFilter('orderId'$event->orderId));
  36.             /** @var WbfkInvoiceDetailsOrderExtensionEntity $orderInvoiceExt */
  37.             $orderInvoiceExt $this->wbfkInvoiceDetailsOrderExtensionRepository->search($criteria$event->context)->first();
  38.             $fileType $orderInvoiceExt?->getInvoiceType();
  39.             if ($fileType && $fileType === WbfkInvoiceDetailsOrderExtensionEntity::$INVOICE_TYPE_X_INVOICE) {
  40.                 $event->fileType WbfkInvoiceDetailsOrderExtensionEntity::$INVOICE_TYPE_X_INVOICE;
  41.             }
  42.         }
  43.     }
  44. }