Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
Stock.php
Go to the documentation of this file.
1 <?php
7 
10 
17 {
21  private $activeTableSwitcher;
22 
26  private $stockStatusSelectBuilder;
27 
31  private $bundleOptionStockDataSelectBuilder;
32 
45  public function __construct(
46  \Magento\Framework\Model\ResourceModel\Db\Context $context,
47  \Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy,
48  \Magento\Eav\Model\Config $eavConfig,
49  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
50  $connectionName = null,
51  \Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher $activeTableSwitcher = null,
52  StockStatusSelectBuilder $stockStatusSelectBuilder = null,
53  BundleOptionStockDataSelectBuilder $bundleOptionStockDataSelectBuilder = null
54  ) {
55  parent::__construct($context, $tableStrategy, $eavConfig, $scopeConfig, $connectionName);
56 
57  $this->activeTableSwitcher = $activeTableSwitcher ?: ObjectManager::getInstance()
58  ->get(\Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher::class);
59 
60  $this->stockStatusSelectBuilder = $stockStatusSelectBuilder ?: ObjectManager::getInstance()
61  ->get(StockStatusSelectBuilder::class);
62 
63  $this->bundleOptionStockDataSelectBuilder = $bundleOptionStockDataSelectBuilder ?: ObjectManager::getInstance()
64  ->get(BundleOptionStockDataSelectBuilder::class);
65  }
66 
72  protected function _getBundleOptionTable()
73  {
74  return $this->getTable('catalog_product_bundle_stock_index');
75  }
76 
84  protected function _prepareBundleOptionStockData($entityIds = null, $usePrimaryTable = false)
85  {
87  $connection = $this->getConnection();
89  ? $this->activeTableSwitcher->getAdditionalTableName($this->getMainTable())
90  : $this->getMainTable();
91  $idxTable = $usePrimaryTable ? $table : $this->getIdxTable();
92  $select = $this->bundleOptionStockDataSelectBuilder->buildSelect($idxTable);
93 
94  $status = new \Zend_Db_Expr(
95  'MAX('
96  . $connection->getCheckSql('e.required_options = 0', 'i.stock_status', '0')
97  . ')'
98  );
99 
100  $select->columns(['status' => $status]);
101 
102  if ($entityIds !== null) {
103  $select->where('product.entity_id IN(?)', $entityIds);
104  }
105 
106  // clone select for bundle product without required bundle options
107  $selectNonRequired = clone $select;
108 
109  $select->where('bo.required = ?', 1);
110  $selectNonRequired->where('bo.required = ?', 0)->having($status . ' = 1');
111  $query = $select->insertFromSelect($this->_getBundleOptionTable());
112  $connection->query($query);
113 
114  $query = $selectNonRequired->insertFromSelect($this->_getBundleOptionTable());
115  $connection->query($query);
116 
117  return $this;
118  }
119 
127  protected function _getStockStatusSelect($entityIds = null, $usePrimaryTable = false)
128  {
129  $this->_prepareBundleOptionStockData($entityIds, $usePrimaryTable);
130  $connection = $this->getConnection();
131 
132  $select = parent::_getStockStatusSelect($entityIds, $usePrimaryTable);
133  $select = $this->stockStatusSelectBuilder->buildSelect($select);
134 
135  $statusNotNullExpr = $connection->getCheckSql('o.stock_status IS NOT NULL', 'o.stock_status', '0');
136  $statusExpr = $this->getStatusExpression($connection);
137 
138  $select->columns(
139  [
140  'status' => $connection->getLeastSql(
141  [
142  new \Zend_Db_Expr('MIN(' . $statusNotNullExpr . ')'),
143  new \Zend_Db_Expr('MIN(' . $statusExpr . ')'),
144  ]
145  ),
146  ]
147  );
148 
149  if ($entityIds !== null) {
150  $select->where('e.entity_id IN(?)', $entityIds);
151  }
152 
153  return $select;
154  }
155 
162  protected function _prepareIndexTable($entityIds = null)
163  {
164  parent::_prepareIndexTable($entityIds);
166 
167  return $this;
168  }
169 
176  protected function _updateIndex($entityIds)
177  {
178  parent::_updateIndex($entityIds);
180 
181  return $this;
182  }
183 
189  protected function _cleanBundleOptionStockData()
190  {
191  $this->getConnection()->delete($this->_getBundleOptionTable());
192  return $this;
193  }
194 }
_getStockStatusSelect($entityIds=null, $usePrimaryTable=false)
Definition: Stock.php:127
getStatusExpression(AdapterInterface $connection, $isAggregate=false)
_prepareBundleOptionStockData($entityIds=null, $usePrimaryTable=false)
Definition: Stock.php:84
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy, \Magento\Eav\Model\Config $eavConfig, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, $connectionName=null, \Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher $activeTableSwitcher=null, StockStatusSelectBuilder $stockStatusSelectBuilder=null, BundleOptionStockDataSelectBuilder $bundleOptionStockDataSelectBuilder=null)
Definition: Stock.php:45
$status
Definition: order_status.php:8
$connection
Definition: bulk.php:13
$table
Definition: trigger.php:14