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

Fixed issue for default stock #2

Open
wants to merge 12 commits into
base: 1.2-develop
Choose a base branch
from
Open
55 changes: 54 additions & 1 deletion InventoryCatalog/Model/BulkInventoryTransfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -59,6 +60,11 @@ class SetDataToLegacyCatalogInventory
*/
private $indexerProcessor;

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

/**
* @param SetDataToLegacyStockItem $setDataToLegacyStockItem
* @param StockItemCriteriaInterfaceFactory $legacyStockItemCriteriaFactory
Expand All @@ -67,6 +73,7 @@ class SetDataToLegacyCatalogInventory
* @param StockStateProviderInterface $stockStateProvider
* @param Processor $indexerProcessor
* @param SetDataToLegacyStockStatus $setDataToLegacyStockStatus
* @param AreProductsSalableInterface $areProductsSalable
*/
public function __construct(
SetDataToLegacyStockItem $setDataToLegacyStockItem,
Expand All @@ -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;
Expand All @@ -84,6 +92,7 @@ public function __construct(
$this->getProductIdsBySkus = $getProductIdsBySkus;
$this->stockStateProvider = $stockStateProvider;
$this->indexerProcessor = $indexerProcessor;
$this->areProductsSalable = $areProductsSalable;
}

/**
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down
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
Expand Up @@ -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 */
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 53 additions & 2 deletions InventoryIndexer/Indexer/SelectBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)],
Expand All @@ -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(
Expand All @@ -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;
}
Expand Down
Loading