Skip to content

Fixed issue for default stock #2

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

Open
wants to merge 12 commits into
base: 1.2-develop
Choose a base branch
from
Open
Changes from all commits
Commits
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
55 changes: 54 additions & 1 deletion InventoryCatalog/Model/BulkInventoryTransfer.php
Original file line number Diff line number Diff line change
@@ -10,6 +10,8 @@
use Magento\CatalogInventory\Model\Indexer\Stock as LegacyIndexer;
use Magento\Framework\Validation\ValidationException;
use Magento\InventoryCatalog\Model\ResourceModel\BulkInventoryTransfer as BulkInventoryTransferResource;
use Magento\InventoryCatalog\Model\ResourceModel\SetDataToLegacyStockItem;
use Magento\InventoryCatalog\Model\ResourceModel\SetDataToLegacyStockStatus;
use Magento\InventoryCatalogApi\Api\BulkInventoryTransferInterface;
use Magento\InventoryCatalogApi\Api\DefaultSourceProviderInterface;
use Magento\InventoryCatalogApi\Model\BulkInventoryTransferValidatorInterface;
@@ -51,13 +53,31 @@ class BulkInventoryTransfer implements BulkInventoryTransferInterface
*/
private $sourceIndexer;

/**
* @var GetSourceItemsBySkuAndSourceCodes
*/
private $getSourceItemsBySkuAndSourceCodes;

/**
* @var SetDataToLegacyStockStatus
*/
private $setDataToLegacyStockStatus;

/**
* @var SetDataToLegacyStockItem
*/
private $setDataToLegacyStockItem;

