Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task 290 import export source code #307

Merged
merged 45 commits into from
Dec 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
8548525
MSI: issue #290
Dec 14, 2017
273860e
Merge remote-tracking branch 'origin/task-288-add-source-code' into t…
Dec 14, 2017
32c1a3e
MSI: issue #290
Dec 14, 2017
ad090a0
MSI: issue #290
Dec 14, 2017
81fc718
MSI: issue #290
deadlexus Dec 15, 2017
6cdfe6e
MSI: issue #290
deadlexus Dec 17, 2017
fcc8334
Merge branch 'develop' into task-290-import-export-source-code
maghamed Dec 20, 2017
1b85ca7
MSI: issue #290
Dec 20, 2017
fb9480d
MSI: issue #290
deadlexus Dec 20, 2017
c070521
MSI: issue #290
deadlexus Dec 20, 2017
bb971db
MSI: issue #290
deadlexus Dec 20, 2017
8461023
MSI: issue #290
deadlexus Dec 21, 2017
8cf6711
Merge remote-tracking branch 'origin/develop' into task-290-import-ex…
deadlexus Dec 21, 2017
044bc6f
MSI: issue #290
deadlexus Dec 21, 2017
ac3175b
MSI: issue #290
deadlexus Dec 21, 2017
9e92360
Merge remote-tracking branch 'origin/develop' into task-290-import-ex…
deadlexus Dec 21, 2017
b7af836
MSI: issue #290
deadlexus Dec 21, 2017
e506891
MSI: issue #290
deadlexus Dec 21, 2017
988655a
MSI: issue #290
deadlexus Dec 22, 2017
7bbd6a5
MSI: issue #290
Dec 22, 2017
94ae490
Merge branch 'develop' into task-290-import-export-source-code
maghamed Dec 22, 2017
cfb2611
Adapt SourceCodes with PreventAssignSourcesToDefaultStockPlugin logic
maghamed Dec 22, 2017
622438b
Adapt SourceCodes with PreventAssignSourcesToDefaultStockPlugin logic
maghamed Dec 22, 2017
f425473
Fixed Sample File for Inventory Import
maghamed Dec 22, 2017
f819625
MSI: issue #290
Dec 22, 2017
3dfe677
Merge branch 'task-290-import-export-source-code' of https://github.c…
Dec 22, 2017
de53a75
Fix conflicts with develop
maghamed Dec 22, 2017
5e7fcae
Make Indexers to use SourceId instead of SourceCode
maghamed Dec 22, 2017
dac634d
Make Indexers to use SourceId instead of SourceCode
maghamed Dec 22, 2017
5364986
Make Indexers to use SourceId instead of SourceCode
maghamed Dec 22, 2017
c6e0ebf
Remove Source Id from Admin UI
maghamed Dec 23, 2017
abacf95
remove Source ID from API Interfaces
maghamed Dec 23, 2017
1ce881b
remove Source ID from API Interfaces
maghamed Dec 23, 2017
54f6e59
Added setSourceId method to the Source model
deadlexus Dec 23, 2017
948d4f6
source_id field constant moved from 'Source' model to resource model
deadlexus Dec 23, 2017
e8f84d4
Removed setSourceId method from source model; fixed integration test;
Dec 26, 2017
b9de5f5
Fixed integration test
Dec 26, 2017
5e93ef5
Fixed static test
Dec 26, 2017
580e823
MSI: Task 290 import export source code
Dec 26, 2017
79972ce
MSI: Task 290 import export source code
Dec 27, 2017
9f210cd
MSI: Task 290 import export source code
Dec 27, 2017
902ca87
MSI: Task 290 import export source code
Dec 27, 2017
6d7782b
MSI: Task 290 import export source code
Dec 27, 2017
8b8ee98
MSI: Task 290 import export source code
Dec 27, 2017
16948bd
MSI: Task 290 import export source code
Dec 27, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public function __construct(
*/
public function execute(): ResultInterface
{
$sourceId = (int)$this->getRequest()->getParam(SourceInterface::SOURCE_ID);
$sourceCode = $this->getRequest()->getParam(SourceInterface::SOURCE_CODE);
try {
$source = $this->sourceRepository->get($sourceId);
$source = $this->sourceRepository->get($sourceCode);

/** @var Page $result */
$result = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
Expand All @@ -64,7 +64,7 @@ public function execute(): ResultInterface
/** @var Redirect $result */
$result = $this->resultRedirectFactory->create();
$this->messageManager->addErrorMessage(
__('Source with id "%value" does not exist.', ['value' => $sourceId])
__('Source with source code "%value" does not exist.', ['value' => $sourceCode])
);
$result->setPath('*/*');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,27 @@ public function execute(): ResultInterface
if ($request->isXmlHttpRequest() && $request->isPost() && $requestData) {
foreach ($requestData as $itemData) {
try {
$sourceId = (int)$itemData[SourceInterface::SOURCE_ID];
$source = $this->sourceRepository->get($sourceId);
$sourceCode = $itemData[SourceInterface::SOURCE_CODE];
$source = $this->sourceRepository->get($sourceCode);
$this->dataObjectHelper->populateWithArray($source, $itemData, SourceInterface::class);
$this->sourceRepository->save($source);
} catch (NoSuchEntityException $e) {
$errorMessages[] = __(
'[ID: %value] The Source does not exist.',
['value' => $sourceId]
['value' => $sourceCode]
);
} catch (ValidationException $e) {
foreach ($e->getErrors() as $localizedError) {
$errorMessages[] = __('[ID: %value] %message', [
'value' => $sourceId,
'value' => $sourceCode,
'message' => $localizedError->getMessage()
]);
}
} catch (CouldNotSaveException $e) {
$errorMessages[] = __(
'[ID: %value] %message',
[
'value' => $sourceId,
'value' => $sourceCode,
'message' => $e->getMessage()
]
);
Expand Down
56 changes: 23 additions & 33 deletions app/code/Magento/Inventory/Controller/Adminhtml/Source/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,55 +69,49 @@ public function execute(): ResultInterface
{
$resultRedirect = $this->resultRedirectFactory->create();
$request = $this->getRequest();
$requestData = $request->getParams();
$requestData = $request->getPost()->toArray();

if (!$request->isPost() || empty($requestData['general'])) {
$this->messageManager->addErrorMessage(__('Wrong request.'));
$this->processRedirectAfterFailureSave($resultRedirect);

return $resultRedirect;
}

$sourceCodeQueryParam = $request->getQuery(SourceInterface::SOURCE_CODE);
try {
$sourceId = isset($requestData['general'][SourceInterface::SOURCE_ID])
? (int)$requestData['general'][SourceInterface::SOURCE_ID]
: null;
$sourceId = $this->processSave($requestData, $sourceId);
$source = (null !== $sourceCodeQueryParam)
? $this->sourceRepository->get($sourceCodeQueryParam)
: $this->sourceFactory->create();

$this->processSave($source, $requestData);

$this->messageManager->addSuccessMessage(__('The Source has been saved.'));
$this->processRedirectAfterSuccessSave($resultRedirect, $sourceId);
$this->processRedirectAfterSuccessSave($resultRedirect, $source->getSourceCode());
} catch (NoSuchEntityException $e) {
$this->messageManager->addErrorMessage(__('The Source does not exist.'));
$this->processRedirectAfterFailureSave($resultRedirect);
} catch (ValidationException $e) {
foreach ($e->getErrors() as $localizedError) {
$this->messageManager->addErrorMessage($localizedError->getMessage());
}
$this->processRedirectAfterFailureSave($resultRedirect, $sourceId);
$this->processRedirectAfterFailureSave($resultRedirect, $sourceCodeQueryParam ?? $sourceCodeQueryParam);
} catch (CouldNotSaveException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$this->processRedirectAfterFailureSave($resultRedirect, $sourceId);
$this->processRedirectAfterFailureSave($resultRedirect, $sourceCodeQueryParam ?? $sourceCodeQueryParam);
} catch (Exception $e) {
$this->messageManager->addErrorMessage(__('Could not save Source.'));
$this->processRedirectAfterFailureSave($resultRedirect, $sourceId ?? null);
$this->processRedirectAfterFailureSave($resultRedirect, $sourceCodeQueryParam ?? $sourceCodeQueryParam);
}

return $resultRedirect;
}

/**
* @param SourceInterface $source
* @param array $requestData
* @param int|null $sourceId
*
* @return int
* @return void
*/
private function processSave(array $requestData, int $sourceId = null): int
private function processSave(SourceInterface $source, array $requestData)
{
if (null === $sourceId) {
/** @var SourceInterface $source */
$source = $this->sourceFactory->create();
} else {
$source = $this->sourceRepository->get($sourceId);
}
$source = $this->sourceHydrator->hydrate($source, $requestData);

$this->_eventManager->dispatch(
Expand All @@ -128,7 +122,7 @@ private function processSave(array $requestData, int $sourceId = null): int
]
);

$sourceId = $this->sourceRepository->save($source);
$this->sourceRepository->save($source);

$this->_eventManager->dispatch(
'controller_action_inventory_source_save_after',
Expand All @@ -137,21 +131,18 @@ private function processSave(array $requestData, int $sourceId = null): int
'source' => $source,
]
);

return $sourceId;
}

/**
* @param Redirect $resultRedirect
* @param int $sourceId
*
* @param string $sourceCode
* @return void
*/
private function processRedirectAfterSuccessSave(Redirect $resultRedirect, int $sourceId)
private function processRedirectAfterSuccessSave(Redirect $resultRedirect, string $sourceCode)
{
if ($this->getRequest()->getParam('back')) {
$resultRedirect->setPath('*/*/edit', [
SourceInterface::SOURCE_ID => $sourceId,
SourceInterface::SOURCE_CODE => $sourceCode,
'_current' => true,
]);
} elseif ($this->getRequest()->getParam('redirect_to_new')) {
Expand All @@ -165,17 +156,16 @@ private function processRedirectAfterSuccessSave(Redirect $resultRedirect, int $

/**
* @param Redirect $resultRedirect
* @param int|null $sourceId
*
* @param string|null $sourceCode
* @return void
*/
private function processRedirectAfterFailureSave(Redirect $resultRedirect, int $sourceId = null)
private function processRedirectAfterFailureSave(Redirect $resultRedirect, string $sourceCode = null)
{
if (null === $sourceId) {
if (null === $sourceCode) {
$resultRedirect->setPath('*/*/new');
} else {
$resultRedirect->setPath('*/*/edit', [
SourceInterface::SOURCE_ID => $sourceId,
SourceInterface::SOURCE_CODE => $sourceCode,
'_current' => true,
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,23 @@ public function process(int $stockId, array $stockSourceLinksData)
$this->validateStockSourceData($stockSourceLinksData);

$assignedSources = $this->getAssignedSourcesForStock->execute($stockId);
$sourceIdsForSave = array_flip(array_column($stockSourceLinksData, StockSourceLink::SOURCE_ID));
$sourceIdsForDelete = [];
$sourceCodesForSave = array_flip(array_column($stockSourceLinksData, StockSourceLink::SOURCE_CODE));
$sourceCodesForDelete = [];

foreach ($assignedSources as $assignedSource) {
if (array_key_exists($assignedSource->getSourceId(), $sourceIdsForSave)) {
unset($sourceIdsForSave[$assignedSource->getSourceId()]);
if (array_key_exists($assignedSource->getSourceCode(), $sourceCodesForSave)) {
unset($sourceCodesForSave[$assignedSource->getSourceCode()]);
} else {
$sourceIdsForDelete[] = $assignedSource->getSourceId();
$sourceCodesForDelete[] = $assignedSource->getSourceCode();
}
}

if ($sourceIdsForSave) {
$this->assignSourcesToStock->execute(array_keys($sourceIdsForSave), $stockId);
if ($sourceCodesForSave) {
$this->assignSourcesToStock->execute(array_keys($sourceCodesForSave), $stockId);
}
if ($sourceIdsForDelete) {
foreach ($sourceIdsForDelete as $sourceIdForDelete) {
$this->unassignSourceFromStock->execute((int) $sourceIdForDelete, $stockId);
if ($sourceCodesForDelete) {
foreach ($sourceCodesForDelete as $sourceCodeForDelete) {
$this->unassignSourceFromStock->execute($sourceCodeForDelete, $stockId);
}
}
}
Expand All @@ -107,7 +107,7 @@ public function process(int $stockId, array $stockSourceLinksData)
private function validateStockSourceData(array $stockSourceLinksData)
{
foreach ($stockSourceLinksData as $stockSourceLinkData) {
if (!isset($stockSourceLinkData[StockSourceLink::SOURCE_ID])) {
if (!isset($stockSourceLinkData[StockSourceLink::SOURCE_CODE])) {
throw new InputException(__('Wrong Stock to Source relation parameters given.'));
}
}
Expand Down
14 changes: 9 additions & 5 deletions app/code/Magento/Inventory/Indexer/SelectBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public function execute($stockId): Select

// find all enabled sources
$select = $connection->select()
->from($sourceTable, [SourceInterface::SOURCE_ID])
->from($sourceTable, [SourceInterface::SOURCE_CODE])
->where(SourceInterface::ENABLED . ' = ?', 1);
$sourceIds = $connection->fetchCol($select);
$sourceCodes = $connection->fetchCol($select);

if (0 === count($sourceIds)) {
if (0 === count($sourceCodes)) {
return $select;
}

Expand All @@ -71,11 +71,15 @@ public function execute($stockId): Select
)
->joinLeft(
['stock_source_link' => $sourceStockLinkTable],
'source_item.' . SourceItemInterface::SOURCE_ID . ' = stock_source_link.' . StockSourceLink::SOURCE_ID,
sprintf(
'source_item.%s = stock_source_link.%s',
SourceItemInterface::SOURCE_CODE,
StockSourceLink::SOURCE_CODE
),
[]
)
->where('stock_source_link.' . StockSourceLink::STOCK_ID . ' = ?', $stockId)
->where('stock_source_link.' . StockSourceLink::SOURCE_ID . ' IN (?)', $sourceIds)
->where('stock_source_link.' . StockSourceLink::SOURCE_CODE . ' IN (?)', $sourceCodes)
->group([SourceItemInterface::SKU]);
return $select;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public function __construct(
}

/**
* @param int[] $sourceIds
* @param string[] $sourceCodes
* @return int[]
*/
public function execute(array $sourceIds): array
public function execute(array $sourceCodes): array
{
$connection = $this->resourceConnection->getConnection();
$sourceStockLinkTable = $this->resourceConnection->getTableName(
Expand All @@ -44,7 +44,7 @@ public function execute(array $sourceIds): array
$select = $connection
->select()
->from($sourceStockLinkTable, StockSourceLink::STOCK_ID)
->where(StockSourceLink::SOURCE_ID . ' IN (?)', $sourceIds)
->where(StockSourceLink::SOURCE_CODE . ' IN (?)', $sourceCodes)
->group(StockSourceLink::STOCK_ID);

$stockIds = $connection->fetchCol($select);
Expand Down
8 changes: 4 additions & 4 deletions app/code/Magento/Inventory/Indexer/Source/SourceIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ public function executeFull()
/**
* @inheritdoc
*/
public function executeRow($sourceId)
public function executeRow($sourceCode)
{
$this->executeList([$sourceId]);
$this->executeList([$sourceCode]);
}

/**
* @inheritdoc
*/
public function executeList(array $sourceIds)
public function executeList(array $sourceCodes)
{
$stockIds = $this->getAssignedStockIds->execute($sourceIds);
$stockIds = $this->getAssignedStockIds->execute($sourceCodes);
$this->stockIndexer->executeList($stockIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ public function execute(array $sourceItemIds): array
]
)->joinInner(
['stock_source_link' => $sourceStockLinkTable],
'source_item.' . SourceItemInterface::SOURCE_ID . ' = stock_source_link.' . StockSourceLink::SOURCE_ID,
sprintf(
'source_item.%s = stock_source_link.%s',
SourceItemInterface::SOURCE_CODE,
StockSourceLink::SOURCE_CODE
),
[StockSourceLink::STOCK_ID]
)->where('source_item.source_item_id IN (?)', $sourceItemIds)
->group(['stock_source_link.' . StockSourceLink::STOCK_ID]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Magento\Framework\MultiDimensionalIndex\IndexHandlerInterface;
use Magento\Framework\MultiDimensionalIndex\IndexNameBuilder;
use Magento\Framework\MultiDimensionalIndex\IndexStructureInterface;
use Magento\Inventory\Indexer\IndexDataProvider;
use Magento\Inventory\Indexer\Stock\StockIndexer;

/**
Expand Down Expand Up @@ -96,9 +95,9 @@ public function executeFull()
/**
* @inheritdoc
*/
public function executeRow($sourceId)
public function executeRow($sourceItemId)
{
$this->executeList([$sourceId]);
$this->executeList([$sourceItemId]);
}

/**
Expand Down
9 changes: 8 additions & 1 deletion app/code/Magento/Inventory/Model/ResourceModel/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ class Source extends AbstractDb
const TABLE_NAME_SOURCE = 'inventory_source';
/**#@-*/

/**
* Primary key auto increment flag
*
* @var bool
*/
protected $_isPkAutoIncrement = false;

/**
* @var SourceCarrierLinkManagementInterface
*/
Expand All @@ -54,7 +61,7 @@ public function __construct(
*/
protected function _construct()
{
$this->_init(self::TABLE_NAME_SOURCE, SourceInterface::SOURCE_ID);
$this->_init(self::TABLE_NAME_SOURCE, SourceInterface::SOURCE_CODE);
}

/**
Expand Down
Empty file modified app/code/Magento/Inventory/Model/ResourceModel/SourceItem.php
100644 → 100755
Empty file.
8 changes: 4 additions & 4 deletions app/code/Magento/Inventory/Model/ResourceModel/SourceItem/DeleteMultiple.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ private function buildWhereSqlPart(array $sourceItems): string
SourceItemInterface::SKU . ' = ?',
$sourceItem->getSku()
);
$sourceIdCondition = $connection->quoteInto(
SourceItemInterface::SOURCE_ID . ' = ?',
$sourceItem->getSourceId()
$sourceCodeCondition = $connection->quoteInto(
SourceItemInterface::SOURCE_CODE . ' = ?',
$sourceItem->getSourceCode()
);
$condition[] = '(' . $skuCondition . ' AND ' . $sourceIdCondition . ')';
$condition[] = '(' . $skuCondition . ' AND ' . $sourceCodeCondition . ')';
}
return implode(' OR ', $condition);
}
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/Inventory/Model/ResourceModel/SourceItem/SaveMultiple.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function execute(array $sourceItems)
$tableName = $this->resourceConnection->getTableName(SourceItemResourceModel::TABLE_NAME_SOURCE_ITEM);

$columnsSql = $this->buildColumnsSqlPart([
SourceItemInterface::SOURCE_ID,
SourceItemInterface::SOURCE_CODE,
SourceItemInterface::SKU,
SourceItemInterface::QUANTITY,
SourceItemInterface::STATUS
Expand Down Expand Up @@ -100,7 +100,7 @@ private function getSqlBindData(array $sourceItems): array
$bind = [];
foreach ($sourceItems as $sourceItem) {
$bind = array_merge($bind, [
$sourceItem->getSourceId(),
$sourceItem->getSourceCode(),
$sourceItem->getSku(),
$sourceItem->getQuantity(),
$sourceItem->getStatus(),
Expand Down
Loading