Skip to content

Commit

Permalink
Make the shipping cost sales order data mapper work with shipping promos
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Jun 24, 2024
1 parent a5a7ab4 commit c15f2a8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
40 changes: 5 additions & 35 deletions src/DataMapper/ShippingCostSalesOrderDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
34 changes: 32 additions & 2 deletions tests/DataMapper/ShippingCostSalesOrderDataMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);

Expand All @@ -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);
}
}
}

0 comments on commit c15f2a8

Please sign in to comment.