Skip to content

Commit 5e7ed2b

Browse files
committed
Create cancel method on API
1 parent 080a341 commit 5e7ed2b

File tree

5 files changed

+140
-2
lines changed

5 files changed

+140
-2
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sales\Api;
7+
8+
/**
9+
* Interface InvoiceCancelInterface
10+
*
11+
* @package Magento\Sales\Api
12+
*/
13+
interface InvoiceCancelInterface
14+
{
15+
16+
/**
17+
* Cancel invoice
18+
*
19+
* @param int $invoiceId
20+
* @return bool
21+
*/
22+
public function cancel($invoiceId);
23+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sales\Model\Order;
7+
8+
use Magento\Sales\Api\InvoiceCancelInterface;
9+
use Magento\Sales\Api\InvoiceRepositoryInterface;
10+
11+
/**
12+
* Class InvoiceCancel
13+
*/
14+
class InvoiceCancel implements InvoiceCancelInterface
15+
{
16+
17+
/**
18+
* @var InvoiceRepositoryInterface
19+
*/
20+
protected $invoiceRepository;
21+
22+
/**
23+
* InvoiceCancel constructor.
24+
* @param InvoiceRepositoryInterface $invoiceRepository
25+
*/
26+
public function __construct(
27+
InvoiceRepositoryInterface $invoiceRepository
28+
) {
29+
$this->invoiceRepository = $invoiceRepository;
30+
}
31+
32+
/**
33+
* Cancel invoice
34+
*
35+
* @param int $invoiceId
36+
* @return bool
37+
*/
38+
public function cancel($invoiceId)
39+
{
40+
/** @var \Magento\Sales\Api\Data\InvoiceInterface $invoice */
41+
$invoice = $this->invoiceRepository->get($invoiceId);
42+
if ($invoice && $invoice->canCancel()) {
43+
$invoice->cancel();
44+
$this->invoiceRepository->save($invoice);
45+
return true;
46+
}
47+
48+
return false;
49+
}
50+
}

app/code/Magento/Sales/Test/Unit/Model/Service/InvoiceServiceTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ class InvoiceServiceTest extends \PHPUnit\Framework\TestCase
1919
*/
2020
protected $repositoryMock;
2121

22+
/**
23+
* Invoice Cancel
24+
*
25+
* @var \Magento\Sales\Api\InvoiceCancelInterface|\PHPUnit_Framework_MockObject_MockObject
26+
*/
27+
protected $invoiceCancelMock;
28+
2229
/**
2330
* Repository
2431
*
@@ -52,6 +59,11 @@ class InvoiceServiceTest extends \PHPUnit\Framework\TestCase
5259
*/
5360
protected $invoiceService;
5461

62+
/**
63+
* @var \Magento\Sales\Model\Order\Invoice
64+
*/
65+
protected $invoiceMock;
66+
5567
/**
5668
* SetUp
5769
*/
@@ -84,6 +96,16 @@ protected function setUp()
8496
['notify']
8597
);
8698

99+
$this->invoiceCancelMock = new \Magento\Sales\Model\Order\InvoiceCancel(
100+
$this->repositoryMock
101+
);
102+
103+
$this->invoiceMock = $this->getMockBuilder(
104+
\Magento\Sales\Model\Order\Invoice::class
105+
)
106+
->disableOriginalConstructor()
107+
->getMock();
108+
87109
$this->invoiceService = $objectManager->getObject(
88110
\Magento\Sales\Model\Service\InvoiceService::class,
89111
[
@@ -204,4 +226,40 @@ public function testSetVoid()
204226

205227
$this->assertTrue($this->invoiceService->setVoid($id));
206228
}
229+
230+
/**
231+
* Run test for Invoice::cancel()
232+
*/
233+
public function testCancel()
234+
{
235+
$this->repositoryMock->expects($this->once())
236+
->method('get')
237+
->with(123)
238+
->willReturn($this->invoiceMock);
239+
$this->invoiceMock->expects($this->once())
240+
->method('cancel')
241+
->willReturn($this->invoiceMock);
242+
$this->invoiceMock->expects($this->once())
243+
->method('canCancel')
244+
->willReturn(true);
245+
$this->assertTrue($this->invoiceCancelMock->cancel(123));
246+
}
247+
248+
/**
249+
* test for Invoice::cancel() fail case
250+
*/
251+
public function testCancelFailed()
252+
{
253+
$this->repositoryMock->expects($this->once())
254+
->method('get')
255+
->with(123)
256+
->willReturn($this->invoiceMock);
257+
$this->invoiceMock->expects($this->never())
258+
->method('cancel')
259+
->willReturn($this->invoiceMock);
260+
$this->invoiceMock->expects($this->once())
261+
->method('canCancel')
262+
->willReturn(false);
263+
$this->assertFalse($this->invoiceCancelMock->cancel(123));
264+
}
207265
}

app/code/Magento/Sales/etc/di.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<preference for="Magento\Sales\Api\Data\InvoiceCreationArgumentsInterface" type="Magento\Sales\Model\Order\Invoice\CreationArguments"/>
5757
<preference for="Magento\Sales\Api\Data\InvoiceItemCreationInterface" type="Magento\Sales\Model\Order\Invoice\ItemCreation"/>
5858
<preference for="Magento\Sales\Api\Data\InvoiceCommentCreationInterface" type="Magento\Sales\Model\Order\Invoice\CommentCreation"/>
59+
<preference for="Magento\Sales\Api\InvoiceCancelInterface" type="Magento\Sales\Model\Order\InvoiceCancel"/>
5960
<preference for="Magento\Sales\Api\Data\ShipmentCommentCreationInterface" type="Magento\Sales\Model\Order\Shipment\CommentCreation"/>
6061
<preference for="Magento\Sales\Api\Data\CreditmemoCommentCreationInterface" type="Magento\Sales\Model\Order\Creditmemo\CommentCreation"/>
6162
<preference for="Magento\Sales\Api\OrderAddressRepositoryInterface" type="Magento\Sales\Model\Order\AddressRepository"/>
@@ -982,4 +983,4 @@
982983
</argument>
983984
</arguments>
984985
</type>
985-
</config>
986+
</config>

app/code/Magento/Sales/etc/webapi.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@
139139
<resource ref="Magento_Sales::sales" />
140140
</resources>
141141
</route>
142+
<route url="/V1/invoice/:invoiceId/cancel" method="POST">
143+
<service class="Magento\Sales\Api\InvoiceCancelInterface" method="cancel"/>
144+
<resources>
145+
<resource ref="Magento_Sales::sales" />
146+
</resources>
147+
</route>
142148
<route url="/V1/creditmemo/:id/comments" method="GET">
143149
<service class="Magento\Sales\Api\CreditmemoManagementInterface" method="getCommentsList"/>
144150
<resources>
@@ -277,4 +283,4 @@
277283
<resource ref="Magento_Sales::sales" />
278284
</resources>
279285
</route>
280-
</routes>
286+
</routes>

0 commit comments

Comments
 (0)