PlaceReservationsForSalesEvent.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventorySales\Model;
  8. use Magento\Framework\Serialize\SerializerInterface;
  9. use Magento\InventoryReservationsApi\Model\AppendReservationsInterface;
  10. use Magento\InventoryReservationsApi\Model\ReservationBuilderInterface;
  11. use Magento\InventorySalesApi\Api\Data\ItemToSellInterface;
  12. use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
  13. use Magento\InventorySalesApi\Api\Data\SalesEventInterface;
  14. use Magento\InventorySalesApi\Api\GetStockBySalesChannelInterface;
  15. use Magento\InventorySalesApi\Api\PlaceReservationsForSalesEventInterface;
  16. use Magento\InventoryConfigurationApi\Model\IsSourceItemManagementAllowedForProductTypeInterface;
  17. use Magento\InventoryCatalogApi\Model\GetProductTypesBySkusInterface;
  18. /**
  19. * @inheritdoc
  20. */
  21. class PlaceReservationsForSalesEvent implements PlaceReservationsForSalesEventInterface
  22. {
  23. /**
  24. * @var ReservationBuilderInterface
  25. */
  26. private $reservationBuilder;
  27. /**
  28. * @var AppendReservationsInterface
  29. */
  30. private $appendReservations;
  31. /**
  32. * @var GetStockBySalesChannelInterface
  33. */
  34. private $getStockBySalesChannel;
  35. /**
  36. * @var GetProductTypesBySkusInterface
  37. */
  38. private $getProductTypesBySkus;
  39. /**
  40. * @var IsSourceItemManagementAllowedForProductTypeInterface
  41. */
  42. private $isSourceItemManagementAllowedForProductType;
  43. /**
  44. * @var SerializerInterface
  45. */
  46. private $serializer;
  47. /**
  48. * @var SalesEventToArrayConverter
  49. */
  50. private $salesEventToArrayConverter;
  51. /**
  52. * @param ReservationBuilderInterface $reservationBuilder
  53. * @param AppendReservationsInterface $appendReservations
  54. * @param GetStockBySalesChannelInterface $getStockBySalesChannel
  55. * @param GetProductTypesBySkusInterface $getProductTypesBySkus
  56. * @param IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType
  57. * @param SerializerInterface $serializer
  58. * @param SalesEventToArrayConverter $salesEventToArrayConverter
  59. */
  60. public function __construct(
  61. ReservationBuilderInterface $reservationBuilder,
  62. AppendReservationsInterface $appendReservations,
  63. GetStockBySalesChannelInterface $getStockBySalesChannel,
  64. GetProductTypesBySkusInterface $getProductTypesBySkus,
  65. IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType,
  66. SerializerInterface $serializer,
  67. SalesEventToArrayConverter $salesEventToArrayConverter
  68. ) {
  69. $this->reservationBuilder = $reservationBuilder;
  70. $this->appendReservations = $appendReservations;
  71. $this->getStockBySalesChannel = $getStockBySalesChannel;
  72. $this->getProductTypesBySkus = $getProductTypesBySkus;
  73. $this->isSourceItemManagementAllowedForProductType = $isSourceItemManagementAllowedForProductType;
  74. $this->serializer = $serializer;
  75. $this->salesEventToArrayConverter = $salesEventToArrayConverter;
  76. }
  77. /**
  78. * @inheritdoc
  79. */
  80. public function execute(array $items, SalesChannelInterface $salesChannel, SalesEventInterface $salesEvent): void
  81. {
  82. if (empty($items)) {
  83. return;
  84. }
  85. $stockId = $this->getStockBySalesChannel->execute($salesChannel)->getStockId();
  86. $skus = [];
  87. /** @var ItemToSellInterface $item */
  88. foreach ($items as $item) {
  89. $skus[] = $item->getSku();
  90. }
  91. $productTypes = $this->getProductTypesBySkus->execute($skus);
  92. $reservations = [];
  93. /** @var ItemToSellInterface $item */
  94. foreach ($items as $item) {
  95. $currentSku = $item->getSku();
  96. $skuNotExistInCatalog = !isset($productTypes[$currentSku]);
  97. if ($skuNotExistInCatalog ||
  98. $this->isSourceItemManagementAllowedForProductType->execute($productTypes[$currentSku])) {
  99. $reservations[] = $this->reservationBuilder
  100. ->setSku($item->getSku())
  101. ->setQuantity((float)$item->getQuantity())
  102. ->setStockId($stockId)
  103. ->setMetadata($this->serializer->serialize($this->salesEventToArrayConverter->execute($salesEvent)))
  104. ->build();
  105. }
  106. }
  107. $this->appendReservations->execute($reservations);
  108. }
  109. }