Skip to content
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

12386: Order Status resets to default Status after Partial Refund. #20378

Merged
merged 3 commits into from
Apr 18, 2019
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
19 changes: 11 additions & 8 deletions app/code/Magento/Sales/Model/Order/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public function canCapture()
}

/**
* Check refund availability
* Check refund availability.
*
* @return bool
*/
Expand All @@ -310,7 +310,7 @@ public function canRefund()
}

/**
* Check partial refund availability for invoice
* Check partial refund availability for invoice.
*
* @return bool
*/
Expand All @@ -320,7 +320,7 @@ public function canRefundPartialPerInvoice()
}

/**
* Check partial capture availability
* Check partial capture availability.
*
* @return bool
*/
Expand Down Expand Up @@ -546,9 +546,7 @@ public function cancelInvoice($invoice)
}

/**
* Create new invoice with maximum qty for invoice for each item
*
* Register this invoice and capture
* Create new invoice with maximum qty for invoice for each item register this invoice and capture
*
* @return Invoice
*/
Expand Down Expand Up @@ -686,6 +684,7 @@ public function refund($creditmemo)
$gateway->refund($this, $baseAmountToRefund);

$creditmemo->setTransactionId($this->getLastTransId());
// phpcs:ignore Magento2.Exceptions.ThrowCatch
} catch (\Magento\Framework\Exception\LocalizedException $e) {
if (!$captureTxn) {
throw new \Magento\Framework\Exception\LocalizedException(
Expand Down Expand Up @@ -732,10 +731,14 @@ public function refund($creditmemo)
$message = $message = $this->prependMessage($message);
$message = $this->_appendTransactionToMessage($transaction, $message);
$orderState = $this->getOrderStateResolver()->getStateForOrder($this->getOrder());
$statuses = $this->getOrder()->getConfig()->getStateStatuses($orderState, false);
$status = in_array($this->getOrder()->getStatus(), $statuses, true)
? $this->getOrder()->getStatus()
: $this->getOrder()->getConfig()->getStateDefaultStatus($orderState);
$this->getOrder()
->addStatusHistoryComment(
$message,
$this->getOrder()->getConfig()->getStateDefaultStatus($orderState)
$status
)->setIsCustomerNotified($creditmemo->getOrder()->getCustomerNoteNotify());
$this->_eventManager->dispatch(
'sales_order_payment_refund',
Expand Down Expand Up @@ -1203,7 +1206,7 @@ public function addTransaction($type, $salesDocument = null, $failSafe = false)
}

/**
* Add message to the specified transaction.
* Add transaction comments to order.
*
* @param Transaction|null $transaction
* @param string $message
Expand Down
12 changes: 8 additions & 4 deletions app/code/Magento/Sales/Model/RefundOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Sales\Model;

use Magento\Framework\App\ResourceConnection;
Expand Down Expand Up @@ -151,10 +152,13 @@ public function execute(
$creditmemo->setState(\Magento\Sales\Model\Order\Creditmemo::STATE_REFUNDED);
$order->setCustomerNoteNotify($notify);
$order = $this->refundAdapter->refund($creditmemo, $order);
$order->setState(
$this->orderStateResolver->getStateForOrder($order, [])
);
$order->setStatus($this->config->getStateDefaultStatus($order->getState()));
$orderState = $this->orderStateResolver->getStateForOrder($order, []);
$order->setState($orderState);
$statuses = $this->config->getStateStatuses($orderState, false);
$status = in_array($order->getStatus(), $statuses, true)
? $order->getStatus()
: $this->config->getStateDefaultStatus($orderState);
$order->setStatus($status);

$order = $this->orderRepository->save($order);
$creditmemo = $this->creditmemoRepository->save($creditmemo);
Expand Down
3 changes: 2 additions & 1 deletion app/code/Magento/Sales/Test/Unit/Model/Order/PaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Sales\Test\Unit\Model\Order;

use Magento\Framework\Model\Context;
Expand Down Expand Up @@ -1526,7 +1527,7 @@ public function testRefund()
$this->orderStateResolver->expects($this->once())->method('getStateForOrder')
->with($this->order)
->willReturn(Order::STATE_CLOSED);
$this->mockGetDefaultStatus(Order::STATE_CLOSED, $status);
$this->mockGetDefaultStatus(Order::STATE_CLOSED, $status, ['first, second']);
$this->assertOrderUpdated(Order::STATE_PROCESSING, $status, $message);

static::assertSame($this->payment, $this->payment->refund($this->creditMemoMock));
Expand Down
7 changes: 4 additions & 3 deletions app/code/Magento/Sales/Test/Unit/Model/RefundOrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Sales\Test\Unit\Model;

use Magento\Framework\App\ResourceConnection;
Expand Down Expand Up @@ -245,9 +246,9 @@ public function testOrderCreditmemo($orderId, $notify, $appendComment)
->method('setState')
->with(Order::STATE_CLOSED)
->willReturnSelf();
$this->orderMock->expects($this->once())
->method('getState')
->willReturn(Order::STATE_CLOSED);
$this->configMock->expects($this->once())
->method('getStateStatuses')
->willReturn(['first, second']);
$this->configMock->expects($this->once())
->method('getStateDefaultStatus')
->with(Order::STATE_CLOSED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Sales\Service\V1;

use Magento\Sales\Model\Order;
Expand Down Expand Up @@ -201,6 +202,73 @@ public function testFullRequest()
}
}

/**
* Test order will keep same(custom) status after partial refund, if state has not been changed.
*
* @magentoApiDataFixture Magento/Sales/_files/order_with_invoice_and_custom_status.php
*/
public function testOrderStatusPartialRefund()
{
/** @var \Magento\Sales\Model\Order $existingOrder */
$existingOrder = $this->objectManager->create(\Magento\Sales\Model\Order::class)
->loadByIncrementId('100000001');

$items = $this->getOrderItems($existingOrder);
$items[0]['qty'] -= 1;
$result = $this->_webApiCall(
$this->getServiceData($existingOrder),
[
'orderId' => $existingOrder->getEntityId(),
'items' => $items,
]
);

$this->assertNotEmpty(
$result,
'Failed asserting that the received response is correct'
);

/** @var \Magento\Sales\Model\Order $updatedOrder */
$updatedOrder = $this->objectManager->create(\Magento\Sales\Model\Order::class)
->loadByIncrementId($existingOrder->getIncrementId());

$this->assertSame('custom_processing', $updatedOrder->getStatus());
$this->assertSame('processing', $updatedOrder->getState());
}

/**
* Test order will change custom status after total refund, when state has been changed.
*
* @magentoApiDataFixture Magento/Sales/_files/order_with_invoice_and_custom_status.php
*/
public function testOrderStatusTotalRefund()
{
/** @var \Magento\Sales\Model\Order $existingOrder */
$existingOrder = $this->objectManager->create(\Magento\Sales\Model\Order::class)
->loadByIncrementId('100000001');

$items = $this->getOrderItems($existingOrder);
$result = $this->_webApiCall(
$this->getServiceData($existingOrder),
[
'orderId' => $existingOrder->getEntityId(),
'items' => $items,
]
);

$this->assertNotEmpty(
$result,
'Failed asserting that the received response is correct'
);

/** @var \Magento\Sales\Model\Order $updatedOrder */
$updatedOrder = $this->objectManager->create(\Magento\Sales\Model\Order::class)
->loadByIncrementId($existingOrder->getIncrementId());

$this->assertSame('complete', $updatedOrder->getStatus());
$this->assertSame('complete', $updatedOrder->getState());
}

/**
* Prepares and returns info for API service.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@

namespace Magento\Sales\Controller\Adminhtml\Order\Creditmemo;

use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Sales\Api\Data\OrderItemInterface;
use Magento\Sales\Model\Order;
use PHPUnit\Framework\Constraint\StringContains;

/**
* Class tests creditmemo creation in backend.
* Provide tests for CreditMemo save controller.
*
* @magentoDbIsolation enabled
* @magentoAppArea adminhtml
Expand All @@ -24,6 +27,8 @@ class SaveTest extends AbstractCreditmemoControllerTest
protected $uri = 'backend/sales/order_creditmemo/save';

/**
* @magentoDbIsolation enabled
* @magentoDataFixture Magento/Sales/_files/invoice.php
* @return void
*/
public function testSendEmailOnCreditmemoSave(): void
Expand Down Expand Up @@ -54,6 +59,91 @@ public function testSendEmailOnCreditmemoSave(): void
$this->assertThat($message->getRawMessage(), $messageConstraint);
}

/**
* Test order will keep same(custom) status after partial refund, if state has not been changed.
*
* @magentoDataFixture Magento/Sales/_files/order_with_invoice_and_custom_status.php
*/
public function testOrderStatusPartialRefund()
{
/** @var Order $existingOrder */
$existingOrder = $this->_objectManager->create(Order::class)->loadByIncrementId('100000001');
$items = $this->getOrderItems($existingOrder, 1);
$requestParams = [
'creditmemo' => [
'items' => $items,
'do_offline' => '1',
'comment_text' => '',
'shipping_amount' => '0',
'adjustment_positive' => '0',
'adjustment_negative' => '0',
],
'order_id' => $existingOrder->getId(),
];
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
$this->getRequest()->setParams($requestParams);
$this->dispatch('backend/sales/order_creditmemo/save');

/** @var Order $updatedOrder */
$updatedOrder = $this->_objectManager->create(Order::class)
->loadByIncrementId($existingOrder->getIncrementId());

$this->assertSame('custom_processing', $updatedOrder->getStatus());
$this->assertSame('processing', $updatedOrder->getState());
}

/**
* Test order will change custom status after total refund, when state has been changed.
*
* @magentoDataFixture Magento/Sales/_files/order_with_invoice_and_custom_status.php
*/
public function testOrderStatusTotalRefund()
{
/** @var Order $existingOrder */
$existingOrder = $this->_objectManager->create(Order::class)->loadByIncrementId('100000001');
$requestParams = [
'creditmemo' => [
'items' => $this->getOrderItems($existingOrder),
'do_offline' => '1',
'comment_text' => '',
'shipping_amount' => '0',
'adjustment_positive' => '0',
'adjustment_negative' => '0',
],
'order_id' => $existingOrder->getId(),
];
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
$this->getRequest()->setParams($requestParams);
$this->dispatch('backend/sales/order_creditmemo/save');

/** @var Order $updatedOrder */
$updatedOrder = $this->_objectManager->create(Order::class)
->loadByIncrementId($existingOrder->getIncrementId());

$this->assertSame('complete', $updatedOrder->getStatus());
$this->assertSame('complete', $updatedOrder->getState());
}

/**
* Gets all items of given Order in proper format.
*
* @param Order $order
* @param int $subQty
* @return array
*/
private function getOrderItems(Order $order, int $subQty = 0)
{
$items = [];
/** @var OrderItemInterface $item */
foreach ($order->getAllItems() as $item) {
$items[$item->getItemId()] = [
'qty' => $item->getQtyOrdered() - $subQty,
];
}

return $items;
}

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

use Magento\Sales\Model\Order\Status;
use Magento\TestFramework\Helper\Bootstrap;

// phpcs:ignore Magento2.Security.IncludeFile
require 'invoice.php';

$orderStatus = Bootstrap::getObjectManager()->create(Status::class);
$data = [
'status' => 'custom_processing',
'label' => 'Custom Processing Status',
];
$orderStatus->setData($data)->setStatus('custom_processing');
$orderStatus->save();
$orderStatus->assignState('processing');

$order->setStatus('custom_processing');
$order->save();
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Sales\Model\Order\Status;
use Magento\TestFramework\Helper\Bootstrap;

// phpcs:ignore Magento2.Security.IncludeFile
require 'default_rollback.php';

/** @var Status $orderStatus */
$orderStatus = Bootstrap::getObjectManager()->create(Status::class);
$orderStatus->load('custom_processing', 'status');
$orderStatus->delete();