From c15f2a841337aeee91fddd248fe98a8e3a4cc6a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Mon, 24 Jun 2024 11:05:57 +0200 Subject: [PATCH] Make the shipping cost sales order data mapper work with shipping promos --- .../ShippingCostSalesOrderDataMapper.php | 40 +++---------------- .../ShippingCostSalesOrderDataMapperTest.php | 34 +++++++++++++++- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/DataMapper/ShippingCostSalesOrderDataMapper.php b/src/DataMapper/ShippingCostSalesOrderDataMapper.php index 3a2624f..9991dd6 100644 --- a/src/DataMapper/ShippingCostSalesOrderDataMapper.php +++ b/src/DataMapper/ShippingCostSalesOrderDataMapper.php @@ -8,50 +8,20 @@ use function Setono\SyliusPeakPlugin\formatAmount; use Setono\SyliusPeakPlugin\Model\OrderInterface; use Sylius\Component\Core\Model\AdjustmentInterface; -use Webmozart\Assert\Assert; final class ShippingCostSalesOrderDataMapper implements SalesOrderDataMapperInterface { public function map(OrderInterface $order, SalesOrder $salesOrder): void { - $shipping = $shippingTax = 0; + $tax = 0; foreach ($order->getShipments() as $shipment) { - $shippingAdjustments = $shipment->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT); - if ($shippingAdjustments->isEmpty()) { - continue; + foreach ($shipment->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT) as $taxAdjustment) { + $tax += $taxAdjustment->getAmount(); } - - // Sylius only adds one shipping adjustment per shipment - Assert::count($shippingAdjustments, 1); - - $shippingAdjustment = $shippingAdjustments->first(); - Assert::isInstanceOf($shippingAdjustment, AdjustmentInterface::class); - - $taxAdjustments = $shipment->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT); - Assert::lessThanEq($taxAdjustments->count(), 1); - - // Because of the way shipping and shipping taxes are done in Sylius, we need to do some gymnastics to calculate the correct unit price. - // If the tax adjustment is neutral we know that the tax is included in the shipping adjustment's amount and hence we subtract the tax. - // If the tax adjustment is not neutral we know that the tax is not included and hence we don't subtract the tax. - // The tax is still the same though, and we need that to calculate the vat percentage - $tax = 0; - $amount = $shippingAdjustment->getAmount(); - - $taxAdjustment = $taxAdjustments->first(); - if (false !== $taxAdjustment) { - $tax = $taxAdjustment->getAmount(); - - if ($taxAdjustment->isNeutral()) { - $amount -= $tax; - } - } - - $shipping += $amount; - $shippingTax += $tax; } - $salesOrder->shippingCost = formatAmount($shipping); - $salesOrder->shippingTaxCost = formatAmount($shippingTax); + $salesOrder->shippingCost = formatAmount($order->getShippingTotal() - $tax); + $salesOrder->shippingTaxCost = formatAmount($tax); } } diff --git a/tests/DataMapper/ShippingCostSalesOrderDataMapperTest.php b/tests/DataMapper/ShippingCostSalesOrderDataMapperTest.php index 2b4fef9..c403164 100644 --- a/tests/DataMapper/ShippingCostSalesOrderDataMapperTest.php +++ b/tests/DataMapper/ShippingCostSalesOrderDataMapperTest.php @@ -52,6 +52,23 @@ public function it_maps_shipping_with_tax_not_included_in_price(): void self::assertSame(0.2, $salesOrder->shippingTaxCost); } + /** + * @test + */ + public function it_maps_shipping_with_shipping_promotion_adjustment_applied(): void + { + $order = self::getOrder(); + self::addShipment($order, 100, 10, true, 50); + + $salesOrder = new SalesOrder(); + + $mapper = new ShippingCostSalesOrderDataMapper(); + $mapper->map($order, $salesOrder); + + self::assertSame(0.4, $salesOrder->shippingCost); + self::assertSame(0.1, $salesOrder->shippingTaxCost); + } + private static function getOrder(): Order { $order = new Order(); @@ -60,8 +77,13 @@ private static function getOrder(): Order return $order; } - private static function addShipment(OrderInterface $order, int $shippingAmount, int $taxAmount, bool $taxNeutral): void - { + private static function addShipment( + OrderInterface $order, + int $shippingAmount, + int $taxAmount, + bool $taxNeutral, + int $shippingPromotionTotal = 0, + ): void { $shipment = new Shipment(); $order->addShipment($shipment); @@ -77,5 +99,13 @@ private static function addShipment(OrderInterface $order, int $shippingAmount, $shipment->addAdjustment($shippingAdjustment); $shipment->addAdjustment($taxAdjustment); + + if ($shippingPromotionTotal > 0) { + $shippingPromotionAdjustment = new Adjustment(); + $shippingPromotionAdjustment->setAmount($shippingPromotionTotal * -1); + $shippingPromotionAdjustment->setNeutral(false); + $shippingPromotionAdjustment->setType(AdjustmentInterface::ORDER_SHIPPING_PROMOTION_ADJUSTMENT); + $shipment->addAdjustment($shippingPromotionAdjustment); + } } }