Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order item discounts #6

Merged
merged 4 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/Form/Type/CustomDiscountCollectionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusOrderEditPlugin\Form\Type;

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;

abstract class CustomDiscountCollectionType extends AbstractType
{
public function __construct(
protected readonly AdjustmentFactoryInterface $adjustmentFactory,
protected readonly string $label,
protected readonly string $adjustmentType,
) {
}

public function getParent(): string
{
return CollectionType::class;
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'entry_type' => OrderDiscountType::class,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'label' => 'sylius.form.order.discounts',
'entry_options' => [
'label' => false,
],
'getter' => function (AdjustableInterface &$adjustable): array {
$adjustments = $adjustable->getAdjustments($this->adjustmentType)->toArray();

return array_map(function (AdjustmentInterface $adjustment): int {
return -1 * $adjustment->getAmount();
}, $adjustments);
},
'setter' => function (AdjustableInterface &$adjustable, array $discounts): void {
$adjustable->removeAdjustments($this->adjustmentType);

/** @var int $discount */
foreach ($discounts as $discount) {
$adjustment = $this->adjustmentFactory->createWithData(
$this->adjustmentType,
$this->label,
-1 * $discount,
);
$adjustable->addAdjustment($adjustment);
}
},
]);
}
}
49 changes: 3 additions & 46 deletions src/Form/Type/OrderDiscountCollectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,12 @@
namespace Setono\SyliusOrderEditPlugin\Form\Type;

use Setono\SyliusOrderEditPlugin\Model\AdjustmentTypes;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Order\Factory\AdjustmentFactoryInterface;
use Sylius\Component\Order\Model\AdjustmentInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class OrderDiscountCollectionType extends AbstractType
final class OrderDiscountCollectionType extends CustomDiscountCollectionType
{
public function __construct(private readonly AdjustmentFactoryInterface $adjustmentFactory)
public function __construct(AdjustmentFactoryInterface $adjustmentFactory)
{
}

public function getParent(): string
{
return CollectionType::class;
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'entry_type' => OrderDiscountType::class,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'label' => 'sylius.form.order.discounts',
'entry_options' => [
'label' => false,
],
'getter' => function (OrderInterface &$order): array {
$adjustments = $order->getAdjustments(AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT)->toArray();

return array_map(function (AdjustmentInterface $adjustment): int {
return -1 * $adjustment->getAmount();
}, $adjustments);
},
'setter' => function (OrderInterface &$order, array $discounts): void {
$order->removeAdjustments(AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT);

/** @var int $discount */
foreach ($discounts as $discount) {
$adjustment = $this->adjustmentFactory->createWithData(
AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT,
'Custom discount',
-1 * $discount,
);
$order->addAdjustment($adjustment);
}
},
]);
parent::__construct($adjustmentFactory, 'Custom discount', AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT);
}
}
16 changes: 16 additions & 0 deletions src/Form/Type/OrderItemDiscountCollectionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusOrderEditPlugin\Form\Type;

use Setono\SyliusOrderEditPlugin\Model\AdjustmentTypes;
use Sylius\Component\Order\Factory\AdjustmentFactoryInterface;

final class OrderItemDiscountCollectionType extends CustomDiscountCollectionType
{
public function __construct(AdjustmentFactoryInterface $adjustmentFactory)
{
parent::__construct($adjustmentFactory, 'Custom item discount', AdjustmentTypes::SETONO_ADMIN_ORDER_ITEM_DISCOUNT);
}
}
16 changes: 16 additions & 0 deletions src/Form/Type/OrderItemDiscountType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusOrderEditPlugin\Form\Type;

use Sylius\Bundle\MoneyBundle\Form\Type\MoneyType;
use Symfony\Component\Form\AbstractType;

final class OrderItemDiscountType extends AbstractType
{
public function getParent(): string

Check warning on line 12 in src/Form/Type/OrderItemDiscountType.php

View check run for this annotation

Codecov / codecov/patch

src/Form/Type/OrderItemDiscountType.php#L12

Added line #L12 was not covered by tests
{
return MoneyType::class;

Check warning on line 14 in src/Form/Type/OrderItemDiscountType.php

View check run for this annotation

Codecov / codecov/patch

src/Form/Type/OrderItemDiscountType.php#L14

Added line #L14 was not covered by tests
}
}
31 changes: 31 additions & 0 deletions src/Form/Type/OrderItemType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
namespace Setono\SyliusOrderEditPlugin\Form\Type;

use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Core\Model\ProductVariant;
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

final class OrderItemType extends AbstractResourceType
{
Expand Down Expand Up @@ -40,5 +43,33 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'choice_label' => 'code',
])
;

