forked from sazonovdmitrii/ortodon
/
Price.php
executable file
·302 lines (258 loc) · 12.5 KB
/
Price.php
1
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
/**
* @category Morphes
* @package Morphes_Filters
* @copyright Copyright (c) http://www.morphes.ru
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Resource type which contains sql code for applying filters and related operations
* @author Morphes Team
* Injected instead of standard resource catalog/layer_filter_attribute in
* Morphes_Filters_Model_Filter_Price::_getResource().
*/
class Morphes_Filters_Resource_Filter_Price extends Mage_Catalog_Model_Resource_Eav_Mysql4_Layer_Filter_Price {
protected function _getSelectOnCollection($collection, $filter)
{
if (Mage::helper('morphes_core')->isMageVersionEqualOrGreater('1.7')) {
return $this->_getSelectOnCollection_1_7($collection, $filter);
}
else {
return $this->_getSelectOnCollection_old($collection, $filter);
}
}
protected function _getSelectOnCollection_1_7($collection, $filter)
{
$collection->addPriceData($filter->getCustomerGroupId(), $filter->getWebsiteId());
$select = clone $collection->getSelect();
// reset columns, order and limitation conditions
$select->reset(Zend_Db_Select::COLUMNS);
$select->reset(Zend_Db_Select::ORDER);
$select->reset(Zend_Db_Select::LIMIT_COUNT);
$select->reset(Zend_Db_Select::LIMIT_OFFSET);
// remove join with main table
$fromPart = $select->getPart(Zend_Db_Select::FROM);
if (!isset($fromPart[Mage_Catalog_Model_Resource_Product_Collection::INDEX_TABLE_ALIAS])
|| !isset($fromPart[Mage_Catalog_Model_Resource_Product_Collection::MAIN_TABLE_ALIAS])
) {
return $select;
}
// processing FROM part
$priceIndexJoinPart = $fromPart[Mage_Catalog_Model_Resource_Product_Collection::INDEX_TABLE_ALIAS];
$priceIndexJoinConditions = explode('AND', $priceIndexJoinPart['joinCondition']);
$priceIndexJoinPart['joinType'] = Zend_Db_Select::FROM;
$priceIndexJoinPart['joinCondition'] = null;
$fromPart[Mage_Catalog_Model_Resource_Product_Collection::MAIN_TABLE_ALIAS] = $priceIndexJoinPart;
unset($fromPart[Mage_Catalog_Model_Resource_Product_Collection::INDEX_TABLE_ALIAS]);
$select->setPart(Zend_Db_Select::FROM, $fromPart);
foreach ($fromPart as $key => $fromJoinItem) {
$fromPart[$key]['joinCondition'] = $this->_replaceTableAlias($fromJoinItem['joinCondition']);
}
$select->setPart(Zend_Db_Select::FROM, $fromPart);
// processing WHERE part
$wherePart = $select->getPart(Zend_Db_Select::WHERE);
foreach ($wherePart as $key => $wherePartItem) {
$wherePart[$key] = $this->_replaceTableAlias($wherePartItem);
}
$select->setPart(Zend_Db_Select::WHERE, $wherePart);
$excludeJoinPart = Mage_Catalog_Model_Resource_Product_Collection::MAIN_TABLE_ALIAS . '.entity_id';
foreach ($priceIndexJoinConditions as $condition) {
if (strpos($condition, $excludeJoinPart) !== false) {
continue;
}
$select->where($this->_replaceTableAlias($condition));
}
$select->where($this->_getPriceExpression($filter, $select) . ' IS NOT NULL');
return $select;
}
protected function _getSelectOnCollection_old($collection, $filter)
{
$collection->addPriceData($filter->getCustomerGroupId(), $filter->getWebsiteId());
$select = clone $collection->getSelect();
// reset columns, order and limitation conditions
$select->reset(Zend_Db_Select::COLUMNS);
$select->reset(Zend_Db_Select::ORDER);
$select->reset(Zend_Db_Select::LIMIT_COUNT);
$select->reset(Zend_Db_Select::LIMIT_OFFSET);
return $select;
}
/**
* @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection
* @param Morphes_Filters_Model_Filter_Price $model
* @return Morphes_Filters_Resource_Filter_Price
*/
public function countOnCollection($collection, $model) {
if (Mage::helper('morphes_core')->isMageVersionEqualOrGreater('1.7')) {
$table = Mage_Catalog_Model_Resource_Product_Collection::MAIN_TABLE_ALIAS;
$select = $this->_getSelectOnCollection($collection, $model);
$rate = $model->getCurrencyRate();
$priceExpression = "({$this->_getPriceExpression($model, $select)}) * {$rate}";
$range = floatval($model->getPriceRange());
if ($range == 0) {
$range = 1;
}
$countExpr = new Zend_Db_Expr('COUNT(*)');
$rangeExpr = new Zend_Db_Expr("FLOOR(({$priceExpression}) / {$range}) + 1");
$select->columns(array(
'range' => $rangeExpr,
'count' => $countExpr
));
$select->group($rangeExpr)->order("$rangeExpr ASC");
Mage::helper('morphes_filters')->resetProductCollectionWhereClause($select);
$select->where("{$table}.min_price > 0");
return $this->_getReadAdapter()->fetchPairs($select);
}
else {
$select = $this->_getSelectOnCollection($collection, $model);
$connection = $this->_getReadAdapter();
$response = $this->_dispatchPreparePriceEvent($model, $select);
$table = $this->_getIndexTableAlias();
$additional = join('', $response->getAdditionalCalculations());
$fix = $this->_getConfigurablePriceFix();
$rate = $model->getCurrencyRate();
$countExpr = new Zend_Db_Expr('COUNT(DISTINCT e.entity_id)');
$rangeExpr = new Zend_Db_Expr("FLOOR((({$table}.min_price {$additional} {$fix}) * {$rate}) / {$model->getPriceRange()}) + 1");
$select->columns(array(
'range' => $rangeExpr,
'count' => $countExpr
));
// MORPHES BEGIN: make sure price filter is not applied
Mage::helper('morphes_filters')->resetProductCollectionWhereClause($select);
// MORPHES END
$select->where("{$table}.min_price > 0");
$select->group('range');
return $connection->fetchPairs($select);
}
}
/**
* @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection
* @param Morphes_Filters_Model_Filter_Price $model
* @param array $value
* @return Morphes_Filters_Resource_Filter_Price
*/
public function applyToCollection($collection, $model, $value) {
$collection->addPriceData($model->getCustomerGroupId(), $model->getWebsiteId());
$select = $collection->getSelect();
$response = $this->_dispatchPreparePriceEvent($model, $select);
$table = $this->_getIndexTableAlias();
$additional = join('', $response->getAdditionalCalculations());
$fix = $this->_getConfigurablePriceFix();
$rate = $model->getCurrencyRate();
$precision = 2;//$filter->getDecimalDigits();
if ($this->isUpperBoundInclusive()) {
$priceExpr = new Zend_Db_Expr("ROUND(({$table}.min_price {$additional} {$fix}) * {$rate}, $precision)");
}
else {
$priceExpr = new Zend_Db_Expr("({$table}.min_price {$additional} {$fix}) * {$rate}");
}
$condition = '';
foreach ($model->getMSelectedValues() as $selection) {
if (strpos($selection, ',') !== false) {
list($index, $range) = explode(',', $selection);
$range = $this->getPriceRange($index, $range);
if ($condition != '') $condition .= ' OR ';
$condition .= '(('.$priceExpr . ' >= '. $range['from'].') '.
'AND ('.$priceExpr . ($this->isUpperBoundInclusive() ? ' <= ' : ' < '). $range['to'].'))';
}
}
if ($condition) {
$select
->distinct()
->where($condition);
}
return $this;
}
public function isUpperBoundInclusive() {
return false;
}
/**
* Retrieve maximal price for attribute
*
* @param Mage_Catalog_Model_Layer_Filter_Price $filter
* @return float
*/
public function getMaxPriceOnCollection($filter, $collection)
{
$select = $this->_getSelectOnCollection($collection, $filter);
$connection = $this->_getReadAdapter();
if (Mage::helper('morphes_core')->isMageVersionEqualOrGreater('1.7')) {
$maxPriceExpr = new Zend_Db_Expr("MAX({$this->_getPriceExpression($filter, $select)}) AS m_max_price");
}
else {
$response = $this->_dispatchPreparePriceEvent($filter, $select);
$table = $this->_getIndexTableAlias();
$additional = join('', $response->getAdditionalCalculations());
$fix = $this->_getConfigurablePriceFix();
$maxPriceExpr = new Zend_Db_Expr("MAX({$table}.min_price {$additional} {$fix}) AS m_max_price");
}
Mage::helper('morphes_filters')->resetProductCollectionWhereClause($select);
$select->columns(array($maxPriceExpr))->order('m_max_price DESC');
$result = $connection->fetchOne($select) * $filter->getCurrencyRate();
// Mage::log('MAX select: ' . ((string)$select), Zend_Log::DEBUG, 'price.log');
// Mage::log("MAX result: $result", Zend_Log::DEBUG, 'price.log');
// Mage::log('LIST select: '. (string)$filter->getLayer()->getProductCollection()->getSelect(), Zend_Log::DEBUG, 'price.log');
// $this->getCount($filter, 1);
return $result;
}
public function getPriceRange($index, $range) {
return array('from' => $range * ($index - 1), 'to' => $range * $index);
}
protected function _getConfigurablePriceFix() {
if (!Mage::getStoreConfigFlag('morphes_filters/general/adjust_configurable_price')) {
return '';
}
/* @var $db Mage_Core_Model_Resource */ $db = Mage::getSingleton('core/resource');
$request = Mage::app()->getRequest();
$subSelect = '';
$values = array();
foreach (Mage::helper('morphes_filters')->getFilterOptionsCollection() as $filter) {
if ($filter->getType() == 'attribute' && ($param = $request->getParam($filter->getCode()))) {
$values = array_merge($values, Mage::helper('morphes_core')->sanitizeNumber($param, array('_')));
}
}
if (count($values) > 0) {
$values = implode(',', $values);
$subSelect = "SELECT SUM(super_price.pricing_value) ".
"FROM {$db->getTableName('catalog/product_super_attribute')} AS super ".
"INNER JOIN {$db->getTableName('catalog/product_super_attribute_pricing')} AS super_price ".
"ON super.product_super_attribute_id = super_price.product_super_attribute_id AND ".
"super_price.is_percent = 0 AND super_price.value_index IN ($values) ".
"WHERE super.product_id = e.entity_id";
}
return $subSelect ? " + COALESCE(($subSelect), 0)" : '';
}
protected $_preparedExpressions = array();
protected function _getPriceExpression($filter, $select, $replaceAlias = true) {
foreach ($this->_preparedExpressions as $expr) {
if ($expr['select'] == $select) {
return $expr['result'];
}
}
$response = new Varien_Object();
$response->setAdditionalCalculations(array());
$tableAliases = array_keys($select->getPart(Zend_Db_Select::FROM));
if (in_array(Mage_Catalog_Model_Resource_Product_Collection::INDEX_TABLE_ALIAS, $tableAliases)) {
$table = Mage_Catalog_Model_Resource_Product_Collection::INDEX_TABLE_ALIAS;
}
else {
$table = reset($tableAliases);
}
// prepare event arguments
$eventArgs = array(
'select' => $select,
'table' => $table,
'store_id' => $filter->getStoreId(),
'response_object' => $response
);
Mage::dispatchEvent('catalog_prepare_price_select', $eventArgs);
$table = Mage_Catalog_Model_Resource_Product_Collection::MAIN_TABLE_ALIAS;
$additional = $this->_replaceTableAlias(join('', $response->getAdditionalCalculations()));
$fix = $this->_getConfigurablePriceFix();
$result = "{$table}.min_price {$additional} {$fix}";
if ($replaceAlias) {
$result = $this->_replaceTableAlias($result);
}
$this->_preparedExpressions[] = compact('select', 'result');
return $result;
}
}