SelectBuilder.php 4.3 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\InventoryIndexer\Indexer;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\Framework\DB\Select;
  10. use Magento\Inventory\Model\ResourceModel\Source as SourceResourceModel;
  11. use Magento\Inventory\Model\ResourceModel\SourceItem as SourceItemResourceModel;
  12. use Magento\Inventory\Model\ResourceModel\StockSourceLink as StockSourceLinkResourceModel;
  13. use Magento\Inventory\Model\StockSourceLink;
  14. use Magento\InventoryApi\Api\Data\SourceInterface;
  15. use Magento\InventoryApi\Api\Data\SourceItemInterface;
  16. use Magento\InventorySales\Model\ResourceModel\IsStockItemSalableCondition\GetIsStockItemSalableConditionInterface;
  17. /**
  18. * Select builder
  19. */
  20. class SelectBuilder
  21. {
  22. /**
  23. * @var ResourceConnection
  24. */
  25. private $resourceConnection;
  26. /**
  27. * @var GetIsStockItemSalableConditionInterface
  28. */
  29. private $getIsStockItemSalableCondition;
  30. /**
  31. * @var string
  32. */
  33. private $productTableName;
  34. /**
  35. * @param ResourceConnection $resourceConnection
  36. * @param GetIsStockItemSalableConditionInterface $getIsStockItemSalableCondition
  37. * @param string $productTableName
  38. */
  39. public function __construct(
  40. ResourceConnection $resourceConnection,
  41. GetIsStockItemSalableConditionInterface $getIsStockItemSalableCondition,
  42. string $productTableName
  43. ) {
  44. $this->resourceConnection = $resourceConnection;
  45. $this->getIsStockItemSalableCondition = $getIsStockItemSalableCondition;
  46. $this->productTableName = $productTableName;
  47. }
  48. /**
  49. * @param int $stockId
  50. * @return Select
  51. */
  52. public function execute(int $stockId): Select
  53. {
  54. $connection = $this->resourceConnection->getConnection();
  55. $sourceItemTable = $this->resourceConnection->getTableName(SourceItemResourceModel::TABLE_NAME_SOURCE_ITEM);
  56. $quantityExpression = (string)$this->resourceConnection->getConnection()->getCheckSql(
  57. 'source_item.' . SourceItemInterface::STATUS . ' = ' . SourceItemInterface::STATUS_OUT_OF_STOCK,
  58. 0,
  59. SourceItemInterface::QUANTITY
  60. );
  61. $sourceCodes = $this->getSourceCodes($stockId);
  62. $select = $connection->select();
  63. $select->joinLeft(
  64. ['product' => $this->resourceConnection->getTableName($this->productTableName)],
  65. 'product.sku = source_item.' . SourceItemInterface::SKU,
  66. []
  67. )->joinLeft(
  68. ['legacy_stock_item' => $this->resourceConnection->getTableName('cataloginventory_stock_item')],
  69. 'product.entity_id = legacy_stock_item.product_id',
  70. []
  71. );
  72. $select->from(
  73. ['source_item' => $sourceItemTable],
  74. [
  75. SourceItemInterface::SKU,
  76. IndexStructure::QUANTITY => 'SUM(' . $quantityExpression . ')',
  77. IndexStructure::IS_SALABLE => $this->getIsStockItemSalableCondition->execute($select),
  78. ]
  79. )
  80. ->where('source_item.' . SourceItemInterface::SOURCE_CODE . ' IN (?)', $sourceCodes)
  81. ->group([SourceItemInterface::SKU]);
  82. return $select;
  83. }
  84. /**
  85. * Get all enabled sources related to stock
  86. *
  87. * @param int $stockId
  88. * @return array
  89. */
  90. private function getSourceCodes(int $stockId): array
  91. {
  92. $connection = $this->resourceConnection->getConnection();
  93. $sourceTable = $this->resourceConnection->getTableName(SourceResourceModel::TABLE_NAME_SOURCE);
  94. $sourceStockLinkTable = $this->resourceConnection->getTableName(
  95. StockSourceLinkResourceModel::TABLE_NAME_STOCK_SOURCE_LINK
  96. );
  97. $select = $connection->select()
  98. ->from(['source' => $sourceTable], [SourceInterface::SOURCE_CODE])
  99. ->joinInner(
  100. ['stock_source_link' => $sourceStockLinkTable],
  101. 'source.' . SourceItemInterface::SOURCE_CODE . ' = stock_source_link.' . StockSourceLink::SOURCE_CODE,
  102. []
  103. )
  104. ->where('stock_source_link.' . StockSourceLink::STOCK_ID . ' = ?', $stockId)
  105. ->where(SourceInterface::ENABLED . ' = ?', 1);
  106. $sourceCodes = $connection->fetchCol($select);
  107. return $sourceCodes;
  108. }
  109. }