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

Conversation

crissanclick
Copy link
Contributor

@crissanclick crissanclick commented Nov 5, 2017

I have worked on this PR at #MM17ES

Description

Magento have not implemented cancel invoice method on API.
I have added the cancel method and created the tests.

Fixed Issues (if relevant)

  1. Prevent invoice cancelation multiple times #11073 (comment)

Contribution checklist

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

@crissanclick crissanclick reopened this Nov 5, 2017
Copy link
Contributor

@ishakhsuvarov ishakhsuvarov left a comment

Choose a reason for hiding this comment

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

Please consider covering this with api-functional test as well

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.

/**
* Interface InvoiceCancelInterface
*
* @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

*
* @package Magento\Sales\Api
*/
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.

* @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.

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.

{
/** @var \Magento\Sales\Api\Data\InvoiceInterface $invoice */
$invoice = $this->invoiceRepository->get($invoiceId);
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?

/**
* @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.

if ($invoice && $invoice->canCancel()) {
$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.

@ishakhsuvarov
Copy link
Contributor

@crissanclick I am closing this PR now due to inactivity.
Please reopen and update if you wish to continue.
Thank you for colaboration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants