Skip to content

Commit

Permalink
Add shipping cost sales order data mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Jun 24, 2024
1 parent 67e68ba commit 76fe424
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/DataMapper/SalesOrderDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function map(OrderInterface $order, SalesOrder $salesOrder): void
phone: $order->getBillingAddress()?->getPhoneNumber(),
);

// todo set shipping cost and discount cost
// todo set discount cost

$salesOrder->paymentMethod = self::getPaymentMethod($order)?->getName();
$salesOrder->forwarderProductId = self::getShippingMethod($order)?->getCode();
Expand Down
57 changes: 57 additions & 0 deletions src/DataMapper/ShippingCostSalesOrderDataMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusPeakPlugin\DataMapper;

use Setono\PeakWMS\DataTransferObject\SalesOrder\SalesOrder;
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;

foreach ($order->getShipments() as $shipment) {
$shippingAdjustments = $shipment->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT);
if ($shippingAdjustments->isEmpty()) {
continue;
}

// 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);
}
}
5 changes: 5 additions & 0 deletions src/Resources/config/services/data_mapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@

<tag name="setono_sylius_peak.sales_order_data_mapper" priority="90"/>
</service>

<service id="setono_sylius_peak.data_mapper.sales_order.shipping_cost"
class="Setono\SyliusPeakPlugin\DataMapper\ShippingCostSalesOrderDataMapper">
<tag name="setono_sylius_peak.sales_order_data_mapper" priority="80"/>
</service>
</services>
</container>
81 changes: 81 additions & 0 deletions tests/DataMapper/ShippingCostSalesOrderDataMapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace Tests\Setono\SyliusPeakPlugin\DataMapper;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Setono\PeakWMS\DataTransferObject\SalesOrder\SalesOrder;
use Setono\SyliusPeakPlugin\DataMapper\ShippingCostSalesOrderDataMapper;
use Setono\SyliusPeakPlugin\Model\OrderInterface;
use Sylius\Component\Core\Model\Adjustment;
use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Core\Model\Shipment;
use Tests\Setono\SyliusPeakPlugin\Application\Model\Order;

final class ShippingCostSalesOrderDataMapperTest extends TestCase
{
use ProphecyTrait;

/**
* @test
*/
public function it_maps_shipping_with_tax_included_in_price(): void
{
$order = self::getOrder();
self::addShipment($order, 100, 20, true);

$salesOrder = new SalesOrder();

$mapper = new ShippingCostSalesOrderDataMapper();
$mapper->map($order, $salesOrder);

self::assertSame(0.8, $salesOrder->shippingCost);
self::assertSame(0.2, $salesOrder->shippingTaxCost);
}

/**
* @test
*/
public function it_maps_shipping_with_tax_not_included_in_price(): void
{
$order = self::getOrder();
self::addShipment($order, 100, 20, false);

$salesOrder = new SalesOrder();

$mapper = new ShippingCostSalesOrderDataMapper();
$mapper->map($order, $salesOrder);

self::assertSame(1.0, $salesOrder->shippingCost);
self::assertSame(0.2, $salesOrder->shippingTaxCost);
}

private static function getOrder(): Order
{
$order = new Order();
$order->setCurrencyCode('USD');

return $order;
}

private static function addShipment(OrderInterface $order, int $shippingAmount, int $taxAmount, bool $taxNeutral): void
{
$shipment = new Shipment();
$order->addShipment($shipment);

$taxAdjustment = new Adjustment();
$taxAdjustment->setAmount($taxAmount);
$taxAdjustment->setNeutral($taxNeutral);
$taxAdjustment->setType(AdjustmentInterface::TAX_ADJUSTMENT);

$shippingAdjustment = new Adjustment();
$shippingAdjustment->setAmount($shippingAmount);
$shippingAdjustment->setNeutral(false);
$shippingAdjustment->setType(AdjustmentInterface::SHIPPING_ADJUSTMENT);

$shipment->addAdjustment($shippingAdjustment);
$shipment->addAdjustment($taxAdjustment);
}
}

0 comments on commit 76fe424

Please sign in to comment.