diff --git a/src/Inventory/Checker/OrderItemAvailabilityChecker.php b/src/Inventory/Checker/OrderItemAvailabilityChecker.php index c48e6ec4..4a470a7d 100644 --- a/src/Inventory/Checker/OrderItemAvailabilityChecker.php +++ b/src/Inventory/Checker/OrderItemAvailabilityChecker.php @@ -14,7 +14,6 @@ use BitBag\SyliusProductBundlePlugin\Entity\ProductInterface; use Sylius\Component\Core\Inventory\Checker\OrderItemAvailabilityCheckerInterface; use Sylius\Component\Core\Model\OrderItemInterface as BaseOrderItemInterface; -use Sylius\Component\Core\Model\ProductVariantInterface; final class OrderItemAvailabilityChecker implements OrderItemAvailabilityCheckerInterface { @@ -31,10 +30,8 @@ public function isReservedStockSufficient(BaseOrderItemInterface $orderItem): bo return $this->decorated->isReservedStockSufficient($orderItem); } - /** @var ProductVariantInterface $variant */ - $variant = $orderItem->getVariant(); /** @var ProductInterface $product */ - $product = $variant->getProduct(); + $product = $orderItem->getProduct(); if (!$product->isBundle()) { return $this->decorated->isReservedStockSufficient($orderItem); } diff --git a/src/Inventory/Checker/ProductBundleOrderItemAvailabilityChecker.php b/src/Inventory/Checker/ProductBundleOrderItemAvailabilityChecker.php index ecf3086c..ccea2370 100644 --- a/src/Inventory/Checker/ProductBundleOrderItemAvailabilityChecker.php +++ b/src/Inventory/Checker/ProductBundleOrderItemAvailabilityChecker.php @@ -24,7 +24,7 @@ public function areOrderedBundledProductVariantsAvailable(BaseOrderItemInterface } foreach ($orderItem->getProductBundleOrderItems() as $bundleOrderItem) { - $quantity = $orderItem->getQuantity() * $bundleOrderItem->getQuantity(); + $quantity = $orderItem->getQuantity() * (int) $bundleOrderItem->getQuantity(); /** @var ProductVariantInterface $variant */ $variant = $bundleOrderItem->getProductVariant(); if (!$variant->isTracked()) { diff --git a/src/Inventory/Operator/OrderInventoryOperator.php b/src/Inventory/Operator/OrderInventoryOperator.php index c8bcbec4..6908bf34 100644 --- a/src/Inventory/Operator/OrderInventoryOperator.php +++ b/src/Inventory/Operator/OrderInventoryOperator.php @@ -69,34 +69,17 @@ public function hold(OrderInterface $order): void $this->productBundleOrderInventoryOperator->hold($order); } - //TODO public function sell(OrderInterface $order): void { $this->lockOrderProductVariants($order); if (!$this->featureFlagChecker->isEnabled()) { $this->decorated->sell($order); - } - - /** @var BaseOrderItemInterface $orderItem */ - foreach ($order->getItems() as $orderItem) { - $variant = $orderItem->getVariant(); - - if (!$variant->isTracked()) { - continue; - } - if (($variant->getOnHold() - $orderItem->getQuantity()) < 0) { - throw new NotEnoughUnitsOnHoldException($variant->getName()); - } - - if (($variant->getOnHand() - $orderItem->getQuantity()) < 0) { - throw new NotEnoughUnitsOnHandException($variant->getName()); - } - - $variant->setOnHold($variant->getOnHold() - $orderItem->getQuantity()); - $variant->setOnHand($variant->getOnHand() - $orderItem->getQuantity()); + return; } + + $this->productBundleOrderInventoryOperator->sell($order); } /** diff --git a/src/Inventory/Operator/ProductBundleOrderInventoryOperator.php b/src/Inventory/Operator/ProductBundleOrderInventoryOperator.php index 296afc34..a88a50f9 100644 --- a/src/Inventory/Operator/ProductBundleOrderInventoryOperator.php +++ b/src/Inventory/Operator/ProductBundleOrderInventoryOperator.php @@ -13,6 +13,8 @@ use BitBag\SyliusProductBundlePlugin\Entity\OrderItemInterface; use BitBag\SyliusProductBundlePlugin\Entity\ProductInterface; +use Sylius\Component\Core\Inventory\Exception\NotEnoughUnitsOnHandException; +use Sylius\Component\Core\Inventory\Exception\NotEnoughUnitsOnHoldException; use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\Model\ProductVariantInterface; @@ -34,7 +36,15 @@ public function hold(OrderInterface $order): void public function sell(OrderInterface $order): void { - // TODO: Implement sell() method. + foreach ($order->getItems() as $orderItem) { + /** @var ProductInterface $product */ + $product = $orderItem->getProduct(); + if ($product->isBundle()) { + $this->sellBundleOrderItem($orderItem); + } else { + $this->sellRegularOrderItem($orderItem); + } + } } private function holdBundleOrderItem(OrderItemInterface $orderItem): void @@ -63,4 +73,40 @@ private function holdProductVariant(ProductVariantInterface $variant, int $quant $variant->setOnHold((int) $variant->getOnHold() + $quantity); } + + private function sellBundleOrderItem(OrderItemInterface $orderItem): void + { + foreach ($orderItem->getProductBundleOrderItems() as $bundleOrderItem) { + $quantity = $orderItem->getQuantity() * $bundleOrderItem->getQuantity(); + $variant = $bundleOrderItem->getProductVariant(); + + $this->sellProductVariant($variant, $quantity); + } + } + + private function sellRegularOrderItem(OrderItemInterface $orderItem): void + { + $quantity = $orderItem->getQuantity(); + $variant = $orderItem->getVariant(); + + $this->sellProductVariant($variant, $quantity); + } + + private function sellProductVariant(ProductVariantInterface $variant, int $quantity): void + { + if (!$variant->isTracked()) { + return; + } + + if (($variant->getOnHold() - $quantity) < 0) { + throw new NotEnoughUnitsOnHoldException($variant->getName()); + } + + if (($variant->getOnHand() - $quantity) < 0) { + throw new NotEnoughUnitsOnHandException($variant->getName()); + } + + $variant->setOnHold($variant->getOnHold() - $quantity); + $variant->setOnHand($variant->getOnHand() - $quantity); + } }