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

#207 - Move Qty product import processing to new stock item importer #211

Merged
merged 14 commits into from
Nov 21, 2017
15 changes: 14 additions & 1 deletion app/code/Magento/CatalogImportExport/Model/Import/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Magento\CatalogImportExport\Model\Import;

use Magento\Catalog\Model\Product\Visibility;
use Magento\CatalogImportExport\Model\StockItemImporterInterface;
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface as ValidatorInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ObjectManager;
Expand Down Expand Up @@ -698,6 +699,13 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
*/
private $catalogConfig;

/**
* Stock Item Importer
*
* @var StockItemImporterInterface $stockItemImporter
*/
private $stockItemImporter;

/**
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
* @param \Magento\ImportExport\Helper\Data $importExportData
Expand Down Expand Up @@ -737,6 +745,7 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
* @param array $data
* @param array $dateAttrCodes
* @param CatalogConfig $catalogConfig
* @param StockItemImporterInterface $stockItemImporter
* @throws \Magento\Framework\Exception\LocalizedException
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
Expand Down Expand Up @@ -780,7 +789,8 @@ public function __construct(
\Magento\Catalog\Model\Product\Url $productUrl,
array $data = [],
array $dateAttrCodes = [],
CatalogConfig $catalogConfig = null
CatalogConfig $catalogConfig = null,
StockItemImporterInterface $stockItemImporter = null
) {
$this->_eventManager = $eventManager;
$this->stockRegistry = $stockRegistry;
Expand Down Expand Up @@ -813,6 +823,8 @@ public function __construct(
$this->dateAttrCodes = array_merge($this->dateAttrCodes, $dateAttrCodes);
$this->catalogConfig = $catalogConfig ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(CatalogConfig::class);
$this->stockItemImporter = $stockItemImporter ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(StockItemImporterInterface::class);

parent::__construct(
$jsonHelper,
Expand Down Expand Up @@ -2261,6 +2273,7 @@ protected function _saveStockItem()
// Insert rows
if (!empty($stockData)) {
$this->_connection->insertOnDuplicate($entityTable, array_values($stockData));
$this->stockItemImporter->import($bunch);
}

$this->reindexProducts($productIdsToReindex);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogImportExport\Model;

/**
* Interface StockItemImporterInterface
*
* @api
*/
interface StockItemImporterInterface
{
/**
* Handle Import of Stock Item Data
*
* @param array $stockData
* @return void
*/
public function import(array $stockData);
}
82 changes: 82 additions & 0 deletions app/code/Magento/InventoryImportExport/Model/StockItemImporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryImportExport\Model;

use Magento\CatalogImportExport\Model\StockItemImporterInterface;
use Magento\CatalogImportExport\Model\Import\Product;
use Magento\Inventory\Model\SourceItemFactory;
use Magento\InventoryApi\Api\SourceItemsSaveInterface;
use Magento\InventoryCatalog\Api\DefaultSourceProviderInterface;

