Skip to content

Commit

Permalink
MSI-518: Simplest “By Priority” Algorithm implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Valeriy Nayda committed Mar 1, 2018
1 parent 1e3752a commit 4a18953
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\TestFramework\TestCase\WebapiAbstract;

class GetAssignedSourcesForStockTest extends WebapiAbstract
class GetSourcesAssignedToStockOrderedByPriorityTest extends WebapiAbstract
{
/**#@+
* Service constants
*/
const RESOURCE_PATH_GET_ASSIGNED_SOURCES_FOR_STOCK = '/V1/inventory/stock/get-assigned-sources';
const SERVICE_NAME_GET_ASSIGNED_SOURCES_FOR_STOCK = 'inventoryApiGetAssignedSourcesForStockV1';
const RESOURCE_PATH_GET_ASSIGNED_SOURCES_FOR_STOCK
= '/V1/inventory/get-sources-assigned-to-stock-ordered-by-priority';
const SERVICE_NAME_GET_ASSIGNED_SOURCES_FOR_STOCK = 'inventoryApiGetSourcesAssignedToStockOrderedByPriorityV1';
/**#@-*/

/**
Expand All @@ -28,7 +29,7 @@ class GetAssignedSourcesForStockTest extends WebapiAbstract
*/
public function testGetAssignedSourcesForStock()
{
$stockId = 10;
$stockId = 30;
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH_GET_ASSIGNED_SOURCES_FOR_STOCK . '/' . $stockId,
Expand All @@ -43,7 +44,7 @@ public function testGetAssignedSourcesForStock()
? $this->_webApiCall($serviceInfo)
: $this->_webApiCall($serviceInfo, ['stockId' => $stockId]);
self::assertEquals(
['eu-1', 'eu-2', 'eu-3', 'eu-disabled'],
['us-1', 'eu-disabled', 'eu-3', 'eu-2', 'eu-1'],
array_column($response, SourceInterface::SOURCE_CODE)
);
}
Expand Down
81 changes: 59 additions & 22 deletions app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
*/
declare(strict_types=1);

use Magento\Framework\Api\DataObjectHelper;
use Magento\InventoryApi\Api\Data\StockSourceLinkInterface;
use Magento\InventoryApi\Api\Data\StockSourceLinkInterfaceFactory;
use Magento\InventoryApi\Api\StockSourceLinksSaveInterface;
use Magento\TestFramework\Helper\Bootstrap;

/** @var DataObjectHelper $dataObjectHelper */
$dataObjectHelper = Bootstrap::getObjectManager()->get(DataObjectHelper::class);
/** @var StockSourceLinksSaveInterface $stockSourceLinksSave */
$stockSourceLinksSave = Bootstrap::getObjectManager()->get(StockSourceLinksSaveInterface::class);
/** @var StockSourceLinkInterfaceFactory $stockSourceLinkFactory */
Expand All @@ -25,35 +28,69 @@
*
* EU-source-1(code:eu-1) - Global-stock(id:30)
* EU-source-2(code:eu-2) - Global-stock(id:30)
* EU-source-2(code:eu-2) - Global-stock(id:30)
* EU-source-3(code:eu-3) - Global-stock(id:30)
* EU-source-disabled(code:eu-disabled) - Global-stock(id:30)
* US-source-1(code:us-1) - Global-stock(id:30)
*/

/**
* $stock ID => list of source codes
*/
$linksData = [
10 => ['eu-1', 'eu-2', 'eu-3', 'eu-disabled'],
20 => ['us-1'],
30 => ['us-1', 'eu-disabled', 'eu-3', 'eu-2', 'eu-1']
[
StockSourceLinkInterface::STOCK_ID => 10,
StockSourceLinkInterface::SOURCE_CODE => 'eu-1',
StockSourceLinkInterface::PRIORITY => 1,
],
[
StockSourceLinkInterface::STOCK_ID => 10,
StockSourceLinkInterface::SOURCE_CODE => 'eu-2',
StockSourceLinkInterface::PRIORITY => 2,
],
[
StockSourceLinkInterface::STOCK_ID => 10,
StockSourceLinkInterface::SOURCE_CODE => 'eu-3',
StockSourceLinkInterface::PRIORITY => 3,
],
[
StockSourceLinkInterface::STOCK_ID => 10,
StockSourceLinkInterface::SOURCE_CODE => 'eu-disabled',
StockSourceLinkInterface::PRIORITY => 4,
],
[
StockSourceLinkInterface::STOCK_ID => 20,
StockSourceLinkInterface::SOURCE_CODE => 'us-1',
StockSourceLinkInterface::PRIORITY => 1,
],
[
StockSourceLinkInterface::STOCK_ID => 30,
StockSourceLinkInterface::SOURCE_CODE => 'eu-1',
StockSourceLinkInterface::PRIORITY => 5,
],
[
StockSourceLinkInterface::STOCK_ID => 30,
StockSourceLinkInterface::SOURCE_CODE => 'eu-2',
StockSourceLinkInterface::PRIORITY => 4,
],
[
StockSourceLinkInterface::STOCK_ID => 30,
StockSourceLinkInterface::SOURCE_CODE => 'eu-3',
StockSourceLinkInterface::PRIORITY => 3,
],
[
StockSourceLinkInterface::STOCK_ID => 30,
StockSourceLinkInterface::SOURCE_CODE => 'eu-disabled',
StockSourceLinkInterface::PRIORITY => 2,
],
[
StockSourceLinkInterface::STOCK_ID => 30,
StockSourceLinkInterface::SOURCE_CODE => 'us-1',
StockSourceLinkInterface::PRIORITY => 1,
],
];



$links = [];
$priority = 0;
foreach ($linksData as $stockId => $sourceCodes) {
foreach ($sourceCodes as $sourceCode) {
/** @var StockSourceLinkInterface $link */
$link = $stockSourceLinkFactory->create();

$link->setStockId($stockId);
$link->setSourceCode($sourceCode);
$link->setPriority(++$priority);

$links[] = $link;
}
foreach ($linksData as $linkData) {
/** @var StockSourceLinkInterface $link */
$link = $stockSourceLinkFactory->create();
$dataObjectHelper->populateWithArray($link, $linkData, StockSourceLinkInterface::class);
$links[] = $link;
}

$stockSourceLinksSave->execute($links);
12 changes: 6 additions & 6 deletions app/code/Magento/InventoryApi/etc/webapi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
<resource ref="Magento_InventoryApi::source_edit"/>
</resources>
</route>
<route url="/V1/inventory/get-sources-assigned-to-stock-ordered-by-priority/:stockId" method="GET">
<service class="Magento\InventoryApi\Api\GetSourcesAssignedToStockOrderedByPriorityInterface" method="execute"/>
<resources>
<resource ref="Magento_InventoryApi::source"/>
</resources>
</route>
<!-- Stock -->
<route url="/V1/inventory/stock" method="GET">
<service class="Magento\InventoryApi\Api\StockRepositoryInterface" method="getList"/>
Expand Down Expand Up @@ -70,12 +76,6 @@
</resources>
</route>
<!-- StockSourceLink -->
<route url="/V1/inventory/stock/get-assigned-sources/:stockId" method="GET">
<service class="Magento\InventoryApi\Api\GetAssignedSourcesForStockInterface" method="execute"/>
<resources>
<resource ref="Magento_InventoryApi::source_stock_link"/>
</resources>
</route>
<route url="/V1/inventory/stock-source-link" method="GET">
<service class="Magento\InventoryApi\Api\GetStockSourceLinksInterface" method="execute"/>
<resources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private function isZero(float $floatNumber): bool
* @param SourceItemSelectionInterface[] $sourceItemSelections
* @return SourceSelectionInterface[]
*/
private function createSourceSelection($sourceItemSelections): array
private function createSourceSelection(array $sourceItemSelections): array
{
$sourceSelections = [];
foreach ($sourceItemSelections as $sourceCode => $items) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
<?php foreach ($block->getSourceSelections() as $sourceSelection) : ?>
<table class="data-table admin__table-primary edit-order-table">
<div class="admin__page-section-item-title">
<span class="title"><?= $block->escapeHtml($this->getSourceName($sourceSelection->getSourceCode())) ?></span>
<span class="title">
<?= $block->escapeHtml($this->getSourceName($sourceSelection->getSourceCode())) ?>
</span>
</div>
<thead>
<tr class="headings">
Expand Down

0 comments on commit 4a18953

Please sign in to comment.