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_Grouped
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_link')),
76: 'e.entity_id = l.product_id AND l.link_type_id=' . Mage_Catalog_Model_Product_Link::LINK_TYPE_GROUPED,
77: array())
78: ->joinLeft(
79: array('le' => $this->getTable('catalog/product')),
80: 'le.entity_id = l.linked_product_id',
81: array())
82: ->joinLeft(
83: array('i' => $idxTable),
84: 'i.product_id = l.linked_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:
92: $psExpr = $this->_addAttributeToSelect($select, 'status', 'e.entity_id', 'cs.store_id');
93: $psCond = $adapter->quoteInto($psExpr . '=?', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
94:
95: if ($this->_isManageStock()) {
96: $statusExpr = $adapter->getCheckSql('cisi.use_config_manage_stock = 0 AND cisi.manage_stock = 0',
97: 1, 'cisi.is_in_stock');
98: } else {
99: $statusExpr = $adapter->getCheckSql('cisi.use_config_manage_stock = 0 AND cisi.manage_stock = 1',
100: 'cisi.is_in_stock', 1);
101: }
102:
103: $optExpr = $adapter->getCheckSql("{$psCond} AND le.required_options = 0", 'i.stock_status', 0);
104: $stockStatusExpr = $adapter->getLeastSql(array("MAX({$optExpr})", "MIN({$statusExpr})"));
105:
106: $select->columns(array(
107: 'status' => $stockStatusExpr
108: ));
109:
110: if (!is_null($entityIds)) {
111: $select->where('e.entity_id IN(?)', $entityIds);
112: }
113:
114: return $select;
115: }
116: }
117: