Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mage-os-ci committed Mar 20, 2024
2 parents 3898193 + b22b951 commit bdb778e
Show file tree
Hide file tree
Showing 16 changed files with 353 additions and 94 deletions.
10 changes: 2 additions & 8 deletions CatalogInventoryDataExporter/Model/Plugin/StockStatusUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@

namespace Magento\CatalogInventoryDataExporter\Model\Plugin;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\SourceItemsSaveInterface;
use Magento\Catalog\Model\ProductRepository;
use Magento\Indexer\Model\IndexerFactory;
use Magento\DataExporter\Model\Logging\CommerceDataExportLoggerInterface as LoggerInterface;
use Magento\InventoryIndexer\Model\ResourceModel\UpdateIsSalable;
use Magento\InventoryMultiDimensionalIndexerApi\Model\IndexName;

Expand Down Expand Up @@ -44,7 +37,8 @@ public function __construct(
*/
public function afterExecute(
UpdateIsSalable $subject,
$result, IndexName $indexName,
$result,
IndexName $indexName,
array $dataForUpdate,
string $connectionName
): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@

namespace Magento\CatalogInventoryDataExporter\Model\Provider\Product;

use Magento\CatalogInventoryDataExporter\Model\Query\CatalogInventoryStockQueryInterface;
use Magento\CatalogInventoryDataExporter\Model\Query\CatalogInventoryQuery;
use Magento\CatalogInventoryDataExporter\Model\Query\InventoryData;
use Magento\CatalogInventoryDataExporter\Model\Query\CatalogInventoryStockQuery;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Module\ModuleList;
use Magento\Framework\App\ObjectManager;

/**
* Provide inventory stock status data depending on current Inventory Management system
Expand All @@ -28,40 +31,61 @@ class InventoryDataProvider
/**
* Provide inventory data when MSI modules enabled
*
* @deprecated Not used anymore. Left for BC
* @see \Magento\CatalogInventoryDataExporter\Model\Query\CatalogInventoryStockQueryInterface
* @var InventoryData
*/
private InventoryData $inventoryData;

/**
* Provide inventory data when only Legacy Inventory modules enabled
*
* @deprecated Not used anymore. Left for BC
* @see \Magento\CatalogInventoryDataExporter\Model\Query\CatalogInventoryStockQueryInterface
* @var CatalogInventoryQuery
*/
private CatalogInventoryQuery $catalogInventoryQuery;

/**
* Provide inventory data
*
* @var CatalogInventoryStockQueryInterface
*/
private CatalogInventoryStockQueryInterface $catalogInventoryStockQuery;

/**
* @deprecated Not used anymore. Left for BC
* @see \Magento\CatalogInventoryDataExporter\Model\Query\CatalogInventoryStockQueryInterface
* @var ModuleList
*/
private ModuleList $moduleList;

/**
* @param ResourceConnection $resourceConnection
* @param InventoryData $inventoryData
* @param CatalogInventoryQuery $catalogInventoryQuery
* @param ModuleList $moduleList
* @param CatalogInventoryStockQueryInterface|null $catalogInventoryStockQuery
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __construct(
ResourceConnection $resourceConnection,
InventoryData $inventoryData,
CatalogInventoryQuery $catalogInventoryQuery,
ModuleList $moduleList
ModuleList $moduleList,
?CatalogInventoryStockQueryInterface $catalogInventoryStockQuery
) {
$this->resourceConnection = $resourceConnection;
$this->inventoryData = $inventoryData;
$this->catalogInventoryQuery = $catalogInventoryQuery;
$this->moduleList = $moduleList;
$this->catalogInventoryStockQuery = $catalogInventoryStockQuery
?? ObjectManager::getInstance()->get(CatalogInventoryStockQuery::class);
}

/**
* @param array $feedItems
* @return array
* @throws \Zend_Db_Select_Exception
* @throws \Zend_Db_Statement_Exception
*/
public function get(array $feedItems): array
{
Expand All @@ -78,11 +102,7 @@ public function get(array $feedItems): array
}

$connection = $this->resourceConnection->getConnection();
if ($this->isMSIEnabled()) {
$select = $this->inventoryData->get($queryArguments);
} else {
$select = $this->catalogInventoryQuery->getInStock($queryArguments);
}
$select = $this->catalogInventoryStockQuery->getInStock($queryArguments);
if (!$select) {
return $output;
}
Expand All @@ -95,14 +115,6 @@ public function get(array $feedItems): array
return $output;
}

/**
* @return bool
*/
private function isMSIEnabled(): bool
{
return $this->moduleList->getOne('Magento_InventoryIndexer') !== null;
}

