Skip to content

Commit

Permalink
Merge pull request #93 from markfischmann/fix/remove-double-quantity-…
Browse files Browse the repository at this point in the history
…refund-on-shipped-orders

Remove plugin that will prevent double quantity refund
  • Loading branch information
convenient authored Jun 22, 2023
2 parents bb2201f + 4e9137e commit fea8363
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 5 deletions.
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ php: 8.1

env:
- TEST_GROUP=2-3-7
- TEST_GROUP=2-3-7-p4
- TEST_GROUP=2-4-0
- TEST_GROUP=2-4-1
- TEST_GROUP=2-4-2
Expand All @@ -11,10 +12,10 @@ env:
- TEST_GROUP=2-4-5
- TEST_GROUP=2-4-6

#jobs:
# allow_failures:
# - env: TEST_GROUP=2-4-3
# - env: TEST_GROUP=2-4-4
jobs:
allow_failures:
- env: TEST_GROUP=2-3-7-p4
- env: TEST_GROUP=2-3-7
# - env: TEST_GROUP=2-4-5
# - env: TEST_GROUP=2-4-6

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "This module disables the inventory reservation logic introduced as part of MSI in Magento 2.3.3",
"type": "magento2-module",
"require": {
"magento/framework": "*",
"magento/framework": ">=103",
"php": "^7.1|^8.0"
},
"autoload": {
Expand Down
107 changes: 107 additions & 0 deletions dev/tests/acceptance/CheckoutCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,111 @@ public function stockDeductionPreventsSubsequentAddToBasket(Step\Acceptance\Mage
// Add 20 of unit, this should work
$I->addSimpleProductToQuote($newCartId, 'amp_verify_stock_deduction_prevents_add_to_basket', 20);
}

/**
* @depends noInventoryIsReservedAndStockHasBeenDeducted
* @param Step\Acceptance\Magento $I
*
* @link https://github.com/AmpersandHQ/magento2-disable-stock-reservation/pull/93#issuecomment-1362938362
*/
public function preventDoubleRefundQuantityOnShippedOrder(Step\Acceptance\Magento $I)
{
$productId = $I->createSimpleProduct('amp_stock_refund_double_quantity', 100);

$cartId = $I->getGuestQuote();
$I->addSimpleProductToQuote($cartId, 'amp_stock_refund_double_quantity', 5);
$orderId = $I->completeGuestCheckout($cartId);

$newQty = $I->grabFromDatabase('cataloginventory_stock_item', 'qty', ['product_id' => $productId]);
$I->assertEquals(95, $newQty);

$I->amBearerAuthenticated(Step\Acceptance\Magento::ACCESS_TOKEN);
$I->haveHttpHeader('Content-Type', 'application/json');
// If the payment method chosen is banktransfer, you must invoice the order
$I->sendPOSTAndVerifyResponseCodeIs200("V1/order/{$orderId}/invoice", json_encode([
"capture" => true,
"notify" => false
]));

// Ship the order by creating a sales_shipment
$I->sendPOSTAndVerifyResponseCodeIs200("V1/order/{$orderId}/ship");

$newQty = $I->grabFromDatabase('cataloginventory_stock_item', 'qty', ['product_id' => $productId]);
$I->assertEquals(95, $newQty, 'The quantity should have been decremented on creation of the order');

$orderItemId = $I->grabFromDatabase('sales_order_item', 'item_id', ['order_id' => $orderId]);
//Create a creditmemo from the invoice and make sure to check the "Return to stock" checkbox
$I->sendPOSTAndVerifyResponseCodeIs200("V1/order/{$orderId}/refund", json_encode([
"items" => [
[
"order_item_id" => $orderItemId,
"qty" => 5
]
],
"notify" => false,
"arguments" => [
"shipping_amount" => 0,
"adjustment_positive" => 0,
"adjustment_negative" => 0,
"extension_attributes" => [
"return_to_stock_items" => [
$orderItemId
]
]
]
]));

$refundedQty = $I->grabFromDatabase('cataloginventory_stock_item', 'qty', ['product_id' => $productId]);
$I->assertEquals(100, $refundedQty, 'The quantity should be reset to 100 after the refund');
}

/**
* @depends noInventoryIsReservedAndStockHasBeenDeducted
* @param Step\Acceptance\Magento $I
*/
public function refundOnShippedOrderDoesNotAffectQuantityWhenNotReturnedToStock(Step\Acceptance\Magento $I)
{
$productId = $I->createSimpleProduct('amp_stock_refund_double_quantity_not_returned', 100);

$cartId = $I->getGuestQuote();
$I->addSimpleProductToQuote($cartId, 'amp_stock_refund_double_quantity_not_returned', 5);
$orderId = $I->completeGuestCheckout($cartId);

$newQty = $I->grabFromDatabase('cataloginventory_stock_item', 'qty', ['product_id' => $productId]);
$I->assertEquals(95, $newQty);

$I->amBearerAuthenticated(Step\Acceptance\Magento::ACCESS_TOKEN);
$I->haveHttpHeader('Content-Type', 'application/json');
// If the payment method chosen is banktransfer, you must invoice the order
$I->sendPOSTAndVerifyResponseCodeIs200("V1/order/{$orderId}/invoice", json_encode([
"capture" => true,
"notify" => false
]));

// Ship the order by creating a sales_shipment
$I->sendPOSTAndVerifyResponseCodeIs200("V1/order/{$orderId}/ship");

$newQty = $I->grabFromDatabase('cataloginventory_stock_item', 'qty', ['product_id' => $productId]);
$I->assertEquals(95, $newQty, 'The quantity should have been decremented on creation of the order');

$orderItemId = $I->grabFromDatabase('sales_order_item', 'item_id', ['order_id' => $orderId]);
//Create a creditmemo from the invoice and make sure return_to_stock_items is not set
$I->sendPOSTAndVerifyResponseCodeIs200("V1/order/{$orderId}/refund", json_encode([
"items" => [
[
"order_item_id" => $orderItemId,
"qty" => 5
]
],
"notify" => false,
"arguments" => [
"shipping_amount" => 0,
"adjustment_positive" => 0,
"adjustment_negative" => 0
]
]));

$refundedQty = $I->grabFromDatabase('cataloginventory_stock_item', 'qty', ['product_id' => $productId]);
$I->assertEquals(95, $refundedQty, 'The quantity should remain at 95 when not returned');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace Ampersand\DisableStockReservation\Plugin\Preference\SalesInventory;

use Magento\Sales\Api\Data\CreditmemoInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\SalesInventory\Model\Order\ReturnProcessor;
use Magento\InventorySales\Model\GetBackorder;
use Magento\InventorySales\Plugin\SalesInventory\ProcessReturnQtyOnCreditMemoPlugin as CorePlugin;

class ProcessReturnQtyOnCreditMemoPlugin extends CorePlugin
{
/**
* Conditionally disable process_return_product_qty_on_credit_memo plugin
*
* It does additional stock replenishment on creditmemo in 2.4.5 and above, we have already covered that case
*
* @see \Ampersand\DisableStockReservation\Observer\RestoreSourceItemQuantityOnRefundObserver
* @link https://github.com/magento/inventory/pull/2896
*
* @param ReturnProcessor $subject
* @param callable $proceed
* @param CreditmemoInterface $creditmemo
* @param OrderInterface $order
* @param array $returnToStockItems
* @param bool $isAutoReturn
* @return void
*/
public function aroundExecute(
ReturnProcessor $subject,
callable $proceed,
CreditmemoInterface $creditmemo,
OrderInterface $order,
array $returnToStockItems = [],
bool $isAutoReturn = false
): void {
// GetBackOrder class only available in 2.4.5 and higher, we need to disable this plugin on those versions
if (\class_exists(GetBackorder::class)) {
return;
}
parent::aroundExecute($subject, $proceed, $creditmemo, $order, $returnToStockItems, $isAutoReturn);
}
}
3 changes: 3 additions & 0 deletions src/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
<type name="Magento\Sales\Model\Service\OrderService">
<plugin name="inventory_sales_source_deduction_processor" type="Ampersand\DisableStockReservation\Plugin\SourceDeductionProcessor"/>
</type>
<!-- Fix the double SourceDeductionService call when an order is shipped by conditionally disabling process_return_product_qty_on_credit_memo plugin-->
<preference for="Magento\InventorySales\Plugin\SalesInventory\ProcessReturnQtyOnCreditMemoPlugin" type="Ampersand\DisableStockReservation\Plugin\Preference\SalesInventory\ProcessReturnQtyOnCreditMemoPlugin"/>

<!--
Fix M2.4 introduced bug caused by new interface implementation.
New interface implementation checks against in-store reserved stock.
Expand Down

0 comments on commit fea8363

Please sign in to comment.