ApplyConfigurationCondition.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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\Model\ResourceModel\Rss\NotifyStock;
  8. use Magento\CatalogInventory\Api\StockConfigurationInterface;
  9. use Magento\Framework\App\ResourceConnection;
  10. use Magento\Framework\DB\Select;
  11. class ApplyConfigurationCondition
  12. {
  13. /**
  14. * @var StockConfigurationInterface
  15. */
  16. private $configuration;
  17. /**
  18. * @var ResourceConnection
  19. */
  20. private $resourceConnection;
  21. /**
  22. * @param StockConfigurationInterface $stockConfiguration
  23. * @param ResourceConnection $resourceConnection
  24. */
  25. public function __construct(
  26. StockConfigurationInterface $stockConfiguration,
  27. ResourceConnection $resourceConnection
  28. ) {
  29. $this->configuration = $stockConfiguration;
  30. $this->resourceConnection = $resourceConnection;
  31. }
  32. /**
  33. * @param Select $select
  34. * @return void
  35. */
  36. public function execute(Select $select)
  37. {
  38. $configManageStock = $this->configuration->getManageStock();
  39. $configNotifyStockQty = $this->configuration->getNotifyStockQty();
  40. $connection = $this->resourceConnection->getConnection();
  41. $qtyCondition = $connection->getIfNullSql(
  42. 'source_item_config.notify_stock_qty',
  43. $configNotifyStockQty
  44. );
  45. $globalManageStockEnabledCondition = implode(
  46. [
  47. $connection->prepareSqlCondition('legacy_stock_item.use_config_manage_stock', 1),
  48. $connection->prepareSqlCondition($configManageStock, 1),
  49. $connection->prepareSqlCondition('main_table.quantity', ['lt' => $qtyCondition]),
  50. ],
  51. ' ' . Select::SQL_AND . ' '
  52. );
  53. $globalManageStockDisabledCondition = implode(
  54. [
  55. $connection->prepareSqlCondition('legacy_stock_item.use_config_manage_stock', 0),
  56. $connection->prepareSqlCondition('legacy_stock_item.manage_stock', 1),
  57. $connection->prepareSqlCondition('main_table.quantity', ['lt' => $qtyCondition]),
  58. ],
  59. ' ' . Select::SQL_AND . ' '
  60. );
  61. $condition = implode(
  62. [
  63. $globalManageStockEnabledCondition,
  64. $globalManageStockDisabledCondition,
  65. ],
  66. ') ' . Select::SQL_OR . ' ('
  67. );
  68. $select->where('(' . $condition . ')');
  69. }
  70. }