custom/plugins/WbfkExtensions/src/Subscriber/DocumentEntitySubscriber.php line 32

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace WbfkExtensions\Subscriber;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use WbfkExtensions\Service\InvoiceStateMachineService;
  9. class DocumentEntitySubscriber implements EventSubscriberInterface
  10. {
  11.     public const INVOICE_DOCUMENT_TYPE_ID '7ebec1ff1f57459684dd3d040a5f87d1';
  12.     public const STORNO_DOCUMENT_TYPE_ID '4c7238b29af340cda90b2ba1e084698a';
  13.     public function __construct(
  14.         private readonly InvoiceStateMachineService $invoiceStateMachineService,
  15.     ) {
  16.     }
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             'document.written' => 'onDocumentWritten',
  21.         ];
  22.     }
  23.     public function onDocumentWritten(EntityWrittenEvent $event): void
  24.     {
  25.         array_map(function (EntityWriteResult $result) use ($event) {
  26.             if ($result->getOperation() !== EntityWriteResult::OPERATION_INSERT) {
  27.                 return;
  28.             }
  29.             match ($result->getPayload()['documentTypeId']) {
  30.                 self::INVOICE_DOCUMENT_TYPE_ID => $this->setInvoiceStateInvoiced($result$event->getContext()),
  31.                 self::STORNO_DOCUMENT_TYPE_ID => $this->setInvoiceStateOpen($result$event->getContext()),
  32.                 default => null,
  33.             };
  34.         }, $event->getWriteResults());
  35.     }
  36.     private function setInvoiceStateInvoiced(EntityWriteResult $resultContext $context): void
  37.     {
  38.         $this->invoiceStateMachineService->invoiceStateTransition(
  39.             $result->getPayload()['orderId'],
  40.             'invoice',
  41.             $context
  42.         );
  43.     }
  44.     private function setInvoiceStateOpen(EntityWriteResult $resultContext $context): void
  45.     {
  46.         $this->invoiceStateMachineService->invoiceStateTransition(
  47.             $result->getPayload()['orderId'],
  48.             'cancel_invoice',
  49.             $context
  50.         );
  51.     }
  52. }