Skip to content

Commit

Permalink
Do not use minimum price distributor
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed Jun 27, 2024
1 parent 49aaa78 commit a24fb0e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/Resources/config/services/order_processing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
id="setono_sylius_order_edit.setter.order_discount_adjustment"
class="Setono\SyliusOrderEditPlugin\Setter\OrderDiscountAdjustmentSetter"
>
<argument type="service" id="Sylius\Component\Core\Distributor\MinimumPriceDistributorInterface" />
<argument type="service" id="sylius.integer_distributor" />
<argument type="service" id="setono_sylius_order_edit.adder.discount_adjustments" />
</service>
</services>
Expand Down
9 changes: 3 additions & 6 deletions src/Setter/OrderDiscountAdjustmentSetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,25 @@

use Setono\SyliusOrderEditPlugin\Adder\DiscountAdjustmentsAdderInterface;
use Setono\SyliusOrderEditPlugin\Model\AdjustmentTypes;
use Sylius\Component\Core\Distributor\MinimumPriceDistributorInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Distributor\IntegerDistributorInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\OrderItemInterface;

final class OrderDiscountAdjustmentSetter implements OrderDiscountAdjustmentSetterInterface
{
public function __construct(
private readonly MinimumPriceDistributorInterface $minimumPriceDistributor,
private readonly IntegerDistributorInterface $integerDistributor,
private readonly DiscountAdjustmentsAdderInterface $orderItemDiscountAdjustmentAdder,
) {
}

public function set(OrderInterface $order, int $discount): void
{
/** @var ChannelInterface $channel */
$channel = $order->getChannel();
$items = $order->getItems();
/** @var int $orderId */
$orderId = $order->getId();

$distributedPrices = $this->minimumPriceDistributor->distribute($items->toArray(), $discount, $channel, true);
$distributedPrices = $this->integerDistributor->distribute($discount, $items->count());

/** @var int $distribution */
foreach ($distributedPrices as $i => $distribution) {
Expand Down
19 changes: 8 additions & 11 deletions tests/Unit/Setter/OrderDiscountAdjustmentSetterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
use Setono\SyliusOrderEditPlugin\Entity\EditableOrderInterface;
use Setono\SyliusOrderEditPlugin\Model\AdjustmentTypes;
use Setono\SyliusOrderEditPlugin\Setter\OrderDiscountAdjustmentSetter;
use Sylius\Component\Core\Distributor\MinimumPriceDistributorInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Distributor\IntegerDistributorInterface;
use Sylius\Component\Core\Model\OrderItemInterface;

final class OrderDiscountAdjustmentSetterTest extends TestCase
Expand All @@ -21,34 +20,32 @@ final class OrderDiscountAdjustmentSetterTest extends TestCase

public function testItDistributesDiscountsOnOrderItemUnits(): void
{
$minimumPriceDistributor = $this->prophesize(MinimumPriceDistributorInterface::class);
$integerDistributor = $this->prophesize(IntegerDistributorInterface::class);
$orderItemDiscountAdjustmentAdder = $this->prophesize(DiscountAdjustmentsAdderInterface::class);

$setter = new OrderDiscountAdjustmentSetter(
$minimumPriceDistributor->reveal(),
$integerDistributor->reveal(),
$orderItemDiscountAdjustmentAdder->reveal(),
);

$order = $this->prophesize(EditableOrderInterface::class);
$firstItem = $this->prophesize(OrderItemInterface::class);
$secondItem = $this->prophesize(OrderItemInterface::class);
$channel = $this->prophesize(ChannelInterface::class);
$order->getItems()->willReturn(new ArrayCollection([$firstItem->reveal(), $secondItem->reveal()]));
$order->getChannel()->willReturn($channel->reveal());
$order->getId()->willReturn(100);

$minimumPriceDistributor
->distribute([$firstItem->reveal(), $secondItem->reveal()], 1000, $channel->reveal(), true)
->willReturn([400, 600])
$integerDistributor
->distribute(1000, 2)
->willReturn([500, 500])
;

$orderItemDiscountAdjustmentAdder
->add($firstItem->reveal(), AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT, AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT . '_100', 'Custom order discount', -400)
->add($firstItem->reveal(), AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT, AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT . '_100', 'Custom order discount', -500)
->shouldBeCalled()
;

$orderItemDiscountAdjustmentAdder
->add($secondItem->reveal(), AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT, AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT . '_100', 'Custom order discount', -600)
->add($secondItem->reveal(), AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT, AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT . '_100', 'Custom order discount', -500)
->shouldBeCalled()
;

Expand Down

0 comments on commit a24fb0e

Please sign in to comment.