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);
}
}