class StockItemImporter implements StockItemImporterInterface
{
/**
* Source Items Save Interface for saving multiple source items
*
* @var SourceItemsSaveInterface $sourceItemsSave
*/
private $sourceItemsSave;

/**
* Source Item Interface Factory
*
* @var SourceItemFactory $sourceItemFactory
*/
private $sourceItemFactory;

/**
* Default Source Provider
*
* @var DefaultSourceProviderInterface $defaultSource
*/
private $defaultSource;

/**
* StockItemImporter constructor
*
* @param SourceItemsSaveInterface $sourceItemsSave
* @param SourceItemFactory $sourceItemFactory
* @param DefaultSourceProviderInterface $defaultSourceProvider
*/
public function __construct(
SourceItemsSaveInterface $sourceItemsSave,
SourceItemFactory $sourceItemFactory,
DefaultSourceProviderInterface $defaultSourceProvider
) {
$this->sourceItemsSave = $sourceItemsSave;
$this->sourceItemFactory = $sourceItemFactory;
$this->defaultSource = $defaultSourceProvider;
}

/**
* Handle Import of Stock Item Data
*
* @param array $stockData
* @return void
*/
public function import(array $stockData)
{
$sourceItems = [];
foreach ($stockData as $stockDatum) {
if (isset($stockDatum[Product::COL_SKU])) {
$inStock = (isset($stockDatum['is_in_stock'])) ? $stockDatum['is_in_stock'] : 0;
$qty = (isset($stockDatum['qty'])) ? $stockDatum['qty'] : 0;
$sourceItem = $this->sourceItemFactory->create();
$sourceItem->setSku($stockDatum[Product::COL_SKU]);
$sourceItem->setSourceId($this->defaultSource->getId());
$sourceItem->setQuantity($qty);
$sourceItem->setStatus($inStock);
$sourceItems[] = $sourceItem;
}
}
if (count($sourceItems) > 0) {
/** Magento\Inventory\Model\SourceItem[] $sourceItems */
$this->sourceItemsSave->execute($sourceItems);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Inventory\Test\Integration\Model;

use Magento\CatalogImportExport\Model\StockItemImporterInterface;
use Magento\Framework\Api\SearchCriteria;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SearchCriteriaBuilderFactory;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\SourceItemRepositoryInterface;
use Magento\InventoryCatalog\Api\DefaultSourceProviderInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

class StockItemImporterTest extends TestCase
{
/**
* @var DefaultSourceProviderInterface
*/
private $defaultSourceProviderInterface;

/**
* @var StockItemImporterInterface
*/
private $importerInterface;

/**
* @var SearchCriteriaBuilderFactory
*/
private $searchCriteriaBuilderFactory;

/**
* @var SourceItemRepositoryInterface
*/
private $sourceItemRepositoryInterface;

/**
* Setup Test for Stock Item Importer
*/
public function setUp()
{
$this->defaultSourceProviderInterface = Bootstrap::getObjectManager()->get(
DefaultSourceProviderInterface::class
);
$this->importerInterface = Bootstrap::getObjectManager()->get(
StockItemImporterInterface::class
);
$this->searchCriteriaBuilderFactory = Bootstrap::getObjectManager()->get(
SearchCriteriaBuilderFactory::class
);
$this->sourceItemRepositoryInterface = Bootstrap::getObjectManager()->get(
SourceItemRepositoryInterface::class
);
}

/**
* Tests Source Item Import of default source
*
* @magentoDbIsolation enabled
*/
public function testSourceItemImportWithDefaultSource()
{
$stockData = [
'sku' => 'SKU-1',
'qty' => 1,
'is_in_stock' => SourceItemInterface::STATUS_IN_STOCK
];

$this->importerInterface->import([$stockData]);

$compareData = $this->buildDataArray($this->getSourceItemList()->getItems());
$expectedData = [
SourceItemInterface::SKU => $stockData['sku'],
SourceItemInterface::QUANTITY => '1.0000',
SourceItemInterface::SOURCE_ID => (string) $this->defaultSourceProviderInterface->getId(),
SourceItemInterface::STATUS => (string) SourceItemInterface::STATUS_IN_STOCK
];

$this->assertArrayHasKey('SKU-1', $compareData);
$this->assertSame($expectedData, $compareData['SKU-1']);
}

/**
* Get List of Source Items which match SKU and Source ID of dummy data
*
* @return \Magento\InventoryApi\Api\Data\SourceItemSearchResultsInterface
*/
private function getSourceItemList()
{
/** @var SearchCriteriaBuilder $searchCriteria */
$searchCriteriaBuilder = $this->searchCriteriaBuilderFactory->create();

$searchCriteriaBuilder->addFilter(
SourceItemInterface::SKU,
'SKU-1'
);

$searchCriteriaBuilder->addFilter(
SourceItemInterface::SOURCE_ID,
$this->defaultSourceProviderInterface->getId()
);

/** @var SearchCriteria $searchCriteria */
$searchCriteria = $searchCriteriaBuilder->create();
return $this->sourceItemRepositoryInterface->getList($searchCriteria);
}

/**
* @param SourceItemInterface[] $sourceItems
* @return array
*/
private function buildDataArray(array $sourceItems)
{
$comparableArray = [];
foreach ($sourceItems as $sourceItem) {
$comparableArray[$sourceItem->getSku()] = [
SourceItemInterface::SKU => $sourceItem->getSku(),
SourceItemInterface::QUANTITY => $sourceItem->getQuantity(),
SourceItemInterface::SOURCE_ID => $sourceItem->getSourceId(),
SourceItemInterface::STATUS => $sourceItem->getStatus()
];
}
return $comparableArray;
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/InventoryImportExport/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
</arguments>
</type>

<!-- Source Item Import -->
<preference for="\Magento\CatalogImportExport\Model\StockItemImporterInterface" type="\Magento\InventoryImportExport\Model\StockItemImporter" />

<!-- Export -->
<preference for="Magento\InventoryImportExport\Model\Export\SourceItemCollectionFactoryInterface" type="Magento\InventoryImportExport\Model\Export\SourceItemCollectionFactory"/>
<preference for="Magento\InventoryImportExport\Model\Export\ColumnProviderInterface" type="\Magento\InventoryImportExport\Model\Export\ColumnProvider"/>
Expand Down