generated from Setono/SyliusPluginSkeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Distribute discount adjustments over units
- Loading branch information
Showing
12 changed files
with
287 additions
and
25 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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusOrderEditPlugin\Adder; | ||
|
||
use Sylius\Component\Core\Distributor\IntegerDistributorInterface; | ||
use Sylius\Component\Core\Model\OrderItemInterface; | ||
use Sylius\Component\Order\Factory\AdjustmentFactoryInterface; | ||
|
||
final class DiscountAdjustmentsAdder implements DiscountAdjustmentsAdderInterface | ||
{ | ||
public function __construct( | ||
private readonly IntegerDistributorInterface $integerDistributor, | ||
private readonly AdjustmentFactoryInterface $adjustmentFactory, | ||
) { | ||
} | ||
|
||
public function add(OrderItemInterface $orderItem, string $adjustmentType, int $discount): void | ||
{ | ||
$discounts = $this->integerDistributor->distribute($discount, $orderItem->getQuantity()); | ||
$units = $orderItem->getUnits(); | ||
|
||
foreach ($discounts as $i => $discount) { | ||
$adjustment = $this->adjustmentFactory->createWithData( | ||
$adjustmentType, | ||
'Custom discount', | ||
$discount, | ||
); | ||
|
||
$units->get($i)->addAdjustment($adjustment); | ||
} | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusOrderEditPlugin\Adder; | ||
|
||
use Sylius\Component\Core\Model\AdjustmentInterface; | ||
use Sylius\Component\Core\Model\OrderItemInterface; | ||
|
||
interface DiscountAdjustmentsAdderInterface | ||
{ | ||
public function add(OrderItemInterface $orderItem, string $adjustmentType, int $discount): void; | ||
} |
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
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
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
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
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
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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusOrderEditPlugin\Setter; | ||
|
||
use Setono\SyliusOrderEditPlugin\Adder\DiscountAdjustmentsAdderInterface; | ||
use Setono\SyliusOrderEditPlugin\Model\AdjustmentTypes; | ||
use Sylius\Component\Core\Distributor\MinimumPriceDistributorInterface; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
|
||
final class OrderDiscountAdjustmentSetter implements OrderDiscountAdjustmentSetterInterface | ||
{ | ||
public function __construct( | ||
private readonly MinimumPriceDistributorInterface $minimumPriceDistributor, | ||
private readonly DiscountAdjustmentsAdderInterface $orderItemDiscountAdjustmentAdder, | ||
) { | ||
} | ||
|
||
public function set(OrderInterface $order, int $discount): void | ||
{ | ||
$channel = $order->getChannel(); | ||
$items = $order->getItems(); | ||
|
||
$distributedPrices = $this->minimumPriceDistributor->distribute($items->toArray(), $discount, $channel, true); | ||
|
||
foreach ($distributedPrices as $i => $distribution) { | ||
$this->orderItemDiscountAdjustmentAdder->add( | ||
$items->get($i), | ||
AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT, | ||
-$distribution, | ||
); | ||
} | ||
} | ||
} |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusOrderEditPlugin\Setter; | ||
|
||
use Sylius\Component\Core\Model\OrderInterface; | ||
|
||
interface OrderDiscountAdjustmentSetterInterface | ||
{ | ||
public function set(OrderInterface $order, int $discount): void; | ||
} |
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
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,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusOrderEditPlugin\Tests\Unit\Adder; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
use Setono\SyliusOrderEditPlugin\Adder\DiscountAdjustmentsAdder; | ||
use Setono\SyliusOrderEditPlugin\Model\AdjustmentTypes; | ||
use Sylius\Component\Core\Distributor\IntegerDistributorInterface; | ||
use Sylius\Component\Core\Model\AdjustmentInterface; | ||
use Sylius\Component\Core\Model\OrderItemInterface; | ||
use Sylius\Component\Core\Model\OrderItemUnitInterface; | ||
use Sylius\Component\Order\Factory\AdjustmentFactoryInterface; | ||
|
||
final class DiscountAdjustmentsAdderTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
public function testItAddsDiscountAdjustmentsOnOrderItemUnits(): void | ||
{ | ||
$integerDistributor = $this->prophesize(IntegerDistributorInterface::class); | ||
$adjustmentFactory = $this->prophesize(AdjustmentFactoryInterface::class); | ||
|
||
$item = $this->prophesize(OrderItemInterface::class); | ||
$firstUnit = $this->prophesize(OrderItemUnitInterface::class); | ||
$secondUnit = $this->prophesize(OrderItemUnitInterface::class); | ||
$thirdUnit = $this->prophesize(OrderItemUnitInterface::class); | ||
|
||
$adder = new DiscountAdjustmentsAdder( | ||
$integerDistributor->reveal(), | ||
$adjustmentFactory->reveal(), | ||
); | ||
|
||
$item->getQuantity()->willReturn(3); | ||
$item->getUnits()->willReturn(new ArrayCollection( | ||
[$firstUnit->reveal(), $secondUnit->reveal(), $thirdUnit->reveal()] | ||
)); | ||
|
||
$integerDistributor->distribute(-1000, 3)->willReturn([-333, -333, -334]); | ||
|
||
$firstAdjustment = $this->prophesize(AdjustmentInterface::class); | ||
$secondAdjustment = $this->prophesize(AdjustmentInterface::class); | ||
$thirdAdjustment = $this->prophesize(AdjustmentInterface::class); | ||
|
||
$adjustmentFactory | ||
->createWithData(AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT, 'Custom discount', -333) | ||
->willReturn($firstAdjustment->reveal(), $secondAdjustment->reveal()) | ||
; | ||
$adjustmentFactory | ||
->createWithData(AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT, 'Custom discount', -334,) | ||
->willReturn($thirdAdjustment->reveal()) | ||
; | ||
|
||
$firstUnit->addAdjustment($firstAdjustment->reveal())->shouldBeCalled(); | ||
$secondUnit->addAdjustment($secondAdjustment->reveal())->shouldBeCalled(); | ||
$thirdUnit->addAdjustment($thirdAdjustment->reveal())->shouldBeCalled(); | ||
|
||
$adder->add($item->reveal(), AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT, -1000); | ||
} | ||
} |
Oops, something went wrong.