$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void {
$form = $event->getForm();
/** @var OrderItemInterface|null $orderItem */
$orderItem = $event->getData();
if ($orderItem === null) {
return;
}

/** @var OrderInterface $order */
$order = $orderItem->getOrder();

$form->add('discounts', OrderItemDiscountCollectionType::class, [
'property_path' => 'adjustments',
'entry_options' => [
'currency' => $order->getCurrencyCode(),
],
]);
});

$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event): void {
/** @var array $orderItem */
$orderItem = $event->getData();
if (!isset($orderItem['discounts'])) {
$orderItem['discounts'] = [];
}
$event->setData($orderItem);
});
}
}
2 changes: 2 additions & 0 deletions src/Model/AdjustmentTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ final class AdjustmentTypes
{
public const SETONO_ADMIN_ORDER_DISCOUNT = 'setono_admin_order_discount';

public const SETONO_ADMIN_ORDER_ITEM_DISCOUNT = 'setono_admin_order_item_discount';

private function __construct()
{
}
Expand Down
8 changes: 8 additions & 0 deletions src/Resources/config/services/form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
<tag name="form.type"/>
</service>

<service
id="setono_sylius_order_edit.form.type.order_item_discount_collection"
class="Setono\SyliusOrderEditPlugin\Form\Type\OrderItemDiscountCollectionType"
>
<argument type="service" id="sylius.factory.adjustment" />
<tag name="form.type"/>
</service>

<!-- Form type extensions -->
<service id="setono_sylius_order_edit.form.extension.order"
class="Setono\SyliusOrderEditPlugin\Form\Extension\OrderTypeExtension">
Expand Down
1 change: 1 addition & 0 deletions src/Resources/translations/messages.en.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
setono_sylius_order_edit:
ui:
discounts: Discounts
order_discounts: Order discounts
order_items: Order items
4 changes: 4 additions & 0 deletions src/Resources/views/admin/order/update/_order_items.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
</button>
</td>
</tr>
<tr>
<td colspan="5">{{ form_widget(itemForm.discounts) }}</td>
<td></td>
</tr>
{% endfor %}
</tbody>
<tfoot>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{% import "@SyliusAdmin/Common/Macro/money.html.twig" as money %}

{% set orderPromotionAdjustment = constant('Sylius\\Component\\Core\\Model\\AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT') %}
{% set unitPromotionAdjustment = constant('Sylius\\Component\\Core\\Model\\AdjustmentInterface::ORDER_UNIT_PROMOTION_ADJUSTMENT') %}
{% 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') %}

{% set variant = item.variant %}
{% set product = variant.product %}

{% set aggregatedUnitPromotionAdjustments = item.getAdjustmentsTotalRecursively(unitPromotionAdjustment) + item.getAdjustmentsTotalRecursively(orderPromotionAdjustment) %}
{% set subtotal = (item.unitPrice * item.quantity) + aggregatedUnitPromotionAdjustments %}

{% set taxIncluded = sylius_admin_order_unit_tax_included(item) %}
{% set taxExcluded = sylius_admin_order_unit_tax_excluded(item) %}

<tr>
<td class="single line">
{% include '@SyliusAdmin/Product/_info.html.twig' %}
</td>
<td class="right aligned unit-price">
{{ money.format(item.unitPrice, order.currencyCode) }}
</td>
<td class="right aligned unit-discount">
{{ money.format(item.units.first.adjustmentsTotal(unitPromotionAdjustment), 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>
</td>
<td class="right aligned discounted-unit-price">
{{ money.format(item.fullDiscountedUnitPrice, order.currencyCode) }}
</td>
<td class="right aligned quantity">
{{ item.quantity }}
</td>
<td class="right aligned subtotal">
{{ money.format(subtotal, order.currencyCode) }}
</td>
<td class="right aligned tax">
<div class="tax-excluded">{{ money.format(taxExcluded, order.currencyCode) }}</div>
<div class="tax-disabled">
<div class="tax-included"> {{ money.format(taxIncluded, order.currencyCode) }}
</div>
<small>({{ 'sylius.ui.included_in_price'|trans }})</small>
</div>
</td>
<td class="right aligned total">
{{ money.format(item.total, order.currencyCode) }}
</td>
</tr>
<tr>
<td colspan="9">
<strong>{{ 'setono_sylius_order_edit.ui.discounts'|trans }}:</strong>
{% for discount in item.getAdjustments(adminOrderItemDiscountAdjustment) %}
{{ money.format(discount.amount, order.currencyCode) }}{% if not loop.last %}, {% endif %}
{% endfor %}
</td>
</tr>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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\\OrderEditDiscountTypes::SETONO_ADMIN_ORDER_DISCOUNT') %}
{% set adminOrderDiscountAdjustment = constant('Setono\\SyliusOrderEditPlugin\\Model\\AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT') %}

<tr>
<td colspan="5" id="promotion-discounts" class="promotion-disabled">
Expand Down
Loading
Loading