Skip to content

Commit a81d4de

Browse files
author
Oleksandr Iegorov
committed
Merge branch '2.2-develop' of github.com:magento/magento2ce into MAGETWO-86554
2 parents 2512cbc + d08e39e commit a81d4de

File tree

56 files changed

+1562
-699
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1562
-699
lines changed

app/code/Magento/Bundle/Model/ResourceModel/Indexer/Price.php

Lines changed: 112 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -153,52 +153,43 @@ protected function _prepareBundlePriceByType($priceType, $entityIds = null)
153153
$specialPrice = $this->_addAttributeToSelect($select, 'special_price', "e.$linkField", 'cs.store_id');
154154
$specialFrom = $this->_addAttributeToSelect($select, 'special_from_date', "e.$linkField", 'cs.store_id');
155155
$specialTo = $this->_addAttributeToSelect($select, 'special_to_date', "e.$linkField", 'cs.store_id');
156-
$curentDate = new \Zend_Db_Expr('cwd.website_date');
157-
158-
$specialExpr = $connection->getCheckSql(
159-
$connection->getCheckSql(
160-
$specialFrom . ' IS NULL',
161-
'1',
162-
$connection->getCheckSql($specialFrom . ' <= ' . $curentDate, '1', '0')
163-
) . " > 0 AND " . $connection->getCheckSql(
164-
$specialTo . ' IS NULL',
165-
'1',
166-
$connection->getCheckSql($specialTo . ' >= ' . $curentDate, '1', '0')
167-
) . " > 0 AND {$specialPrice} > 0 AND {$specialPrice} < 100 ",
168-
$specialPrice,
169-
'0'
170-
);
156+
$currentDate = new \Zend_Db_Expr('cwd.website_date');
171157

172-
$tierExpr = new \Zend_Db_Expr("tp.min_price");
158+
$specialFromDate = $connection->getDatePartSql($specialFrom);
159+
$specialToDate = $connection->getDatePartSql($specialTo);
160+
$specialFromExpr = "{$specialFrom} IS NULL OR {$specialFromDate} <= {$currentDate}";
161+
$specialToExpr = "{$specialTo} IS NULL OR {$specialToDate} >= {$currentDate}";
162+
$specialExpr = "{$specialPrice} IS NOT NULL AND {$specialPrice} > 0 AND {$specialPrice} < 100"
163+
. " AND {$specialFromExpr} AND {$specialToExpr}";
164+
$tierExpr = new \Zend_Db_Expr('tp.min_price');
173165

174166
if ($priceType == \Magento\Bundle\Model\Product\Price::PRICE_TYPE_FIXED) {
175-
$finalPrice = $connection->getCheckSql(
176-
$specialExpr . ' > 0',
177-
'ROUND(' . $price . ' * (' . $specialExpr . ' / 100), 4)',
178-
$price
167+
$specialPriceExpr = $connection->getCheckSql(
168+
$specialExpr,
169+
'ROUND(' . $price . ' * (' . $specialPrice . ' / 100), 4)',
170+
'NULL'
179171
);
180172
$tierPrice = $connection->getCheckSql(
181173
$tierExpr . ' IS NOT NULL',
182-
'ROUND(' . $price . ' - ' . '(' . $price . ' * (' . $tierExpr . ' / 100)), 4)',
174+
'ROUND((1 - ' . $tierExpr . ' / 100) * ' . $price . ', 4)',
183175
'NULL'
184176
);
185-
186-
$finalPrice = $connection->getCheckSql(
187-
"{$tierPrice} < {$finalPrice}",
188-
$tierPrice,
189-
$finalPrice
190-
);
177+
$finalPrice = $connection->getLeastSql([
178+
$price,
179+
$connection->getIfNullSql($specialPriceExpr, $price),
180+
$connection->getIfNullSql($tierPrice, $price),
181+
]);
191182
} else {
192-
$finalPrice = new \Zend_Db_Expr("0");
183+
$finalPrice = new \Zend_Db_Expr('0');
193184
$tierPrice = $connection->getCheckSql($tierExpr . ' IS NOT NULL', '0', 'NULL');
194185
}
195186

196187
$select->columns(
197188
[
198189
'price_type' => new \Zend_Db_Expr($priceType),
199-
'special_price' => $specialExpr,
190+
'special_price' => $connection->getCheckSql($specialExpr, $specialPrice, '0'),
200191
'tier_percent' => $tierExpr,
201-
'orig_price' => $connection->getCheckSql($price . ' IS NULL', '0', $price),
192+
'orig_price' => $connection->getIfNullSql($price, '0'),
202193
'price' => $finalPrice,
203194
'min_price' => $finalPrice,
204195
'max_price' => $finalPrice,
@@ -246,63 +237,29 @@ protected function _calculateBundleOptionPrice()
246237
$this->_prepareBundleOptionTable();
247238

248239
$select = $connection->select()->from(
249-
['i' => $this->_getBundleSelectionTable()],
240+
$this->_getBundleSelectionTable(),
250241
['entity_id', 'customer_group_id', 'website_id', 'option_id']
251242
)->group(
252-
['entity_id', 'customer_group_id', 'website_id', 'option_id', 'is_required', 'group_type']
253-
)->columns(
243+
['entity_id', 'customer_group_id', 'website_id', 'option_id']
244+
);
245+
$minPrice = $connection->getCheckSql('is_required = 1', 'price', 'NULL');
246+
$tierPrice = $connection->getCheckSql('is_required = 1', 'tier_price', 'NULL');
247+
$select->columns(
254248
[
255-
'min_price' => $connection->getCheckSql('i.is_required = 1', 'MIN(i.price)', '0'),
256-
'alt_price' => $connection->getCheckSql('i.is_required = 0', 'MIN(i.price)', '0'),
257-
'max_price' => $connection->getCheckSql('i.group_type = 1', 'SUM(i.price)', 'MAX(i.price)'),
258-
'tier_price' => $connection->getCheckSql('i.is_required = 1', 'MIN(i.tier_price)', '0'),
259-
'alt_tier_price' => $connection->getCheckSql('i.is_required = 0', 'MIN(i.tier_price)', '0'),
249+
'min_price' => new \Zend_Db_Expr('MIN(' . $minPrice . ')'),
250+
'alt_price' => new \Zend_Db_Expr('MIN(price)'),
251+
'max_price' => $connection->getCheckSql('group_type = 0', 'MAX(price)', 'SUM(price)'),
252+
'tier_price' => new \Zend_Db_Expr('MIN(' . $tierPrice . ')'),
253+
'alt_tier_price' => new \Zend_Db_Expr('MIN(tier_price)'),
260254
]
261255
);
262256

263257
$query = $select->insertFromSelect($this->_getBundleOptionTable());
264258
$connection->query($query);
265259

266260
$this->_prepareDefaultFinalPriceTable();
267-
268-
$minPrice = new \Zend_Db_Expr(
269-
$connection->getCheckSql('SUM(io.min_price) = 0', 'MIN(io.alt_price)', 'SUM(io.min_price)') . ' + i.price'
270-
);
271-
$maxPrice = new \Zend_Db_Expr("SUM(io.max_price) + i.price");
272-
$tierPrice = $connection->getCheckSql(
273-
'MIN(i.tier_percent) IS NOT NULL',
274-
$connection->getCheckSql(
275-
'SUM(io.tier_price) = 0',
276-
'SUM(io.alt_tier_price)',
277-
'SUM(io.tier_price)'
278-
) . ' + MIN(i.tier_price)',
279-
'NULL'
280-
);
281-
282-
$select = $connection->select()->from(
283-
['io' => $this->_getBundleOptionTable()],
284-
['entity_id', 'customer_group_id', 'website_id']
285-
)->join(
286-
['i' => $this->_getBundlePriceTable()],
287-
'i.entity_id = io.entity_id AND i.customer_group_id = io.customer_group_id' .
288-
' AND i.website_id = io.website_id',
289-
[]
290-
)->group(
291-
['io.entity_id', 'io.customer_group_id', 'io.website_id', 'i.tax_class_id', 'i.orig_price', 'i.price']
292-
)->columns(
293-
[
294-
'i.tax_class_id',
295-
'orig_price' => 'i.orig_price',
296-
'price' => 'i.price',
297-
'min_price' => $minPrice,
298-
'max_price' => $maxPrice,
299-
'tier_price' => $tierPrice,
300-
'base_tier' => 'MIN(i.base_tier)',
301-
]
302-
);
303-
304-
$query = $select->insertFromSelect($this->_getDefaultFinalPriceTable());
305-
$connection->query($query);
261+
$this->applyBundlePrice();
262+
$this->applyBundleOptionPrice();
306263

307264
return $this;
308265
}
@@ -348,33 +305,33 @@ protected function _calculateBundleSelectionPrice($priceType)
348305
'ROUND(i.base_tier - (i.base_tier * (' . $selectionPriceValue . ' / 100)),4)',
349306
$connection->getCheckSql(
350307
'i.tier_percent > 0',
351-
'ROUND(' .
352-
$selectionPriceValue .
353-
' - (' .
354-
$selectionPriceValue .
355-
' * (i.tier_percent / 100)),4)',
308+
'ROUND((1 - i.tier_percent / 100) * ' . $selectionPriceValue . ',4)',
356309
$selectionPriceValue
357310
)
358311
) . ' * bs.selection_qty',
359312
'NULL'
360313
);
361314

362-
$priceExpr = new \Zend_Db_Expr(
363-
$connection->getCheckSql("{$tierExpr} < {$priceExpr}", $tierExpr, $priceExpr)
364-
);
315+
$priceExpr = $connection->getLeastSql([
316+
$priceExpr,
317+
$connection->getIfNullSql($tierExpr, $priceExpr),
318+
]);
365319
} else {
366-
$priceExpr = new \Zend_Db_Expr(
367-
$connection->getCheckSql(
368-
'i.special_price > 0 AND i.special_price < 100',
369-
'ROUND(idx.min_price * (i.special_price / 100), 4)',
370-
'idx.min_price'
371-
) . ' * bs.selection_qty'
320+
$price = 'idx.min_price * bs.selection_qty';
321+
$specialExpr = $connection->getCheckSql(
322+
'i.special_price > 0 AND i.special_price < 100',
323+
'ROUND(' . $price . ' * (i.special_price / 100), 4)',
324+
$price
372325
);
373326
$tierExpr = $connection->getCheckSql(
374-
'i.base_tier IS NOT NULL',
375-
'ROUND(idx.min_price * (i.base_tier / 100), 4)* bs.selection_qty',
327+
'i.tier_percent IS NOT NULL',
328+
'ROUND((1 - i.tier_percent / 100) * ' . $price . ', 4)',
376329
'NULL'
377330
);
331+
$priceExpr = $connection->getLeastSql([
332+
$specialExpr,
333+
$connection->getIfNullSql($tierExpr, $price),
334+
]);
378335
}
379336

380337
$linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
@@ -508,4 +465,66 @@ protected function _prepareTierPriceIndex($entityIds = null)
508465

509466
return $this;
510467
}
468+
469+
private function applyBundlePrice()
470+
{
471+
$select = $this->getConnection()->select();
472+
$select->from(
473+
$this->_getBundlePriceTable(),
474+
[
475+
'entity_id',
476+
'customer_group_id',
477+
'website_id',
478+
'tax_class_id',
479+
'orig_price',
480+
'price',
481+
'min_price',
482+
'max_price',
483+
'tier_price',
484+
'base_tier',
485+
]
486+
);
487+
488+
$query = $select->insertFromSelect($this->_getDefaultFinalPriceTable());
489+
$this->getConnection()->query($query);
490+
}
491+
492+
private function applyBundleOptionPrice()
493+
{
494+
$connection = $this->getConnection();
495+
496+
$subSelect = $connection->select()->from(
497+
$this->_getBundleOptionTable(),
498+
[
499+
'entity_id',
500+
'customer_group_id',
501+
'website_id',
502+
'min_price' => new \Zend_Db_Expr('SUM(min_price)'),
503+
'alt_price' => new \Zend_Db_Expr('MIN(alt_price)'),
504+
'max_price' => new \Zend_Db_Expr('SUM(max_price)'),
505+
'tier_price' => new \Zend_Db_Expr('SUM(tier_price)'),
506+
'alt_tier_price' => new \Zend_Db_Expr('MIN(alt_tier_price)'),
507+
]
508+
)->group(
509+
['entity_id', 'customer_group_id', 'website_id']
510+
);
511+
512+
$minPrice = 'i.min_price + ' . $connection->getIfNullSql('io.min_price', '0');
513+
$tierPrice = 'i.tier_price + ' . $connection->getIfNullSql('io.tier_price', '0');
514+
$select = $connection->select()->join(
515+
['io' => $subSelect],
516+
'i.entity_id = io.entity_id AND i.customer_group_id = io.customer_group_id' .
517+
' AND i.website_id = io.website_id',
518+
[]
519+
)->columns(
520+
[
521+
'min_price' => $connection->getCheckSql("{$minPrice} = 0", 'io.alt_price', $minPrice),
522+
'max_price' => new \Zend_Db_Expr('io.max_price + i.max_price'),
523+
'tier_price' => $connection->getCheckSql("{$tierPrice} = 0", 'io.alt_tier_price', $tierPrice),
524+
]
525+
);
526+
527+
$query = $select->crossUpdateFromSelect(['i' => $this->_getDefaultFinalPriceTable()]);
528+
$connection->query($query);
529+
}
511530
}

app/code/Magento/Catalog/Model/Indexer/Category/Product/AbstractAction.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Magento\Framework\DB\Select;
1717
use Magento\Framework\EntityManager\MetadataPool;
1818
use Magento\Store\Model\Store;
19-
use Magento\Catalog\Model\Indexer\Category\Product\TableResolver;
19+
use Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer;
2020

2121
/**
2222
* Class AbstractAction
@@ -40,7 +40,6 @@ abstract class AbstractAction
4040

4141
/**
4242
* Catalog category index table name
43-
* @deprecated
4443
*/
4544
const MAIN_INDEX_TABLE = 'catalog_category_product_index';
4645

@@ -112,9 +111,9 @@ abstract class AbstractAction
112111
protected $metadataPool;
113112

114113
/**
115-
* @var TableResolver
114+
* @var TableMaintainer
116115
*/
117-
protected $tableResolver;
116+
protected $tableMaintainer;
118117

119118
/**
120119
* @var string
@@ -133,23 +132,23 @@ abstract class AbstractAction
133132
* @param \Magento\Catalog\Model\Config $config
134133
* @param QueryGenerator $queryGenerator
135134
* @param MetadataPool|null $metadataPool
136-
* @param TableResolver|null $tableResolver
135+
* @param TableMaintainer|null $tableMaintainer
137136
*/
138137
public function __construct(
139138
\Magento\Framework\App\ResourceConnection $resource,
140139
\Magento\Store\Model\StoreManagerInterface $storeManager,
141140
\Magento\Catalog\Model\Config $config,
142141
QueryGenerator $queryGenerator = null,
143142
MetadataPool $metadataPool = null,
144-
TableResolver $tableResolver = null
143+
TableMaintainer $tableMaintainer = null
145144
) {
146145
$this->resource = $resource;
147146
$this->connection = $resource->getConnection();
148147
$this->storeManager = $storeManager;
149148
$this->config = $config;
150149
$this->queryGenerator = $queryGenerator ?: ObjectManager::getInstance()->get(QueryGenerator::class);
151150
$this->metadataPool = $metadataPool ?: ObjectManager::getInstance()->get(MetadataPool::class);
152-
$this->tableResolver = $tableResolver ?: ObjectManager::getInstance()->get(TableResolver::class);
151+
$this->tableMaintainer = $tableMaintainer ?: ObjectManager::getInstance()->get(TableMaintainer::class);
153152
}
154153

155154
/**
@@ -222,8 +221,8 @@ protected function getMainTmpTable()
222221
protected function getIndexTable($storeId)
223222
{
224223
return $this->useTempTable
225-
? $this->tableResolver->getMainReplicaTable($storeId)
226-
: $this->tableResolver->getMainTable($storeId);
224+
? $this->tableMaintainer->getMainReplicaTable($storeId)
225+
: $this->tableMaintainer->getMainTable($storeId);
227226
}
228227

229228
/**

0 commit comments

Comments
 (0)