Skip to content

Commit

Permalink
OP-289: On-hold&on-hand handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hmfilar committed Sep 16, 2024
1 parent 917c6f9 commit 20ac6e5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 26 deletions.
5 changes: 1 addition & 4 deletions src/Inventory/Checker/OrderItemAvailabilityChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
23 changes: 3 additions & 20 deletions src/Inventory/Operator/OrderInventoryOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
48 changes: 47 additions & 1 deletion src/Inventory/Operator/ProductBundleOrderInventoryOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 20ac6e5

Please sign in to comment.