-
Notifications
You must be signed in to change notification settings - Fork 247
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
MSI: Update old StockItem and Stock Status DB table at the time of Reservation appended #170
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
dece474
WIP MSI: #167
0192187
wip
12aacd1
Merge remote-tracking branch 'origin/develop' into msi-add-reservatio…
c143657
Improve plugin to update legacy tables
enriquei4 2d7f35d
Deleted Resolver id files unnecesary
enriquei4 b9dbba5
add integration test to check in the new sources the stock
enriquei4 7ba4e2a
Merge branch 'msi-add-reservation-to-stockItem-table' of github.com:m…
8e2e1bf
fix static tests
f2ca93a
Fixed integration test for checking old/new stock changes, improved d…
sinisa86 789c16b
Fixed conflicts because of changes in di.xml
sinisa86 4c0ca74
Fixed code sniffer failure because of empty line at the end of the file
sinisa86 74465a8
Merge branch 'msi-develop' into msi-add-reservation-to-stockItem-table
maghamed 2e6976c
Fixed broken builds for GitHub issue #167
maghamed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
app/code/Magento/InventoryCatalog/Plugin/Model/UpdateLegacyCatalogInventoryPlugin.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\InventoryCatalog\Plugin\Model; | ||
|
||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\InventoryApi\Api\Data\ReservationInterface; | ||
use Magento\InventoryApi\Api\ReservationsAppendInterface; | ||
use Magento\Catalog\Api\ProductRepositoryInterface; | ||
use Magento\CatalogInventory\Api\StockRegistryInterface; | ||
|
||
/** | ||
* Plugin help to fill the legacy catalog inventory tables cataloginventory_stock_status and | ||
* cataloginventory_stock_item to don't break the backward compatible. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
*/ | ||
class UpdateLegacyCatalogInventoryPlugin | ||
{ | ||
|
||
/** | ||
* @var ResourceConnection | ||
*/ | ||
private $resourceConnection; | ||
|
||
/** | ||
* @var ProductRepositoryInterface | ||
*/ | ||
private $productRepository; | ||
|
||
/** | ||
* @var \Magento\CatalogInventory\Api\StockRegistryInterface | ||
*/ | ||
private $stockRegistry; | ||
|
||
/** | ||
* @param ResourceConnection $resourceConnection | ||
* @param ProductRepositoryInterface $productRepository | ||
* @param StockRegistryInterface $stockRegistry | ||
*/ | ||
public function __construct( | ||
ResourceConnection $resourceConnection, | ||
ProductRepositoryInterface $productRepository, | ||
StockRegistryInterface $stockRegistry | ||
) { | ||
$this->resourceConnection = $resourceConnection; | ||
$this->productRepository = $productRepository; | ||
$this->stockRegistry = $stockRegistry; | ||
} | ||
|
||
/** | ||
* Plugin method to fill the legacy tables. | ||
* | ||
* @param ReservationsAppendInterface $subject | ||
* @param void $result | ||
* @param ReservationInterface[] $reservations | ||
* | ||
* @see ReservationsAppendInterface::execute | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
* @return void | ||
*/ | ||
public function afterExecute(ReservationsAppendInterface $subject, $result, array $reservations) | ||
{ | ||
$this->updateStockItemAndStatusTable($reservations); | ||
return $result; | ||
} | ||
|
||
/** | ||
* Updates cataloginventory_stock_item and cataloginventory_stock_status qty with reservation information. | ||
* | ||
* @param ReservationInterface[] $reservations | ||
* @return void | ||
*/ | ||
private function updateStockItemAndStatusTable(array $reservations) | ||
{ | ||
foreach ($reservations as $reservation) { | ||
$sku = $reservation->getSku(); | ||
$stockItem = $this->stockRegistry->getStockItemBySku($sku); | ||
$stockItem->setQty($stockItem->getQty() + $reservation->getQuantity()); | ||
$this->stockRegistry->updateStockItemBySku($sku, $stockItem); | ||
|
||
$stockStatus = $this->stockRegistry->getStockStatus($stockItem->getProductId()); | ||
$stockStatus->setQty($stockStatus->getQty() + $reservation->getQuantity()); | ||
$stockStatus->save(); | ||
} | ||
} | ||
} |
152 changes: 152 additions & 0 deletions
152
...code/Magento/InventoryCatalog/Test/Integration/UpdateLegacyCatalogInventoryPluginTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\InventoryCatalog\Test\Integration; | ||
|
||
use Magento\Catalog\Api\ProductRepositoryInterface; | ||
use Magento\Catalog\Model\Product; | ||
use Magento\InventoryApi\Api\ReservationBuilderInterface; | ||
use Magento\InventoryApi\Api\ReservationsAppendInterface; | ||
use PHPUnit\Framework\TestCase; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
use Magento\CatalogInventory\Api\StockItemRepositoryInterface; | ||
use Magento\CatalogInventory\Api\StockItemCriteriaInterface; | ||
use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory; | ||
use Magento\CatalogInventory\Api\Data\StockItemCollectionInterface; | ||
use Magento\CatalogInventory\Api\Data\StockItemInterface; | ||
use Magento\InventoryApi\Api\GetProductQuantityInStockInterface; | ||
use Magento\Indexer\Model\Indexer; | ||
use Magento\Inventory\Indexer\SourceItem\SourceItemIndexer; | ||
|
||
class UpdateLegacyCatalogInventoryPluginTest extends TestCase | ||
{ | ||
/** | ||
* @var ReservationBuilderInterface | ||
*/ | ||
private $reservationBuilder; | ||
|
||
/** | ||
* @var ReservationsAppendInterface | ||
*/ | ||
private $reservationsAppend; | ||
|
||
/** | ||
* @var StockItemRepositoryInterface | ||
*/ | ||
private $oldStockItemRepository; | ||
|
||
/** | ||
* @var StockItemCriteriaInterfaceFactory | ||
*/ | ||
private $stockItemCriteriaFactory; | ||
|
||
/** | ||
* @var GetProductQuantityInStockInterface | ||
*/ | ||
private $getProductQtyInStock; | ||
|
||
/** | ||
* @var IndexerInterface | ||
*/ | ||
private $indexer; | ||
|
||
/** | ||
* @var ProductRepositoryInterface | ||
*/ | ||
private $productRepository; | ||
|
||
protected function setUp() | ||
{ | ||
$this->reservationBuilder = Bootstrap::getObjectManager()->get(ReservationBuilderInterface::class); | ||
$this->reservationsAppend = Bootstrap::getObjectManager()->get(ReservationsAppendInterface::class); | ||
$this->oldStockItemRepository = Bootstrap::getObjectManager()->get(StockItemRepositoryInterface::class); | ||
$this->stockItemCriteriaFactory = Bootstrap::getObjectManager()->get(StockItemCriteriaInterfaceFactory::class); | ||
|
||
$this->indexer = Bootstrap::getObjectManager()->get(Indexer::class); | ||
$this->indexer->load(SourceItemIndexer::INDEXER_ID); | ||
$this->getProductQtyInStock = Bootstrap::getObjectManager()->get(GetProductQuantityInStockInterface::class); | ||
$this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class); | ||
} | ||
|
||
/** | ||
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php | ||
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php | ||
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php | ||
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php | ||
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php | ||
*/ | ||
public function testUpdateStockItemTable() | ||
{ | ||
$reservationQuantity = -5; | ||
|
||
/** @var Product $product */ | ||
$product = $this->productRepository->get('SKU-1'); | ||
|
||
/** @var StockItemCriteriaInterface $criteria */ | ||
$criteria = $this->stockItemCriteriaFactory->create(); | ||
$criteria->setProductsFilter([$product->getId()]); | ||
|
||
/** @var StockItemCollectionInterface $collectionBeforeChange */ | ||
$collectionBeforeChange = $this->oldStockItemRepository->getList($criteria); | ||
/** @var StockItemInterface $oldStockItem */ | ||
$oldStockItem = current($collectionBeforeChange->getItems()); | ||
$initialQuantity = $oldStockItem->getQty(); | ||
|
||
$this->reservationsAppend->execute([ | ||
$this->reservationBuilder->setStockId(1)->setSku('SKU-1')->setQuantity($reservationQuantity)->build() | ||
]); | ||
|
||
/** @var StockItemCollectionInterface $collectionAfterChange */ | ||
$collectionAfterChange = $this->oldStockItemRepository->getList($criteria); | ||
$oldStockItem = current($collectionAfterChange->getItems()); | ||
$quantityAfterCheck = $oldStockItem->getQty(); | ||
|
||
$this->assertEquals($initialQuantity + $reservationQuantity, $quantityAfterCheck); | ||
} | ||
|
||
/** | ||
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php | ||
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php | ||
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php | ||
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php | ||
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php | ||
*/ | ||
public function testThatReservationPlacedUpdatesBothOldAndNewStocks() | ||
{ | ||
$this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class); | ||
$reservationQuantity = -5; | ||
|
||
$this->indexer->reindexAll(); | ||
$this->assertEquals(8.5, $this->getProductQtyInStock->execute('SKU-1', 10)); | ||
|
||
/** @var Product $product */ | ||
$product = $this->productRepository->get('SKU-1'); | ||
|
||
/** @var StockItemCriteriaInterface $criteria */ | ||
$criteria = $this->stockItemCriteriaFactory->create(); | ||
$criteria->setProductsFilter([$product->getId()]); | ||
|
||
/** @var StockItemCollectionInterface $collectionBeforeChange */ | ||
$collectionBeforeChange = $this->oldStockItemRepository->getList($criteria); | ||
|
||
/** @var StockItemInterface $oldStockItem */ | ||
$oldStockItem = current($collectionBeforeChange->getItems()); | ||
$initialQuantity = $oldStockItem->getQty(); | ||
$this->assertEquals(8.5, $initialQuantity); | ||
|
||
$this->reservationsAppend->execute([ | ||
$this->reservationBuilder->setStockId(10)->setSku('SKU-1')->setQuantity($reservationQuantity)->build() | ||
]); | ||
|
||
/** @var StockItemCollectionInterface $collectionAfterChange */ | ||
$collectionAfterChange = $this->oldStockItemRepository->getList($criteria); | ||
$oldStockItem = current($collectionAfterChange->getItems()); | ||
$quantityAfterCheck = $oldStockItem->getQty(); | ||
|
||
$this->assertEquals(8.5 - 5, $this->getProductQtyInStock->execute('SKU-1', 10)); | ||
$this->assertEquals($this->getProductQtyInStock->execute('SKU-1', 10), $quantityAfterCheck); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
helpS