Skip to content

Commit

Permalink
MSI: issue #290
Browse files Browse the repository at this point in the history
- Adapted SourceItem Import/Export to use Source Code instead of SourceId;
- Fixed issue when in Stock Sources export process all fields ware excluded;
- Fixed type of return value for SourceItem->getSourceId method to int(was string);
- Adapted integrational test to the new Export/Import system(with source code).
  • Loading branch information
System User committed Dec 14, 2017
1 parent b224061 commit 8548525
Show file tree
Hide file tree
Showing 20 changed files with 241 additions and 66 deletions.
56 changes: 56 additions & 0 deletions app/code/Magento/Inventory/Model/Source/Command/GetByCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Inventory\Model\Source\Command;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Inventory\Model\ResourceModel\Source as SourceResourceModel;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\Data\SourceInterfaceFactory;

/**
* @inheritdoc
*/
class GetByCode implements GetByCodeInterface
{
/**
* @var SourceResourceModel
*/
private $sourceResource;

/**
* @var SourceInterfaceFactory
*/
private $sourceFactory;

/**
* @param SourceResourceModel $sourceResource
* @param SourceInterfaceFactory $sourceFactory
*/
public function __construct(
SourceResourceModel $sourceResource,
SourceInterfaceFactory $sourceFactory
) {
$this->sourceResource = $sourceResource;
$this->sourceFactory = $sourceFactory;
}

/**
* @inheritdoc
*/
public function execute(string $code): SourceInterface
{
/** @var SourceInterface $source */
$source = $this->sourceFactory->create();
$this->sourceResource->load($source, $code, SourceInterface::CODE);

if (null === $source->getSourceId()) {
throw new NoSuchEntityException(__('Source with code "%value" does not exist.', ['value' => $code]));
}
return $source;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Inventory\Model\Source\Command;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\InventoryApi\Api\Data\SourceInterface;

/**
* Get Source by code command (Service Provider Interface - SPI)
*
* Separate command interface to which Repository proxies initial Get call, could be considered as SPI - Interfaces
* that you should extend and implement to customize current behaviour, but NOT expected to be used (called) in the code
* of business logic directly
*
* @see \Magento\InventoryApi\Api\SourceRepositoryInterface
* @api
*/
interface GetByCodeInterface
{
/**
* Get Source data by given code
*
* @param string $code
* @return SourceInterface
* @throws NoSuchEntityException
*/
public function execute(string $code): SourceInterface;
}
2 changes: 1 addition & 1 deletion app/code/Magento/Inventory/Model/SourceItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function setSku($sku)
*/
public function getSourceId()
{
return $this->getData(self::SOURCE_ID);
return (int)$this->getData(self::SOURCE_ID);
}

/**
Expand Down
16 changes: 16 additions & 0 deletions app/code/Magento/Inventory/Model/SourceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Inventory\Model\Source\Command\GetInterface;
use Magento\Inventory\Model\Source\Command\GetByCodeInterface;
use Magento\Inventory\Model\Source\Command\GetListInterface;
use Magento\Inventory\Model\Source\Command\SaveInterface;
use Magento\InventoryApi\Api\Data\SourceInterface;
Expand All @@ -30,6 +31,11 @@ class SourceRepository implements SourceRepositoryInterface
*/
private $commandGet;

/**
* @var GetByCodeInterface
*/
private $commandGetByCode;

/**
* @var GetListInterface
*/
Expand All @@ -43,10 +49,12 @@ class SourceRepository implements SourceRepositoryInterface
public function __construct(
SaveInterface $commandSave,
GetInterface $commandGet,
GetByCodeInterface $commandGetByCode,
GetListInterface $commandGetList
) {
$this->commandSave = $commandSave;
$this->commandGet = $commandGet;
$this->commandGetByCode = $commandGetByCode;
$this->commandGetList = $commandGetList;
}

Expand All @@ -66,6 +74,14 @@ public function get(int $sourceId): SourceInterface
return $this->commandGet->execute($sourceId);
}

/**
* @inheritdoc
*/
public function getByCode(string $code): SourceInterface
{
return $this->commandGetByCode->execute($code);
}

/**
* @inheritdoc
*/
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Inventory/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<preference for="Magento\InventoryApi\Api\Data\SourceSearchResultsInterface" type="Magento\Inventory\Model\SourceSearchResults"/>
<preference for="Magento\Inventory\Model\SourceCarrierLinkManagementInterface" type="Magento\Inventory\Model\SourceCarrierLinkManagement"/>
<preference for="Magento\Inventory\Model\Source\Command\GetInterface" type="Magento\Inventory\Model\Source\Command\Get"/>
<preference for="Magento\Inventory\Model\Source\Command\GetByCodeInterface" type="Magento\Inventory\Model\Source\Command\GetByCode"/>
<preference for="Magento\Inventory\Model\Source\Command\GetListInterface" type="Magento\Inventory\Model\Source\Command\GetList"/>
<preference for="Magento\Inventory\Model\Source\Command\SaveInterface" type="Magento\Inventory\Model\Source\Command\Save"/>
<type name="Magento\Inventory\Model\Source\Validator\ValidatorChain">
Expand Down
10 changes: 10 additions & 0 deletions app/code/Magento/InventoryApi/Api/SourceRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ public function save(SourceInterface $source): int;
*/
public function get(int $sourceId): SourceInterface;

/**
* Get Source data by given code. If you want to create plugin on get method, also you need to create separate
* plugin on getList method, because entity loading way is different for these methods
*
* @param int $code
* @return \Magento\InventoryApi\Api\Data\SourceInterface
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getByCode(string $code): SourceInterface;

/**
* Find Sources by SearchCriteria
* SearchCriteria is not required because load all stocks is useful case
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/InventoryApi/Test/_files/source.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
$source,
[
SourceInterface::SOURCE_ID => 10,
SourceInterface::CODE => 'source-code',
SourceInterface::NAME => 'source-name-1',
SourceInterface::CONTACT_NAME => 'source-contact-name',
SourceInterface::EMAIL => 'source-email',
Expand Down
5 changes: 5 additions & 0 deletions app/code/Magento/InventoryApi/Test/_files/sources.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
[
// define only required and needed for tests fields
SourceInterface::SOURCE_ID => 10,
SourceInterface::CODE => 'eu-1',
SourceInterface::NAME => 'EU-source-1',
SourceInterface::ENABLED => true,
SourceInterface::PRIORITY => 100,
Expand All @@ -30,6 +31,7 @@
],
[
SourceInterface::SOURCE_ID => 20,
SourceInterface::CODE => 'eu-2',
SourceInterface::NAME => 'EU-source-2',
SourceInterface::ENABLED => true,
SourceInterface::PRIORITY => 200,
Expand All @@ -38,6 +40,7 @@
],
[
SourceInterface::SOURCE_ID => 30,
SourceInterface::CODE => 'eu-3',
SourceInterface::NAME => 'EU-source-3',
SourceInterface::ENABLED => true,
SourceInterface::PRIORITY => 300,
Expand All @@ -46,6 +49,7 @@
],
[
SourceInterface::SOURCE_ID => 40,
SourceInterface::CODE => 'eu-dis',
SourceInterface::NAME => 'EU-source-disabled',
SourceInterface::ENABLED => false,
SourceInterface::PRIORITY => 10,
Expand All @@ -54,6 +58,7 @@
],
[
SourceInterface::SOURCE_ID => 50,
SourceInterface::CODE => 'us-1',
SourceInterface::NAME => 'US-source-1',
SourceInterface::ENABLED => true,
SourceInterface::PRIORITY => 100,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Eav\Model\Entity\AttributeFactory;
use Magento\Framework\Data\Collection;
use Magento\ImportExport\Model\Export\Factory as CollectionFactory;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryImportExport\Model\Export\Source\StockStatus;

Expand Down Expand Up @@ -49,12 +50,12 @@ public function get(): Collection
{
if (count($this->collection) === 0) {
/** @var \Magento\Eav\Model\Entity\Attribute $sourceIdAttribute */
$sourceIdAttribute = $this->attributeFactory->create();
$sourceIdAttribute->setId(SourceItemInterface::SOURCE_ID);
$sourceIdAttribute->setDefaultFrontendLabel(SourceItemInterface::SOURCE_ID);
$sourceIdAttribute->setAttributeCode(SourceItemInterface::SOURCE_ID);
$sourceIdAttribute->setBackendType('int');
$this->collection->addItem($sourceIdAttribute);
$sourceCodeAttribute = $this->attributeFactory->create();
$sourceCodeAttribute->setId('source_' . SourceInterface::CODE);
$sourceCodeAttribute->setDefaultFrontendLabel('source_' . SourceInterface::CODE);
$sourceCodeAttribute->setAttributeCode('source_' . SourceInterface::CODE);
$sourceCodeAttribute->setBackendType('varchar');
$this->collection->addItem($sourceCodeAttribute);

/** @var \Magento\Eav\Model\Entity\Attribute $skuAttribute */
$skuAttribute = $this->attributeFactory->create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Framework\Data\Collection as AttributeCollection;
use Magento\InventoryImportExport\Model\Export\ColumnProviderInterface;
use Magento\ImportExport\Model\Export;
use \Magento\Framework\Exception\LocalizedException;

/**
* @inheritdoc
Expand All @@ -30,6 +31,10 @@ public function getHeaders(AttributeCollection $attributeCollection, array $filt
return $columns;
}

if (count($filters[Export::FILTER_ELEMENT_SKIP]) === count($columns)) {
throw new LocalizedException(__('There is no data for the export.'));
}

// remove the skipped from columns
$skippedAttributes = array_flip($filters[Export::FILTER_ELEMENT_SKIP]);
foreach ($columns as $key => $value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@

namespace Magento\InventoryImportExport\Model\Export;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Data\Collection as AttributeCollection;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\ObjectManagerInterface;
use Magento\Inventory\Model\ResourceModel\Source as SourceResourceModel;
use Magento\Inventory\Model\ResourceModel\SourceItem\Collection;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryImportExport\Model\Export\ColumnProviderInterface;
use Magento\InventoryImportExport\Model\Export\SourceItemCollectionFactoryInterface;
use Magento\ImportExport\Model\Export;
Expand All @@ -20,6 +24,11 @@
*/
class SourceItemCollectionFactory implements SourceItemCollectionFactoryInterface
{
/**
* Source code field name
*/
const SOURCE_CODE_FIELD = 'source_' . SourceInterface::CODE;

/**
* @var ObjectManagerInterface
*/
Expand All @@ -35,19 +44,27 @@ class SourceItemCollectionFactory implements SourceItemCollectionFactoryInterfac
*/
private $columnProvider;

/**
* @var ResourceConnection
*/
private $resourceConnection;

/**
* @param ObjectManagerInterface $objectManager
* @param FilterProcessorAggregator $filterProcessor
* @param ColumnProviderInterface $columnProvider
* @param ResourceConnection $resourceConnection
*/
public function __construct(
ObjectManagerInterface $objectManager,
FilterProcessorAggregator $filterProcessor,
ColumnProviderInterface $columnProvider
ColumnProviderInterface $columnProvider,
ResourceConnection $resourceConnection
) {
$this->objectManager = $objectManager;
$this->filterProcessor = $filterProcessor;
$this->columnProvider = $columnProvider;
$this->resourceConnection = $resourceConnection;
}

/**
Expand All @@ -60,7 +77,17 @@ public function create(AttributeCollection $attributeCollection, array $filters)
{
/** @var Collection $collection */
$collection = $this->objectManager->create(Collection::class);
$collection->addFieldToSelect($this->columnProvider->getColumns($attributeCollection, $filters));
$columns = $this->columnProvider->getColumns($attributeCollection, $filters);
if (($key = array_search(self::SOURCE_CODE_FIELD, $columns)) !== false) {
unset($columns[$key]);
$collection->join(
['s' => $this->resourceConnection->getTableName(SourceResourceModel::TABLE_NAME_SOURCE)],
sprintf('s.%s = main_table.%s', SourceInterface::SOURCE_ID, SourceItemInterface::SOURCE_ID),
['source_code' => SourceInterface::CODE]
);
$collection->addFilterToMap('source_code', sprintf('s.%s', SourceInterface::CODE));
}
$collection->addFieldToSelect($columns);

foreach ($this->retrieveFilterData($filters) as $columnName => $value) {
$attributeDefinition = $attributeCollection->getItemById($columnName);
Expand All @@ -81,7 +108,6 @@ public function create(AttributeCollection $attributeCollection, array $filters)

$this->filterProcessor->process($type, $collection, $columnName, $value);
}

return $collection;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

namespace Magento\InventoryImportExport\Model\Import;

use Magento\Customer\Model\Indexer\Source;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory;
use Magento\InventoryApi\Api\SourceRepositoryInterface;

class SourceItemConvert
{
Expand All @@ -17,12 +19,20 @@ class SourceItemConvert
*/
private $sourceItemFactory;

/**´
* @var SourceRepositoryInterface
*/
private $sourceRepository;

/**
* @param SourceItemInterfaceFactory $sourceItemFactory
*/
public function __construct(SourceItemInterfaceFactory $sourceItemFactory)
{
public function __construct(
SourceItemInterfaceFactory $sourceItemFactory,
SourceRepositoryInterface $sourceRepository
) {
$this->sourceItemFactory = $sourceItemFactory;
$this->sourceRepository = $sourceRepository;
}

/**
Expand All @@ -34,9 +44,10 @@ public function convert(array $bunch): array
{
$sourceItems = [];
foreach ($bunch as $rowData) {
$source = $this->sourceRepository->getByCode($rowData[Sources::COL_SOURCE_CODE]);
/** @var SourceItemInterface $sourceItem */
$sourceItem = $this->sourceItemFactory->create();
$sourceItem->setSourceId($rowData[Sources::COL_SOURCE]);
$sourceItem->setSourceId($source->getSourceId());
$sourceItem->setSku($rowData[Sources::COL_SKU]);
$sourceItem->setQuantity($rowData[Sources::COL_QTY]);

Expand Down
Loading

0 comments on commit 8548525

Please sign in to comment.