1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_CatalogInventory_Model_Resource_Indexer_Stock_Configurable
36: extends Mage_CatalogInventory_Model_Resource_Indexer_Stock_Default
37: {
38: 39: 40: 41: 42: 43:
44: public function reindexEntity($entityIds)
45: {
46: $this->_updateIndex($entityIds);
47: return $this;
48: }
49:
50: 51: 52: 53: 54: 55: 56:
57: protected function _getStockStatusSelect($entityIds = null, $usePrimaryTable = false)
58: {
59: $adapter = $this->_getWriteAdapter();
60: $idxTable = $usePrimaryTable ? $this->getMainTable() : $this->getIdxTable();
61: $select = $adapter->select()
62: ->from(array('e' => $this->getTable('catalog/product')), array('entity_id'));
63: $this->_addWebsiteJoinToSelect($select, true);
64: $this->_addProductWebsiteJoinToSelect($select, 'cw.website_id', 'e.entity_id');
65: $select->columns('cw.website_id')
66: ->join(
67: array('cis' => $this->getTable('cataloginventory/stock')),
68: '',
69: array('stock_id'))
70: ->joinLeft(
71: array('cisi' => $this->getTable('cataloginventory/stock_item')),
72: 'cisi.stock_id = cis.stock_id AND cisi.product_id = e.entity_id',
73: array())
74: ->joinLeft(
75: array('l' => $this->getTable('catalog/product_super_link')),
76: 'l.parent_id = e.entity_id',
77: array())
78: ->join(
79: array('le' => $this->getTable('catalog/product')),
80: 'le.entity_id = l.product_id',
81: array())
82: ->joinLeft(
83: array('i' => $idxTable),
84: 'i.product_id = l.product_id AND cw.website_id = i.website_id AND cis.stock_id = i.stock_id',
85: array())
86: ->columns(array('qty' => new Zend_Db_Expr('0')))
87: ->where('cw.website_id != 0')
88: ->where('e.type_id = ?', $this->getTypeId())
89: ->group(array('e.entity_id', 'cw.website_id', 'cis.stock_id'));
90:
91: $psExpr = $this->_addAttributeToSelect($select, 'status', 'e.entity_id', 'cs.store_id');
92: $psCond = $adapter->quoteInto($psExpr . '=?', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
93:
94: if ($this->_isManageStock()) {
95: $statusExpr = $adapter->getCheckSql('cisi.use_config_manage_stock = 0 AND cisi.manage_stock = 0',
96: 1, 'cisi.is_in_stock');
97: } else {
98: $statusExpr = $adapter->getCheckSql('cisi.use_config_manage_stock = 0 AND cisi.manage_stock = 1',
99: 'cisi.is_in_stock', 1);
100: }
101:
102: $optExpr = $adapter->getCheckSql("{$psCond} AND le.required_options = 0", 'i.stock_status', 0);
103: $stockStatusExpr = $adapter->getLeastSql(array("MAX({$optExpr})", "MIN({$statusExpr})"));
104:
105: $select->columns(array(
106: 'status' => $stockStatusExpr
107: ));
108:
109: if (!is_null($entityIds)) {
110: $select->where('e.entity_id IN(?)', $entityIds);
111: }
112:
113: return $select;
114: }
115: }
116: