diff --git a/README.md b/README.md index 7ba82be..613c061 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,33 @@ class PaymentMethod extends BasePaymentMethod implements ShipmondoPaymentMethodI } ``` +#### `PaymentMethod` entity + +```php + $paymentMethods */ $paymentMethods = $this->paymentMethodRepository->findAll(); + /** @var list $shippingMethods */ + $shippingMethods = $this->shippingMethodRepository->findAll(); + /** * @var list $shipmondoPaymentMethods */ @@ -46,13 +53,24 @@ public function index(Request $request): Response } } + /** + * @var list $shipmondoShipmentTemplates + */ + $shipmondoShipmentTemplates = []; + + foreach (Endpoint::paginate($this->client->shipmentTemplates()->get(...)) as $collection) { + foreach ($collection as $item) { + $shipmondoShipmentTemplates[] = $item; + } + } + // todo this should be made using Symfony forms if ($request->isMethod('POST') && $request->request->has('payment_methods')) { /** - * Example of posted array (the key is Sylius payment method id, they value is Shipmondo payment method id): + * Example of posted array (the key is a Sylius payment method id, the value is a Shipmondo payment method id): * * [ - * 1 => "1" + * 1 => "1", * 2 => "" * ] * @@ -64,21 +82,55 @@ public function index(Request $request): Response continue; } - /** @var PaymentMethodInterface|null $paymentMethod */ - $paymentMethod = $this->paymentMethodRepository->find($paymentMethodId); - if (null === $paymentMethod) { + /** @var PaymentMethodInterface|null $shippingMethod */ + $shippingMethod = $this->paymentMethodRepository->find($paymentMethodId); + if (null === $shippingMethod) { + continue; + } + + Assert::isInstanceOf($shippingMethod, PaymentMethodInterface::class); + $shippingMethod->setShipmondoId((int) $shipmondoPaymentMethodId); + $this->paymentMethodRepository->add($shippingMethod); + } + } + + // todo this should be made using Symfony forms + if ($request->isMethod('POST') && $request->request->has('shipping_methods')) { + /** + * Example of posted array (the key is a Sylius shipping method id, the value is an array of Shipmondo shipment template ids): + * + * [ + * 1 => [ + * 0 => "664116", + * 1 => "664115" + * ], + * 2 => [ + * 0 => "664114", + * 1 => "664112" + * ] + * ] + * + * @var array> $postedShippingMethods + */ + $postedShippingMethods = $request->request->all('shipping_methods'); + foreach ($postedShippingMethods as $shippingMethodId => $shipmondoShipmentTemplateIds) { + /** @var ShippingMethodInterface|null $shippingMethod */ + $shippingMethod = $this->shippingMethodRepository->find($shippingMethodId); + if (null === $shippingMethod) { continue; } - Assert::isInstanceOf($paymentMethod, PaymentMethodInterface::class); - $paymentMethod->setShipmondoId((int) $shipmondoPaymentMethodId); - $this->paymentMethodRepository->add($paymentMethod); + Assert::isInstanceOf($shippingMethod, ShippingMethodInterface::class); + $shippingMethod->setAllowedShipmentTemplates($shipmondoShipmentTemplateIds); + $this->shippingMethodRepository->add($shippingMethod); } } return $this->render('@SetonoSyliusShipmondoPlugin/admin/shipmondo/index.html.twig', [ 'paymentMethods' => $paymentMethods, + 'shippingMethods' => $shippingMethods, 'shipmondoPaymentMethods' => $shipmondoPaymentMethods, + 'shipmondoShipmentTemplates' => $shipmondoShipmentTemplates, 'registeredWebhooks' => $this->registeredWebhooksRepository->findOneByVersion($this->webhookRegistrar->getVersion()), ]); } diff --git a/src/DataMapper/ShipmentTemplateSalesOrderDataMapper.php b/src/DataMapper/ShipmentTemplateSalesOrderDataMapper.php index f511b99..a74db4b 100644 --- a/src/DataMapper/ShipmentTemplateSalesOrderDataMapper.php +++ b/src/DataMapper/ShipmentTemplateSalesOrderDataMapper.php @@ -9,9 +9,9 @@ use Setono\Shipmondo\Request\SalesOrders\SalesOrder; use Setono\Shipmondo\Resolver\Shipment; use Setono\Shipmondo\Resolver\ShipmentTemplateResolver; -use Setono\Shipmondo\Resolver\SimilarTextBasedShipmentsResemblanceChecker; use Setono\Shipmondo\Response\ShipmentTemplates\ShipmentTemplate; use Setono\SyliusShipmondoPlugin\Model\OrderInterface; +use Setono\SyliusShipmondoPlugin\Model\ShippingMethodInterface; final class ShipmentTemplateSalesOrderDataMapper implements SalesOrderDataMapperInterface { @@ -26,21 +26,41 @@ public function map(OrderInterface $order, SalesOrder $salesOrder): void return; } - $shippingMethodName = self::getShippingMethodName($order); + $shipment = $order->getShipments()->first(); + if (false === $shipment) { + return; + } + + $shippingMethod = $shipment->getMethod(); + if (!$shippingMethod instanceof ShippingMethodInterface) { + return; + } + + $shippingMethodName = $shippingMethod->getName(); if (null === $shippingMethodName) { return; } + $allowedShipmentTemplates = $shippingMethod->getAllowedShipmentTemplates(); + /** @var list $shipmentTemplates */ $shipmentTemplates = []; foreach (Endpoint::paginate($this->client->shipmentTemplates()->get(...)) as $collection) { /** @var ShipmentTemplate $shipmentTemplate */ foreach ($collection as $shipmentTemplate) { + if (!in_array($shipmentTemplate->id, $allowedShipmentTemplates, true)) { + continue; + } + $shipmentTemplates[] = $shipmentTemplate; } } + if ([] === $shipmentTemplates) { + return; + } + $shipment = new Shipment( $shippingMethodName, 'DK', @@ -48,7 +68,8 @@ public function map(OrderInterface $order, SalesOrder $salesOrder): void self::getWeight($order), ); - $resolver = new ShipmentTemplateResolver(new SimilarTextBasedShipmentsResemblanceChecker(10)); + // todo this should be a service + $resolver = new ShipmentTemplateResolver(); $shipmentTemplate = $resolver->resolve($shipment, $shipmentTemplates); $salesOrder->shipmentTemplateId = $shipmentTemplate?->id; @@ -72,7 +93,7 @@ private static function getWeight(OrderInterface $order): int private static function getShippingMethodName(OrderInterface $order): ?string { - $shipment = $order->getshipments()->first(); + $shipment = $order->getShipments()->first(); if (false === $shipment) { return null; } diff --git a/src/Model/ShippingMethodInterface.php b/src/Model/ShippingMethodInterface.php new file mode 100644 index 0000000..c007fe0 --- /dev/null +++ b/src/Model/ShippingMethodInterface.php @@ -0,0 +1,27 @@ + + */ + public function getAllowedShipmentTemplates(): array; + + /** + * @param list $allowedShipmentTemplates + */ + public function setAllowedShipmentTemplates(?array $allowedShipmentTemplates): void; + + public function addAllowedShipmentTemplate(int|ShipmentTemplate $shipmentTemplate): void; + + public function hasAllowedShipmentTemplate(int|ShipmentTemplate $shipmentTemplate): bool; +} diff --git a/src/Model/ShippingMethodTrait.php b/src/Model/ShippingMethodTrait.php new file mode 100644 index 0000000..49dbe41 --- /dev/null +++ b/src/Model/ShippingMethodTrait.php @@ -0,0 +1,61 @@ + + * + * @ORM\Column(type="json", nullable=true) + */ + #[ORM\Column(type: 'json', nullable: true)] + protected ?array $allowedShipmentTemplates = []; + + public function getAllowedShipmentTemplates(): array + { + return $this->allowedShipmentTemplates ?? []; + } + + public function setAllowedShipmentTemplates(?array $allowedShipmentTemplates): void + { + $this->allowedShipmentTemplates = null; + + if (null === $allowedShipmentTemplates) { + return; + } + + foreach ($allowedShipmentTemplates as $allowedShipmentTemplate) { + $this->addAllowedShipmentTemplate($allowedShipmentTemplate instanceof ShipmentTemplate ? $allowedShipmentTemplate->id : (int) $allowedShipmentTemplate); + } + } + + public function addAllowedShipmentTemplate(int|ShipmentTemplate $shipmentTemplate): void + { + if (null === $this->allowedShipmentTemplates) { + $this->allowedShipmentTemplates = []; + } + + $id = $shipmentTemplate instanceof ShipmentTemplate ? $shipmentTemplate->id : $shipmentTemplate; + + if ($this->hasAllowedShipmentTemplate($id)) { + return; + } + + $this->allowedShipmentTemplates[] = $id; + } + + public function hasAllowedShipmentTemplate(int|ShipmentTemplate $shipmentTemplate): bool + { + if (null === $this->allowedShipmentTemplates) { + return false; + } + + return in_array($shipmentTemplate instanceof ShipmentTemplate ? $shipmentTemplate->id : $shipmentTemplate, $this->allowedShipmentTemplates, true); + } +} diff --git a/src/Resources/config/services/controller.xml b/src/Resources/config/services/controller.xml index 9feb206..23ca9ab 100644 --- a/src/Resources/config/services/controller.xml +++ b/src/Resources/config/services/controller.xml @@ -15,6 +15,7 @@ + diff --git a/src/Resources/translations/messages.en.yaml b/src/Resources/translations/messages.en.yaml index 65001fe..fd39097 100644 --- a/src/Resources/translations/messages.en.yaml +++ b/src/Resources/translations/messages.en.yaml @@ -17,3 +17,7 @@ setono_sylius_shipmondo: uploading_to_shipmondo: Uploading to Shipmondo webhooks: Webhooks webhooks_registered_information: Webhooks were registered on %date%. + shipping_method_in_sylius: Shipping method in Sylius + shipment_templates_in_shipmondo: Shipment templates in Shipmondo + map_shipping_methods: Map shipping methods + map_shipping_methods_information: Map your shipping methods to the shipment templates in Shipmondo. This way, shipments inside Shipmondo will have a shipment template assigned automatically. If there's no relevant shipment template in Shipmondo, leave it empty. diff --git a/src/Resources/views/admin/shipmondo/index.html.twig b/src/Resources/views/admin/shipmondo/index.html.twig index b797c9d..7da71c2 100644 --- a/src/Resources/views/admin/shipmondo/index.html.twig +++ b/src/Resources/views/admin/shipmondo/index.html.twig @@ -66,4 +66,54 @@ +
+
+
+

{{ 'setono_sylius_shipmondo.ui.map_shipping_methods'|trans }}

+ + {% if shipmondoShipmentTemplates|length > 0 %} +
+

{{ 'setono_sylius_shipmondo.ui.map_shipping_methods_information'|trans|raw }}

+ + + + + + + + + {% for shippingMethod in shippingMethods %} + + + + + {% endfor %} + +
{{ 'setono_sylius_shipmondo.ui.shipping_method_in_sylius'|trans }}{{ 'setono_sylius_shipmondo.ui.shipment_templates_in_shipmondo'|trans }}
{{ shippingMethod.name }} + +
+ +
+ {% else %} +

{{ 'setono_sylius_shipmondo.ui.map_shipping_methods_empty_information'|trans|raw }}

+ {% endif %} +
+
+
+{% endblock %} + +{% block javascripts %} + {{ parent() }} + {% endblock %} diff --git a/tests/Application/Model/ShippingMethod.php b/tests/Application/Model/ShippingMethod.php new file mode 100644 index 0000000..5d227a7 --- /dev/null +++ b/tests/Application/Model/ShippingMethod.php @@ -0,0 +1,20 @@ +