Stevebauman\Inventory\Traits\InventoryTransactionTrait::cancel PHP Method

cancel() public method

Transactions with states of opened, checkout, reserved, back ordered, ordered-pending, and inventory on hold CAN be cancelled Transactions with states such as sold, returned, order-received, and inventory released CAN NOT be cancelled.
public cancel ( string $reason = '', integer | float | string $cost )
$reason string
$cost integer | float | string
    public function cancel($reason = '', $cost = 0)
    {
        $this->validatePreviousState([null, $this::STATE_OPENED, $this::STATE_COMMERCE_CHECKOUT, $this::STATE_COMMERCE_RESERVED, $this::STATE_COMMERCE_BACK_ORDERED, $this::STATE_ORDERED_PENDING, $this::STATE_INVENTORY_ON_HOLD], $this::STATE_CANCELLED);
        $beforeQuantity = $this->getAttribute('quantity');
        $beforeState = $this->getAttribute('state');
        $this->setAttribute('quantity', 0);
        $this->setAttribute('state', $this::STATE_CANCELLED);
        $event = 'inventory.transaction.cancelled';
        if (empty($reason)) {
            $reason = $this->getTransactionReason('cancelled');
        }
        switch ($beforeState) {
            case $this::STATE_COMMERCE_CHECKOUT:
                return $this->processStockPutAndSave($beforeQuantity, $event, $reason, $cost);
            case $this::STATE_COMMERCE_RESERVED:
                return $this->processStockPutAndSave($beforeQuantity, $event, $reason, $cost);
            case $this::STATE_INVENTORY_ON_HOLD:
                return $this->processStockPutAndSave($beforeQuantity, $event, $reason, $cost);
            default:
                return $this->processSave($event);
        }
    }