Skip to content

Commit

Permalink
Display custom discounts properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed Jun 26, 2024
1 parent d6f3b60 commit 2003840
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 29 deletions.
7 changes: 7 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@
<function name="var_dump"/>
<function name="print_r"/>
</forbiddenFunctions>
<issueHandlers>
<UndefinedInterfaceMethod>
<errorLevel type="suppress">
<file name="src/Form/Type/CustomDiscountCollectionType.php"/>
</errorLevel>
</UndefinedInterfaceMethod>
</issueHandlers>
</psalm>
12 changes: 9 additions & 3 deletions src/Adder/DiscountAdjustmentsAdder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ public function __construct(
) {
}

public function add(OrderItemInterface $orderItem, string $adjustmentType, int $discount): void
{
public function add(
OrderItemInterface $orderItem,
string $adjustmentType,
string $originCode,
string $label,
int $discount,
): void {
$discounts = $this->integerDistributor->distribute($discount, $orderItem->getQuantity());
$units = $orderItem->getUnits();

Expand All @@ -28,9 +33,10 @@ public function add(OrderItemInterface $orderItem, string $adjustmentType, int $
/** @var AdjustmentInterface $adjustment */
$adjustment = $this->adjustmentFactory->createWithData(
$adjustmentType,
'Custom discount',
$label,
$discount,
);
$adjustment->setOriginCode($originCode);

/** @var OrderItemUnitInterface $unit */
$unit = $units->get($i);
Expand Down
8 changes: 7 additions & 1 deletion src/Adder/DiscountAdjustmentsAdderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,11 @@

interface DiscountAdjustmentsAdderInterface
{
public function add(OrderItemInterface $orderItem, string $adjustmentType, int $discount): void;
public function add(
OrderItemInterface $orderItem,
string $adjustmentType,
string $originCode,
string $label,
int $discount,
): void;
}
27 changes: 23 additions & 4 deletions src/Form/Type/CustomDiscountCollectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

namespace Setono\SyliusOrderEditPlugin\Form\Type;

use Doctrine\Common\Collections\Collection;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Order\Factory\AdjustmentFactoryInterface;
use Sylius\Component\Order\Model\AdjustableInterface;
use Sylius\Component\Order\Model\AdjustmentInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Webmozart\Assert\Assert;

abstract class CustomDiscountCollectionType extends AbstractType
{
Expand Down Expand Up @@ -37,11 +41,26 @@ public function configureOptions(OptionsResolver $resolver): void
'label' => false,
],
'getter' => function (AdjustableInterface &$adjustable): array {
$adjustments = $adjustable->getAdjustments($this->adjustmentType)->toArray();
Assert::isInstanceOfAny($adjustable, [OrderInterface::class, OrderItemInterface::class]);
/** @var Collection $adjustments */
$adjustments = $adjustable->getAdjustmentsRecursively($this->adjustmentType);

return array_map(function (AdjustmentInterface $adjustment): int {
return -1 * $adjustment->getAmount();
}, $adjustments);
$notDistributedAdjustments = [];
/** @var AdjustmentInterface $adjustment */
foreach ($adjustments as $adjustment) {
/** @var string $originCode */
$originCode = $adjustment->getOriginCode();

if (isset($notDistributedAdjustments[$originCode])) {
$notDistributedAdjustments[$originCode] += ($adjustment->getAmount()) * -1;

continue;
}

$notDistributedAdjustments[$originCode] = ($adjustment->getAmount()) * -1;
}

return $notDistributedAdjustments;
},
'setter' => function (AdjustableInterface &$adjustable, array $discounts): void {
$this->setDiscounts($adjustable, $discounts);
Expand Down
5 changes: 4 additions & 1 deletion src/Form/Type/OrderItemDiscountCollectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Setono\SyliusOrderEditPlugin\Form\Type;

use Ramsey\Uuid\Uuid;
use Setono\SyliusOrderEditPlugin\Adder\DiscountAdjustmentsAdderInterface;
use Setono\SyliusOrderEditPlugin\Model\AdjustmentTypes;
use Sylius\Component\Core\Model\OrderItemInterface;
Expand All @@ -26,9 +27,11 @@ public function setDiscounts(AdjustableInterface $adjustable, array $discounts):

$adjustable->removeAdjustmentsRecursively($this->adjustmentType);

$originCode = Uuid::uuid4()->toString();

/** @var int $discount */
foreach ($discounts as $discount) {
$this->discountAdjustmentsAdder->add($adjustable, $this->adjustmentType, -$discount);
$this->discountAdjustmentsAdder->add($adjustable, $this->adjustmentType, $originCode, 'Custom item discount', -$discount);
}
}
}
5 changes: 5 additions & 0 deletions src/Setter/OrderDiscountAdjustmentSetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Setono\SyliusOrderEditPlugin\Setter;

use Ramsey\Uuid\Uuid;
use Setono\SyliusOrderEditPlugin\Adder\DiscountAdjustmentsAdderInterface;
use Setono\SyliusOrderEditPlugin\Model\AdjustmentTypes;
use Sylius\Component\Core\Distributor\MinimumPriceDistributorInterface;
Expand All @@ -27,13 +28,17 @@ public function set(OrderInterface $order, int $discount): void

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

Check failure on line 29 in src/Setter/OrderDiscountAdjustmentSetter.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.1 | Deps: lowest | SF~5.4.0)

MixedAssignment

src/Setter/OrderDiscountAdjustmentSetter.php:29:9: MixedAssignment: Unable to determine the type that $distributedPrices is being assigned to (see https://psalm.dev/032)

Check failure on line 29 in src/Setter/OrderDiscountAdjustmentSetter.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.1 | Deps: lowest | SF~5.4.0)

UndefinedClass

src/Setter/OrderDiscountAdjustmentSetter.php:29:30: UndefinedClass: Class, interface or enum named Sylius\Component\Core\Distributor\MinimumPriceDistributorInterface does not exist (see https://psalm.dev/019)

Check failure on line 29 in src/Setter/OrderDiscountAdjustmentSetter.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.2 | Deps: lowest | SF~5.4.0)

MixedAssignment

src/Setter/OrderDiscountAdjustmentSetter.php:29:9: MixedAssignment: Unable to determine the type that $distributedPrices is being assigned to (see https://psalm.dev/032)

Check failure on line 29 in src/Setter/OrderDiscountAdjustmentSetter.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.2 | Deps: lowest | SF~5.4.0)

UndefinedClass

src/Setter/OrderDiscountAdjustmentSetter.php:29:30: UndefinedClass: Class, interface or enum named Sylius\Component\Core\Distributor\MinimumPriceDistributorInterface does not exist (see https://psalm.dev/019)

$originCode = Uuid::uuid4()->toString();

/** @var int $distribution */
foreach ($distributedPrices as $i => $distribution) {

Check failure on line 34 in src/Setter/OrderDiscountAdjustmentSetter.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.1 | Deps: lowest | SF~5.4.0)

MixedAssignment

src/Setter/OrderDiscountAdjustmentSetter.php:34:40: MixedAssignment: Unable to determine the type that $i is being assigned to (see https://psalm.dev/032)

Check failure on line 34 in src/Setter/OrderDiscountAdjustmentSetter.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.2 | Deps: lowest | SF~5.4.0)

MixedAssignment

src/Setter/OrderDiscountAdjustmentSetter.php:34:40: MixedAssignment: Unable to determine the type that $i is being assigned to (see https://psalm.dev/032)
/** @var OrderItemInterface $item */
$item = $items->get($i);

Check failure on line 36 in src/Setter/OrderDiscountAdjustmentSetter.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.1 | Deps: lowest | SF~5.4.0)

MixedArgument

src/Setter/OrderDiscountAdjustmentSetter.php:36:33: MixedArgument: Argument 1 of Doctrine\Common\Collections\Collection::get cannot be mixed, expecting array-key (see https://psalm.dev/030)

Check failure on line 36 in src/Setter/OrderDiscountAdjustmentSetter.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.2 | Deps: lowest | SF~5.4.0)

MixedArgument

src/Setter/OrderDiscountAdjustmentSetter.php:36:33: MixedArgument: Argument 1 of Doctrine\Common\Collections\Collection::get cannot be mixed, expecting array-key (see https://psalm.dev/030)
$this->orderItemDiscountAdjustmentAdder->add(
$item,
AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT,
$originCode,
'Custom order discount',
-$distribution,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

{% set orderPromotionAdjustment = constant('Sylius\\Component\\Core\\Model\\AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT') %}
{% set unitPromotionAdjustment = constant('Sylius\\Component\\Core\\Model\\AdjustmentInterface::ORDER_UNIT_PROMOTION_ADJUSTMENT') %}
{% set adminOrderDiscountAdjustment = constant('Setono\\SyliusOrderEditPlugin\\Model\\AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT') %}
{% set adminOrderItemDiscountAdjustment = constant('Setono\\SyliusOrderEditPlugin\\Model\\AdjustmentTypes::SETONO_ADMIN_ORDER_ITEM_DISCOUNT') %}
{% set shippingAdjustment = constant('Sylius\\Component\\Core\\Model\\AdjustmentInterface::SHIPPING_ADJUSTMENT') %}
{% set taxAdjustment = constant('Sylius\\Component\\Core\\Model\\AdjustmentInterface::TAX_ADJUSTMENT') %}
Expand All @@ -23,13 +24,22 @@
{{ money.format(item.unitPrice, order.currencyCode) }}
</td>
<td class="right aligned unit-discount">
{{ money.format(item.units.first.adjustmentsTotal(unitPromotionAdjustment), order.currencyCode) }}
{{ money.format(
item.units.first.adjustmentsTotal(unitPromotionAdjustment) + item.units.first.adjustmentsTotal(adminOrderItemDiscountAdjustment),
order.currencyCode
) }}
</td>
<td class="right aligned unit-order-discount">
<span style="font-style: italic;">~ {{ money.format(item.units.first.adjustmentsTotal(orderPromotionAdjustment), order.currencyCode) }}</span>
<span style="font-style: italic;">~ {{ money.format(
item.units.first.adjustmentsTotal(orderPromotionAdjustment) + item.units.first.adjustmentsTotal(adminOrderDiscountAdjustment),
order.currencyCode
) }}</span>
</td>
<td class="right aligned discounted-unit-price">
{{ money.format(item.fullDiscountedUnitPrice, order.currencyCode) }}
{{ money.format(
item.fullDiscountedUnitPrice + item.units.first.adjustmentsTotal(adminOrderItemDiscountAdjustment) + item.units.first.adjustmentsTotal(adminOrderDiscountAdjustment),
order.currencyCode
) }}
</td>
<td class="right aligned quantity">
{{ item.quantity }}
Expand All @@ -49,14 +59,3 @@
{{ money.format(item.total, order.currencyCode) }}
</td>
</tr>
{% set discounts = item.getAdjustments(adminOrderItemDiscountAdjustment) %}
{% if discounts is not empty %}
<tr>
<td colspan="9">
<strong>{{ 'setono_sylius_order_edit.ui.discounts'|trans }}:</strong>
{% for discount in discounts %}
{{ money.format(discount.amount, order.currencyCode) }}{% if not loop.last %}, {% endif %}
{% endfor %}
</td>
</tr>
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
{% set orderPromotionAdjustment = constant('Sylius\\Component\\Core\\Model\\AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT') %}
{% set unitPromotionAdjustment = constant('Sylius\\Component\\Core\\Model\\AdjustmentInterface::ORDER_UNIT_PROMOTION_ADJUSTMENT') %}
{% set adminOrderDiscountAdjustment = constant('Setono\\SyliusOrderEditPlugin\\Model\\AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT') %}
{% set adminOrderItemDiscountAdjustment = constant('Setono\\SyliusOrderEditPlugin\\Model\\AdjustmentTypes::SETONO_ADMIN_ORDER_ITEM_DISCOUNT') %}

<tr>
<td colspan="5" id="promotion-discounts" class="promotion-disabled">
{% set orderPromotionAdjustments = sylius_aggregate_adjustments(order.getAdjustmentsRecursively(orderPromotionAdjustment)) %}
{% set unitPromotionAdjustments = sylius_aggregate_adjustments(order.getAdjustmentsRecursively(unitPromotionAdjustment)) %}
{% set adminOrderDiscountAdjustments = sylius_aggregate_adjustments(order.getAdjustmentsRecursively(adminOrderDiscountAdjustment)) %}
{% set adminOrderItemDiscountAdjustments = sylius_aggregate_adjustments(order.getAdjustmentsRecursively(adminOrderItemDiscountAdjustment)) %}
{% set promotionAdjustments = orderPromotionAdjustments|merge(unitPromotionAdjustments)|merge(adminOrderDiscountAdjustments) %}
{% if not promotionAdjustments is empty %}
<div class="ui relaxed divided list">
Expand All @@ -28,7 +30,8 @@
{% set orderPromotionTotal = order.getAdjustmentsTotalRecursively(orderPromotionAdjustment) %}
{% set unitPromotionTotal = order.getAdjustmentsTotalRecursively(unitPromotionAdjustment) %}
{% set adminOrderDiscountTotal = order.getAdjustmentsTotalRecursively(adminOrderDiscountAdjustment) %}
{% set adminOrderItemDiscountTotal = order.getAdjustmentsTotalRecursively(adminOrderItemDiscountAdjustment) %}
<strong>{{ 'sylius.ui.promotion_total'|trans }}</strong>:
{{ money.format(orderPromotionTotal + unitPromotionTotal + adminOrderDiscountTotal, order.currencyCode) }}
{{ money.format(orderPromotionTotal + unitPromotionTotal + adminOrderDiscountTotal + adminOrderItemDiscountTotal, order.currencyCode) }}
</td>
</tr>
10 changes: 7 additions & 3 deletions tests/Unit/Adder/DiscountAdjustmentsAdderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,22 @@ public function testItAddsDiscountAdjustmentsOnOrderItemUnits(): void
$thirdAdjustment = $this->prophesize(AdjustmentInterface::class);

$adjustmentFactory
->createWithData(AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT, 'Custom discount', -333)
->createWithData(AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT, 'Label', -333)
->willReturn($firstAdjustment->reveal(), $secondAdjustment->reveal())
;
$adjustmentFactory
->createWithData(AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT, 'Custom discount', -334, )
->createWithData(AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT, 'Label', -334, )
->willReturn($thirdAdjustment->reveal())
;

$firstAdjustment->setOriginCode('ORIGIN_CODE')->shouldBeCalled();
$secondAdjustment->setOriginCode('ORIGIN_CODE')->shouldBeCalled();
$thirdAdjustment->setOriginCode('ORIGIN_CODE')->shouldBeCalled();

$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);
$adder->add($item->reveal(), AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT, 'ORIGIN_CODE', 'Label', -1000);
}
}
5 changes: 3 additions & 2 deletions tests/Unit/Setter/OrderDiscountAdjustmentSetterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\Common\Collections\ArrayCollection;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Setono\SyliusOrderEditPlugin\Adder\DiscountAdjustmentsAdderInterface;
use Setono\SyliusOrderEditPlugin\Entity\EditableOrderInterface;
Expand Down Expand Up @@ -42,12 +43,12 @@ public function testItDistributesDiscountsOnOrderItemUnits(): void
;

$orderItemDiscountAdjustmentAdder
->add($firstItem->reveal(), AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT, -400)
->add($firstItem->reveal(), AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT, Argument::type('string'), 'Custom order discount', -400)
->shouldBeCalled()
;

$orderItemDiscountAdjustmentAdder
->add($secondItem->reveal(), AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT, -600)
->add($secondItem->reveal(), AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT, Argument::type('string'), 'Custom order discount', -600)
->shouldBeCalled()
;

Expand Down

0 comments on commit 2003840

Please sign in to comment.