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

Add store notes #23

Merged
merged 1 commit into from
Jun 26, 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
7 changes: 7 additions & 0 deletions src/DependencyInjection/SetonoSyliusOrderEditExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@
],
],
],
'sylius.admin.order.show.summary' => [
'blocks' => [
'store_notes' => [
'template' => '@SetonoSyliusOrderEditPlugin/admin/order/show/_store_notes.html.twig',
],
],
],

Check warning on line 60 in src/DependencyInjection/SetonoSyliusOrderEditExtension.php

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/SetonoSyliusOrderEditExtension.php#L54-L60

Added lines #L54 - L60 were not covered by tests
],
]);

Expand Down
4 changes: 4 additions & 0 deletions src/Entity/EditableOrderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@

use Sylius\Component\Core\Model\OrderInterface;

interface EditableOrderInterface extends OrderInterface

Check failure on line 9 in src/Entity/EditableOrderInterface.php

View workflow job for this annotation

GitHub Actions / Backwards Compatibility Check

Method getStoreNotes() was added to interface Setono\SyliusOrderEditPlugin\Entity\EditableOrderInterface

Check failure on line 9 in src/Entity/EditableOrderInterface.php

View workflow job for this annotation

GitHub Actions / Backwards Compatibility Check

Method setStoreNotes() was added to interface Setono\SyliusOrderEditPlugin\Entity\EditableOrderInterface
{
public function isAlreadyPaid(): bool;

public function getInitialTotal(): int;

public function setInitialTotal(int $initialTotal): void;

public function getStoreNotes(): ?string;

public function setStoreNotes(?string $storeNotes): void;
}
14 changes: 14 additions & 0 deletions src/Entity/EditableOrderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ trait EditableOrderTrait
#[ORM\Column(type: 'integer')]
private int $initialTotal = 0;

/** @ORM\Column(type="text", nullable=true) */
#[ORM\Column(type: 'text', nullable: true)]
private ?string $storeNotes = null;

public function isAlreadyPaid(): bool
{
return $this->getPaymentState() === OrderPaymentStates::STATE_PAID;
Expand All @@ -29,4 +33,14 @@ public function setInitialTotal(int $initialTotal): void
{
$this->initialTotal = $initialTotal;
}

public function getStoreNotes(): ?string
{
return $this->storeNotes;
}

public function setStoreNotes(?string $storeNotes): void
{
$this->storeNotes = $storeNotes;
}
}
24 changes: 16 additions & 8 deletions src/Form/Extension/OrderTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Sylius\Bundle\OrderBundle\Form\Type\OrderType;
use Sylius\Component\Core\Model\OrderInterface;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
Expand All @@ -17,6 +18,14 @@ final class OrderTypeExtension extends AbstractTypeExtension
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('storeNotes', TextareaType::class, [
'required' => false,
'label' => 'setono_sylius_order_edit.ui.store_notes',
'attr' => [
'placeholder' => 'setono_sylius_order_edit.ui.store_notes_placeholder',
],
]);

$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void {
$form = $event->getForm();
/** @var OrderInterface $order */
Expand All @@ -26,15 +35,14 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
->add('items', OrderItemCollectionType::class, [
'entry_options' => ['currency_code' => $order->getCurrencyCode()],
])
->add('discounts', OrderDiscountCollectionType::class, [
'property_path' => 'adjustments',
'entry_options' => [
'currency' => $order->getCurrencyCode(),
],
'button_add_label' => 'setono_sylius_order_edit.ui.add_discount',
])
;

$form->add('discounts', OrderDiscountCollectionType::class, [
'property_path' => 'adjustments',
'entry_options' => [
'currency' => $order->getCurrencyCode(),
],
'button_add_label' => 'setono_sylius_order_edit.ui.add_discount',
]);
});

$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event): void {
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/translations/messages.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ setono_sylius_order_edit:
discounts: Discounts
order_discounts: Order discounts
order_items: Order items
store_notes: Store notes
store_notes_placeholder: If you want to add a note regarding your changes, you can do it here. The customer will not be able to see this note...
10 changes: 10 additions & 0 deletions src/Resources/views/admin/order/show/_store_notes.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% if order.storeNotes is not null %}
<div style="margin-top: 1rem">
<h4 class="ui top attached styled header">
{{ 'setono_sylius_order_edit.ui.store_notes'|trans }}
</h4>
<div class="ui attached segment text-break">
{{ order.storeNotes }}
</div>
</div>
{% endif %}
3 changes: 3 additions & 0 deletions src/Resources/views/admin/order/update/_store_notes.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="ui segment">
{{ form_row(form.storeNotes) }}
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<input type="hidden" name="_method" value="PATCH" />

