custom/plugins/KayloManagement/src/Subscriber/WorkshopCheckoutSubscriber.php line 128

Open in your IDE?
  1. <?php
  2. namespace Kaylo\Management\Subscriber;
  3. use Kaylo\Management\Service\WorkshopCart\WorkshopCartLineItemHandler;
  4. use NetzpEvents6\Components\EventsHelper;
  5. use NetzpEvents6\Components\SlotStruct;
  6. use NetzpEvents6\Core\Content\Participant\ParticipantEntity;
  7. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  8. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemQuantityChangedEvent;
  9. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  10. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  11. use Shopware\Core\Checkout\Document\DocumentConfigurationFactory;
  12. use Shopware\Core\Checkout\Document\DocumentService;
  13. use Shopware\Core\Checkout\Document\FileGenerator\FileTypes;
  14. use Shopware\Core\Checkout\Order\Event\OrderStateMachineStateChangeEvent;
  15. use Shopware\Core\Defaults;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  17. use Shopware\Core\Framework\Uuid\Uuid;
  18. use Shopware\Core\System\NumberRange\ValueGenerator\NumberRangeValueGeneratorInterface;
  19. use Shopware\Core\System\SystemConfig\SystemConfigService;
  20. use Shopware\Storefront\Page\Account\Order\AccountOrderPageLoadedEvent;
  21. use Symfony\Component\DependencyInjection\ContainerInterface;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. use Symfony\Component\HttpFoundation\RequestStack;
  24. class WorkshopCheckoutSubscriber implements EventSubscriberInterface {
  25.     private $container;
  26.     private $config;
  27.     private $requestStack;
  28.     private $helper;
  29.     private $withoutParticipants;
  30.     private $documentService;
  31.     private $numberRangeValueGenerator;
  32.     public function __construct(
  33.         ContainerInterface                 $container,
  34.         SystemConfigService                $config,
  35.         RequestStack                       $requestStack,
  36.         EventsHelper                       $helper,
  37.         DocumentService                    $documentService,
  38.         NumberRangeValueGeneratorInterface $numberRangeValueGenerator
  39.     ) {
  40.         $this->container                 $container;
  41.         $this->config                    $config;
  42.         $this->requestStack              $requestStack;
  43.         $this->helper                    $helper;
  44.         $this->documentService           $documentService;
  45.         $this->numberRangeValueGenerator $numberRangeValueGenerator;
  46.         $pluginConfig              $this->config->get('NetzpEvents6.config');
  47.         $this->withoutParticipants = (bool)$pluginConfig['anonymous'];
  48.     }
  49.     public static function getSubscribedEvents() {
  50.         return [
  51.             BeforeLineItemAddedEvent::class => 'onLineItemAdded',
  52.             BeforeLineItemQuantityChangedEvent::class => 'onLineItemUpdated',
  53.             CartConvertedEvent::class => 'onCartConverted',
  54.             CheckoutOrderPlacedEvent::class => 'onOrderPlaced',
  55.             AccountOrderPageLoadedEvent::class => 'onOrderLoaded',
  56.             'state_enter.order.state.cancelled' => 'orderStateCanceled',
  57.         ];
  58.     }
  59.     public function onCartConverted(CartConvertedEvent $event) {
  60.         $cart      $event->getCart();
  61.         $lineItems $cart->getLineItems();
  62.         foreach ($lineItems as $lineItem) {
  63.             if ($lineItem->getType() !== WorkshopCartLineItemHandler::TYPE) {
  64.                 return;
  65.             }
  66.             foreach ($lineItem->getChildren() as $childLineItem) {
  67.                 if ($childLineItem->hasPayloadValue('netzp_event')) {
  68.                     $this->helper->processEvent($childLineItem$event->getContext());
  69.                 }
  70.             }
  71.         }
  72.     }
  73.     public function onLineItemAdded(BeforeLineItemAddedEvent $event) {
  74.         $lineItem $event->getLineItem();
  75.         if ($lineItem->getType() === WorkshopCartLineItemHandler::TYPE) {
  76.             foreach ($lineItem->getChildren() as $childLineItem) {
  77.                 $eventId  $childLineItem->getPayloadValue('netzpEventId');
  78.                 $repo     $this->container->get('s_plugin_netzp_events_slots.repository');
  79.                 $criteria = new Criteria([ $eventId ]);
  80.                 $criteria->addAssociation('event');
  81.                 $netzpEventSlot $repo->search($criteria$event->getContext())->getEntities()->first();
  82.                 if ($netzpEventSlot) {
  83.                     $dateFrom  $netzpEventSlot->getFrom() ?
  84.                         $netzpEventSlot->getFrom()->format(\DateTimeInterface::ATOM) :
  85.                         '';
  86.                     $dateUntil $netzpEventSlot->getUntil() ?
  87.                         $netzpEventSlot->getUntil()->format(\DateTimeInterface::ATOM) :
  88.                         '';
  89.                     $slot = new SlotStruct();
  90.                     $slot->setId($netzpEventSlot->getId());
  91.                     $slot->setTitle($netzpEventSlot->getEvent()->getTranslated()['title']);
  92.                     $slot->setLocation($netzpEventSlot->getTranslated()['location']);
  93.                     $slot->setAddress($netzpEventSlot->getTranslated()['address']);
  94.                     $slot->setFrom($dateFrom);
  95.                     $slot->setUntil($dateUntil);
  96.                     $slot->setAvailable($netzpEventSlot->getTicketsAvailable() - $netzpEventSlot->getTicketsBooked());
  97.                     if ($this->withoutParticipants) {
  98.                         $participants    = [];
  99.                         $participantsIds = [];
  100.                         for ($i 1$i <= $lineItem->getQuantity(); $i++) {
  101.                             $participants[$i]    = '(' $i ')';
  102.                             $participantsIds[$i] = Uuid::randomHex();
  103.                         }
  104.                         $slot->setParticipants($participants);
  105.                         $slot->setParticipantsIds($participantsIds);
  106.                     }
  107.                     $childLineItem->setPayloadValue('netzp_event'$slot->getVars());
  108.                 }
  109.             }
  110.         }
  111.     }
  112.     public function onLineItemUpdated(BeforeLineItemQuantityChangedEvent $event) {
  113.         $lineItem $event->getLineItem();
  114.         if ($lineItem->getType() !== WorkshopCartLineItemHandler::TYPE) {
  115.             return;
  116.         }
  117.         foreach ($lineItem->getChildren() as $childLineItem) {
  118.             if (!$childLineItem->hasPayloadValue('netzp_event')) {
  119.                 continue;
  120.             }
  121.             if ($this->withoutParticipants) {
  122.                 $participants    = [];
  123.                 $participantsIds = [];
  124.                 for ($i 1$i <= $childLineItem->getQuantity(); $i++) {
  125.                     $participants[$i]    = '(' $i ')';
  126.                     $participantsIds[$i] = Uuid::randomHex();
  127.                 }
  128.                 $payload                    $childLineItem->getPayloadValue('netzp_event');
  129.                 $payload['participants']    = $participants;
  130.                 $payload['participantsIds'] = $participantsIds;
  131.                 $childLineItem->setPayloadValue('netzp_event'$payload);
  132.             }
  133.         }
  134.     }
  135.     public function onOrderLoaded(AccountOrderPageLoadedEvent $event) {
  136.     }
  137.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event) {
  138.         $this->processOrder($event);
  139.     }
  140.     public function orderStateCanceled(OrderStateMachineStateChangeEvent $event) {
  141.         $repoSlots        $this->container->get('s_plugin_netzp_events_slots.repository');
  142.         $repoParticipants $this->container->get('s_plugin_netzp_events_participants.repository');
  143.         $context   $event->getContext();
  144.         $lineItems $event->getOrder()->getLineItems();
  145.         foreach ($lineItems as $lineItem) {
  146.             if ($lineItem->getType() !== WorkshopCartLineItemHandler::TYPE) {
  147.                 continue;
  148.             }
  149.             foreach ($lineItem->getChildren() as $childLineItem) {
  150.                 $payload $childLineItem->getPayload();
  151.                 if (array_key_exists('netzp_event'$payload)) {
  152.                     $ticket $payload['netzp_event'];
  153.                     $criteria = new Criteria([ $ticket['id'] ]);
  154.                     $criteria->addAssociations([ 'event''participants''event.product' ]);
  155.                     $slot $repoSlots->search($criteria$context)->getEntities()->first();
  156.                     $numberOfParticipants count($ticket['participants']);
  157.                     $newTicketsBooked     $slot->getTicketsBooked() - $numberOfParticipants;
  158.                     $repoSlots->update([
  159.                         [
  160.                             'id' => $slot->getId(),
  161.                             'ticketsBooked' => $newTicketsBooked
  162.                         ]
  163.                     ], $context);
  164.                     $this->helper->invalidateProductCache($slot->getEvent()->getProductid());
  165.                     $this->helper->invalidateEventListing();
  166.                     foreach ($ticket['participants'] as $participantNo => $participant) {
  167.                         $participantId $ticket['participantsIds'][$participantNo];
  168.                         $repoParticipants->update([
  169.                             [
  170.                                 'id' => $participantId,
  171.                                 'status' => ParticipantEntity::STATUS_CANCELED
  172.                             ]
  173.                         ], $context);
  174.                     }
  175.                 }
  176.             }
  177.         }
  178.     }
  179.     public function processOrder(CheckoutOrderPlacedEvent $event) {
  180.         $order   $event->getOrder();
  181.         $context $event->getContext();
  182.         $orderId $order->getId();
  183.         $pluginConfig      $this->config->get('NetzpEvents6.config'$event->getSalesChannelId());
  184.         $repo              $this->container->get('s_plugin_netzp_events_slots.repository');
  185.         $repoParticipants  $this->container->get('s_plugin_netzp_events_participants.repository');
  186.         $lineItems         $order->getLineItems();
  187.         $tickets           = [];
  188.         $firstTicketNumber '';
  189.         $n                 0;
  190.         foreach ($lineItems as $lineItem) {
  191.             if ($lineItem->getType() !== WorkshopCartLineItemHandler::CHILD_PRODUCT_TYPE) {
  192.                 continue;
  193.             }
  194.             $payload      $lineItem->getPayload();
  195.             $productLabel $lineItem->getLabel();
  196.             if (array_key_exists('netzp_event'$payload)) {
  197.                 $ticket $payload['netzp_event'];
  198.                 $criteria = new Criteria([ $ticket['id'] ]);
  199.                 $criteria->addAssociations([ 'event''participants''event.product' ]);
  200.                 $slot $repo->search($criteria$context)->getEntities()->first();
  201.                 foreach ($ticket['participants'] as $index => $participant) {
  202.                     $participantId $ticket['participantsIds'][$index];
  203.                     $ticketNumber '';
  204.                     if ($pluginConfig['createpdf']) {
  205.                         $ticketNumber $this->numberRangeValueGenerator->getValue(
  206.                             'event_ticket',
  207.                             $context,
  208.                             $event->getSalesChannelId()
  209.                         );
  210.                         if ($n === 0) {
  211.                             $firstTicketNumber $ticketNumber;
  212.                         }
  213.                         $tickets[] = [
  214.                             'participant' => $participant,
  215.                             'number' => $ticketNumber,
  216.                             'slot' => $slot,
  217.                             'product' => $productLabel
  218.                         ];
  219.                     }
  220.                     $repoParticipants->update([
  221.                         [
  222.                             'id' => $participantId,
  223.                             'ticketNumber' => $ticketNumber,
  224.                             'orderId' => $orderId
  225.                         ]
  226.                     ], $context);
  227.                     $n++;
  228.                 }
  229.             }
  230.         }
  231.         if ($pluginConfig['createpdf'] && $firstTicketNumber) {
  232.             $repoAddress     $this->container->get('order_address.repository');
  233.             $criteriaAddress = new Criteria([ $order->getBillingAddressId() ]);
  234.             $billingAddress  $repoAddress->search($criteriaAddress$context)->getEntities()->first();
  235.             $pluginConfig    $this->config->get('NetzpEvents6.config');
  236.             $config DocumentConfigurationFactory::createConfiguration([
  237.                 'tickets' => $tickets,
  238.                 'documentDate' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  239.                 'documentNumber' => $firstTicketNumber,
  240.                 'billingAddress' => $billingAddress,
  241.                 'withoutParticipants' => (bool)$pluginConfig['anonymous']
  242.             ]);
  243.             $doc $this->documentService->create(
  244.                 $orderId'event_ticket'FileTypes::PDF$config$contextnullfalse
  245.             );
  246.             // for testing document generation only
  247.             /*
  248.             echo $doc->getDeepLinkCode() . " - ";
  249.             echo "<a href='/api/v3/_action/document/" . $doc->getId() . "/" . $doc->getDeepLinkCode() . "' target='_blank'>Download</a>";
  250.             die;
  251.             */
  252.         }
  253.     }
  254. }