Skip to content

Can't cancelled invoice if invoice state == STATE_PAID #18509

@omaxmo

Description

@omaxmo
vendor/magento/module-sales/Model/Order/Invoice.php
public function cancel()
    {
        if (!$this->canCancel()) {
            return $this;
        }
       ..

        if ($this->getState() == self::STATE_PAID) {
            $order->setTotalPaid($order->getTotalPaid() - $this->getGrandTotal());
            $order->setBaseTotalPaid($order->getBaseTotalPaid() - $this->getBaseGrandTotal());
        }
...
        return $this;
    }

I suppose that this code will never be used :

  if ($this->getState() == self::STATE_PAID) {
            $order->setTotalPaid($order->getTotalPaid() - $this->getGrandTotal());
            $order->setBaseTotalPaid($order->getBaseTotalPaid() - $this->getBaseGrandTotal());
        }

if we have:

  if (!$this->canCancel()) {
            return $this;
        }

Problem:

public function cancel()
    {
        if (!$this->canCancel()) {
            return $this;
        }

Possible solution:

public function cancel()
    {
        if ($this->getState() == self::STATE_CANCELED) {
            return $this;
        }

Preconditions

  1. Tested on Magento 2.2.6

Steps to reproduce

  1. Load a invoice programmatically where state == STATE_PAID
  2. Call $invoice->cancel()

Expected result

  1. The invoice should be canceled.

Actual result

  1. Can't cancel invoice if invoice state == STATE_PAID

Metadata

Metadata

Assignees

No one assigned

    Labels

    Fixed in 2.3.xThe issue has been fixed in 2.3 release lineIssue: Format is validGate 1 Passed. Automatic verification of issue format passednon-issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions