Skip to content

Inventory sales backorder functionality #156

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

Merged
merged 8 commits into from
Nov 7, 2017
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions app/code/Magento/InventoryApi/Test/_files/products.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
$productFactory = $objectManager->get(ProductInterfaceFactory::class);
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
$productRepository->cleanCache();

for ($i = 1; $i <= 3; $i++) {
$product = $productFactory->create();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventorySales\Plugin\InventoryApi;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory;
use Magento\CatalogInventory\Api\StockItemRepositoryInterface;
use Magento\InventoryApi\Api\IsProductInStockInterface;

/**
* Adapt backorders to IsProductInStockInterface
*/
class BackorderStockStatusPlugin
{
/**
* @var StockItemRepositoryInterface
*/
private $stockItemRepository;

/**
* @var StockItemCriteriaInterfaceFactory
*/
private $stockItemCriteriaFactory;

/**
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @param StockItemRepositoryInterface $stockItemRepository
* @param StockItemCriteriaInterfaceFactory $stockItemCriteriaFactory
* @param ProductRepositoryInterface $productRepository
*/
public function __construct(
StockItemRepositoryInterface $stockItemRepository,
StockItemCriteriaInterfaceFactory $stockItemCriteriaFactory,
ProductRepositoryInterface $productRepository
) {
$this->stockItemRepository = $stockItemRepository;
$this->stockItemCriteriaFactory = $stockItemCriteriaFactory;
$this->productRepository = $productRepository;
}

/**
* Return true status if backorders is enabled for the item
*
* @param IsProductInStockInterface $subject
* @param callable $proceed
* @param string $sku
* @param int $stockId
* @return bool
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundExecute(
IsProductInStockInterface $subject,
callable $proceed,
string $sku,
int $stockId
): bool {
$productData = $this->productRepository->get($sku);
$productId = $productData->getId();

$stockItemCriteria = $this->stockItemCriteriaFactory->create();
$stockItemCriteria->setProductsFilter($productId);
$stockItemsCollection = $this->stockItemRepository->getList($stockItemCriteria);

/** @var StockItemInterface $legacyStockItem */
$legacyStockItem = current($stockItemsCollection->getItems());

if ($legacyStockItem->getBackorders() > 0) {
return true;
}
return $proceed($sku, $stockId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\InventorySales\Test\Integration;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory;
use Magento\CatalogInventory\Api\StockItemRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Indexer\IndexerInterface;
use Magento\Inventory\Indexer\StockItemIndexerInterface;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\IsProductInStockInterface;
use Magento\InventoryApi\Api\SourceItemRepositoryInterface;
use Magento\InventoryApi\Api\SourceItemsSaveInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

class IsBackorderedProductInStockTest extends TestCase
{
/**
* @var SourceItemRepositoryInterface
*/
private $sourceItemRepository;

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

/**
* @var SourceItemsSaveInterface
*/
private $sourceItemsSave;

/**
* @var IndexerInterface
*/
private $indexer;

/**
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @var IsProductInStockInterface
*/
private $isProductInStock;

/**
* @var StockItemRepositoryInterface
*/
private $stockItemRepository;

/**
* @var StockItemCriteriaInterfaceFactory
*/
private $stockItemCriteriaFactory;

protected function setUp()
{
$this->productRepository = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class);
$this->stockItemRepository = Bootstrap::getObjectManager()->create(StockItemRepositoryInterface::class);
$this->stockItemCriteriaFactory = Bootstrap::getObjectManager()->create(
StockItemCriteriaInterfaceFactory::class
);
$this->sourceItemRepository = Bootstrap::getObjectManager()->create(SourceItemRepositoryInterface::class);
$this->searchCriteriaBuilder = Bootstrap::getObjectManager()->create(SearchCriteriaBuilder::class);
$this->sourceItemsSave = Bootstrap::getObjectManager()->create(SourceItemsSaveInterface::class);
$this->indexer = Bootstrap::getObjectManager()->create(IndexerInterface::class);
$this->indexer->load(StockItemIndexerInterface::INDEXER_ID);
$this->isProductInStock = Bootstrap::getObjectManager()->create(IsProductInStockInterface::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 testBackorderedZeroQtyProductIsInStock()
{
$product = $this->productRepository->get('SKU-2');
$stockItemSearchCriteria = $this->stockItemCriteriaFactory->create();
$stockItemSearchCriteria->setProductsFilter($product->getId());
$stockItemsCollection = $this->stockItemRepository->getList($stockItemSearchCriteria);

/** @var StockItemInterface $legacyStockItem */
$legacyStockItem = current($stockItemsCollection->getItems());
$legacyStockItem->setBackorders(1);
$legacyStockItem->setUseConfigBackorders(0);
$this->stockItemRepository->save($legacyStockItem);

$sourceItem = $this->getSourceItemBySku('SKU-2');
$this->changeSourceItemQty($sourceItem, -15);

$this->assertTrue($this->isProductInStock->execute('SKU-2', 1));
}

/**
* @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 testZeroQtyProductIsOutOfStock()
{
$sourceItem = $this->getSourceItemBySku('SKU-2');
$this->changeSourceItemQty($sourceItem, 0);

$this->assertFalse($this->isProductInStock->execute('SKU-2', 1));
}

/**
* @param string $sku
* @return SourceItemInterface
*/
private function getSourceItemBySku(string $sku): SourceItemInterface
{
$sourceItemSearchCriteria = $this->searchCriteriaBuilder
->addFilter('sku', $sku)
->create();
$sourceItemSearchResult = $this->sourceItemRepository->getList($sourceItemSearchCriteria);

return current($sourceItemSearchResult->getItems());
}

/**
* @param SourceItemInterface $sourceItem
* @param float $qty
*/
private function changeSourceItemQty(SourceItemInterface $sourceItem, float $qty)
{
$sourceItem->setQuantity($qty);
$this->sourceItemsSave->execute([$sourceItem]);
$this->indexer->reindexRow(5);
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/InventorySales/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
<type name="Magento\Inventory\Ui\DataProvider\StockDataProvider">
<plugin name="sales_channel_data" type="Magento\InventorySales\Plugin\Inventory\Ui\StockDataProvider\SalesChannels" />
</type>
<type name="Magento\InventoryApi\Api\IsProductInStockInterface">
<plugin name="backorder_inventory_stock_status" type="Magento\InventorySales\Plugin\InventoryApi\BackorderStockStatusPlugin"/>
</type>
</config>