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 4 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
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not correct to cast to int here,
because when the factory creates SourceId there is no guarantee that factory will set something to SourceId

that's why PHP Doc says (@return int|null)

}

/**
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
2 changes: 1 addition & 1 deletion app/code/Magento/InventoryApi/Test/_files/source.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
$source,
[
SourceInterface::SOURCE_ID => 10,
SourceInterface::CODE => 'source-code-1',
SourceInterface::CODE => 'source-code',
SourceInterface::NAME => 'source-name-1',
SourceInterface::CONTACT_NAME => 'source-contact-name',
SourceInterface::EMAIL => 'source-email',
Expand Down
10 changes: 5 additions & 5 deletions app/code/Magento/InventoryApi/Test/_files/sources.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
[
// define only required and needed for tests fields
SourceInterface::SOURCE_ID => 10,
SourceInterface::CODE => 'source-code-1',
SourceInterface::CODE => 'eu-1',
SourceInterface::NAME => 'EU-source-1',
SourceInterface::ENABLED => true,
SourceInterface::PRIORITY => 100,
Expand All @@ -31,7 +31,7 @@
],
[
SourceInterface::SOURCE_ID => 20,
SourceInterface::CODE => 'source-code-2',
SourceInterface::CODE => 'eu-2',
SourceInterface::NAME => 'EU-source-2',
SourceInterface::ENABLED => true,
SourceInterface::PRIORITY => 200,
Expand All @@ -40,7 +40,7 @@
],
[
SourceInterface::SOURCE_ID => 30,
SourceInterface::CODE => 'source-code-3',
SourceInterface::CODE => 'eu-3',
SourceInterface::NAME => 'EU-source-3',
SourceInterface::ENABLED => true,
SourceInterface::PRIORITY => 300,
Expand All @@ -49,7 +49,7 @@
],
[
SourceInterface::SOURCE_ID => 40,
SourceInterface::CODE => 'source-code-4',
SourceInterface::CODE => 'eu-dis',
SourceInterface::NAME => 'EU-source-disabled',
SourceInterface::ENABLED => false,
SourceInterface::PRIORITY => 10,
Expand All @@ -58,7 +58,7 @@
],
[
SourceInterface::SOURCE_ID => 50,
SourceInterface::CODE => 'source-code-5',
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 @@ -9,6 +9,7 @@

use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory;
use Magento\InventoryApi\Api\SourceRepositoryInterface;

class SourceItemConvert
{
Expand All @@ -17,12 +18,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 +43,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