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

processStockTakeAndSave() protected method

Processes removing the specified quantity from transaction stock and saving the current transaction.
protected processStockTakeAndSave ( integer | float | string $quantity, string $event = '', string $reason = '', integer | float | string $cost )
$quantity integer | float | string
$event string
$reason string
$cost integer | float | string
    protected function processStockTakeAndSave($quantity, $event = '', $reason = '', $cost = 0)
    {
        $stock = $this->getStockRecord();
        $stock->isValidQuantity($quantity);
        $stock->hasEnoughStock($this->getAttribute('quantity'));
        $this->dbStartTransaction();
        try {
            if ($stock->take(floatval($quantity), $reason, $cost) && $this->save()) {
                $this->dbCommitTransaction();
                if ($event) {
                    $this->fireEvent($event, ['transaction' => $this]);
                }
                return $this;
            }
        } catch (\Exception $e) {
            $this->dbRollbackTransaction();
        }
        return false;
    }