Skip to content

Create cancel invoice method on API #12049

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

Closed
Closed
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
23 changes: 23 additions & 0 deletions app/code/Magento/Sales/Api/InvoiceCancelInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Api;

/**
* Interface InvoiceCancelInterface
Copy link
Contributor

@ishakhsuvarov ishakhsuvarov Nov 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide description for the interface in the doc block.
It may be some information regarding it's responsibility and reasons for introduction.

*
* @package Magento\Sales\Api
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@package annotation is not used in Magento

*/
interface InvoiceCancelInterface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be a good idea to mark this with @api. This would also require providing PRs for other branches.

{

/**
* Cancel invoice
*
* @param int $invoiceId
* @return bool
*/
public function cancel($invoiceId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this PR is targeting 2.2-develop it is possible to declare scalar type hint and return type for the method.

}
50 changes: 50 additions & 0 deletions app/code/Magento/Sales/Model/Order/InvoiceCancel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Model\Order;

use Magento\Sales\Api\InvoiceCancelInterface;
use Magento\Sales\Api\InvoiceRepositoryInterface;

/**
* Class InvoiceCancel
*/
class InvoiceCancel implements InvoiceCancelInterface
{

/**
* @var InvoiceRepositoryInterface
*/
protected $invoiceRepository;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid creating protected properties in the core codebase, as usage of inheritance is discouraged.


/**
* InvoiceCancel constructor.
* @param InvoiceRepositoryInterface $invoiceRepository
*/
public function __construct(
InvoiceRepositoryInterface $invoiceRepository
) {
$this->invoiceRepository = $invoiceRepository;
}

/**
* Cancel invoice
*
* @param int $invoiceId
* @return bool
*/
public function cancel($invoiceId)
{
/** @var \Magento\Sales\Api\Data\InvoiceInterface $invoice */
$invoice = $this->invoiceRepository->get($invoiceId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method may throw an exception, which has to be either caught or declared in the interface.

if ($invoice && $invoice->canCancel()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe there is no reason to check for $invoice variable, as repository would throw an exception or return an invoice, this variable would not be empty at this point.
Additionally, canCancel check is already performed in the cancel method implementation, is there ay particular reason for this?

$invoice->cancel();
$this->invoiceRepository->save($invoice);
return true;
Copy link
Contributor

@ishakhsuvarov ishakhsuvarov Nov 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to return value of $invoice->getState() here? This would allow to get rid of the if statement.

}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ class InvoiceServiceTest extends \PHPUnit\Framework\TestCase
*/
protected $repositoryMock;

/**
* Invoice Cancel
*
* @var \Magento\Sales\Api\InvoiceCancelInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $invoiceCancelMock;

/**
* Repository
*
Expand Down Expand Up @@ -52,6 +59,11 @@ class InvoiceServiceTest extends \PHPUnit\Framework\TestCase
*/
protected $invoiceService;

/**
* @var \Magento\Sales\Model\Order\Invoice
*/
protected $invoiceMock;

/**
* SetUp
*/
Expand Down Expand Up @@ -84,6 +96,16 @@ protected function setUp()
['notify']
);

$this->invoiceCancelMock = new \Magento\Sales\Model\Order\InvoiceCancel(
$this->repositoryMock
);

$this->invoiceMock = $this->getMockBuilder(
\Magento\Sales\Model\Order\Invoice::class
)
->disableOriginalConstructor()
->getMock();

$this->invoiceService = $objectManager->getObject(
\Magento\Sales\Model\Service\InvoiceService::class,
[
Expand Down Expand Up @@ -204,4 +226,40 @@ public function testSetVoid()

$this->assertTrue($this->invoiceService->setVoid($id));
}

/**
* Run test for Invoice::cancel()
*/
public function testCancel()
{
$this->repositoryMock->expects($this->once())
->method('get')
->with(123)
->willReturn($this->invoiceMock);
$this->invoiceMock->expects($this->once())
->method('cancel')
->willReturn($this->invoiceMock);
$this->invoiceMock->expects($this->once())
->method('canCancel')
->willReturn(true);
$this->assertTrue($this->invoiceCancelMock->cancel(123));
}

/**
* test for Invoice::cancel() fail case
*/
public function testCancelFailed()
{
$this->repositoryMock->expects($this->once())
->method('get')
->with(123)
->willReturn($this->invoiceMock);
$this->invoiceMock->expects($this->never())
->method('cancel')
->willReturn($this->invoiceMock);
$this->invoiceMock->expects($this->once())
->method('canCancel')
->willReturn(false);
$this->assertFalse($this->invoiceCancelMock->cancel(123));
}
}
3 changes: 2 additions & 1 deletion app/code/Magento/Sales/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<preference for="Magento\Sales\Api\Data\InvoiceCreationArgumentsInterface" type="Magento\Sales\Model\Order\Invoice\CreationArguments"/>
<preference for="Magento\Sales\Api\Data\InvoiceItemCreationInterface" type="Magento\Sales\Model\Order\Invoice\ItemCreation"/>
<preference for="Magento\Sales\Api\Data\InvoiceCommentCreationInterface" type="Magento\Sales\Model\Order\Invoice\CommentCreation"/>
<preference for="Magento\Sales\Api\InvoiceCancelInterface" type="Magento\Sales\Model\Order\InvoiceCancel"/>
<preference for="Magento\Sales\Api\Data\ShipmentCommentCreationInterface" type="Magento\Sales\Model\Order\Shipment\CommentCreation"/>
<preference for="Magento\Sales\Api\Data\CreditmemoCommentCreationInterface" type="Magento\Sales\Model\Order\Creditmemo\CommentCreation"/>
<preference for="Magento\Sales\Api\OrderAddressRepositoryInterface" type="Magento\Sales\Model\Order\AddressRepository"/>
Expand Down Expand Up @@ -982,4 +983,4 @@
</argument>
</arguments>
</type>
</config>
</config>
8 changes: 7 additions & 1 deletion app/code/Magento/Sales/etc/webapi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@
<resource ref="Magento_Sales::sales" />
</resources>
</route>
<route url="/V1/invoice/:invoiceId/cancel" method="POST">
<service class="Magento\Sales\Api\InvoiceCancelInterface" method="cancel"/>
<resources>
<resource ref="Magento_Sales::sales" />
</resources>
</route>
<route url="/V1/creditmemo/:id/comments" method="GET">
<service class="Magento\Sales\Api\CreditmemoManagementInterface" method="getCommentsList"/>
<resources>
Expand Down Expand Up @@ -277,4 +283,4 @@
<resource ref="Magento_Sales::sales" />
</resources>
</route>
</routes>
</routes>