Skip to content

Commit

Permalink
Merge pull request #269 from magento-engcom/delete_source_items_stock…
Browse files Browse the repository at this point in the history
…_items_synchronization

Synchronization between sourceItem and stockItem deletion for defaultSourceId
  • Loading branch information
Valeriy Nayda authored Dec 21, 2017
2 parents 0fe24a0 + ca6c78a commit dce1e25
Show file tree
Hide file tree
Showing 23 changed files with 1,073 additions and 253 deletions.
11 changes: 6 additions & 5 deletions app/code/Magento/InventoryApi/Test/_files/products.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\SourceItemRepositoryInterface;
use Magento\InventoryApi\Api\SourceItemsDeleteInterface;
use Magento\InventoryCatalog\Api\DefaultStockProviderInterface;
use Magento\InventoryCatalog\Api\DefaultSourceProviderInterface;
use Magento\TestFramework\Helper\Bootstrap;

$objectManager = Bootstrap::getObjectManager();
Expand All @@ -39,7 +39,7 @@
'qty' => 0,
'is_in_stock' => false,
'manage_stock' => true
]
],
];

for ($i = 1; $i <= 3; $i++) {
Expand All @@ -60,16 +60,17 @@
if ($moduleManager->isEnabled('Magento_InventoryCatalog')) {
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
$searchCriteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class);
/** @var DefaultStockProviderInterface $defaultStockProvider */
$defaultStockProvider = $objectManager->get(DefaultStockProviderInterface::class);
/** @var DefaultSourceProviderInterface $defaultSourceProvider */
$defaultSourceProvider = $objectManager->get(DefaultSourceProviderInterface::class);
/** @var SourceItemRepositoryInterface $sourceItemRepository */
$sourceItemRepository = $objectManager->get(SourceItemRepositoryInterface::class);
/** @var SourceItemsDeleteInterface $sourceItemsDelete */
$sourceItemsDelete = $objectManager->get(SourceItemsDeleteInterface::class);

// Unassign created product from default Source
$searchCriteria = $searchCriteriaBuilder
->addFilter(SourceItemInterface::SKU, ['SKU-1', 'SKU-2', 'SKU-3'], 'in')
->addFilter(SourceItemInterface::SOURCE_ID, $defaultStockProvider->getId())
->addFilter(SourceItemInterface::SOURCE_ID, $defaultSourceProvider->getId())
->create();
$sourceItems = $sourceItemRepository->getList($searchCriteria)->getItems();
if (count($sourceItems)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@
$registry->register('isSecureArea', true);

foreach ($products as $product) {
/** @var \Magento\CatalogInventory\Api\StockStatusCriteriaInterfaceFactory $stockStatusCriteriaFactory */
$criteria = $stockStatusCriteriaFactory->create();
$criteria->setProductsFilter($product->getId());

$result = $stockStatusRepository->getList($criteria);
$stockStatus = current($result->getItems());
$stockStatusRepository->delete($stockStatus);
if ($result->getTotalCount()) {
$stockStatus = current($result->getItems());
$stockStatusRepository->delete($stockStatus);
}

$productRepository->delete($product);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,40 @@
*/
declare(strict_types=1);

namespace Magento\InventoryCatalog\Model;
namespace Magento\InventoryCatalog\Model\ResourceModel;

use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\InventoryCatalog\Api\DefaultSourceProviderInterface;
use Magento\InventoryCatalog\Model\GetProductIdsBySkusInterface;

/**
* Update Legacy catalocinventory_stock_item database data
* Apply data to legacy catalocinventory_stock_item table via plain MySql query
*/
class UpdateLegacyStockItemByPlainQuery
class ApplyDataToLegacyStockItem
{
/**
* @var ResourceConnection
*/
private $resourceConnection;

/**
* @var DefaultSourceProviderInterface
*/
private $defaultSourceProvider;

/**
* @var GetProductIdsBySkusInterface
*/
private $getProductIdsBySkus;

/**
* @param ResourceConnection $resourceConnection
* @param DefaultSourceProviderInterface $defaultSourceProvider
* @param GetProductIdsBySkusInterface $getProductIdsBySkus
*/
public function __construct(
ResourceConnection $resourceConnection,
DefaultSourceProviderInterface $defaultSourceProvider,
GetProductIdsBySkusInterface $getProductIdsBySkus
) {
$this->resourceConnection = $resourceConnection;
$this->defaultSourceProvider = $defaultSourceProvider;
$this->getProductIdsBySkus = $getProductIdsBySkus;
}

/**
* Execute Plain MySql query on catalaginventory_stock_item
*
* @param string $sku
* @param float $quantity
* @return void
Expand All @@ -64,7 +54,6 @@ public function execute(string $sku, float $quantity)
StockItemInterface::QTY => new \Zend_Db_Expr(sprintf('%s + %s', StockItemInterface::QTY, $quantity)),
],
[
StockItemInterface::STOCK_ID . ' = ?' => $this->defaultSourceProvider->getId(),
StockItemInterface::PRODUCT_ID . ' = ?' => $productId,
'website_id = ?' => 0,
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,40 @@
*/
declare(strict_types=1);

namespace Magento\InventoryCatalog\Model;
namespace Magento\InventoryCatalog\Model\ResourceModel;

use Magento\CatalogInventory\Api\Data\StockStatusInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\InventoryCatalog\Api\DefaultSourceProviderInterface;
use Magento\InventoryCatalog\Model\GetProductIdsBySkusInterface;

/**
* Update Legacy catalocinventory_stock_status database data
* Apply data to legacy cataloginventory_stock_status table via plain MySql query
*/
class UpdateLegacyStockStatusByPlainQuery
class ApplyDataToLegacyStockStatus
{
/**
* @var ResourceConnection
*/
private $resourceConnection;

/**
* @var DefaultSourceProviderInterface
*/
private $defaultSourceProvider;

/**
* @var GetProductIdsBySkusInterface
*/
private $getProductIdsBySkus;

/**
* @param ResourceConnection $resourceConnection
* @param DefaultSourceProviderInterface $defaultSourceProvider
* @param GetProductIdsBySkusInterface $getProductIdsBySkus
*/
public function __construct(
ResourceConnection $resourceConnection,
DefaultSourceProviderInterface $defaultSourceProvider,
GetProductIdsBySkusInterface $getProductIdsBySkus
) {
$this->resourceConnection = $resourceConnection;
$this->defaultSourceProvider = $defaultSourceProvider;
$this->getProductIdsBySkus = $getProductIdsBySkus;
}

/**
* Execute Plain MySql query on catalaginventory_stock_status
*
* @param string $sku
* @param float $quantity
* @return void
Expand All @@ -64,7 +54,6 @@ public function execute(string $sku, float $quantity)
StockStatusInterface::QTY => new \Zend_Db_Expr(sprintf('%s + %s', StockStatusInterface::QTY, $quantity))
],
[
StockStatusInterface::STOCK_ID . ' = ?' => $this->defaultSourceProvider->getId(),
StockStatusInterface::PRODUCT_ID . ' = ?' => $productId,
'website_id = ?' => 0,
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryCatalog\Model\ResourceModel;

use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\InventoryCatalog\Model\GetProductIdsBySkusInterface;

/**
* Set data to legacy catalocinventory_stock_item table via plain MySql query
*/
class SetDataToLegacyStockItem
{
/**
* @var ResourceConnection
*/
private $resourceConnection;

/**
* @var GetProductIdsBySkusInterface
*/
private $getProductIdsBySkus;

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

/**
* @param string $sku
* @param float $quantity
* @param int $status
* @return void
*/
public function execute(string $sku, float $quantity, int $status)
{
$productId = $this->getProductIdsBySkus->execute([$sku])[$sku];

$connection = $this->resourceConnection->getConnection();
$connection->update(
$this->resourceConnection->getTableName('cataloginventory_stock_item'),
[
StockItemInterface::IS_IN_STOCK => $status,
StockItemInterface::QTY => $quantity,
],
[
StockItemInterface::PRODUCT_ID . ' = ?' => $productId,
'website_id = ?' => 0,
]
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryCatalog\Model\ResourceModel;

use Magento\CatalogInventory\Api\Data\StockStatusInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\InventoryCatalog\Model\GetProductIdsBySkusInterface;

/**
* Set data to legacy cataloginventory_stock_status table via plain MySql query
*/
class SetDataToLegacyStockStatus
{
/**
* @var ResourceConnection
*/
private $resourceConnection;

/**
* @var GetProductIdsBySkusInterface
*/
private $getProductIdsBySkus;

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

/**
* @param string $sku
* @param float $quantity
* @param int $status
* @return void
*/
public function execute(string $sku, float $quantity, int $status)
{
$productId = $this->getProductIdsBySkus->execute([$sku])[$sku];

$connection = $this->resourceConnection->getConnection();
$connection->update(
$this->resourceConnection->getTableName('cataloginventory_stock_status'),
[
StockStatusInterface::STOCK_STATUS => $status,
StockStatusInterface::QTY => $quantity,
],
[
StockStatusInterface::PRODUCT_ID . ' = ?' => $productId,
'website_id = ?' => 0,
]
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* Class provides around Plugin on \Magento\CatalogInventory\Model\ResourceModel\Stock\Item::save
* to update data in Inventory source item based on legacy Stock Item data
*/
class UpdateSourceItemAtLegacyStockSettingPlugin
class UpdateSourceItemAtLegacyStockItemSavePlugin
{
/**
* @var SourceItemRepositoryInterface
Expand Down
Loading

0 comments on commit dce1e25

Please sign in to comment.