Skip to content

Commit 1ddb686

Browse files
authored
Merge pull request #211 from magento-engcom/qty-processing-import-export
#207 - Move Qty product import processing to new stock item importer
2 parents eaf1b21 + 290d919 commit 1ddb686

File tree

5 files changed

+253
-1
lines changed

5 files changed

+253
-1
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Product.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\CatalogImportExport\Model\Import;
77

88
use Magento\Catalog\Model\Product\Visibility;
9+
use Magento\CatalogImportExport\Model\StockItemImporterInterface;
910
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface as ValidatorInterface;
1011
use Magento\Framework\App\Filesystem\DirectoryList;
1112
use Magento\Framework\App\ObjectManager;
@@ -698,6 +699,13 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
698699
*/
699700
private $catalogConfig;
700701

702+
/**
703+
* Stock Item Importer
704+
*
705+
* @var StockItemImporterInterface $stockItemImporter
706+
*/
707+
private $stockItemImporter;
708+
701709
/**
702710
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
703711
* @param \Magento\ImportExport\Helper\Data $importExportData
@@ -737,6 +745,7 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
737745
* @param array $data
738746
* @param array $dateAttrCodes
739747
* @param CatalogConfig $catalogConfig
748+
* @param StockItemImporterInterface $stockItemImporter
740749
* @throws \Magento\Framework\Exception\LocalizedException
741750
*
742751
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -780,7 +789,8 @@ public function __construct(
780789
\Magento\Catalog\Model\Product\Url $productUrl,
781790
array $data = [],
782791
array $dateAttrCodes = [],
783-
CatalogConfig $catalogConfig = null
792+
CatalogConfig $catalogConfig = null,
793+
StockItemImporterInterface $stockItemImporter = null
784794
) {
785795
$this->_eventManager = $eventManager;
786796
$this->stockRegistry = $stockRegistry;
@@ -813,6 +823,8 @@ public function __construct(
813823
$this->dateAttrCodes = array_merge($this->dateAttrCodes, $dateAttrCodes);
814824
$this->catalogConfig = $catalogConfig ?: \Magento\Framework\App\ObjectManager::getInstance()
815825
->get(CatalogConfig::class);
826+
$this->stockItemImporter = $stockItemImporter ?: \Magento\Framework\App\ObjectManager::getInstance()
827+
->get(StockItemImporterInterface::class);
816828

817829
parent::__construct(
818830
$jsonHelper,
@@ -2261,6 +2273,7 @@ protected function _saveStockItem()
22612273
// Insert rows
22622274
if (!empty($stockData)) {
22632275
$this->_connection->insertOnDuplicate($entityTable, array_values($stockData));
2276+
$this->stockItemImporter->import($bunch);
22642277
}
22652278

22662279
$this->reindexProducts($productIdsToReindex);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogImportExport\Model;
9+
10+
/**
11+
* Interface StockItemImporterInterface
12+
*
13+
* @api
14+
*/
15+
interface StockItemImporterInterface
16+
{
17+
/**
18+
* Handle Import of Stock Item Data
19+
*
20+
* @param array $stockData
21+
* @return void
22+
*/
23+
public function import(array $stockData);
24+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\InventoryImportExport\Model;
9+
10+
use Magento\CatalogImportExport\Model\StockItemImporterInterface;
11+
use Magento\CatalogImportExport\Model\Import\Product;
12+
use Magento\Inventory\Model\SourceItemFactory;
13+
use Magento\InventoryApi\Api\SourceItemsSaveInterface;
14+
use Magento\InventoryCatalog\Api\DefaultSourceProviderInterface;
15+
16+
class StockItemImporter implements StockItemImporterInterface
17+
{
18+
/**
19+
* Source Items Save Interface for saving multiple source items
20+
*
21+
* @var SourceItemsSaveInterface $sourceItemsSave
22+
*/
23+
private $sourceItemsSave;
24+
25+
/**
26+
* Source Item Interface Factory
27+
*
28+
* @var SourceItemFactory $sourceItemFactory
29+
*/
30+
private $sourceItemFactory;
31+
32+
/**
33+
* Default Source Provider
34+
*
35+
* @var DefaultSourceProviderInterface $defaultSource
36+
*/
37+
private $defaultSource;
38+
39+
/**
40+
* StockItemImporter constructor
41+
*
42+
* @param SourceItemsSaveInterface $sourceItemsSave
43+
* @param SourceItemFactory $sourceItemFactory
44+
* @param DefaultSourceProviderInterface $defaultSourceProvider
45+
*/
46+
public function __construct(
47+
SourceItemsSaveInterface $sourceItemsSave,
48+
SourceItemFactory $sourceItemFactory,
49+
DefaultSourceProviderInterface $defaultSourceProvider
50+
) {
51+
$this->sourceItemsSave = $sourceItemsSave;
52+
$this->sourceItemFactory = $sourceItemFactory;
53+
$this->defaultSource = $defaultSourceProvider;
54+
}
55+
56+
/**
57+
* Handle Import of Stock Item Data
58+
*
59+
* @param array $stockData
60+
* @return void
61+
*/
62+
public function import(array $stockData)
63+
{
64+
$sourceItems = [];
65+
foreach ($stockData as $stockDatum) {
66+
if (isset($stockDatum[Product::COL_SKU])) {
67+
$inStock = (isset($stockDatum['is_in_stock'])) ? $stockDatum['is_in_stock'] : 0;
68+
$qty = (isset($stockDatum['qty'])) ? $stockDatum['qty'] : 0;
69+
$sourceItem = $this->sourceItemFactory->create();
70+
$sourceItem->setSku($stockDatum[Product::COL_SKU]);
71+
$sourceItem->setSourceId($this->defaultSource->getId());
72+
$sourceItem->setQuantity($qty);
73+
$sourceItem->setStatus($inStock);
74+
$sourceItems[] = $sourceItem;
75+
}
76+
}
77+
if (count($sourceItems) > 0) {
78+
/** Magento\Inventory\Model\SourceItem[] $sourceItems */
79+
$this->sourceItemsSave->execute($sourceItems);
80+
}
81+
}
82+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Inventory\Test\Integration\Model;
9+
10+
use Magento\CatalogImportExport\Model\StockItemImporterInterface;
11+
use Magento\Framework\Api\SearchCriteria;
12+
use Magento\Framework\Api\SearchCriteriaBuilder;
13+
use Magento\Framework\Api\SearchCriteriaBuilderFactory;
14+
use Magento\InventoryApi\Api\Data\SourceItemInterface;
15+
use Magento\InventoryApi\Api\SourceItemRepositoryInterface;
16+
use Magento\InventoryCatalog\Api\DefaultSourceProviderInterface;
17+
use Magento\TestFramework\Helper\Bootstrap;
18+
use PHPUnit\Framework\TestCase;
19+
20+
class StockItemImporterTest extends TestCase
21+
{
22+
/**
23+
* @var DefaultSourceProviderInterface
24+
*/
25+
private $defaultSourceProviderInterface;
26+
27+
/**
28+
* @var StockItemImporterInterface
29+
*/
30+
private $importerInterface;
31+
32+
/**
33+
* @var SearchCriteriaBuilderFactory
34+
*/
35+
private $searchCriteriaBuilderFactory;
36+
37+
/**
38+
* @var SourceItemRepositoryInterface
39+
*/
40+
private $sourceItemRepositoryInterface;
41+
42+
/**
43+
* Setup Test for Stock Item Importer
44+
*/
45+
public function setUp()
46+
{
47+
$this->defaultSourceProviderInterface = Bootstrap::getObjectManager()->get(
48+
DefaultSourceProviderInterface::class
49+
);
50+
$this->importerInterface = Bootstrap::getObjectManager()->get(
51+
StockItemImporterInterface::class
52+
);
53+
$this->searchCriteriaBuilderFactory = Bootstrap::getObjectManager()->get(
54+
SearchCriteriaBuilderFactory::class
55+
);
56+
$this->sourceItemRepositoryInterface = Bootstrap::getObjectManager()->get(
57+
SourceItemRepositoryInterface::class
58+
);
59+
}
60+
61+
/**
62+
* Tests Source Item Import of default source
63+
*
64+
* @magentoDbIsolation enabled
65+
*/
66+
public function testSourceItemImportWithDefaultSource()
67+
{
68+
$stockData = [
69+
'sku' => 'SKU-1',
70+
'qty' => 1,
71+
'is_in_stock' => SourceItemInterface::STATUS_IN_STOCK
72+
];
73+
74+
$this->importerInterface->import([$stockData]);
75+
76+
$compareData = $this->buildDataArray($this->getSourceItemList()->getItems());
77+
$expectedData = [
78+
SourceItemInterface::SKU => $stockData['sku'],
79+
SourceItemInterface::QUANTITY => '1.0000',
80+
SourceItemInterface::SOURCE_ID => (string) $this->defaultSourceProviderInterface->getId(),
81+
SourceItemInterface::STATUS => (string) SourceItemInterface::STATUS_IN_STOCK
82+
];
83+
84+
$this->assertArrayHasKey('SKU-1', $compareData);
85+
$this->assertSame($expectedData, $compareData['SKU-1']);
86+
}
87+
88+
/**
89+
* Get List of Source Items which match SKU and Source ID of dummy data
90+
*
91+
* @return \Magento\InventoryApi\Api\Data\SourceItemSearchResultsInterface
92+
*/
93+
private function getSourceItemList()
94+
{
95+
/** @var SearchCriteriaBuilder $searchCriteria */
96+
$searchCriteriaBuilder = $this->searchCriteriaBuilderFactory->create();
97+
98+
$searchCriteriaBuilder->addFilter(
99+
SourceItemInterface::SKU,
100+
'SKU-1'
101+
);
102+
103+
$searchCriteriaBuilder->addFilter(
104+
SourceItemInterface::SOURCE_ID,
105+
$this->defaultSourceProviderInterface->getId()
106+
);
107+
108+
/** @var SearchCriteria $searchCriteria */
109+
$searchCriteria = $searchCriteriaBuilder->create();
110+
return $this->sourceItemRepositoryInterface->getList($searchCriteria);
111+
}
112+
113+
/**
114+
* @param SourceItemInterface[] $sourceItems
115+
* @return array
116+
*/
117+
private function buildDataArray(array $sourceItems)
118+
{
119+
$comparableArray = [];
120+
foreach ($sourceItems as $sourceItem) {
121+
$comparableArray[$sourceItem->getSku()] = [
122+
SourceItemInterface::SKU => $sourceItem->getSku(),
123+
SourceItemInterface::QUANTITY => $sourceItem->getQuantity(),
124+
SourceItemInterface::SOURCE_ID => $sourceItem->getSourceId(),
125+
SourceItemInterface::STATUS => $sourceItem->getStatus()
126+
];
127+
}
128+
return $comparableArray;
129+
}
130+
}

app/code/Magento/InventoryImportExport/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
</arguments>
2929
</type>
3030

31+
<!-- Source Item Import -->
32+
<preference for="\Magento\CatalogImportExport\Model\StockItemImporterInterface" type="\Magento\InventoryImportExport\Model\StockItemImporter" />
33+
3134
<!-- Export -->
3235
<preference for="Magento\InventoryImportExport\Model\Export\SourceItemCollectionFactoryInterface" type="Magento\InventoryImportExport\Model\Export\SourceItemCollectionFactory"/>
3336
<preference for="Magento\InventoryImportExport\Model\Export\ColumnProviderInterface" type="\Magento\InventoryImportExport\Model\Export\ColumnProvider"/>

0 commit comments

Comments
 (0)