00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 class Mage_CatalogIndex_Model_Mysql4_Price extends Mage_CatalogIndex_Model_Mysql4_Abstract
00034 {
00035 protected $_rate = 1;
00036 protected $_customerGroupId;
00037 protected $_taxRates = null;
00038
00039 protected function _construct()
00040 {
00041 $this->_init('catalogindex/price', 'index_id');
00042 }
00043
00044 public function setRate($rate)
00045 {
00046 $this->_rate = $rate;
00047 }
00048
00049 public function getRate()
00050 {
00051 if (!$this->_rate) {
00052 $this->_rate = 1;
00053 }
00054 return $this->_rate;
00055 }
00056
00057 public function setCustomerGroupId($customerGroupId)
00058 {
00059 $this->_customerGroupId = $customerGroupId;
00060 }
00061
00062 public function getCustomerGroupId()
00063 {
00064 return $this->_customerGroupId;
00065 }
00066
00067 public function getMaxValue($attribute = null, $entitySelect)
00068 {
00069 $select = clone $entitySelect;
00070 $select->reset(Zend_Db_Select::COLUMNS);
00071 $select->reset(Zend_Db_Select::ORDER);
00072 $select->reset(Zend_Db_Select::LIMIT_COUNT);
00073 $select->reset(Zend_Db_Select::LIMIT_OFFSET);
00074
00075 $response = new Varien_Object();
00076 $response->setAdditionalCalculations(array());
00077
00078 $select->join(array('price_table'=>$this->getMainTable()), 'price_table.entity_id=e.entity_id', array());
00079
00080 if ($attribute->getAttributeCode() == 'price') {
00081 $select->where('price_table.customer_group_id = ?', $this->getCustomerGroupId());
00082 $args = array(
00083 'select'=>$select,
00084 'table'=>'price_table',
00085 'store_id'=>$this->getStoreId(),
00086 'response_object'=>$response,
00087 );
00088 Mage::dispatchEvent('catalogindex_prepare_price_select', $args);
00089 }
00090
00091 $select
00092 ->from('', "MAX(price_table.value".implode('', $response->getAdditionalCalculations()).")")
00093 ->where('price_table.website_id = ?', $this->getWebsiteId())
00094 ->where('price_table.attribute_id = ?', $attribute->getId());
00095
00096 return $this->_getReadAdapter()->fetchOne($select)*$this->getRate();
00097 }
00098
00099 public function getCount($range, $attribute, $entitySelect)
00100 {
00101 $select = clone $entitySelect;
00102 $select->reset(Zend_Db_Select::COLUMNS);
00103 $select->reset(Zend_Db_Select::ORDER);
00104 $select->reset(Zend_Db_Select::LIMIT_COUNT);
00105 $select->reset(Zend_Db_Select::LIMIT_OFFSET);
00106
00107 $select->join(array('price_table'=>$this->getMainTable()), 'price_table.entity_id=e.entity_id', array());
00108 $response = new Varien_Object();
00109 $response->setAdditionalCalculations(array());
00110
00111 if ($attribute->getAttributeCode() == 'price') {
00112 $select->where('price_table.customer_group_id = ?', $this->getCustomerGroupId());
00113 $args = array(
00114 'select'=>$select,
00115 'table'=>'price_table',
00116 'store_id'=>$this->getStoreId(),
00117 'response_object'=>$response,
00118 );
00119 Mage::dispatchEvent('catalogindex_prepare_price_select', $args);
00120 }
00121
00122
00123 $fields = array('count'=>'COUNT(DISTINCT price_table.entity_id)', 'range'=>"FLOOR(((price_table.value".implode('', $response->getAdditionalCalculations()).")*{$this->getRate()})/{$range})+1");
00124
00125 $select->from('', $fields)
00126 ->group('range')
00127 ->where('price_table.website_id = ?', $this->getWebsiteId())
00128 ->where('price_table.attribute_id = ?', $attribute->getId());
00129
00130
00131 $result = $this->_getReadAdapter()->fetchAll($select);
00132
00133 $counts = array();
00134 foreach ($result as $row) {
00135 $counts[$row['range']] = $row['count'];
00136 }
00137
00138 return $counts;
00139 }
00140
00141 public function getFilteredEntities($range, $index, $attribute, $entityIdsFilter, $tableName = 'price_table')
00142 {
00143 $select = $this->_getReadAdapter()->select();
00144 $select->from(array($tableName=>$this->getMainTable()), $tableName . '.entity_id');
00145
00146 $response = new Varien_Object();
00147 $response->setAdditionalCalculations(array());
00148
00149 $select
00150 ->distinct(true)
00151 ->where($tableName . '.entity_id in (?)', $entityIdsFilter)
00152 ->where($tableName . '.website_id = ?', $this->getWebsiteId())
00153 ->where($tableName . '.attribute_id = ?', $attribute->getId());
00154
00155 if ($attribute->getAttributeCode() == 'price') {
00156 $select->where($tableName . '.customer_group_id = ?', $this->getCustomerGroupId());
00157 $args = array(
00158 'select'=>$select,
00159 'table'=>$tableName,
00160 'store_id'=>$this->getStoreId(),
00161 'response_object'=>$response,
00162 );
00163 Mage::dispatchEvent('catalogindex_prepare_price_select', $args);
00164 }
00165
00166 $select->where("(({$tableName}.value".implode('', $response->getAdditionalCalculations()).")*{$this->getRate()}) >= ?", ($index-1)*$range);
00167 $select->where("(({$tableName}.value".implode('', $response->getAdditionalCalculations()).")*{$this->getRate()}) < ?", $index*$range);
00168
00169
00170 return $this->_getReadAdapter()->fetchCol($select);
00171 }
00172
00173 public function applyFilterToCollection($collection, $attribute, $range, $index, $tableName = 'price_table')
00174 {
00175
00176
00177
00178
00179 $collection->getSelect()->distinct(true);
00180 $tableName = $tableName.'_'.$attribute->getAttributeCode();
00181 $collection->getSelect()->joinLeft(
00182 array($tableName => $this->getMainTable()),
00183 $tableName .'.entity_id=e.entity_id',
00184 array()
00185 );
00186
00187 $response = new Varien_Object();
00188 $response->setAdditionalCalculations(array());
00189
00190 $collection->getSelect()
00191 ->where($tableName . '.website_id = ?', $this->getWebsiteId())
00192 ->where($tableName . '.attribute_id = ?', $attribute->getId());
00193
00194 if ($attribute->getAttributeCode() == 'price') {
00195 $collection->getSelect()->where($tableName . '.customer_group_id = ?', $this->getCustomerGroupId());
00196 $args = array(
00197 'select'=>$collection->getSelect(),
00198 'table'=>$tableName,
00199 'store_id'=>$this->getStoreId(),
00200 'response_object'=>$response,
00201 );
00202
00203 Mage::dispatchEvent('catalogindex_prepare_price_select', $args);
00204 }
00205
00206 $collection->getSelect()->where("(({$tableName}.value".implode('', $response->getAdditionalCalculations()).")*{$this->getRate()}) >= ?", ($index-1)*$range);
00207 $collection->getSelect()->where("(({$tableName}.value".implode('', $response->getAdditionalCalculations()).")*{$this->getRate()}) < ?", $index*$range);
00208
00209 return $this;
00210 }
00211
00212 public function getMinimalPrices($ids)
00213 {
00214 if (!$ids) {
00215 return array();
00216 }
00217 $select = $this->_getReadAdapter()->select();
00218 $select->from(array('price_table'=>$this->getTable('catalogindex/minimal_price')),
00219 array('price_table.entity_id', 'value'=>"(price_table.value)", 'tax_class_id'=>'(price_table.tax_class_id)'))
00220 ->where('price_table.entity_id in (?)', $ids)
00221 ->where('price_table.website_id = ?', $this->getWebsiteId())
00222 ->where('price_table.customer_group_id = ?', $this->getCustomerGroupId());
00223 return $this->_getReadAdapter()->fetchAll($select);
00224 }
00225 }