Skip to content

Commit

Permalink
re-commit PR from pull-request #269
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruslan committed Dec 14, 2017
1 parent 7fbffe6 commit d28054c
Show file tree
Hide file tree
Showing 13 changed files with 719 additions and 39 deletions.
32 changes: 8 additions & 24 deletions app/code/Magento/InventoryApi/Test/_files/products.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Magento\InventoryCatalog\Api\DefaultStockProviderInterface;
use Magento\TestFramework\Helper\Bootstrap;

const PRODUCT_COUNT = 4;

$objectManager = Bootstrap::getObjectManager();
/** @var ProductInterfaceFactory $productFactory */
$productFactory = $objectManager->get(ProductInterfaceFactory::class);
Expand All @@ -39,10 +41,15 @@
'qty' => 0,
'is_in_stock' => false,
'manage_stock' => true
],
'SKU-4' => [
'qty' => 10,
'is_in_stock' => true,
'manage_stock' => true
]
];

for ($i = 1; $i <= 3; $i++) {
for ($i = 1; $i <= PRODUCT_COUNT; $i++) {
$product = $productFactory->create();
$product->setTypeId(Type::TYPE_SIMPLE)
->setAttributeSetId(4)
Expand All @@ -53,26 +60,3 @@
->setStatus(Status::STATUS_ENABLED);
$productRepository->save($product);
}

/** @var Manager $moduleManager */
$moduleManager = Bootstrap::getObjectManager()->get(Manager::class);
// soft dependency in tests because we don't have possibility replace fixture from different modules
if ($moduleManager->isEnabled('Magento_InventoryCatalog')) {
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
$searchCriteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class);
/** @var DefaultStockProviderInterface $defaultStockProvider */
$defaultStockProvider = $objectManager->get(DefaultStockProviderInterface::class);
/** @var SourceItemRepositoryInterface $sourceItemRepository */
$sourceItemRepository = $objectManager->get(SourceItemRepositoryInterface::class);
/** @var SourceItemsDeleteInterface $sourceItemsDelete */
$sourceItemsDelete = $objectManager->get(SourceItemsDeleteInterface::class);

$searchCriteria = $searchCriteriaBuilder
->addFilter(SourceItemInterface::SKU, ['SKU-1', 'SKU-2', 'SKU-3'], 'in')
->addFilter(SourceItemInterface::SOURCE_ID, $defaultStockProvider->getId())
->create();
$sourceItems = $sourceItemRepository->getList($searchCriteria)->getItems();
if (count($sourceItems)) {
$sourceItemsDelete->execute($sourceItems);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
$searchCriteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class);
$searchCriteria = $searchCriteriaBuilder->addFilter(
ProductInterface::SKU,
['SKU-1', 'SKU-2', 'SKU-3'],
['SKU-1', 'SKU-2', 'SKU-3', 'SKU-4'],
'in'
)->create();
$products = $productRepository->getList($searchCriteria)->getItems();
Expand All @@ -47,8 +47,11 @@
$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
6 changes: 6 additions & 0 deletions app/code/Magento/InventoryApi/Test/_files/source_items.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@
SourceItemInterface::QUANTITY => 5,
SourceItemInterface::STATUS => SourceItemInterface::STATUS_IN_STOCK,
],
[
SourceItemInterface::SOURCE_ID => 1, // Default Source
SourceItemInterface::SKU => 'SKU-4',
SourceItemInterface::QUANTITY => 10,
SourceItemInterface::STATUS => SourceItemInterface::STATUS_IN_STOCK,
],
];

$sourceItems = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
declare(strict_types=1);

use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\SourceItemRepositoryInterface;
use Magento\InventoryApi\Api\SourceItemsDeleteInterface;
Expand All @@ -20,7 +21,7 @@

$searchCriteria = $searchCriteriaBuilder->addFilter(
SourceItemInterface::SKU,
['SKU-1', 'SKU-2', 'SKU-3'],
['SKU-1', 'SKU-2', 'SKU-3', 'SKU-4'],
'in'
)->create();
$sourceItems = $sourceItemRepository->getList($searchCriteria)->getItems();
Expand All @@ -30,5 +31,10 @@
* In that case there is "if" which checks that SKU1, SKU2 and SKU3 still exists in database.
*/
if (!empty($sourceItems)) {
$sourceItemsDelete->execute($sourceItems);
try{
$sourceItemsDelete->execute($sourceItems);
} catch (NoSuchEntityException $e) {

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryCatalog\Plugin\CatalogInventory;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\CatalogInventory\Model\Stock\StockItemRepository;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\InputException;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\SourceItemRepositoryInterface;
use Magento\InventoryApi\Api\SourceItemsDeleteInterface;
use Magento\InventoryCatalog\Api\DefaultSourceProviderInterface;

/**
* Class provides around Plugin on Magento\CatalogInventory\Model\ResourceModel\Stock\Item::delete
* to update data in Inventory source item
*/
class DeleteSourceItemsAtLegacyStockItemDelete
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;

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

/**
* @var SourceItemRepositoryInterface
*/
private $sourceItemRepository;

/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;

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

/**
* @param ProductRepositoryInterface $productRepository
* @param ResourceConnection $resourceConnection
* @param SourceItemRepositoryInterface $sourceItemRepository
* @param SourceItemsDeleteInterface $sourceItemsDelete
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param DefaultSourceProviderInterface $defaultSourceProvider
*/
public function __construct(
ProductRepositoryInterface $productRepository,
ResourceConnection $resourceConnection,
SourceItemRepositoryInterface $sourceItemRepository,
SourceItemsDeleteInterface $sourceItemsDelete,
SearchCriteriaBuilder $searchCriteriaBuilder,
DefaultSourceProviderInterface $defaultSourceProvider
) {
$this->productRepository = $productRepository;
$this->resourceConnection = $resourceConnection;
$this->sourceItemRepository = $sourceItemRepository;
$this->sourceItemsDelete = $sourceItemsDelete;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->defaultSourceProvider = $defaultSourceProvider;
}

/**
* @param StockItemRepository $subject
* @param callable $proceed
* @param StockItemInterface $stockItem
*
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function aroundDelete(StockItemRepository $subject, callable $proceed, StockItemInterface $stockItem)
{
$connection = $this->resourceConnection->getConnection();
$connection->beginTransaction();

try {
$proceed($stockItem);

$product = $this->productRepository->getById($stockItem->getProductId());
$searchCriteria = $this->searchCriteriaBuilder
->addFilter(SourceItemInterface::SKU, $product->getSku())
->addFilter(SourceItemInterface::SOURCE_ID, $this->defaultSourceProvider->getId())
->create();
$sourceItems = $this->sourceItemRepository->getList($searchCriteria)->getItems();

$this->sourceItemsDelete->execute($sourceItems);
$connection->commit();
} catch (CouldNotSaveException $e) {
$connection->rollBack();
} catch (InputException $e) {
$connection->rollBack();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryCatalog\Plugin\InventoryApi;

use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\SourceItemsDeleteInterface;
use Magento\InventoryCatalog\Api\DefaultSourceProviderInterface;
use Magento\InventoryCatalog\Model\DeleteLegacyStockItemByDefaultSourceItem;
use Magento\InventoryCatalog\Model\DeleteLegacyStockStatusByDefaultSourceItem;

/**
* Plugin help to delete related entries from the legacy catalog inventory tables cataloginventory_stock_status and
* cataloginventory_stock_item if deleted source item is default source item.
*/
class DeleteLegacyCatalogInventoryPlugin
{
/**
* @var DefaultSourceProviderInterface
*/
private $defaultSourceProvider;

/**
* @var DeleteLegacyStockItemByDefaultSourceItem
*/
private $deleteStockItemBySourceItem;

/**
* @var DeleteLegacyStockStatusByDefaultSourceItem
*/
private $deleteStockStatusBySourceItem;

/**
* @param DefaultSourceProviderInterface $defaultSourceProvider
* @param DeleteLegacyStockItemByDefaultSourceItem $deleteStockItemBySourceItem
* @param DeleteLegacyStockStatusByDefaultSourceItem $deleteStockStatusBySourceItem
*/
public function __construct(
DefaultSourceProviderInterface $defaultSourceProvider,
DeleteLegacyStockItemByDefaultSourceItem $deleteStockItemBySourceItem,
DeleteLegacyStockStatusByDefaultSourceItem $deleteStockStatusBySourceItem
) {
$this->defaultSourceProvider = $defaultSourceProvider;
$this->deleteStockItemBySourceItem = $deleteStockItemBySourceItem;
$this->deleteStockStatusBySourceItem = $deleteStockStatusBySourceItem;
}

/**
* Plugin method to delete entry from the legacy tables.
*
* @param SourceItemsDeleteInterface $subject
* @param void $result
* @param SourceItemInterface[] $sourceItems
*
* @see SourceItemsDeleteInterface::execute
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @return void
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function afterExecute(SourceItemsDeleteInterface $subject, $result, array $sourceItems)
{
$defaultSourceId = $this->defaultSourceProvider->getId();

foreach ($sourceItems as $sourceItem) {
if ($sourceItem->getSourceId() == $defaultSourceId) {
$this->deleteStockItemBySourceItem->execute($sourceItem);
$this->deleteStockStatusBySourceItem->execute($sourceItem);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\InventoryCatalog\Plugin\InventoryApi;

use Magento\CatalogInventory\Api\StockConfigurationInterface;
use Magento\InventoryApi\Api\Data\ReservationInterface;
use Magento\InventoryApi\Api\ReservationsAppendInterface;
use Magento\InventoryCatalog\Model\UpdateLegacyStockItemByPlainQuery;
Expand All @@ -28,16 +29,24 @@ class UpdateLegacyCatalogInventoryAtStockDeductionPlugin
*/
private $updateLegacyStockStatus;

/**
* @var StockConfigurationInterface
*/
private $stockConfiguration;

/**
* @param UpdateLegacyStockItemByPlainQuery $updateLegacyStockItem
* @param UpdateLegacyStockStatusByPlainQuery $updateLegacyStockStatus
* @param StockConfigurationInterface $stockConfiguration
*/
public function __construct(
UpdateLegacyStockItemByPlainQuery $updateLegacyStockItem,
UpdateLegacyStockStatusByPlainQuery $updateLegacyStockStatus
UpdateLegacyStockStatusByPlainQuery $updateLegacyStockStatus,
StockConfigurationInterface $stockConfiguration
) {
$this->updateLegacyStockItem = $updateLegacyStockItem;
$this->updateLegacyStockStatus = $updateLegacyStockStatus;
$this->stockConfiguration = $stockConfiguration;
}

/**
Expand All @@ -52,9 +61,11 @@ public function __construct(
*/
public function afterExecute(ReservationsAppendInterface $subject, $result, array $reservations)
{
foreach ($reservations as $reservation) {
$this->updateLegacyStockItem->execute($reservation->getSku(), (float)$reservation->getQuantity());
$this->updateLegacyStockStatus->execute($reservation->getSku(), (float)$reservation->getQuantity());
if ($this->stockConfiguration->canSubtractQty()) {
foreach ($reservations as $reservation) {
$this->updateLegacyStockItem->execute($reservation->getSku(), (float)$reservation->getQuantity());
$this->updateLegacyStockStatus->execute($reservation->getSku(), (float)$reservation->getQuantity());
}
}
}
}
}
Loading

0 comments on commit d28054c

Please sign in to comment.