/**
* Format output
*
Expand All @@ -119,10 +131,10 @@ private function format(array $row) : array
}

/**
* @param $item
* @param array $item
* @return string
*/
private function getKey($item): string
private function getKey(array $item): string
{
return $item['productId'] . '-' . $item['storeViewCode'];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* Class CatalogInventoryQuery
*
* Gets information about product inventory
* @deprecated use CatalogInventoryStockQueryInterface to get stock status
* @see \Magento\CatalogInventoryDataExporter\Model\Query\CatalogInventoryStockQueryInterface
*/
class CatalogInventoryQuery
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogInventoryDataExporter\Model\Query;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Select;

/**
* Class CatalogInventoryQuery
*
* Gets information about product inventory
*/
class CatalogInventoryStockQuery implements CatalogInventoryStockQueryInterface
{
/**
* @var ResourceConnection
*/
private $resourceConnection;

/**
* @param ResourceConnection $resourceConnection
*/
public function __construct(
ResourceConnection $resourceConnection
) {
$this->resourceConnection = $resourceConnection;
}

/**
* Get table name
*
* @param string $tableName
* @return string
*/
private function getTable(string $tableName) : string
{
return $this->resourceConnection->getTableName($tableName);
}

/**
* Get query with information about in_stock status
*
* @param array $arguments
* @return Select
*/
public function getInStock(array $arguments) : Select
{
$productIds = isset($arguments['productId']) ? $arguments['productId'] : [];
$storeViewCodes = isset($arguments['storeViewCode']) ? $arguments['storeViewCode'] : [];
$connection = $this->resourceConnection->getConnection();
$select = $connection->select()
->from(['cpe' => $this->getTable('catalog_product_entity')], '')
->joinCross(
['s' => $this->getTable('store')],
''
)
->joinInner(
['csi' => $this->getTable('cataloginventory_stock_status')],
"cpe.entity_id = csi.product_id",
''
)
->columns(
[
'productId' => 'csi.product_id',
'storeViewCode' => 's.code',
'qty' => 'csi.qty',
'is_in_stock' => 'csi.stock_status'
]
)
->where('s.code IN (?)', $storeViewCodes)
->where('csi.product_id IN (?)', $productIds);
return $select;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogInventoryDataExporter\Model\Query;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Select;

/**
* Gets information about product inventory
*/
interface CatalogInventoryStockQueryInterface
{
/**
* Get query with information about in_stock status
*
* @param array $arguments
* @return Select|null
*/
public function getInStock(array $arguments) : ?Select;
}
12 changes: 6 additions & 6 deletions CatalogInventoryDataExporter/Model/Query/InventoryData.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
namespace Magento\CatalogInventoryDataExporter\Model\Query;

use Magento\DataExporter\Model\Logging\CommerceDataExportLoggerInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Select;
use Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface;
use Magento\CatalogInventory\Model\Stock;

/**
* @deprecated use CatalogInventoryStockQueryInterface to get stock status
* @see CatalogInventoryStockQueryInterface
*/
class InventoryData
{
private ResourceConnection $resourceConnection;
Expand All @@ -26,13 +29,10 @@ class InventoryData
public function __construct(
ResourceConnection $resourceConnection,
CommerceDataExportLoggerInterface $logger,
DefaultStockProviderInterface $defaultStockProvider = null
) {
$this->resourceConnection = $resourceConnection;
$this->logger = $logger;
$defaultStockProvider = $defaultStockProvider ??
ObjectManager::getInstance()->get(DefaultStockProviderInterface::class);
$this->defaultStockId = $defaultStockProvider->getId();
$this->defaultStockId = Stock::DEFAULT_STOCK_ID;
}

/**
Expand Down
1 change: 1 addition & 0 deletions CatalogInventoryDataExporter/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<argument name="mainTable" xsi:type="string">catalog_product_entity</argument>
</arguments>
</type>
<preference for="Magento\CatalogInventoryDataExporter\Model\Query\CatalogInventoryStockQueryInterface" type="Magento\CatalogInventoryDataExporter\Model\Query\CatalogInventoryStockQuery" />
<type name="Magento\CatalogDataExporter\Model\Provider\Product\Buyable">
<plugin name="modified-buyable-value" type="Magento\CatalogInventoryDataExporter\Model\Plugin\Buyable"/>
</type>
Expand Down
18 changes: 13 additions & 5 deletions InventoryDataExporter/Model/Provider/StockStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ public function __construct(
*/
public function get(array $values): array
{
$skus = \array_column($values, 'sku');
// For stock statuses we are operating with product ids
$ids = \array_column($values, 'sku');
$connection = $this->resourceConnection->getConnection();
$output = [];

try {
$select = $this->query->getQuery($skus);
$select = $this->query->getQuery($ids);
// $select can be null if no stocks exists except default
if ($select) {
$cursor = $connection->query($select);
Expand All @@ -89,7 +90,7 @@ public function get(array $values): array
}
}

$select = $this->query->getQueryForDefaultStock($skus);
$select = $this->query->getQueryForDefaultStock($ids);
$cursor = $connection->query($select);
while ($row = $cursor->fetch()) {
$output[] = $this->fillWithDefaultValues($row);
Expand All @@ -112,8 +113,15 @@ public function get(array $values): array
*/
private function fillWithDefaultValues(array $row): array
{
if (!isset($row['qty'], $row['isSalable'], $row['sku'], $row['stockId'], $row['manageStock'],
$row['useConfigManageStock'], $row['backorders'], $row['useConfigBackorders'])) {
if (!isset($row['qty'],
$row['isSalable'],
$row['sku'],
$row['stockId'],
$row['manageStock'],
$row['useConfigManageStock'],
$row['backorders'],
$row['useConfigBackorders'])
) {
throw new \RuntimeException("missed required field: " . \var_export($row, true));
}
// set updated at
Expand Down
Loading

0 comments on commit bdb778e

Please sign in to comment.