public function removePartial($quantity, $reason = '', $cost = 0)
{
/*
* If a partial remove is called and quantity is given, then we are removing
* a partial amount from the on hold transaction. Otherwise we are just processing
* a transaction for removing a quantity from the current stock
*/
if ($this->isOnHold()) {
$current = $this->getAttribute('quantity');
if ((double) $quantity === (double) $current || $quantity > $current) {
return $this->removeAll();
}
$this->validatePreviousState([$this::STATE_INVENTORY_ON_HOLD], $this::STATE_INVENTORY_REMOVED_PARTIAL);
$left = (double) $current - (double) $quantity;
$this->setAttribute('quantity', $left);
$previousState = $this->getAttribute('state');
$this->setAttribute('state', $this::STATE_INVENTORY_REMOVED_PARTIAL);
if ($this->processSave('inventory.transaction.removed.partial')) {
return $this->returnToPreviousState($previousState);
}
} else {
/*
* We must be processing a pure removal transaction, make sure
* previous state was null or opened
*/
$this->validatePreviousState([null, $this::STATE_OPENED], $this::STATE_INVENTORY_REMOVED);
$this->setAttribute('state', $this::STATE_INVENTORY_REMOVED);
$this->setAttribute('quantity', (double) $quantity);
if (empty($reason)) {
$reason = $this->getTransactionReason('removed');
}
return $this->processStockTakeAndSave($quantity, 'inventory.transaction.removed', $reason, $cost);
}
return false;
}