UpdateLegacyStockItemConfigurationAtSourceItemConfigurationSavePlugin.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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\InventoryLowQuantityNotification\Plugin\InventoryLowQuantityNotificationApi;
  8. use Magento\CatalogInventory\Api\Data\StockItemInterface;
  9. use Magento\Framework\App\ResourceConnection;
  10. use Magento\InventoryLowQuantityNotificationApi\Api\Data\SourceItemConfigurationInterface;
  11. use Magento\InventoryLowQuantityNotificationApi\Api\SourceItemConfigurationsSaveInterface;
  12. use Magento\InventoryCatalogApi\Api\DefaultSourceProviderInterface;
  13. use Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface;
  14. use Magento\InventoryCatalogApi\Model\GetProductIdsBySkusInterface;
  15. use Magento\InventoryCatalogApi\Model\IsSingleSourceModeInterface;
  16. class UpdateLegacyStockItemConfigurationAtSourceItemConfigurationSavePlugin
  17. {
  18. /**
  19. * @var IsSingleSourceModeInterface
  20. */
  21. private $isSingleSourceMode;
  22. /**
  23. * @var ResourceConnection
  24. */
  25. private $resourceConnection;
  26. /**
  27. * @var DefaultSourceProviderInterface
  28. */
  29. private $defaultSourceProvider;
  30. /**
  31. * @var DefaultStockProviderInterface
  32. */
  33. private $defaultStockProvider;
  34. /**
  35. * @var GetProductIdsBySkusInterface
  36. */
  37. private $getProductIdsBySkus;
  38. /**
  39. * @param IsSingleSourceModeInterface $isSingleSourceMode
  40. * @param ResourceConnection $resourceConnection
  41. * @param DefaultSourceProviderInterface $defaultSourceProvider
  42. * @param DefaultStockProviderInterface $defaultStockProvider
  43. * @param GetProductIdsBySkusInterface $getProductIdsBySkus
  44. */
  45. public function __construct(
  46. IsSingleSourceModeInterface $isSingleSourceMode,
  47. ResourceConnection $resourceConnection,
  48. DefaultSourceProviderInterface $defaultSourceProvider,
  49. DefaultStockProviderInterface $defaultStockProvider,
  50. GetProductIdsBySkusInterface $getProductIdsBySkus
  51. ) {
  52. $this->isSingleSourceMode = $isSingleSourceMode;
  53. $this->resourceConnection = $resourceConnection;
  54. $this->defaultSourceProvider = $defaultSourceProvider;
  55. $this->defaultStockProvider = $defaultStockProvider;
  56. $this->getProductIdsBySkus = $getProductIdsBySkus;
  57. }
  58. /**
  59. * @param SourceItemConfigurationsSaveInterface $subject
  60. * @param $result
  61. * @param SourceItemConfigurationInterface[] $sourceItemConfigurations
  62. * @return void
  63. *
  64. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  65. */
  66. public function afterExecute(
  67. SourceItemConfigurationsSaveInterface $subject,
  68. $result,
  69. array $sourceItemConfigurations
  70. ): void {
  71. if ($this->isSingleSourceMode->execute()) {
  72. return;
  73. }
  74. $sourceItemsConfigurationToUpdate = [];
  75. $skus = [];
  76. foreach ($sourceItemConfigurations as $sourceItemConfiguration) {
  77. if ($sourceItemConfiguration->getSourceCode() !== $this->defaultSourceProvider->getCode()) {
  78. continue;
  79. }
  80. $skus[] = $sourceItemConfiguration->getSku();
  81. $notifyStockQty = $sourceItemConfiguration->getNotifyStockQty();
  82. $sourceItemConfigurationData[StockItemInterface::USE_CONFIG_NOTIFY_STOCK_QTY] = $notifyStockQty ? 0 : 1;
  83. $sourceItemConfigurationData[StockItemInterface::NOTIFY_STOCK_QTY] = $notifyStockQty ?? 1;
  84. $sourceItemsConfigurationToUpdate[$sourceItemConfiguration->getSku()] = $sourceItemConfigurationData;
  85. }
  86. if (empty($sourceItemsConfigurationToUpdate)) {
  87. return;
  88. }
  89. $productIds = $this->getProductIdsBySkus->execute($skus);
  90. foreach ($sourceItemsConfigurationToUpdate as $sku => &$sourceItemConfiguration) {
  91. if (!empty($productIds[$sku])) {
  92. $sourceItemConfiguration[StockItemInterface::PRODUCT_ID] = $productIds[$sku];
  93. } else {
  94. unset($sourceItemsConfigurationToUpdate[$sku]);
  95. }
  96. }
  97. $connection = $this->resourceConnection->getConnection();
  98. $tableName = $this->resourceConnection
  99. ->getTableName('cataloginventory_stock_item');
  100. $columnsSql = $this->buildColumnsSqlPart([
  101. StockItemInterface::STOCK_ID,
  102. StockItemInterface::PRODUCT_ID,
  103. StockItemInterface::NOTIFY_STOCK_QTY,
  104. StockItemInterface::USE_CONFIG_NOTIFY_STOCK_QTY
  105. ]);
  106. $valuesSql = $this->buildValuesSqlPart($sourceItemsConfigurationToUpdate);
  107. $onDuplicateSql = $this->buildOnDuplicateSqlPart([
  108. StockItemInterface::NOTIFY_STOCK_QTY,
  109. StockItemInterface::USE_CONFIG_NOTIFY_STOCK_QTY
  110. ]);
  111. $bind = $this->getSqlBindData($sourceItemsConfigurationToUpdate);
  112. $insertSql = sprintf(
  113. 'INSERT INTO %s (%s) VALUES %s %s',
  114. $tableName,
  115. $columnsSql,
  116. $valuesSql,
  117. $onDuplicateSql
  118. );
  119. $connection->query($insertSql, $bind);
  120. }
  121. /**
  122. * @param array $columns
  123. * @return string
  124. */
  125. private function buildColumnsSqlPart(array $columns): string
  126. {
  127. $connection = $this->resourceConnection->getConnection();
  128. $processedColumns = array_map([$connection, 'quoteIdentifier'], $columns);
  129. $sql = implode(', ', $processedColumns);
  130. return $sql;
  131. }
  132. /**
  133. * @param array $sourceItemConfigurations
  134. * @return string
  135. */
  136. private function buildValuesSqlPart(array $sourceItemConfigurations): string
  137. {
  138. $sql = rtrim(str_repeat('(?, ?, ?, ?), ', count($sourceItemConfigurations)), ', ');
  139. return $sql;
  140. }
  141. /**
  142. * @param array $sourceItemConfigurations
  143. * @return array
  144. */
  145. private function getSqlBindData(array $sourceItemConfigurations): array
  146. {
  147. $bind = [];
  148. foreach ($sourceItemConfigurations as $sourceItemConfiguration) {
  149. $bind = array_merge($bind, [
  150. $this->defaultStockProvider->getId(),
  151. $sourceItemConfiguration[StockItemInterface::PRODUCT_ID],
  152. $sourceItemConfiguration[StockItemInterface::NOTIFY_STOCK_QTY],
  153. $sourceItemConfiguration[StockItemInterface::USE_CONFIG_NOTIFY_STOCK_QTY],
  154. ]);
  155. }
  156. return $bind;
  157. }
  158. /**
  159. * @param array $fields
  160. * @return string
  161. */
  162. private function buildOnDuplicateSqlPart(array $fields): string
  163. {
  164. $connection = $this->resourceConnection->getConnection();
  165. $processedFields = [];
  166. foreach ($fields as $field) {
  167. $processedFields[] = sprintf('%1$s = VALUES(%1$s)', $connection->quoteIdentifier($field));
  168. }
  169. $sql = 'ON DUPLICATE KEY UPDATE ' . implode(', ', $processedFields);
  170. return $sql;
  171. }
  172. }