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_Catalog_Model_Resource_Product_Indexer_Price_Configurable
36: extends Mage_Catalog_Model_Resource_Product_Indexer_Price_Default
37: {
38: 39: 40: 41: 42:
43: public function reindexAll()
44: {
45: $this->useIdxTable(true);
46: $this->beginTransaction();
47: try {
48: $this->_prepareFinalPriceData();
49: $this->_applyCustomOption();
50: $this->_applyConfigurableOption();
51: $this->_movePriceDataToIndexTable();
52: $this->commit();
53: } catch (Exception $e) {
54: $this->rollBack();
55: throw $e;
56: }
57: return $this;
58: }
59:
60: 61: 62: 63: 64: 65:
66: public function reindexEntity($entityIds)
67: {
68: $this->_prepareFinalPriceData($entityIds);
69: $this->_applyCustomOption();
70: $this->_applyConfigurableOption();
71: $this->_movePriceDataToIndexTable();
72:
73: return $this;
74: }
75:
76: 77: 78: 79: 80:
81: protected function _getConfigurableOptionAggregateTable()
82: {
83: if ($this->useIdxTable()) {
84: return $this->getTable('catalog/product_price_indexer_cfg_option_aggregate_idx');
85: }
86: return $this->getTable('catalog/product_price_indexer_cfg_option_aggregate_tmp');
87: }
88:
89: 90: 91: 92: 93:
94: protected function _getConfigurableOptionPriceTable()
95: {
96: if ($this->useIdxTable()) {
97: return $this->getTable('catalog/product_price_indexer_cfg_option_idx');
98: }
99: return $this->getTable('catalog/product_price_indexer_cfg_option_tmp');
100: }
101:
102: 103: 104: 105: 106:
107: protected function _prepareConfigurableOptionAggregateTable()
108: {
109: $this->_getWriteAdapter()->delete($this->_getConfigurableOptionAggregateTable());
110: return $this;
111: }
112:
113: 114: 115: 116: 117:
118: protected function _prepareConfigurableOptionPriceTable()
119: {
120: $this->_getWriteAdapter()->delete($this->_getConfigurableOptionPriceTable());
121: return $this;
122: }
123:
124: 125: 126: 127: 128: 129:
130: protected function _applyConfigurableOption()
131: {
132: $write = $this->_getWriteAdapter();
133: $coaTable = $this->_getConfigurableOptionAggregateTable();
134: $copTable = $this->_getConfigurableOptionPriceTable();
135:
136: $this->_prepareConfigurableOptionAggregateTable();
137: $this->_prepareConfigurableOptionPriceTable();
138:
139: $select = $write->select()
140: ->from(array('i' => $this->_getDefaultFinalPriceTable()), array())
141: ->join(
142: array('l' => $this->getTable('catalog/product_super_link')),
143: 'l.parent_id = i.entity_id',
144: array('parent_id', 'product_id'))
145: ->columns(array('customer_group_id', 'website_id'), 'i')
146: ->join(
147: array('a' => $this->getTable('catalog/product_super_attribute')),
148: 'l.parent_id = a.product_id',
149: array())
150: ->join(
151: array('cp' => $this->getValueTable('catalog/product', 'int')),
152: 'l.product_id = cp.entity_id AND cp.attribute_id = a.attribute_id AND cp.store_id = 0',
153: array())
154: ->joinLeft(
155: array('apd' => $this->getTable('catalog/product_super_attribute_pricing')),
156: 'a.product_super_attribute_id = apd.product_super_attribute_id'
157: . ' AND apd.website_id = 0 AND cp.value = apd.value_index',
158: array())
159: ->joinLeft(
160: array('apw' => $this->getTable('catalog/product_super_attribute_pricing')),
161: 'a.product_super_attribute_id = apw.product_super_attribute_id'
162: . ' AND apw.website_id = i.website_id AND cp.value = apw.value_index',
163: array())
164: ->join(
165: array('le' => $this->getTable('catalog/product')),
166: 'le.entity_id = l.product_id',
167: array())
168:
169: ->where('le.required_options=0')
170: ->group(array('l.parent_id', 'i.customer_group_id', 'i.website_id', 'l.product_id'));
171:
172: $priceExpression = $write->getCheckSql('apw.value_id IS NOT NULL', 'apw.pricing_value', 'apd.pricing_value');
173: $percentExpr = $write->getCheckSql('apw.value_id IS NOT NULL', 'apw.is_percent', 'apd.is_percent');
174: $roundExpr = "ROUND(i.price * ({$priceExpression} / 100), 4)";
175: $roundPriceExpr = $write->getCheckSql("{$percentExpr} = 1", $roundExpr, $priceExpression);
176: $priceColumn = $write->getCheckSql("{$priceExpression} IS NULL", '0', $roundPriceExpr);
177: $priceColumn = new Zend_Db_Expr("SUM({$priceColumn})");
178:
179: $tierPrice = $priceExpression;
180: $tierRoundPriceExp = $write->getCheckSql("{$percentExpr} = 1", $roundExpr, $tierPrice);
181: $tierPriceExp = $write->getCheckSql("{$tierPrice} IS NULL", '0', $tierRoundPriceExp);
182: $tierPriceColumn = $write->getCheckSql("MIN(i.tier_price) IS NOT NULL", "SUM({$tierPriceExp})", 'NULL');
183:
184: $groupPrice = $priceExpression;
185: $groupRoundPriceExp = $write->getCheckSql("{$percentExpr} = 1", $roundExpr, $groupPrice);
186: $groupPriceExp = $write->getCheckSql("{$groupPrice} IS NULL", '0', $groupRoundPriceExp);
187: $groupPriceColumn = $write->getCheckSql("MIN(i.group_price) IS NOT NULL", "SUM({$groupPriceExp})", 'NULL');
188:
189: $select->columns(array(
190: 'price' => $priceColumn,
191: 'tier_price' => $tierPriceColumn,
192: 'group_price' => $groupPriceColumn,
193: ));
194:
195: $query = $select->insertFromSelect($coaTable);
196: $write->query($query);
197:
198: $select = $write->select()
199: ->from(
200: array($coaTable),
201: array(
202: 'parent_id', 'customer_group_id', 'website_id',
203: 'MIN(price)', 'MAX(price)', 'MIN(tier_price)', 'MIN(group_price)'
204: ))
205: ->group(array('parent_id', 'customer_group_id', 'website_id'));
206:
207: $query = $select->insertFromSelect($copTable);
208: $write->query($query);
209:
210: $table = array('i' => $this->_getDefaultFinalPriceTable());
211: $select = $write->select()
212: ->join(
213: array('io' => $copTable),
214: 'i.entity_id = io.entity_id AND i.customer_group_id = io.customer_group_id'
215: .' AND i.website_id = io.website_id',
216: array());
217: $select->columns(array(
218: 'min_price' => new Zend_Db_Expr('i.min_price + io.min_price'),
219: 'max_price' => new Zend_Db_Expr('i.max_price + io.max_price'),
220: 'tier_price' => $write->getCheckSql('i.tier_price IS NOT NULL', 'i.tier_price + io.tier_price', 'NULL'),
221: 'group_price' => $write->getCheckSql(
222: 'i.group_price IS NOT NULL',
223: 'i.group_price + io.group_price', 'NULL'
224: ),
225: ));
226:
227: $query = $select->crossUpdateFromSelect($table);
228: $write->query($query);
229:
230: $write->delete($coaTable);
231: $write->delete($copTable);
232:
233: return $this;
234: }
235: }
236: