GetSalableQuantityDataBySku.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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\InventorySalesAdminUi\Model;
  8. use Magento\InventorySalesAdminUi\Model\ResourceModel\GetAssignedStockIdsBySku;
  9. use Magento\InventoryApi\Api\StockRepositoryInterface;
  10. use Magento\InventorySalesApi\Api\GetProductSalableQtyInterface;
  11. use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
  12. /**
  13. * Get salable quantity data by sku
  14. */
  15. class GetSalableQuantityDataBySku
  16. {
  17. /**
  18. * @var GetProductSalableQtyInterface
  19. */
  20. private $getProductSalableQty;
  21. /**
  22. * @var StockRepositoryInterface
  23. */
  24. private $stockRepository;
  25. /**
  26. * @var GetAssignedStockIdsBySku
  27. */
  28. private $getAssignedStockIdsBySku;
  29. /**
  30. * @var GetStockItemConfigurationInterface
  31. */
  32. private $getStockItemConfiguration;
  33. /**
  34. * @param GetProductSalableQtyInterface $getProductSalableQty
  35. * @param StockRepositoryInterface $stockRepository
  36. * @param GetAssignedStockIdsBySku $getAssignedStockIdsBySku
  37. * @param GetStockItemConfigurationInterface $getStockItemConfiguration
  38. */
  39. public function __construct(
  40. GetProductSalableQtyInterface $getProductSalableQty,
  41. StockRepositoryInterface $stockRepository,
  42. GetAssignedStockIdsBySku $getAssignedStockIdsBySku,
  43. GetStockItemConfigurationInterface $getStockItemConfiguration
  44. ) {
  45. $this->getProductSalableQty = $getProductSalableQty;
  46. $this->stockRepository = $stockRepository;
  47. $this->getAssignedStockIdsBySku = $getAssignedStockIdsBySku;
  48. $this->getStockItemConfiguration = $getStockItemConfiguration;
  49. }
  50. /**
  51. * @param string $sku
  52. * @return array
  53. */
  54. public function execute(string $sku): array
  55. {
  56. $stockInfo = [];
  57. $stockIds = $this->getAssignedStockIdsBySku->execute($sku);
  58. if (count($stockIds)) {
  59. foreach ($stockIds as $stockId) {
  60. $stockId = (int)$stockId;
  61. $stock = $this->stockRepository->get($stockId);
  62. $stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);
  63. $isManageStock = $stockItemConfiguration->isManageStock();
  64. $stockInfo[] = [
  65. 'stock_name' => $stock->getName(),
  66. 'qty' => $isManageStock ? $this->getProductSalableQty->execute($sku, $stockId) : null,
  67. 'manage_stock' => $isManageStock,
  68. ];
  69. }
  70. }
  71. return $stockInfo;
  72. }
  73. }