/**
* @param BulkInventoryTransferValidatorInterface $inventoryTransferValidator
* @param BulkInventoryTransferResource $bulkInventoryTransfer
* @param SourceIndexer $sourceIndexer
* @param DefaultSourceProviderInterface $defaultSourceProvider
* @param GetProductIdsBySkusInterface $getProductIdsBySkus
* @param LegacyIndexer $legacyIndexer
* @param GetSourceItemsBySkuAndSourceCodes $getSourceItemsBySkuAndSourceCodes
* @param SetDataToLegacyStockStatus $setDataToLegacyStockStatus
* @param SetDataToLegacyStockItem $setDataToLegacyStockItem
* @SuppressWarnings(PHPMD.LongVariable)
*/
public function __construct(
@@ -66,14 +86,20 @@ public function __construct(
SourceIndexer $sourceIndexer,
DefaultSourceProviderInterface $defaultSourceProvider,
GetProductIdsBySkusInterface $getProductIdsBySkus,
LegacyIndexer $legacyIndexer
LegacyIndexer $legacyIndexer,
GetSourceItemsBySkuAndSourceCodes $getSourceItemsBySkuAndSourceCodes,
SetDataToLegacyStockStatus $setDataToLegacyStockStatus,
SetDataToLegacyStockItem $setDataToLegacyStockItem
) {
$this->bulkInventoryTransferValidator = $inventoryTransferValidator;
$this->bulkInventoryTransfer = $bulkInventoryTransfer;
$this->getProductIdsBySkus = $getProductIdsBySkus;
$this->legacyIndexer = $legacyIndexer;
$this->defaultSourceProvider = $defaultSourceProvider;
$this->sourceIndexer = $sourceIndexer;
$this->getSourceItemsBySkuAndSourceCodes = $getSourceItemsBySkuAndSourceCodes;
$this->setDataToLegacyStockStatus = $setDataToLegacyStockStatus;
$this->setDataToLegacyStockItem = $setDataToLegacyStockItem;
}

/**
@@ -111,13 +137,40 @@ public function execute(

if (($this->defaultSourceProvider->getCode() === $originSource) ||
($this->defaultSourceProvider->getCode() === $destinationSource)) {
$this->updateStockStatusForLegacyStock($skus);
$productIds = array_values($this->getProductIdsBySkus->execute($skus));
$this->reindexLegacy($productIds);
}

return true;
}

/**
* @param array $skus
* @return void
*/
private function updateStockStatusForLegacyStock(array $skus): void
{
foreach ($skus as $sku) {
$sourceItems = $this->getSourceItemsBySkuAndSourceCodes->execute(
$sku,
[$this->defaultSourceProvider->getCode()]
);
foreach ($sourceItems as $sourceItem) {
$this->setDataToLegacyStockItem->execute(
(string)$sourceItem->getSku(),
(float)$sourceItem->getQuantity(),
(int)$sourceItem->getStatus()
);
$this->setDataToLegacyStockStatus->execute(
(string)$sourceItem->getSku(),
(float)$sourceItem->getQuantity(),
(int)$sourceItem->getStatus()
);
}
}
}

/**
* Reindex legacy stock (for default source).
*
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@
use Magento\InventoryCatalog\Model\ResourceModel\SetDataToLegacyStockItem;
use Magento\InventoryCatalog\Model\ResourceModel\SetDataToLegacyStockStatus;
use Magento\InventoryCatalogApi\Model\SourceItemsSaveSynchronizationInterface;
use Magento\InventorySalesApi\Api\AreProductsSalableInterface;

/**
* Set Qty and status for legacy CatalogInventory Stock Information tables.
@@ -59,6 +60,11 @@ class SetDataToLegacyCatalogInventory
*/
private $indexerProcessor;

/**
* @var AreProductsSalableInterface
*/
private $areProductsSalable;

/**
* @param SetDataToLegacyStockItem $setDataToLegacyStockItem
* @param StockItemCriteriaInterfaceFactory $legacyStockItemCriteriaFactory
@@ -67,6 +73,7 @@ class SetDataToLegacyCatalogInventory
* @param StockStateProviderInterface $stockStateProvider
* @param Processor $indexerProcessor
* @param SetDataToLegacyStockStatus $setDataToLegacyStockStatus
* @param AreProductsSalableInterface $areProductsSalable
*/
public function __construct(
SetDataToLegacyStockItem $setDataToLegacyStockItem,
@@ -75,7 +82,8 @@ public function __construct(
GetProductIdsBySkusInterface $getProductIdsBySkus,
StockStateProviderInterface $stockStateProvider,
Processor $indexerProcessor,
SetDataToLegacyStockStatus $setDataToLegacyStockStatus
SetDataToLegacyStockStatus $setDataToLegacyStockStatus,
AreProductsSalableInterface $areProductsSalable
) {
$this->setDataToLegacyStockItem = $setDataToLegacyStockItem;
$this->setDataToLegacyStockStatus = $setDataToLegacyStockStatus;
@@ -84,6 +92,7 @@ public function __construct(
$this->getProductIdsBySkus = $getProductIdsBySkus;
$this->stockStateProvider = $stockStateProvider;
$this->indexerProcessor = $indexerProcessor;
$this->areProductsSalable = $areProductsSalable;
}

/**
@@ -94,6 +103,16 @@ public function __construct(
*/
public function execute(array $sourceItems): void
{
$skus = [];
foreach ($sourceItems as $sourceItem) {
$skus[] = $sourceItem->getSku();
}

$stockStatuses = [];
foreach ($this->areProductsSalable->execute($skus, Stock::DEFAULT_STOCK_ID) as $productSalable) {
$stockStatuses[$productSalable->getSku()] = $productSalable->isSalable();
}

$productIds = [];
foreach ($sourceItems as $sourceItem) {
$sku = $sourceItem->getSku();
@@ -129,7 +148,7 @@ public function execute(array $sourceItems): void
$this->setDataToLegacyStockStatus->execute(
(string)$sourceItem->getSku(),
(float)$sourceItem->getQuantity(),
$isInStock
$stockStatuses[(string)$sourceItem->getSku()] === true ? 1 : 0
);
$productIds[] = $productId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\InventoryCatalog\Plugin\CatalogInventory\Api\StockRegistry;

use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\InventoryCatalog\Model\ResourceModel\SetDataToLegacyStockStatus;

class AdaptUpdateStockStatusBySkuPlugin
{
/**
* @var SetDataToLegacyStockStatus
*/
private $setDataToLegacyStockStatus;

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

/**
* @param StockRegistryInterface $subject
* @param int $itemId
* @param string $productSku
* @param StockItemInterface $stockItem
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterUpdateStockItemBySku(
StockRegistryInterface $subject,
int $itemId,
string $productSku,
StockItemInterface $stockItem
): void {
$this->setDataToLegacyStockStatus->execute(
$productSku,
(float)$stockItem->getQty(),
$stockItem->getIsInStock()
);
}
}
22 changes: 22 additions & 0 deletions InventoryCatalog/Test/_files/source_items_on_default_source.php
Original file line number Diff line number Diff line change
@@ -5,11 +5,14 @@
*/
declare(strict_types=1);

use Magento\CatalogInventory\Model\Stock;
use Magento\Framework\Api\DataObjectHelper;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory;
use Magento\InventoryApi\Api\SourceItemsSaveInterface;
use Magento\InventoryCatalog\Model\ResourceModel\SetDataToLegacyStockStatus;
use Magento\InventoryCatalogApi\Api\DefaultSourceProviderInterface;
use Magento\InventorySalesApi\Api\AreProductsSalableInterface;
use Magento\TestFramework\Helper\Bootstrap;

/** @var DataObjectHelper $dataObjectHelper */
@@ -20,6 +23,10 @@
$sourceItemsSave = Bootstrap::getObjectManager()->get(SourceItemsSaveInterface::class);
/** @var DefaultSourceProviderInterface $defaultSourceProvider */
$defaultSourceProvider = Bootstrap::getObjectManager()->get(DefaultSourceProviderInterface::class);
/** @var AreProductsSalableInterface $areProductsSalable */
$areProductsSalable = Bootstrap::getObjectManager()->get(AreProductsSalableInterface::class);
/** @var SetDataToLegacyStockStatus $setDataToLegacyStockStatus */
$setDataToLegacyStockStatus = Bootstrap::getObjectManager()->get(SetDataToLegacyStockStatus::class);

/**
* SKU-1 - Default-source-1(id:10) - 5.5qty
@@ -60,10 +67,25 @@
];

$sourceItems = [];
$skus = [];
foreach ($sourcesItemsData as $sourceItemData) {
/** @var SourceItemInterface $source */
$sourceItem = $sourceItemFactory->create();
$dataObjectHelper->populateWithArray($sourceItem, $sourceItemData, SourceItemInterface::class);
$sourceItems[] = $sourceItem;
$skus[] = $sourceItemData[SourceItemInterface::SKU];
}
$sourceItemsSave->execute($sourceItems);

$stockStatuses = [];
foreach ($areProductsSalable->execute($skus, Stock::DEFAULT_STOCK_ID) as $productSalable) {
$stockStatuses[$productSalable->getSku()] = $productSalable->isSalable();
}

foreach ($sourceItems as $sourceItem) {
$setDataToLegacyStockStatus->execute(
(string)$sourceItem->getSku(),
(float)$sourceItem->getQuantity(),
$stockStatuses[(string)$sourceItem->getSku()] === true ? 1 : 0
);
}
1 change: 1 addition & 0 deletions InventoryCatalog/etc/di.xml
Original file line number Diff line number Diff line change
@@ -100,6 +100,7 @@
<plugin name="adapt_get_stock_status_by_sku" type="Magento\InventoryCatalog\Plugin\CatalogInventory\Api\StockRegistry\AdaptGetStockStatusBySkuPlugin"/>
<plugin name="adapt_get_product_stock_status" type="Magento\InventoryCatalog\Plugin\CatalogInventory\Api\StockRegistry\AdaptGetProductStockStatusPlugin"/>
<plugin name="adapt_get_product_stock_status_by_sku" type="Magento\InventoryCatalog\Plugin\CatalogInventory\Api\StockRegistry\AdaptGetProductStockStatusBySkuPlugin"/>
<plugin name="adapt_update_product_stock_status_by_sku" type="Magento\InventoryCatalog\Plugin\CatalogInventory\Api\StockRegistry\AdaptUpdateStockStatusBySkuPlugin"/>
</type>
<!-- Mass Source Assignment -->
<preference for="Magento\InventoryCatalogApi\Api\BulkSourceAssignInterface"
Original file line number Diff line number Diff line change
@@ -104,7 +104,6 @@ public function testPlaceOrderWithOutOffStockProductAndBackOrdersTurnedOn(): voi

/**
* @magentoDataFixture Magento_InventoryGroupedProduct::Test/_files/default_stock_grouped_products.php
* @magentoDataFixture Magento_InventoryCatalog::Test/_files/source_items_on_default_source.php
* @magentoDataFixture Magento_InventorySalesApi::Test/_files/quote.php
* @magentoDataFixture Magento_InventoryIndexer::Test/_files/reindex_inventory.php
* @magentoConfigFixture current_store cataloginventory/item_options/manage_stock 0
55 changes: 53 additions & 2 deletions InventoryIndexer/Indexer/SelectBuilder.php
Original file line number Diff line number Diff line change
@@ -8,6 +8,8 @@
namespace Magento\InventoryIndexer\Indexer;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\DB\Select;
use Magento\Inventory\Model\ResourceModel\Source as SourceResourceModel;
use Magento\Inventory\Model\ResourceModel\SourceItem as SourceItemResourceModel;
@@ -66,10 +68,55 @@ public function execute(int $stockId): Select
$quantityExpression = (string)$this->resourceConnection->getConnection()->getCheckSql(
'source_item.' . SourceItemInterface::STATUS . ' = ' . SourceItemInterface::STATUS_OUT_OF_STOCK,
0,
SourceItemInterface::QUANTITY
'source_item.' . SourceItemInterface::QUANTITY
);
$sourceCodes = $this->getSourceCodes($stockId);

$reservationsTableName = 'reservations_temp_for_stock_' . $stockId;
$table = $connection->newTable($reservationsTableName);
$table->addColumn(
'sku',
Table::TYPE_TEXT,
64,
[
Table::OPTION_PRIMARY => true,
Table::OPTION_NULLABLE => false,
],
'Sku'
);
$table->addColumn(
'reservation_qty',
Table::TYPE_DECIMAL,
null,
[
Table::OPTION_UNSIGNED => false,
Table::OPTION_NULLABLE => false,
Table::OPTION_DEFAULT => 0,
Table::OPTION_PRECISION => 10,
Table::OPTION_SCALE => 4,
],
'Reservation Qty'
);
$table->addIndex(
'index_sku_qty',
['sku'],
['type' => AdapterInterface::INDEX_TYPE_INDEX]
);
$connection->createTemporaryTable($table);

$reservationsData = $connection->select();
$reservationsData->from(
['reservations' => $this->resourceConnection->getTableName('inventory_reservation')],
[
'sku',
'reservation_qty' => 'SUM(reservations.quantity)'
]
);
$reservationsData->group(['sku', 'stock_id']);

$insRes = $connection->insertFromSelect($reservationsData, $reservationsTableName);
$connection->query($insRes);

$select = $connection->select();
$select->joinLeft(
['product' => $this->resourceConnection->getTableName($this->productTableName)],
@@ -79,6 +126,10 @@ public function execute(int $stockId): Select
['legacy_stock_item' => $this->resourceConnection->getTableName('cataloginventory_stock_item')],
'product.entity_id = legacy_stock_item.product_id',
[]
)->joinLeft(
['reservations_qty' => $this->resourceConnection->getTableName($reservationsTableName)],
' source_item.' . SourceItemInterface::SKU . ' = reservations_qty.sku',
[]
);

$select->from(
@@ -90,7 +141,7 @@ public function execute(int $stockId): Select
]
)
->where('source_item.' . SourceItemInterface::SOURCE_CODE . ' IN (?)', $sourceCodes)
->group([SourceItemInterface::SKU]);
->group(['source_item.' .SourceItemInterface::SKU]);

return $select;
}
15 changes: 14 additions & 1 deletion InventoryIndexer/Indexer/SourceItem/Strategy/Sync.php
Original file line number Diff line number Diff line change
@@ -8,6 +8,8 @@
namespace Magento\InventoryIndexer\Indexer\SourceItem\Strategy;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Ddl\Table;
use Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface;
use Magento\InventoryIndexer\Indexer\InventoryIndexer;
use Magento\InventoryIndexer\Indexer\SourceItem\GetSkuListInStock;
@@ -58,6 +60,11 @@ class Sync
*/
private $defaultStockProvider;

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

/**
* $indexStructure is reserved name for construct variable (in index internal mechanism)
*
@@ -76,7 +83,8 @@ public function __construct(
IndexDataBySkuListProvider $indexDataBySkuListProvider,
IndexNameBuilder $indexNameBuilder,
StockIndexer $stockIndexer,
DefaultStockProviderInterface $defaultStockProvider
DefaultStockProviderInterface $defaultStockProvider,
ResourceConnection $resourceConnection
) {
$this->getSkuListInStock = $getSkuListInStockToUpdate;
$this->indexStructure = $indexStructureHandler;
@@ -85,6 +93,7 @@ public function __construct(
$this->indexNameBuilder = $indexNameBuilder;
$this->stockIndexer = $stockIndexer;
$this->defaultStockProvider = $defaultStockProvider;
$this->resourceConnection = $resourceConnection;
}

/**
@@ -126,6 +135,10 @@ public function executeList(array $sourceItemIds) : void
$indexData,
ResourceConnection::DEFAULT_CONNECTION
);

$reservationsTableName = 'reservations_temp_for_stock_' . $stockId;
$connection = $this->resourceConnection->getConnection();
$connection->dropTemporaryTable($reservationsTableName);
}
}

13 changes: 12 additions & 1 deletion InventoryIndexer/Indexer/Stock/Strategy/Sync.php
Original file line number Diff line number Diff line change
@@ -57,6 +57,11 @@ class Sync
*/
private $defaultStockProvider;

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

/**
* $indexStructure is reserved name for construct variable in index internal mechanism
*
@@ -75,7 +80,8 @@ public function __construct(
IndexNameBuilder $indexNameBuilder,
IndexDataProviderByStockId $indexDataProviderByStockId,
IndexTableSwitcherInterface $indexTableSwitcher,
DefaultStockProviderInterface $defaultStockProvider
DefaultStockProviderInterface $defaultStockProvider,
ResourceConnection $resourceConnection
) {
$this->getAllStockIds = $getAllStockIds;
$this->indexStructure = $indexStructureHandler;
@@ -84,6 +90,7 @@ public function __construct(
$this->indexDataProviderByStockId = $indexDataProviderByStockId;
$this->indexTableSwitcher = $indexTableSwitcher;
$this->defaultStockProvider = $defaultStockProvider;
$this->resourceConnection = $resourceConnection;
}

/**
@@ -147,6 +154,10 @@ public function executeList(array $stockIds): void
);
$this->indexTableSwitcher->switch($mainIndexName, ResourceConnection::DEFAULT_CONNECTION);
$this->indexStructure->delete($replicaIndexName, ResourceConnection::DEFAULT_CONNECTION);

$reservationsTableName = 'reservations_temp_for_stock_' . $stockId;
$connection = $this->resourceConnection->getConnection();
$connection->dropTemporaryTable($reservationsTableName);
}
}
}
Original file line number Diff line number Diff line change
@@ -21,6 +21,11 @@ class IsStockItemSalableConditionChain implements GetIsStockItemSalableCondition
*/
private $conditions = [];

/**
* @var GetIsStockItemSalableConditionInterface[]
*/
private $requiredConditions = [];

/**
* @var ResourceConnection
*/
@@ -29,12 +34,13 @@ class IsStockItemSalableConditionChain implements GetIsStockItemSalableCondition
/**
* @param ResourceConnection $resourceConnection
* @param array $conditions
*
* @param array $requiredConditions
* @throws LocalizedException
*/
public function __construct(
ResourceConnection $resourceConnection,
array $conditions = []
array $conditions = [],
array $requiredConditions = []
) {
foreach ($conditions as $getIsSalableCondition) {
if (!$getIsSalableCondition instanceof GetIsStockItemSalableConditionInterface) {
@@ -43,8 +49,16 @@ public function __construct(
);
}
}
foreach ($requiredConditions as $getIsSalableCondition) {
if (!$getIsSalableCondition instanceof GetIsStockItemSalableConditionInterface) {
throw new LocalizedException(
__('Condition must implement %1', GetIsStockItemSalableConditionInterface::class)
);
}
}
$this->resourceConnection = $resourceConnection;
$this->conditions = $conditions;
$this->requiredConditions = $requiredConditions;
}

/**
@@ -65,7 +79,20 @@ public function execute(Select $select): string
}
}

$isSalableString = '(' . implode(') OR (', $conditionStrings) . ')';
if (empty($this->requiredConditions)) {
$isSalableString = '(' . implode(') OR (', $conditionStrings) . ')';
} else {
$requiredConditionsStrings = [];
foreach ($this->requiredConditions as $requiredCondition) {
$requiredConditionString = $requiredCondition->execute($select);
if ('' !== trim($requiredConditionString)) {
$requiredConditionsStrings[] = $requiredConditionString;
}
}
$isSalableString = '(' . implode(') AND (', $requiredConditionsStrings) . ')'
. ' AND ((' . implode(') OR (', $conditionStrings) . '))';
}

return (string)$this->resourceConnection->getConnection()->getCheckSql($isSalableString, 1, 0);
}
}
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@ public function execute(Select $select): string
$quantityExpression = (string)$this->resourceConnection->getConnection()->getCheckSql(
'source_item.' . SourceItemInterface::STATUS . ' = ' . SourceItemInterface::STATUS_OUT_OF_STOCK,
0,
SourceItemInterface::QUANTITY
'source_item.' . SourceItemInterface::QUANTITY
);
$quantityExpression = 'SUM(' . $quantityExpression . ')';

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\InventorySales\Model\ResourceModel\IsStockItemSalableCondition;

use Magento\Framework\DB\Select;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\CatalogInventory\Api\StockConfigurationInterface;
use Magento\Framework\App\ResourceConnection;

class ReservationsCondition implements GetIsStockItemSalableConditionInterface
{
/**
* @var StockConfigurationInterface
*/
private $configuration;

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

/**
* @inheritdoc
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function execute(Select $select): string
{
$globalMinQty = $this->configuration->getMinQty();
return 'reservation_qty IS NULL OR (source_item.' . SourceItemInterface::QUANTITY . ' + reservation_qty) > ' . $globalMinQty;

}
}
3 changes: 3 additions & 0 deletions InventorySales/etc/di.xml
Original file line number Diff line number Diff line change
@@ -48,6 +48,9 @@
<item name="min_qty" xsi:type="object">Magento\InventorySales\Model\ResourceModel\IsStockItemSalableCondition\MinQtyStockCondition</item>
<item name="non_existing_legacy_sku" xsi:type="object">Magento\InventorySales\Model\ResourceModel\IsStockItemSalableCondition\SkuIsAbsentInCatalogCondition</item>
</argument>
<argument name="requiredConditions" xsi:type="array">
<item name="reservations" xsi:type="object">Magento\InventorySales\Model\ResourceModel\IsStockItemSalableCondition\ReservationsCondition</item>
</argument>
</arguments>
</type>
<!-- AreProductsSalable -->
30 changes: 30 additions & 0 deletions InventoryShipping/Test/_files/source_items_for_bundle_children.php
Original file line number Diff line number Diff line change
@@ -5,11 +5,15 @@
*/
declare(strict_types=1);

use Magento\CatalogInventory\Model\Stock;
use Magento\Framework\Api\DataObjectHelper;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory;
use Magento\InventoryApi\Api\SourceItemsSaveInterface;
use Magento\InventoryCatalog\Model\ResourceModel\SetDataToLegacyStockItem;
use Magento\InventoryCatalog\Model\ResourceModel\SetDataToLegacyStockStatus;
use Magento\InventoryCatalogApi\Api\DefaultSourceProviderInterface;
use Magento\InventorySalesApi\Api\AreProductsSalableInterface;
use Magento\TestFramework\Helper\Bootstrap;

/** @var DataObjectHelper $dataObjectHelper */
@@ -43,10 +47,36 @@
];

$sourceItems = [];
$skus = [];
foreach ($sourcesItemsData as $sourceItemData) {
/** @var SourceItemInterface $source */
$sourceItem = $sourceItemFactory->create();
$dataObjectHelper->populateWithArray($sourceItem, $sourceItemData, SourceItemInterface::class);
$sourceItems[] = $sourceItem;
$skus[] = $sourceItemData[SourceItemInterface::SKU];
}
$sourceItemsSave->execute($sourceItems);

/** @var AreProductsSalableInterface $areProductsSalable */
$areProductsSalable = Bootstrap::getObjectManager()->get(AreProductsSalableInterface::class);
/** @var SetDataToLegacyStockStatus $setDataToLegacyStockStatus */
$setDataToLegacyStockStatus = Bootstrap::getObjectManager()->get(SetDataToLegacyStockStatus::class);
/** @var SetDataToLegacyStockItem $setDataToLegacyStockItem */
$setDataToLegacyStockItem = Bootstrap::getObjectManager()->get(SetDataToLegacyStockItem::class);

$stockStatuses = [];
foreach ($areProductsSalable->execute($skus, Stock::DEFAULT_STOCK_ID) as $productSalable) {
$stockStatuses[$productSalable->getSku()] = $productSalable->isSalable();
}
foreach ($sourceItems as $sourceItem) {
$setDataToLegacyStockItem->execute(
(string)$sourceItem->getSku(),
(float)$sourceItem->getQuantity(),
(int)$sourceItem->getStatus()
);
$setDataToLegacyStockStatus->execute(
(string)$sourceItem->getSku(),
(float)$sourceItem->getQuantity(),
$stockStatuses[(string)$sourceItem->getSku()] === true ? 1 : 0
);
}
Original file line number Diff line number Diff line change
@@ -5,11 +5,15 @@
*/
declare(strict_types=1);

use Magento\CatalogInventory\Model\Stock;
use Magento\Framework\Api\DataObjectHelper;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory;
use Magento\InventoryApi\Api\SourceItemsSaveInterface;
use Magento\InventoryCatalog\Model\ResourceModel\SetDataToLegacyStockItem;
use Magento\InventoryCatalog\Model\ResourceModel\SetDataToLegacyStockStatus;
use Magento\InventoryCatalogApi\Api\DefaultSourceProviderInterface;
use Magento\InventorySalesApi\Api\AreProductsSalableInterface;
use Magento\TestFramework\Helper\Bootstrap;

/** @var DataObjectHelper $dataObjectHelper */
@@ -57,10 +61,36 @@
];

$sourceItems = [];
$skus = [];
foreach ($sourcesItemsData as $sourceItemData) {
/** @var SourceItemInterface $source */
$sourceItem = $sourceItemFactory->create();
$dataObjectHelper->populateWithArray($sourceItem, $sourceItemData, SourceItemInterface::class);
$sourceItems[] = $sourceItem;
$skus[] = $sourceItemData[SourceItemInterface::SKU];
}
$sourceItemsSave->execute($sourceItems);

/** @var AreProductsSalableInterface $areProductsSalable */
$areProductsSalable = Bootstrap::getObjectManager()->get(AreProductsSalableInterface::class);
/** @var SetDataToLegacyStockStatus $setDataToLegacyStockStatus */
$setDataToLegacyStockStatus = Bootstrap::getObjectManager()->get(SetDataToLegacyStockStatus::class);
/** @var SetDataToLegacyStockItem $setDataToLegacyStockItem */
$setDataToLegacyStockItem = Bootstrap::getObjectManager()->get(SetDataToLegacyStockItem::class);

$stockStatuses = [];
foreach ($areProductsSalable->execute($skus, Stock::DEFAULT_STOCK_ID) as $productSalable) {
$stockStatuses[$productSalable->getSku()] = $productSalable->isSalable();
}
foreach ($sourceItems as $sourceItem) {
$setDataToLegacyStockItem->execute(
(string)$sourceItem->getSku(),
(float)$sourceItem->getQuantity(),
(int)$sourceItem->getStatus()
);
$setDataToLegacyStockStatus->execute(
(string)$sourceItem->getSku(),
(float)$sourceItem->getQuantity(),
$stockStatuses[(string)$sourceItem->getSku()] === true ? 1 : 0
);
}