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_Bundle_Model_Resource_Indexer_Stock extends Mage_CatalogInventory_Model_Resource_Indexer_Stock_Default
36: {
37: 38: 39: 40: 41: 42:
43: public function reindexEntity($entityIds)
44: {
45: $this->_updateIndex($entityIds);
46:
47: return $this;
48: }
49:
50: 51: 52: 53: 54:
55: protected function _getBundleOptionTable()
56: {
57: return $this->getTable('bundle/stock_index');
58: }
59:
60: 61: 62: 63: 64: 65: 66:
67: protected function _prepareBundleOptionStockData($entityIds = null, $usePrimaryTable = false)
68: {
69: $this->_cleanBundleOptionStockData();
70: $idxTable = $usePrimaryTable ? $this->getMainTable() : $this->getIdxTable();
71: $adapter = $this->_getWriteAdapter();
72: $select = $adapter->select()
73: ->from(array('bo' => $this->getTable('bundle/option')), array('parent_id'));
74: $this->_addWebsiteJoinToSelect($select, false);
75: $status = new Zend_Db_Expr('MAX(' .
76: $adapter->getCheckSql('e.required_options = 0', 'i.stock_status', '0') . ')');
77: $select->columns('website_id', 'cw')
78: ->join(
79: array('cis' => $this->getTable('cataloginventory/stock')),
80: '',
81: array('stock_id')
82: )
83: ->joinLeft(
84: array('bs' => $this->getTable('bundle/selection')),
85: 'bs.option_id = bo.option_id',
86: array()
87: )
88: ->joinLeft(
89: array('i' => $idxTable),
90: 'i.product_id = bs.product_id AND i.website_id = cw.website_id AND i.stock_id = cis.stock_id',
91: array()
92: )
93: ->joinLeft(
94: array('e' => $this->getTable('catalog/product')),
95: 'e.entity_id = bs.product_id',
96: array()
97: )
98: ->where('cw.website_id != 0')
99: ->group(array('bo.parent_id', 'cw.website_id', 'cis.stock_id', 'bo.option_id'))
100: ->columns(array(
101: 'option_id' => 'bo.option_id',
102: 'status' => $status
103: ));
104:
105: if (!is_null($entityIds)) {
106: $select->where('bo.parent_id IN(?)', $entityIds);
107: }
108:
109:
110: $selectNonRequired = clone $select;
111:
112: $select->where('bo.required = ?', 1);
113: $selectNonRequired->where('bo.required = ?', 0)
114: ->having($status . ' = 1');
115: $query = $select->insertFromSelect($this->_getBundleOptionTable());
116: $adapter->query($query);
117:
118: $query = $selectNonRequired->insertFromSelect($this->_getBundleOptionTable());
119: $adapter->query($query);
120:
121: return $this;
122: }
123:
124: 125: 126: 127: 128: 129: 130:
131: protected function _getStockStatusSelect($entityIds = null, $usePrimaryTable = false)
132: {
133: $this->_prepareBundleOptionStockData($entityIds, $usePrimaryTable);
134:
135: $adapter = $this->_getWriteAdapter();
136: $select = $adapter->select()
137: ->from(array('e' => $this->getTable('catalog/product')), array('entity_id'));
138: $this->_addWebsiteJoinToSelect($select, true);
139: $this->_addProductWebsiteJoinToSelect($select, 'cw.website_id', 'e.entity_id');
140: $select->columns('cw.website_id')
141: ->join(
142: array('cis' => $this->getTable('cataloginventory/stock')),
143: '',
144: array('stock_id')
145: )
146: ->joinLeft(
147: array('cisi' => $this->getTable('cataloginventory/stock_item')),
148: 'cisi.stock_id = cis.stock_id AND cisi.product_id = e.entity_id',
149: array()
150: )
151: ->joinLeft(
152: array('o' => $this->_getBundleOptionTable()),
153: 'o.entity_id = e.entity_id AND o.website_id = cw.website_id AND o.stock_id = cis.stock_id',
154: array()
155: )
156: ->columns(array('qty' => new Zend_Db_Expr('0')))
157: ->where('cw.website_id != 0')
158: ->where('e.type_id = ?', $this->getTypeId())
159: ->group(array('e.entity_id', 'cw.website_id', 'cis.stock_id'));
160:
161:
162: $condition = $adapter->quoteInto('=?', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
163: $this->_addAttributeToSelect($select, 'status', 'e.entity_id', 'cs.store_id', $condition);
164:
165: if ($this->_isManageStock()) {
166: $statusExpr = $adapter->getCheckSql(
167: 'cisi.use_config_manage_stock = 0 AND cisi.manage_stock = 0',
168: '1',
169: 'cisi.is_in_stock'
170: );
171: } else {
172: $statusExpr = $adapter->getCheckSql(
173: 'cisi.use_config_manage_stock = 0 AND cisi.manage_stock = 1',
174: 'cisi.is_in_stock',
175: '1'
176: );
177: }
178:
179: $select->columns(array('status' => $adapter->getLeastSql(array(
180: new Zend_Db_Expr('MIN(' . $adapter->getCheckSql('o.stock_status IS NOT NULL','o.stock_status', '0') .')'),
181: new Zend_Db_Expr('MIN(' . $statusExpr . ')'),
182: ))));
183:
184: if (!is_null($entityIds)) {
185: $select->where('e.entity_id IN(?)', $entityIds);
186: }
187:
188: return $select;
189: }
190:
191: 192: 193: 194: 195: 196:
197: protected function _prepareIndexTable($entityIds = null)
198: {
199: parent::_prepareIndexTable($entityIds);
200: $this->_cleanBundleOptionStockData();
201:
202: return $this;
203: }
204:
205: 206: 207: 208: 209: 210:
211: protected function _updateIndex($entityIds)
212: {
213: parent::_updateIndex($entityIds);
214: $this->_cleanBundleOptionStockData();
215:
216: return $this;
217: }
218:
219: 220: 221: 222: 223:
224: protected function _cleanBundleOptionStockData()
225: {
226: $this->_getWriteAdapter()->delete($this->_getBundleOptionTable());
227: return $this;
228: }
229: }
230: