custom/plugins/SwagCommercial/src/MultiWarehouse/Subscriber/CheckoutOrderPlacedSubscriber.php line 46

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Commercial\MultiWarehouse\Subscriber;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Commercial\Licensing\License;
  5. use Shopware\Commercial\MultiWarehouse\Domain\Order\MultiWarehouseStockUpdater;
  6. use Shopware\Commercial\MultiWarehouse\Domain\Product\MultiWarehouseProductFilter;
  7. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  8. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  9. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\Uuid\Uuid;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. /**
  14.  * @internal
  15.  *
  16.  * @package inventory
  17.  */
  18. class CheckoutOrderPlacedSubscriber implements EventSubscriberInterface
  19. {
  20.     private MultiWarehouseStockUpdater $stockUpdater;
  21.     private MultiWarehouseProductFilter $productFilter;
  22.     private Connection $connection;
  23.     public function __construct(
  24.         MultiWarehouseStockUpdater $stockUpdater,
  25.         MultiWarehouseProductFilter $productFilter,
  26.         Connection $connection
  27.     ) {
  28.         $this->stockUpdater $stockUpdater;
  29.         $this->productFilter $productFilter;
  30.         $this->connection $connection;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             CheckoutOrderPlacedEvent::class => 'onOrderPlaced',
  36.         ];
  37.     }
  38.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void
  39.     {
  40.         if (!License::get('MULTI_INVENTORY-3749997')) {
  41.             return;
  42.         }
  43.         $lineItems $event->getOrder()->getLineItems();
  44.         if ($lineItems === null || $lineItems->count() <= 0) {
  45.             return;
  46.         }
  47.         $groupIds $this->getOrderWarehouseGroups($event->getContext(), $event->getOrder()->getId());
  48.         if (empty($groupIds)) {
  49.             return;
  50.         }
  51.         /** @var list<string> $lineItemRefIds */
  52.         $lineItemRefIds $lineItems->fmap(static function (OrderLineItemEntity $orderLineItem) {
  53.             if ($orderLineItem->getType() !== LineItem::PRODUCT_LINE_ITEM_TYPE) {
  54.                 return null;
  55.             }
  56.             return $orderLineItem->getReferencedId();
  57.         });
  58.         $warehouseLineItemIds $this->productFilter->filterProductIdsWithWarehouses($lineItemRefIds);
  59.         if (empty($warehouseLineItemIds)) {
  60.             return;
  61.         }
  62.         $ids = [];
  63.         /** @var OrderLineItemEntity $lineItem */
  64.         foreach ($lineItems as $lineItem) {
  65.             if (!\in_array($lineItem->getReferencedId(), $warehouseLineItemIdstrue)) {
  66.                 continue;
  67.             }
  68.             if (!\array_key_exists($lineItem->getReferencedId(), $ids)) {
  69.                 $ids[$lineItem->getReferencedId()] = 0;
  70.             }
  71.             $ids[$lineItem->getReferencedId()] += $lineItem->getQuantity();
  72.         }
  73.         $this->stockUpdater->update(
  74.             $event->getContext(),
  75.             $event->getOrderId(),
  76.             $groupIds,
  77.             $ids
  78.         );
  79.     }
  80.     /**
  81.      * @return list<string>
  82.      */
  83.     private function getOrderWarehouseGroups(Context $contextstring $orderId): array
  84.     {
  85.         /** @var list<string> $groups */
  86.         $groups $this->connection->fetchFirstColumn(
  87.             'SELECT LOWER(HEX(warehouse_group_id)) FROM order_warehouse_group WHERE order_id = :orderId AND order_version_id = :versionId',
  88.             [
  89.                 'orderId' => Uuid::fromHexToBytes($orderId),
  90.                 'versionId' => Uuid::fromHexToBytes($context->getVersionId()),
  91.             ]
  92.         );
  93.         return $groups;
  94.     }
  95. }