123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- declare(strict_types=1);
- namespace Magento\InventorySales\Plugin\Sales\OrderManagement;
- use Magento\InventoryCatalogApi\Model\GetProductTypesBySkusInterface;
- use Magento\InventoryConfigurationApi\Model\IsSourceItemManagementAllowedForProductTypeInterface;
- use Magento\Sales\Api\Data\OrderInterface;
- use Magento\Sales\Api\OrderManagementInterface;
- use Magento\InventorySalesApi\Api\PlaceReservationsForSalesEventInterface;
- use Magento\InventorySalesApi\Api\Data\SalesEventInterface;
- use Magento\InventorySalesApi\Api\Data\SalesEventInterfaceFactory;
- use Magento\InventoryCatalogApi\Model\GetSkusByProductIdsInterface;
- use Magento\Store\Api\WebsiteRepositoryInterface;
- use Magento\InventorySalesApi\Api\Data\SalesChannelInterfaceFactory;
- use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
- use Magento\InventorySalesApi\Api\Data\ItemToSellInterfaceFactory;
- use Magento\InventorySales\Model\CheckItemsQuantity;
- use Magento\InventorySalesApi\Model\StockByWebsiteIdResolverInterface;
- class AppendReservationsAfterOrderPlacementPlugin
- {
- /**
- * @var PlaceReservationsForSalesEventInterface
- */
- private $placeReservationsForSalesEvent;
- /**
- * @var GetSkusByProductIdsInterface
- */
- private $getSkusByProductIds;
- /**
- * @var WebsiteRepositoryInterface
- */
- private $websiteRepository;
- /**
- * @var SalesChannelInterfaceFactory
- */
- private $salesChannelFactory;
- /**
- * @var SalesEventInterfaceFactory
- */
- private $salesEventFactory;
- /**
- * @var ItemToSellInterfaceFactory
- */
- private $itemsToSellFactory;
- /**
- * @var CheckItemsQuantity
- */
- private $checkItemsQuantity;
- /**
- * @var StockByWebsiteIdResolverInterface
- */
- private $stockByWebsiteIdResolver;
- /**
- * @var GetProductTypesBySkusInterface
- */
- private $getProductTypesBySkus;
- /**
- * @var IsSourceItemManagementAllowedForProductTypeInterface
- */
- private $isSourceItemManagementAllowedForProductType;
- /**
- * @param PlaceReservationsForSalesEventInterface $placeReservationsForSalesEvent
- * @param GetSkusByProductIdsInterface $getSkusByProductIds
- * @param WebsiteRepositoryInterface $websiteRepository
- * @param SalesChannelInterfaceFactory $salesChannelFactory
- * @param SalesEventInterfaceFactory $salesEventFactory
- * @param ItemToSellInterfaceFactory $itemsToSellFactory
- * @param CheckItemsQuantity $checkItemsQuantity
- * @param StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver
- * @param GetProductTypesBySkusInterface $getProductTypesBySkus
- * @param IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType
- */
- public function __construct(
- PlaceReservationsForSalesEventInterface $placeReservationsForSalesEvent,
- GetSkusByProductIdsInterface $getSkusByProductIds,
- WebsiteRepositoryInterface $websiteRepository,
- SalesChannelInterfaceFactory $salesChannelFactory,
- SalesEventInterfaceFactory $salesEventFactory,
- ItemToSellInterfaceFactory $itemsToSellFactory,
- CheckItemsQuantity $checkItemsQuantity,
- StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver,
- GetProductTypesBySkusInterface $getProductTypesBySkus,
- IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType
- ) {
- $this->placeReservationsForSalesEvent = $placeReservationsForSalesEvent;
- $this->getSkusByProductIds = $getSkusByProductIds;
- $this->websiteRepository = $websiteRepository;
- $this->salesChannelFactory = $salesChannelFactory;
- $this->salesEventFactory = $salesEventFactory;
- $this->itemsToSellFactory = $itemsToSellFactory;
- $this->checkItemsQuantity = $checkItemsQuantity;
- $this->stockByWebsiteIdResolver = $stockByWebsiteIdResolver;
- $this->getProductTypesBySkus = $getProductTypesBySkus;
- $this->isSourceItemManagementAllowedForProductType = $isSourceItemManagementAllowedForProductType;
- }
- /**
- * @param OrderManagementInterface $subject
- * @param OrderInterface $order
- * @return OrderInterface
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- public function afterPlace(OrderManagementInterface $subject, OrderInterface $order) : OrderInterface
- {
- $itemsById = $itemsBySku = $itemsToSell = [];
- foreach ($order->getItems() as $item) {
- if (!isset($itemsById[$item->getProductId()])) {
- $itemsById[$item->getProductId()] = 0;
- }
- $itemsById[$item->getProductId()] += $item->getQtyOrdered();
- }
- $productSkus = $this->getSkusByProductIds->execute(array_keys($itemsById));
- $productTypes = $this->getProductTypesBySkus->execute($productSkus);
- foreach ($productSkus as $productId => $sku) {
- if (false === $this->isSourceItemManagementAllowedForProductType->execute($productTypes[$sku])) {
- continue;
- }
- $itemsBySku[$sku] = (float)$itemsById[$productId];
- $itemsToSell[] = $this->itemsToSellFactory->create([
- 'sku' => $sku,
- 'qty' => -(float)$itemsById[$productId]
- ]);
- }
- $websiteId = (int)$order->getStore()->getWebsiteId();
- $websiteCode = $this->websiteRepository->getById($websiteId)->getCode();
- $stockId = (int)$this->stockByWebsiteIdResolver->execute((int)$websiteId)->getStockId();
- $this->checkItemsQuantity->execute($itemsBySku, $stockId);
- /** @var SalesEventInterface $salesEvent */
- $salesEvent = $this->salesEventFactory->create([
- 'type' => SalesEventInterface::EVENT_ORDER_PLACED,
- 'objectType' => SalesEventInterface::OBJECT_TYPE_ORDER,
- 'objectId' => (string)$order->getEntityId()
- ]);
- $salesChannel = $this->salesChannelFactory->create([
- 'data' => [
- 'type' => SalesChannelInterface::TYPE_WEBSITE,
- 'code' => $websiteCode
- ]
- ]);
- $this->placeReservationsForSalesEvent->execute($itemsToSell, $salesChannel, $salesEvent);
- return $order;
- }
- }
|