Skip to content

Commit

Permalink
MAGETWO-97203: [GitHub] Next generated increment id based on the high…
Browse files Browse the repository at this point in the history
…est increment number from all stores after migration #615 #607
  • Loading branch information
victor-v-rad committed Dec 20, 2018
1 parent 080333d commit 5d93087
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Migration/Step/SalesIncrement/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ protected function createSequenceTable(array $entityType, $storeId)
$columnOptions
);
$adapter->createTable($table);
$incrementMaxNumber = $this->helper->getMaxIncrementForEntityType($entityType['entity_type_id']);
$incrementMaxNumber = $this->helper->getMaxIncrementForEntityType($entityType['entity_type_id'], $storeId);
if ($incrementMaxNumber !== false) {
$adapter->insert($tableName, [$entityType['column'] => $incrementMaxNumber]);
}
Expand Down
7 changes: 5 additions & 2 deletions src/Migration/Step/SalesIncrement/Delta.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,14 @@ protected function transformData($data, $sourceDocument, $destDocument, $recordT
/** @var \Magento\Framework\DB\Adapter\Pdo\Mysql $adapter */
$adapter = $this->destination->getAdapter()->getSelect()->getAdapter();
$entityType = $this->helper->getEntityTypeData('entity_type_id', $data['entity_type_id']);
$incrementMaxNumber = $this->helper->getMaxIncrementForEntityType($data['entity_type_id']);
$incrementMaxNumber = $this->helper->getMaxIncrementForEntityType(
$data['entity_type_id'],
$data['store_id']
);
if ($incrementMaxNumber === false || empty($entityType)) {
return;
}
foreach ($this->helper->getStoreIds() as $storeId) {
foreach ($this->helper->getStoreIdsOfStoreGroup($data['store_id']) as $storeId) {
$tableName = $this->helper->getTableName($entityType['entity_type_table'], $storeId);
$adapter->insertOnDuplicate($tableName, [$entityType['column'] => $incrementMaxNumber]);
}
Expand Down
48 changes: 36 additions & 12 deletions src/Migration/Step/SalesIncrement/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@ class Helper
/**
* @var Source
*/
protected $source;
private $source;

/**
* @var Destination
*/
protected $destination;
private $destination;

/**
* @var string
*/
protected $eavEntityStore = 'eav_entity_store';
private $eavEntityStoreTable = 'eav_entity_store';

/**
* @var string
*/
protected $storeTable = 'core_store';
private $storeTable = 'core_store';

/**
* @var array
*/
protected $sequenceMetaTable = [
private $sequenceMetaTable = [
'name' => 'sales_sequence_meta',
'structure' => [
'meta_id',
Expand All @@ -50,7 +50,7 @@ class Helper
/**
* @var array
*/
protected $sequenceProfileTable = [
private $sequenceProfileTable = [
'name' => 'sales_sequence_profile',
'structure' => [
'profile_id',
Expand All @@ -68,7 +68,7 @@ class Helper
/**
* @var array
*/
protected $entityTypeTablesMap = [
private $entityTypeTablesMap = [
[
'entity_type_code' => 'order',
'entity_type_table' => 'sequence_order',
Expand Down Expand Up @@ -106,24 +106,48 @@ public function __construct(

/**
* @param int $entityTypeId
* @param int $storeId
* @return bool|int
*/
public function getMaxIncrementForEntityType($entityTypeId)
public function getMaxIncrementForEntityType($entityTypeId, $storeId)
{
/** @var \Migration\ResourceModel\Adapter\Mysql $adapter */
$adapter = $this->source->getAdapter();
$query = $adapter->getSelect()->from(
$this->source->addDocumentPrefix($this->eavEntityStore),
$this->source->addDocumentPrefix($this->eavEntityStoreTable),
['increment_prefix', 'increment_last_id']
)->where('entity_type_id = ?', $entityTypeId);
)->where('entity_type_id = ?', $entityTypeId
)->where('store_id IN (?)', $this->getStoreIdsOfStoreGroup($storeId));
$data = $query->getAdapter()->fetchAll($query);
if (!$data) {
return false;
}
$cutPrefixFunction = function (array $data) {
return (int) substr($data['increment_last_id'], strlen($data['increment_prefix']));
};
return max(array_map($cutPrefixFunction, $data));
$maxIncrement = max(array_map($cutPrefixFunction, $data));
return $maxIncrement;
}

/**
* Return store ids of store group
*
* @param $storeId
* @return array
*/
public function getStoreIdsOfStoreGroup($storeId)
{
/** @var \Migration\ResourceModel\Adapter\Mysql $adapter */
$adapter = $this->source->getAdapter();
$select = $adapter->getSelect()->from(
['cs' => $this->source->addDocumentPrefix($this->storeTable)],
['store_id']
)->join(
['css' => $this->source->addDocumentPrefix($this->storeTable)],
'css.group_id = cs.group_id',
[]
)->where('css.store_id = ?', $storeId);
return $select->getAdapter()->fetchCol($select);
}

/**
Expand Down Expand Up @@ -200,7 +224,7 @@ public function getTableName($table, $storeId = false)
* @param array $entityTypeCodes
* @return array
*/
protected function getEntityTypeIdByCode($entityTypeCodes)
private function getEntityTypeIdByCode($entityTypeCodes)
{
/** @var Mysql $adapter */
$adapter = $this->destination->getAdapter();
Expand Down
5 changes: 4 additions & 1 deletion src/Migration/Step/SalesIncrement/Volume.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ public function perform()
$adapter = $this->destination->getAdapter()->getSelect()->getAdapter();
foreach ($this->helper->getEntityTypeTablesMap() as $entityType) {
foreach ($this->helper->getStoreIds() as $storeId) {
$incrementMaxNumber = $this->helper->getMaxIncrementForEntityType($entityType['entity_type_id']);
$incrementMaxNumber = $this->helper->getMaxIncrementForEntityType(
$entityType['entity_type_id'],
$storeId
);
$select = $adapter->select()
->from($this->helper->getTableName($entityType['entity_type_table'], $storeId))
->order("{$entityType['column']} DESC")
Expand Down

0 comments on commit 5d93087

Please sign in to comment.