/** * Update stock. * * @param PurchasableInterface $purchasable Purchasable * @param int $stockToDecrease Stock to decrease * * @return false|int Real decreased stock or false if error */ public function updateStock(PurchasableInterface $purchasable, $stockToDecrease) { $namespace = $this->getPurchasableNamespace(); if (!$purchasable instanceof $namespace) { return false; } /** * @var $purchasable PackInterface */ $stockType = $purchasable->getStockType(); if ($stockType === ElcodiProductStock::INHERIT_STOCK) { return $this->updateStockOfPurchasablesByLoadedStockUpdaters($purchasable->getPurchasables(), $stockToDecrease); } $decreasedStock = $this->updateSimplePurchasableStock($purchasable, $stockToDecrease); if ($decreasedStock) { $this->packObjectManager->flush($purchasable); } return $decreasedStock; }
/** * Gets purchasable validation. * * @param PurchasableInterface $purchasable Purchasable * @param int $stockRequired Stock required * @param bool $useStock Use stock * * @return bool|int Is valid or the number of elements that can be used */ public function isStockAvailable(PurchasableInterface $purchasable, $stockRequired, $useStock) { $namespace = $this->getPurchasableNamespace(); if (!$purchasable instanceof $namespace) { return false; } /** * @var PackInterface $purchasable */ $isInheritStock = ElcodiProductStock::INHERIT_STOCK === $purchasable->getStockType(); $isInheritanceValid = $this->areValidByLoadedValidators($purchasable->getPurchasables(), $stockRequired, $isInheritStock); /** * Happens when their related elements do not allow this pack to be used. */ if (false === $isInheritanceValid) { return false; } $isPackValid = $this->isValidUsingSimplePurchasableValidation($purchasable, $stockRequired, !$isInheritStock); /** * Happens when the pack itself is not valid. */ if (false === $isPackValid) { return false; } /** * At this point we need to check both values for determining the result * value of the whole validation. In this case, both results allow the * pack to be used. */ if (true === $isInheritanceValid && true === $isPackValid) { return true; } /** * Some of them have returned an int value. There is a problem with * stock, so we need to check which one is valid. */ return $isInheritStock ? $isInheritanceValid : $isPackValid; }