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_Downloadable_Model_Resource_Indexer_Price extends Mage_Catalog_Model_Resource_Product_Indexer_Price_Default
36: {
37: 38: 39: 40: 41:
42: public function reindexAll()
43: {
44: $this->useIdxTable(true);
45: $this->beginTransaction();
46: try {
47: $this->_prepareFinalPriceData();
48: $this->_applyCustomOption();
49: $this->_applyDownloadableLink();
50: $this->_movePriceDataToIndexTable();
51: $this->commit();
52: } catch (Exception $e) {
53: $this->rollBack();
54: throw $e;
55: }
56: return $this;
57: }
58:
59: 60: 61: 62: 63: 64:
65: public function reindexEntity($entityIds)
66: {
67: $this->_prepareFinalPriceData($entityIds);
68: $this->_applyCustomOption();
69: $this->_applyDownloadableLink();
70: $this->_movePriceDataToIndexTable();
71:
72: return $this;
73: }
74:
75: 76: 77: 78: 79: 80: 81:
82: protected function _getDownloadableLinkPriceTable()
83: {
84: if ($this->useIdxTable()) {
85: return $this->getTable('downloadable/product_price_indexer_idx');
86: }
87: return $this->getTable('downloadable/product_price_indexer_tmp');
88: }
89:
90: 91: 92: 93: 94:
95: protected function _prepareDownloadableLinkPriceTable()
96: {
97: $this->_getWriteAdapter()->delete($this->_getDownloadableLinkPriceTable());
98: return $this;
99: }
100:
101: 102: 103: 104: 105:
106: protected function _applyDownloadableLink()
107: {
108: $write = $this->_getWriteAdapter();
109: $table = $this->_getDownloadableLinkPriceTable();
110:
111: $this->_prepareDownloadableLinkPriceTable();
112:
113: $dlType = $this->_getAttribute('links_purchased_separately');
114:
115: $ifPrice = $write->getIfNullSql('dlpw.price_id', 'dlpd.price');
116:
117: $select = $write->select()
118: ->from(
119: array('i' => $this->_getDefaultFinalPriceTable()),
120: array('entity_id', 'customer_group_id', 'website_id'))
121: ->join(
122: array('dl' => $dlType->getBackend()->getTable()),
123: "dl.entity_id = i.entity_id AND dl.attribute_id = {$dlType->getAttributeId()}"
124: . " AND dl.store_id = 0",
125: array())
126: ->join(
127: array('dll' => $this->getTable('downloadable/link')),
128: 'dll.product_id = i.entity_id',
129: array())
130: ->join(
131: array('dlpd' => $this->getTable('downloadable/link_price')),
132: 'dll.link_id = dlpd.link_id AND dlpd.website_id = 0',
133: array())
134: ->joinLeft(
135: array('dlpw' => $this->getTable('downloadable/link_price')),
136: 'dlpd.link_id = dlpw.link_id AND dlpw.website_id = i.website_id',
137: array())
138: ->where('dl.value = ?', 1)
139: ->group(array('i.entity_id', 'i.customer_group_id', 'i.website_id'))
140: ->columns(array(
141: 'min_price' => new Zend_Db_Expr('MIN('.$ifPrice.')'),
142: 'max_price' => new Zend_Db_Expr('SUM('.$ifPrice.')')
143: ));
144:
145: $query = $select->insertFromSelect($table);
146: $write->query($query);
147:
148: $ifTierPrice = $write->getCheckSql('i.tier_price IS NOT NULL', '(i.tier_price + id.min_price)', 'NULL');
149: $ifGroupPrice = $write->getCheckSql('i.group_price IS NOT NULL', '(i.group_price + id.min_price)', 'NULL');
150:
151: $select = $write->select()
152: ->join(
153: array('id' => $table),
154: 'i.entity_id = id.entity_id AND i.customer_group_id = id.customer_group_id'
155: .' AND i.website_id = id.website_id',
156: array())
157: ->columns(array(
158: 'min_price' => new Zend_Db_Expr('i.min_price + id.min_price'),
159: 'max_price' => new Zend_Db_Expr('i.max_price + id.max_price'),
160: 'tier_price' => new Zend_Db_Expr($ifTierPrice),
161: 'group_price' => new Zend_Db_Expr($ifGroupPrice),
162: ));
163:
164: $query = $select->crossUpdateFromSelect(array('i' => $this->_getDefaultFinalPriceTable()));
165: $write->query($query);
166:
167: $write->delete($table);
168:
169: return $this;
170: }
171: }
172: