Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PISHPS-393: Add shipping costs when nothing is left to ship #911

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading