Skip to content

Commit

Permalink
ACP2E-2227: Can't create credit memo of orders that contain deleted s…
Browse files Browse the repository at this point in the history
…imple products of a configurable
  • Loading branch information
viktym committed Aug 11, 2023
1 parent 8e0a981 commit 78eb154
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 3 deletions.
20 changes: 17 additions & 3 deletions InventorySalesAdminUi/Model/GetIsManageStockForProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

namespace Magento\InventorySalesAdminUi\Model;

use Magento\Catalog\Model\ProductRepository;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\InventoryConfigurationApi\Exception\SkuIsNotAssignedToStockException;
use Magento\InventorySalesAdminUi\Model\ResourceModel\GetAssignedStockIdsBySku;
use Magento\InventoryApi\Api\StockRepositoryInterface;
use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
Expand All @@ -35,19 +35,27 @@ class GetIsManageStockForProduct
*/
private $getStockItemConfiguration;

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

/**
* @param GetSalableQuantityDataBySku $getSalableQuantityDataBySku
* @param StockRepositoryInterface $stockRepository
* @param GetStockItemConfigurationInterface $getStockItemConfiguration
* @param ProductRepository $productRepository
*/
public function __construct(
GetSalableQuantityDataBySku $getSalableQuantityDataBySku,
StockRepositoryInterface $stockRepository,
GetStockItemConfigurationInterface $getStockItemConfiguration
GetStockItemConfigurationInterface $getStockItemConfiguration,
ProductRepository $productRepository
) {
$this->getSalableQuantityDataBySku = $getSalableQuantityDataBySku;
$this->stockRepository = $stockRepository;
$this->getStockItemConfiguration = $getStockItemConfiguration;
$this->productRepository = $productRepository;
}

/**
Expand All @@ -62,6 +70,12 @@ public function __construct(
*/
public function execute(string $sku, string $websiteCode): ?bool
{
try {
$this->productRepository->get($sku);
} catch (NoSuchEntityException $e) {
return false;
}

$isManageStock = null;
$stockIds = $this->getProductStockIds($sku);
foreach ($stockIds as $stockId) {
Expand Down Expand Up @@ -92,7 +106,7 @@ public function execute(string $sku, string $websiteCode): ?bool
private function getProductStockIds(string $sku): array
{
$stockIds = [];
$stocksInfo =$this->getSalableQuantityDataBySku->execute($sku);
$stocksInfo = $this->getSalableQuantityDataBySku->execute($sku);
foreach ($stocksInfo as $stockInfo) {
$stockIds[] = (int)$stockInfo['stock_id'];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventorySalesAdminUi\Test\Integration\Model;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
use Magento\Checkout\Test\Fixture\PlaceOrder as PlaceOrderFixture;
use Magento\Checkout\Test\Fixture\SetBillingAddress as SetBillingAddressFixture;
use Magento\Checkout\Test\Fixture\SetDeliveryMethod as SetDeliveryMethodFixture;
use Magento\Checkout\Test\Fixture\SetGuestEmail as SetGuestEmailFixture;
use Magento\Checkout\Test\Fixture\SetPaymentMethod as SetPaymentMethodFixture;
use Magento\Checkout\Test\Fixture\SetShippingAddress as SetShippingAddressFixture;
use Magento\ConfigurableProduct\Test\Fixture\AddProductToCart as AddConfigurableProductToCartFixture;
use Magento\ConfigurableProduct\Test\Fixture\Attribute as AttributeFixture;
use Magento\ConfigurableProduct\Test\Fixture\Product as ConfigurableProductFixture;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\ObjectManagerInterface;
use Magento\InventorySalesAdminUi\Model\GetIsManageStockForProduct;
use Magento\Quote\Test\Fixture\GuestCart as GuestCartFixture;
use Magento\Sales\Api\CreditmemoRepositoryInterface;
use Magento\Sales\Model\Order\CreditmemoFactory;
use Magento\Sales\Test\Fixture\Invoice as InvoiceFixture;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorage;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

/**
* Class to test if stock is manageable for a product.
*
* @magentoAppArea adminhtml
* @magentoAppIsolation enabled
*/
class GetIsManageStockForProductTest extends TestCase
{
/**
* @var GetIsManageStockForProduct
*/
private $getIsManageStockForProduct;

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

/**
* @var DataFixtureStorage
*/
private $fixtures;

/**
* @var CreditmemoFactory
*/
private $creditMemoFactory;

/**
* @var CreditmemoRepositoryInterface
*/
private $creditMemoRepositoryInterface;

/**
* @var ObjectManagerInterface
*/
private $objectManager;

/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();

$this->objectManager = Bootstrap::getObjectManager();
$this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage();
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$this->creditMemoFactory = $this->objectManager->create(CreditmemoFactory::class);
$this->creditMemoRepositoryInterface = $this->objectManager->create(CreditmemoRepositoryInterface::class);
$this->getIsManageStockForProduct = $this->objectManager->get(GetIsManageStockForProduct::class);
}

#[
DataFixture(ProductFixture::class, as: 'p1'),
DataFixture(AttributeFixture::class, as: 'attr'),
DataFixture(
ConfigurableProductFixture::class,
['_options' => ['$attr$'], '_links' => ['$p1$']],
'cp1'
),
DataFixture(GuestCartFixture::class, as: 'cart'),
DataFixture(
AddConfigurableProductToCartFixture::class,
['cart_id' => '$cart.id$', 'product_id' => '$cp1.id$', 'child_product_id' => '$p1.id$', 'qty' => 2],
),
DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(SetGuestEmailFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(SetDeliveryMethodFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(SetPaymentMethodFixture::class, ['cart_id' => '$cart.id$']),
DataFixture(PlaceOrderFixture::class, ['cart_id' => '$cart.id$'], 'order'),
DataFixture(InvoiceFixture::class, ['order_id' => '$order.id$']),
]
public function testCreditMemoForConfigurableWithDeletedSimpleProductOption(): void
{
$productSKU = $this->fixtures->get('p1')->getSku();
$order = $this->fixtures->get('order');
$this->deleteProductBySku($productSKU);
$this->assertFalse(
$this->getIsManageStockForProduct->execute(
$productSKU,
$order->getStore()->getWebsite()->getCode()
)
);
$creditMemo = $this->creditMemoFactory->createByOrder($order);
$this->creditMemoRepositoryInterface->save($creditMemo);
self::assertGreaterThan(0, count($creditMemo->getItems()));
}

/**
* Delete product by sku
*
* @param string $sku
* @return void
*/
private function deleteProductBySku(string $sku): void
{
try {
$product = $this->productRepository->get($sku);
$this->productRepository->delete($product);
} catch (NoSuchEntityException $exception) {
// product doesn't exist;
}
}
}

0 comments on commit 78eb154

Please sign in to comment.