-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 1.12: [Inventory] Add a BC layer for refactored PaymentPreCompleteListener Add a note to UPGRADE file about changing PaymentPreCompleteListener's constructor [Inventory] Extract service for checking reserved stock availability for given order item
- Loading branch information
Showing
3 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Component\Core\Inventory\Checker; | ||
|
||
use Sylius\Component\Core\Model\OrderItemInterface; | ||
|
||
final class OrderItemAvailabilityChecker implements OrderItemAvailabilityCheckerInterface | ||
{ | ||
public function isReservedStockSufficient(OrderItemInterface $orderItem): bool | ||
{ | ||
$variant = $orderItem->getVariant(); | ||
if (!$variant->isTracked()) { | ||
return true; | ||
} | ||
|
||
$quantity = $orderItem->getQuantity(); | ||
|
||
return | ||
$variant->getOnHold() - $quantity >= 0 && | ||
$variant->getOnHand() - $quantity >= 0 | ||
; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
Inventory/Checker/OrderItemAvailabilityCheckerInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Component\Core\Inventory\Checker; | ||
|
||
use Sylius\Component\Core\Model\OrderItemInterface; | ||
|
||
interface OrderItemAvailabilityCheckerInterface | ||
{ | ||
public function isReservedStockSufficient(OrderItemInterface $orderItem): bool; | ||
} |
79 changes: 79 additions & 0 deletions
79
spec/Inventory/Checker/OrderItemAvailabilityCheckerSpec.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\Component\Core\Inventory\Checker; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Component\Core\Inventory\Checker\OrderItemAvailabilityCheckerInterface; | ||
use Sylius\Component\Core\Model\OrderItemInterface; | ||
use Sylius\Component\Core\Model\ProductVariantInterface; | ||
|
||
final class OrderItemAvailabilityCheckerSpec extends ObjectBehavior | ||
{ | ||
function it_implements_an_order_item_availability_checker_interface(): void | ||
{ | ||
$this->shouldImplement(OrderItemAvailabilityCheckerInterface::class); | ||
} | ||
|
||
function it_returns_true_if_variant_is_untracked( | ||
OrderItemInterface $orderItem, | ||
ProductVariantInterface $variant, | ||
): void { | ||
$orderItem->getVariant()->willReturn($variant); | ||
$variant->isTracked()->willReturn(false); | ||
|
||
$this->isReservedStockSufficient($orderItem)->shouldReturn(true); | ||
} | ||
|
||
function it_returns_true_if_stock_is_sufficient( | ||
OrderItemInterface $orderItem, | ||
ProductVariantInterface $variant, | ||
): void { | ||
$orderItem->getVariant()->willReturn($variant); | ||
$orderItem->getQuantity()->willReturn(2); | ||
|
||
$variant->isTracked()->willReturn(true); | ||
$variant->getOnHold()->willReturn(2); | ||
$variant->getOnHand()->willReturn(2); | ||
|
||
$this->isReservedStockSufficient($orderItem)->shouldReturn(true); | ||
} | ||
|
||
function it_returns_false_if_on_hold_value_is_not_sufficient( | ||
OrderItemInterface $orderItem, | ||
ProductVariantInterface $variant, | ||
): void { | ||
$orderItem->getVariant()->willReturn($variant); | ||
$orderItem->getQuantity()->willReturn(2); | ||
|
||
$variant->isTracked()->willReturn(true); | ||
$variant->getOnHold()->willReturn(1); | ||
$variant->getOnHand()->willReturn(2); | ||
|
||
$this->isReservedStockSufficient($orderItem)->shouldReturn(false); | ||
} | ||
|
||
function it_returns_false_if_on_hand_value_is_not_sufficient( | ||
OrderItemInterface $orderItem, | ||
ProductVariantInterface $variant, | ||
): void { | ||
$orderItem->getVariant()->willReturn($variant); | ||
$orderItem->getQuantity()->willReturn(2); | ||
|
||
$variant->isTracked()->willReturn(true); | ||
$variant->getOnHold()->willReturn(2); | ||
$variant->getOnHand()->willReturn(1); | ||
|
||
$this->isReservedStockSufficient($orderItem)->shouldReturn(false); | ||
} | ||
} |