Skip to content

Commit

Permalink
Merge pull request #13 from vgelani/2.3-develop-PR-port-15332
Browse files Browse the repository at this point in the history
[Forwardport] magento#14063 - Wrong invoice prefix in multistore setup due to default stor…
  • Loading branch information
gelanivishal authored Jun 1, 2018
2 parents 91810bc + d5c3952 commit 4ac0fc0
Show file tree
Hide file tree
Showing 51 changed files with 1,431 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,10 @@ protected function _reindexRows($changedIds = [])
if (!empty($notCompositeIds)) {
$parentProductsTypes = $this->getParentProductsTypes($notCompositeIds);
$productsTypes = array_merge_recursive($productsTypes, $parentProductsTypes);
$parentProductsIds = array_keys($parentProductsTypes);
$compositeIds = $compositeIds + array_combine($parentProductsIds, $parentProductsIds);
$changedIds = array_merge($changedIds, $parentProductsIds);
foreach ($parentProductsTypes as $parentProductsIds) {
$compositeIds = $compositeIds + $parentProductsIds;
$changedIds = array_merge($changedIds, $parentProductsIds);
}
}

if (!empty($compositeIds)) {
Expand Down Expand Up @@ -370,7 +371,8 @@ protected function _copyRelationIndexData($parentIds, $excludeIds = null)
['child_id']
)->join(
['e' => $this->_defaultIndexerResource->getTable('catalog_product_entity')],
'e.' . $linkField . ' = parent_id'
'e.' . $linkField . ' = parent_id',
[]
)->where(
'e.entity_id IN(?)',
$parentIds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function execute($ids = null)
// Prepare replica table for indexation.
$this->_defaultIndexerResource->getConnection()->truncateTable($replicaTable);

/** @var \Magento\Catalog\Model\ResourceModel\Product\Indexer\AbstractIndexer $indexer */
/** @var \Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\DefaultPrice $indexer */
foreach ($this->getTypeIndexers() as $indexer) {
$indexer->getTableStrategy()->setUseIdxTable(false);
$connection = $indexer->getConnection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ class DefaultPrice extends AbstractIndexer implements PriceInterface
*/
private $hasEntity = null;

/**
* @var IndexTableStructureFactory
*/
private $indexTableStructureFactory;

/**
* @var PriceModifierInterface[]
*/
private $priceModifiers = [];

/**
* DefaultPrice constructor.
*
Expand All @@ -61,19 +71,34 @@ class DefaultPrice extends AbstractIndexer implements PriceInterface
* @param \Magento\Framework\Event\ManagerInterface $eventManager
* @param \Magento\Framework\Module\Manager $moduleManager
* @param string|null $connectionName
* @param null|\Magento\Indexer\Model\Indexer\StateFactory $stateFactory
* @param null|IndexTableStructureFactory $indexTableStructureFactory
* @param PriceModifierInterface[] $priceModifiers
*/
public function __construct(
\Magento\Framework\Model\ResourceModel\Db\Context $context,
\Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy,
\Magento\Eav\Model\Config $eavConfig,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Framework\Module\Manager $moduleManager,
$connectionName = null
$connectionName = null,
IndexTableStructureFactory $indexTableStructureFactory = null,
array $priceModifiers = []
) {
$this->_eventManager = $eventManager;
$this->moduleManager = $moduleManager;
parent::__construct($context, $tableStrategy, $eavConfig, $connectionName);

$this->indexTableStructureFactory = $indexTableStructureFactory ?:
\Magento\Framework\App\ObjectManager::getInstance()->get(IndexTableStructureFactory::class);
foreach ($priceModifiers as $priceModifier) {
if (!($priceModifier instanceof PriceModifierInterface)) {
throw new \InvalidArgumentException(
'Argument \'priceModifiers\' must be of the type ' . PriceModifierInterface::class . '[]'
);
}

$this->priceModifiers[] = $priceModifier;
}
}

/**
Expand Down Expand Up @@ -209,13 +234,41 @@ protected function _getDefaultFinalPriceTable()
* Prepare final price temporary index table
*
* @return $this
* @deprecated
* @see prepareFinalPriceTable()
*/
protected function _prepareDefaultFinalPriceTable()
{
$this->getConnection()->delete($this->_getDefaultFinalPriceTable());
return $this;
}

/**
* Create (if needed), clean and return structure of final price table
*
* @return IndexTableStructure
*/
private function prepareFinalPriceTable()
{
$tableName = $this->_getDefaultFinalPriceTable();
$this->getConnection()->delete($tableName);

$finalPriceTable = $this->indexTableStructureFactory->create([
'tableName' => $tableName,
'entityField' => 'entity_id',
'customerGroupField' => 'customer_group_id',
'websiteField' => 'website_id',
'taxClassField' => 'tax_class_id',
'originalPriceField' => 'orig_price',
'finalPriceField' => 'price',
'minPriceField' => 'min_price',
'maxPriceField' => 'max_price',
'tierPriceField' => 'tier_price',
]);

return $finalPriceTable;
}

/**
* Retrieve website current dates table name
*
Expand Down Expand Up @@ -248,11 +301,14 @@ protected function _prepareFinalPriceData($entityIds = null)
*/
protected function prepareFinalPriceDataForType($entityIds, $type)
{
$this->_prepareDefaultFinalPriceTable();
$finalPriceTable = $this->prepareFinalPriceTable();

$select = $this->getSelect($entityIds, $type);
$query = $select->insertFromSelect($this->_getDefaultFinalPriceTable(), [], false);
$query = $select->insertFromSelect($finalPriceTable->getTableName(), [], false);
$this->getConnection()->query($query);

$this->applyDiscountPrices($finalPriceTable);

return $this;
}

Expand Down Expand Up @@ -359,7 +415,7 @@ protected function getSelect($entityIds = null, $type = null)
'e.' . $metadata->getLinkField(),
'cs.store_id'
);
$currentDate = $connection->getDatePartSql('cwd.website_date');
$currentDate = 'cwd.website_date';

$maxUnsignedBigint = '~0';
$specialFromDate = $connection->getDatePartSql($specialFrom);
Expand Down Expand Up @@ -409,6 +465,7 @@ protected function getSelect($entityIds = null, $type = null)
'store_field' => new \Zend_Db_Expr('cs.store_id'),
]
);

return $select;
}

Expand Down Expand Up @@ -454,6 +511,19 @@ protected function _prepareCustomOptionPriceTable()
return $this;
}

/**
* Apply discount prices to final price index table.
*
* @param IndexTableStructure $finalPriceTable
* @return void
*/
private function applyDiscountPrices(IndexTableStructure $finalPriceTable) : void
{
foreach ($this->priceModifiers as $priceModifier) {
$priceModifier->modifyPrice($finalPriceTable);
}
}

/**
* Apply custom option minimal and maximal price to temporary final price index table
*
Expand All @@ -463,14 +533,15 @@ protected function _prepareCustomOptionPriceTable()
protected function _applyCustomOption()
{
$connection = $this->getConnection();
$finalPriceTable = $this->_getDefaultFinalPriceTable();
$coaTable = $this->_getCustomOptionAggregateTable();
$copTable = $this->_getCustomOptionPriceTable();

$this->_prepareCustomOptionAggregateTable();
$this->_prepareCustomOptionPriceTable();

$select = $connection->select()->from(
['i' => $this->_getDefaultFinalPriceTable()],
['i' => $finalPriceTable],
['entity_id', 'customer_group_id', 'website_id']
)->join(
['cw' => $this->getTable('store_website')],
Expand Down Expand Up @@ -537,7 +608,7 @@ protected function _applyCustomOption()
$connection->query($query);

$select = $connection->select()->from(
['i' => $this->_getDefaultFinalPriceTable()],
['i' => $finalPriceTable],
['entity_id', 'customer_group_id', 'website_id']
)->join(
['cw' => $this->getTable('store_website')],
Expand Down Expand Up @@ -606,7 +677,7 @@ protected function _applyCustomOption()
$query = $select->insertFromSelect($copTable);
$connection->query($query);

$table = ['i' => $this->_getDefaultFinalPriceTable()];
$table = ['i' => $finalPriceTable];
$select = $connection->select()->join(
['io' => $copTable],
'i.entity_id = io.entity_id AND i.customer_group_id = io.customer_group_id' .
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\ResourceModel\Product\Indexer\Price;

/**
* Wrapper for structure of price index table.
*/
class IndexTableStructure
{
/**
* @var string
*/
private $tableName;

/**
* @var string
*/
private $entityField;

/**
* @var string
*/
private $customerGroupField;

/**
* @var string
*/
private $websiteField;

/**
* @var string
*/
private $taxClassField;

/**
* @var string
*/
private $originalPriceField;

/**
* @var string
*/
private $finalPriceField;

/**
* @var string
*/
private $minPriceField;

/**
* @var string
*/
private $maxPriceField;

/**
* @var string
*/
private $tierPriceField;

/**
* @param string $tableName
* @param string $entityField
* @param string $customerGroupField
* @param string $websiteField
* @param string $taxClassField
* @param string $originalPriceField
* @param string $finalPriceField
* @param string $minPriceField
* @param string $maxPriceField
* @param string $tierPriceField
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
string $tableName,
string $entityField,
string $customerGroupField,
string $websiteField,
string $taxClassField,
string $originalPriceField,
string $finalPriceField,
string $minPriceField,
string $maxPriceField,
string $tierPriceField
) {
$this->tableName = $tableName;
$this->entityField = $entityField;
$this->customerGroupField = $customerGroupField;
$this->websiteField = $websiteField;
$this->taxClassField = $taxClassField;
$this->originalPriceField = $originalPriceField;
$this->finalPriceField = $finalPriceField;
$this->minPriceField = $minPriceField;
$this->maxPriceField = $maxPriceField;
$this->tierPriceField = $tierPriceField;
}

/**
* @return string
*/
public function getTableName(): string
{
return $this->tableName;
}

/**
* @return string
*/
public function getEntityField(): string
{
return $this->entityField;
}

/**
* @return string
*/
public function getCustomerGroupField(): string
{
return $this->customerGroupField;
}

/**
* @return string
*/
public function getWebsiteField(): string
{
return $this->websiteField;
}

/**
* @return string
*/
public function getTaxClassField(): string
{
return $this->taxClassField;
}

/**
* @return string
*/
public function getOriginalPriceField(): string
{
return $this->originalPriceField;
}

/**
* @return string
*/
public function getFinalPriceField(): string
{
return $this->finalPriceField;
}

/**
* @return string
*/
public function getMinPriceField(): string
{
return $this->minPriceField;
}

/**
* @return string
*/
public function getMaxPriceField(): string
{
return $this->maxPriceField;
}

/**
* @return string
*/
public function getTierPriceField(): string
{
return $this->tierPriceField;
}
}
Loading

0 comments on commit 4ac0fc0

Please sign in to comment.