Skip to content

Commit

Permalink
PISHPS-393: Add shipping costs when nothing is left to ship (#911)
Browse files Browse the repository at this point in the history
* NTR: PISHPS-393: Add shipping costs when nothing is left to ship

* NTR: fix tests

---------

Co-authored-by: Vitalij Mik <[email protected]>
  • Loading branch information
BlackScorp and Vitalij Mik authored Dec 10, 2024
1 parent e2f6ced commit 27af0b9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/Service/MollieApi/Shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function shipOrder(string $mollieOrderId, string $salesChannelId, array $
];
}

$options = $this->addShippingCosts($mollieOrder, $options);
return $mollieOrder->createShipment($options);
} catch (ApiException $e) {
throw new MollieOrderCouldNotBeShippedException(
Expand Down Expand Up @@ -120,6 +121,7 @@ public function shipItem(string $mollieOrderId, string $salesChannelId, string $
}

$mollieOrder = $this->orderApiService->getMollieOrder($mollieOrderId, $salesChannelId);
$options = $this->addShippingCosts($mollieOrder, $options);

return $mollieOrder->createShipment($options);
} catch (ApiException $e) {
Expand All @@ -134,6 +136,54 @@ public function shipItem(string $mollieOrderId, string $salesChannelId, string $
}
}

/**
* @param \Mollie\Api\Resources\Order $mollieOrder
* @param array<mixed> $options
* @return array<mixed>
*/
private function addShippingCosts(\Mollie\Api\Resources\Order $mollieOrder, array $options): array
{
$shippingOptions = [];

$mollieLines = $mollieOrder->lines();

$shippableLines = [];

/**
* @var OrderLine $line
*/
foreach ($mollieLines as $line) {
if ($line->type === OrderLineType::TYPE_SHIPPING_FEE) {
$shippingOptions[] = [
'id' => $line->id,
'quantity' => $line->quantity,
];
continue;
}
if ($line->shippableQuantity > 0) {
$shippableLines[$line->id] = $line;
}
}


foreach ($options['lines'] as $line) {
$shippableLine = $shippableLines[$line['id']]??null;
if ($shippableLine === null) {
continue;
}
$shippableQuantity = $shippableLine->shippableQuantity - $line['quantity'];
if ($shippableQuantity === 0) {
unset($shippableLines[$line['id']]);
}
}
if (count($shippableLines) === 0) {
$options['lines'] = array_merge($options['lines'], $shippingOptions);
}


return $options;
}

/**
* @param string $mollieOrderId
* @param string $salesChannelId
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPUnit/Service/MollieApi/ShipmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,16 @@ public function testShipOrderCannotBeShippedException()
*/
public function testShipItem()
{

$this->mollieOrder
->expects($this->once())
->method('createShipment')
->willReturn($this->createMock(MollieShipment::class));

$this->mollieOrder->method('lines')->willReturn([]);



$this->shipmentApiService->shipItem('mollieOrderId', 'salesChannelId', 'mollieOrderLineId', 1, null);
}

Expand All @@ -146,6 +151,7 @@ public function testShipItemCannotBeShippedException()
->expects($this->once())
->method('createShipment')
->willThrowException(new ApiException());
$this->mollieOrder->method('lines')->willReturn([]);

$this->expectException(MollieOrderCouldNotBeShippedException::class);

Expand Down

0 comments on commit 27af0b9

Please sign in to comment.