public function returnedPartial($quantity, $reason = '', $cost = 0)
{
$current = $this->getAttribute('quantity');
if ((double) $quantity === (double) $current || $quantity > $current) {
return $this->returnedAll($reason, $cost);
}
/*
* Only allow partial returns when the transaction state is
* sold, reserved, checkout, or returned partial
*/
$this->validatePreviousState([$this::STATE_COMMERCE_SOLD, $this::STATE_COMMERCE_RESERVED, $this::STATE_COMMERCE_CHECKOUT, $this::STATE_COMMERCE_RETURNED_PARTIAL], $this::STATE_COMMERCE_RETURNED_PARTIAL);
// Retrieve the previous state for returning the transaction to it's original state
$previousState = $this->getAttribute('state');
// Set a new state so a history record is created
$this->setAttribute('state', $this::STATE_COMMERCE_RETURNED_PARTIAL);
// Set the new left-over quantity from removing the amount returned
$left = (double) $current - (double) $quantity;
$this->setAttribute('quantity', $left);
if (empty($reason)) {
$reason = $this->getTransactionReason('returned-partial');
}
if ($this->processStockPutAndSave($quantity, 'inventory.transaction.returned.partial', $reason, $cost)) {
return $this->returnToPreviousState($previousState);
}
return false;
}