{{ include('@SetonoSyliusOrderEditPlugin/admin/order/update/_order_items.html.twig') }}
{{ include('@SetonoSyliusOrderEditPlugin/admin/order/update/_store_notes.html.twig') }}

<div class="ui grid">
<div class="eight wide column">
Expand Down
40 changes: 37 additions & 3 deletions tests/Functional/OrderUpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Setono\SyliusOrderEditPlugin\Entity\EditableOrderInterface;
use Setono\SyliusOrderEditPlugin\Model\AdjustmentTypes;
use Setono\SyliusOrderEditPlugin\Tests\Application\Entity\Order;
use Sylius\Bundle\ApiBundle\Command\Cart\AddItemToCart;
use Sylius\Bundle\ApiBundle\Command\Cart\PickupCart;
use Sylius\Bundle\ApiBundle\Command\Checkout\ChoosePaymentMethod;
Expand All @@ -15,7 +16,6 @@
use Sylius\Bundle\ApiBundle\Command\Checkout\UpdateCart;
use Sylius\Component\Core\Model\Address;
use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Core\Model\Order;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
Expand Down Expand Up @@ -190,6 +190,24 @@ public function testItAllowsToAddAndRemoveDiscountsForTheOrderItemMultipleTimes(
);
}

public function testItAllowsToAddStoreNotes(): void
{
$this->makeVariantTrackedWithStockAndPrice('000F_office_grey_jeans-variant-0', 100);

$order = $this->placeOrderProgramatically(quantity: 5);

$this->loginAsAdmin();
$this->addStoreNotes($order->getId(), 'store notes');

self::assertResponseStatusCodeSame(302);

$this->getEntityManager()->clear();

/** @var EditableOrderInterface $order */
$order = $this->getOrderRepository()->findOneBy(['tokenValue' => 'TOKEN']);
self::assertSame('store notes', $order->getStoreNotes());
}
loevgaard marked this conversation as resolved.
Show resolved Hide resolved

private function placeOrderProgramatically(
string $variantCode = '000F_office_grey_jeans-variant-0',
int $quantity = 1,
Expand Down Expand Up @@ -346,6 +364,22 @@ private function addDiscountsToOrderItem(int $orderId, array $discounts): void
);
}

private function addStoreNotes(int $orderId, ?string $storeNotes): void
{
static::$client->request(
'PATCH',
sprintf('/admin/orders/%d/update-and-process', $orderId),
[],
[],
['CONTENT_TYPE' => 'application/json'],
json_encode([
'sylius_order' => [
'storeNotes' => $storeNotes,
],
]),
);
}

private function getOrderRepository(): OrderRepositoryInterface
{
return self::getContainer()->get('sylius.repository.order');
Expand All @@ -361,12 +395,12 @@ private function getEntityManager(): EntityManagerInterface
return self::getContainer()->get('doctrine.orm.entity_manager');
}

private function getInitialTotal(Order|InitialTotalAwareOrderInterface $order)
private function getInitialTotal(Order $order): int
{
return $order->getInitialTotal() - $order->getAdjustmentsTotalRecursively(AdjustmentInterface::TAX_ADJUSTMENT);
}

private function getResultTotal(Order|InitialTotalAwareOrderInterface $order)
private function getResultTotal(Order $order): int
{
return $order->getTotal() - $order->getAdjustmentsTotalRecursively(AdjustmentInterface::TAX_ADJUSTMENT);
}
Expand Down
30 changes: 30 additions & 0 deletions tests/Unit/Entity/EditableOrderTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusOrderEditPlugin\Tests\Unit\Entity;

use PHPUnit\Framework\TestCase;
use Setono\SyliusOrderEditPlugin\Tests\Application\Entity\Order;

final class EditableOrderTraitTest extends TestCase
loevgaard marked this conversation as resolved.
Show resolved Hide resolved
{
public function testItAllowsSettingInitialTotal(): void
{
$order = new Order();
$order->setInitialTotal(1000);

self::assertSame(1000, $order->getInitialTotal());
}

public function testItAllowsSettingStoreNotesAndNullingThem(): void
{
$order = new Order();
$order->setStoreNotes('Store notes');

self::assertSame('Store notes', $order->getStoreNotes());

$order->setStoreNotes(null);
self::assertNull($order->getStoreNotes());
}
}
Loading