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_CatalogIndex_Model_Resource_Price extends Mage_CatalogIndex_Model_Resource_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_rate = 1;
43:
44: 45: 46: 47: 48:
49: protected $_customerGroupId;
50:
51: 52: 53: 54: 55:
56: protected $_taxRates = null;
57:
58: 59: 60: 61:
62: protected function _construct()
63: {
64: $this->_init('catalogindex/price', 'index_id');
65: }
66:
67: 68: 69: 70: 71:
72: public function setRate($rate)
73: {
74: $this->_rate = $rate;
75: }
76:
77: 78: 79: 80: 81:
82: public function getRate()
83: {
84: if (!$this->_rate) {
85: $this->_rate = 1;
86: }
87: return $this->_rate;
88: }
89:
90: 91: 92: 93: 94:
95: public function setCustomerGroupId($customerGroupId)
96: {
97: $this->_customerGroupId = $customerGroupId;
98: }
99:
100: 101: 102: 103: 104:
105: public function getCustomerGroupId()
106: {
107: return $this->_customerGroupId;
108: }
109:
110: 111: 112: 113: 114: 115: 116:
117: public function getMaxValue($attribute, $entitySelect)
118: {
119: $select = clone $entitySelect;
120: $select->reset(Zend_Db_Select::COLUMNS);
121: $select->reset(Zend_Db_Select::ORDER);
122: $select->reset(Zend_Db_Select::LIMIT_COUNT);
123: $select->reset(Zend_Db_Select::LIMIT_OFFSET);
124:
125: $response = new Varien_Object();
126: $response->setAdditionalCalculations(array());
127:
128: $select->join(array('price_table'=>$this->getMainTable()), 'price_table.entity_id=e.entity_id', array());
129:
130: if ($attribute->getAttributeCode() == 'price') {
131: $select->where('price_table.customer_group_id = ?', $this->getCustomerGroupId());
132: $args = array(
133: 'select'=>$select,
134: 'table'=>'price_table',
135: 'store_id'=>$this->getStoreId(),
136: 'response_object'=>$response,
137: );
138: Mage::dispatchEvent('catalogindex_prepare_price_select', $args);
139: }
140:
141: $select
142: ->columns("MAX(price_table.value".implode('', $response->getAdditionalCalculations()).")")
143: ->where('price_table.website_id = ?', $this->getWebsiteId())
144: ->where('price_table.attribute_id = ?', $attribute->getId());
145:
146: return $this->_getReadAdapter()->fetchOne($select)*$this->getRate();
147: }
148:
149: 150: 151: 152: 153: 154: 155: 156:
157: public function getCount($range, $attribute, $entitySelect)
158: {
159: $select = clone $entitySelect;
160: $select->reset(Zend_Db_Select::COLUMNS);
161: $select->reset(Zend_Db_Select::ORDER);
162: $select->reset(Zend_Db_Select::LIMIT_COUNT);
163: $select->reset(Zend_Db_Select::LIMIT_OFFSET);
164:
165: $select->join(array('price_table'=>$this->getMainTable()), 'price_table.entity_id=e.entity_id', array());
166: $response = new Varien_Object();
167: $response->setAdditionalCalculations(array());
168:
169: if ($attribute->getAttributeCode() == 'price') {
170: $select->where('price_table.customer_group_id = ?', $this->getCustomerGroupId());
171: $args = array(
172: 'select'=>$select,
173: 'table'=>'price_table',
174: 'store_id'=>$this->getStoreId(),
175: 'response_object'=>$response,
176: );
177: Mage::dispatchEvent('catalogindex_prepare_price_select', $args);
178: }
179:
180:
181: $fields = array('count'=>'COUNT(DISTINCT price_table.entity_id)', 'range'=>"FLOOR(((price_table.value".implode('', $response->getAdditionalCalculations()).")*{$this->getRate()})/{$range})+1");
182:
183: $select->columns($fields)
184: ->group('range')
185: ->where('price_table.website_id = ?', $this->getWebsiteId())
186: ->where('price_table.attribute_id = ?', $attribute->getId());
187:
188:
189: $result = $this->_getReadAdapter()->fetchAll($select);
190:
191: $counts = array();
192: foreach ($result as $row) {
193: $counts[$row['range']] = $row['count'];
194: }
195:
196: return $counts;
197: }
198:
199: 200: 201: 202: 203: 204: 205: 206: 207: 208:
209: public function getFilteredEntities($range, $index, $attribute, $entityIdsFilter, $tableName = 'price_table')
210: {
211: $select = $this->_getReadAdapter()->select();
212: $select->from(array($tableName=>$this->getMainTable()), $tableName . '.entity_id');
213:
214: $response = new Varien_Object();
215: $response->setAdditionalCalculations(array());
216:
217: $select
218: ->distinct(true)
219: ->where($tableName . '.entity_id in (?)', $entityIdsFilter)
220: ->where($tableName . '.website_id = ?', $this->getWebsiteId())
221: ->where($tableName . '.attribute_id = ?', $attribute->getId());
222:
223: if ($attribute->getAttributeCode() == 'price') {
224: $select->where($tableName . '.customer_group_id = ?', $this->getCustomerGroupId());
225: $args = array(
226: 'select'=>$select,
227: 'table'=>$tableName,
228: 'store_id'=>$this->getStoreId(),
229: 'response_object'=>$response,
230: );
231: Mage::dispatchEvent('catalogindex_prepare_price_select', $args);
232: }
233:
234: $select->where("(({$tableName}.value".implode('', $response->getAdditionalCalculations()).")*{$this->getRate()}) >= ?", ($index-1)*$range);
235: $select->where("(({$tableName}.value".implode('', $response->getAdditionalCalculations()).")*{$this->getRate()}) < ?", $index*$range);
236:
237:
238: return $this->_getReadAdapter()->fetchCol($select);
239: }
240:
241: 242: 243: 244: 245: 246: 247: 248: 249: 250:
251: public function applyFilterToCollection($collection, $attribute, $range, $index, $tableName = 'price_table')
252: {
253: 254: 255: 256:
257: $collection->getSelect()->distinct(true);
258: $tableName = $tableName.'_'.$attribute->getAttributeCode();
259: $collection->getSelect()->joinLeft(
260: array($tableName => $this->getMainTable()),
261: $tableName .'.entity_id=e.entity_id',
262: array()
263: );
264:
265: $response = new Varien_Object();
266: $response->setAdditionalCalculations(array());
267:
268: $collection->getSelect()
269: ->where($tableName . '.website_id = ?', $this->getWebsiteId())
270: ->where($tableName . '.attribute_id = ?', $attribute->getId());
271:
272: if ($attribute->getAttributeCode() == 'price') {
273: $collection->getSelect()->where($tableName . '.customer_group_id = ?', $this->getCustomerGroupId());
274: $args = array(
275: 'select'=>$collection->getSelect(),
276: 'table'=>$tableName,
277: 'store_id'=>$this->getStoreId(),
278: 'response_object'=>$response,
279: );
280:
281: Mage::dispatchEvent('catalogindex_prepare_price_select', $args);
282: }
283:
284: $collection->getSelect()->where("(({$tableName}.value".implode('', $response->getAdditionalCalculations()).")*{$this->getRate()}) >= ?", ($index-1)*$range);
285: $collection->getSelect()->where("(({$tableName}.value".implode('', $response->getAdditionalCalculations()).")*{$this->getRate()}) < ?", $index*$range);
286:
287: return $this;
288: }
289:
290: 291: 292: 293: 294: 295:
296: public function getMinimalPrices($ids)
297: {
298: if (!$ids) {
299: return array();
300: }
301: $select = $this->_getReadAdapter()->select();
302: $select->from(array('price_table'=>$this->getTable('catalogindex/minimal_price')),
303: array('price_table.entity_id', 'value'=>"(price_table.value)", 'tax_class_id'=>'(price_table.tax_class_id)'))
304: ->where('price_table.entity_id in (?)', $ids)
305: ->where('price_table.website_id = ?', $this->getWebsiteId())
306: ->where('price_table.customer_group_id = ?', $this->getCustomerGroupId());
307: return $this->_getReadAdapter()->fetchAll($select);
308: }
309: }
310: