Skip to content

Commit

Permalink
MSI: issue #290
Browse files Browse the repository at this point in the history
- source_id field usings ware replaced by source_code field - part;
  • Loading branch information
deadlexus committed Dec 20, 2017
1 parent 1b85ca7 commit fb9480d
Show file tree
Hide file tree
Showing 83 changed files with 330 additions and 446 deletions.
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 = (string)$this->getRequest()->getParam(SourceInterface::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 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 = (string)$itemData[SourceInterface::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]
'[Code: %value] The Source does not exist.',
['value' => $sourceCode]
);
} catch (ValidationException $e) {
foreach ($e->getErrors() as $localizedError) {
$errorMessages[] = __('[ID: %value] %message', [
'value' => $sourceId,
$errorMessages[] = __('[Code: %value] %message', [
'value' => $sourceCode,
'message' => $localizedError->getMessage()
]);
}
} catch (CouldNotSaveException $e) {
$errorMessages[] = __(
'[ID: %value] %message',
'[Code: %value] %message',
[
'value' => $sourceId,
'value' => $sourceCode,
'message' => $e->getMessage()
]
);
Expand Down
42 changes: 21 additions & 21 deletions app/code/Magento/Inventory/Controller/Adminhtml/Source/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,45 +78,45 @@ public function execute(): ResultInterface
}

try {
$sourceId = isset($requestData['general'][SourceInterface::SOURCE_ID])
? (int)$requestData['general'][SourceInterface::SOURCE_ID]
$sourceCode = isset($requestData['general'][SourceInterface::CODE])
? (string)$requestData['general'][SourceInterface::CODE]
: null;
$sourceId = $this->processSave($requestData, $sourceId);
$sourceCode = $this->processSave($requestData, $sourceCode);

$this->messageManager->addSuccessMessage(__('The Source has been saved.'));
$this->processRedirectAfterSuccessSave($resultRedirect, $sourceId);
$this->processRedirectAfterSuccessSave($resultRedirect, $sourceCode);
} 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, $sourceCode);
} catch (CouldNotSaveException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$this->processRedirectAfterFailureSave($resultRedirect, $sourceId);
$this->processRedirectAfterFailureSave($resultRedirect, $sourceCode);
} catch (Exception $e) {
$this->messageManager->addErrorMessage(__('Could not save Source.'));
$this->processRedirectAfterFailureSave($resultRedirect, $sourceId ?? null);
$this->processRedirectAfterFailureSave($resultRedirect, $sourceCode ?? null);
}

return $resultRedirect;
}

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

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

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

$this->_eventManager->dispatch(
'controller_action_inventory_source_save_after',
Expand All @@ -138,20 +138,20 @@ private function processSave(array $requestData, int $sourceId = null): int
]
);

return $sourceId;
return $sourceCode;
}

/**
* @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::CODE => $sourceCode,
'_current' => true,
]);
} elseif ($this->getRequest()->getParam('redirect_to_new')) {
Expand All @@ -165,17 +165,17 @@ 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::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->getCode(), $sourceCodesForSave)) {
unset($sourceCodesForSave[$assignedSource->getCode()]);
} else {
$sourceIdsForDelete[] = $assignedSource->getSourceId();
$sourceCodesForDelete[] = $assignedSource->getCode();
}
}

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($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
10 changes: 5 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::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,11 @@ public function execute($stockId): Select
)
->joinLeft(
['stock_source_link' => $sourceStockLinkTable],
'source_item.' . SourceItemInterface::SOURCE_ID . ' = stock_source_link.' . StockSourceLink::SOURCE_ID,
'source_item.' . SourceItemInterface::SOURCE_CODE . ' = stock_source_link.' . 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 $sourceCode)
{
$stockIds = $this->getAssignedStockIds->execute($sourceIds);
$stockIds = $this->getAssignedStockIds->execute($sourceCode);
$this->stockIndexer->executeList($stockIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function execute(array $sourceItemIds): array
]
)->joinInner(
['stock_source_link' => $sourceStockLinkTable],
'source_item.' . SourceItemInterface::SOURCE_ID . ' = stock_source_link.' . StockSourceLink::SOURCE_ID,
'source_item.' . SourceItemInterface::SOURCE_CODE . ' = stock_source_link.' . 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 @@ -96,9 +96,9 @@ public function executeFull()
/**
* @inheritdoc
*/
public function executeRow($sourceId)
public function executeRow($sourceItemId)
{
$this->executeList([$sourceId]);
$this->executeList([$sourceItemId]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Inventory/Model/ResourceModel/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct(
*/
protected function _construct()
{
$this->_init(self::TABLE_NAME_SOURCE, SourceInterface::SOURCE_ID);
$this->_init(self::TABLE_NAME_SOURCE, SourceInterface::CODE);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SourceItem extends AbstractDb
* Constants related to specific db layer
*/
const TABLE_NAME_SOURCE_ITEM = 'inventory_source_item';
const ID_FIELD_NAME = 'source_code';
const ID_FIELD_NAME = 'source_item_id';
/**#@-*/

/**
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public function __construct(
}

/**
* @param array $sourceIds
* @param array $sourceCodes
* @param int $stockId
* @return void
*/
public function execute(array $sourceIds, int $stockId)
public function execute(array $sourceCodes, int $stockId)
{
if (!count($sourceIds)) {
if (!count($sourceCodes)) {
return;
}
$connection = $this->resourceConnection->getConnection();
Expand All @@ -47,13 +47,13 @@ public function execute(array $sourceIds, int $stockId)
);

$columns = [
StockSourceLink::SOURCE_ID,
StockSourceLink::SOURCE_CODE,
StockSourceLink::STOCK_ID,
];

$data = [];
foreach ($sourceIds as $sourceId) {
$data[] = [$sourceId, $stockId];
foreach ($sourceCodes as $sourceCode) {
$data[] = [$sourceCode, $stockId];
}
if ($data) {
$connection->insertArray($tableName, $columns, $data);
Expand Down
8 changes: 4 additions & 4 deletions app/code/Magento/Inventory/Model/Source/Command/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ public function __construct(
/**
* @inheritdoc
*/
public function execute(int $sourceId): SourceInterface
public function execute(string $code): SourceInterface
{
/** @var SourceInterface $source */
$source = $this->sourceFactory->create();
$this->sourceResource->load($source, $sourceId, SourceInterface::SOURCE_ID);
$this->sourceResource->load($source, $code, SourceInterface::CODE);

if (null === $source->getSourceId()) {
throw new NoSuchEntityException(__('Source with id "%value" does not exist.', ['value' => $sourceId]));
if (null === $source->getCode()) {
throw new NoSuchEntityException(__('Source with code "%value" does not exist.', ['value' => $code]));
}
return $source;
}
Expand Down
Loading

0 comments on commit fb9480d

Please sign in to comment.