From dc4ab1ee9b7e59ec5c7250a5ec95651d645eb6af Mon Sep 17 00:00:00 2001 From: Vitalij Mik Date: Tue, 10 Dec 2024 13:58:43 +0100 Subject: [PATCH 1/2] NTR: PISHPS-393: Add shipping costs when nothing is left to ship --- src/Service/MollieApi/Shipment.php | 50 +++++++++++++++++++ .../Service/MollieApi/ShipmentTest.php | 6 +++ 2 files changed, 56 insertions(+) diff --git a/src/Service/MollieApi/Shipment.php b/src/Service/MollieApi/Shipment.php index a2daf793f..5b89ea282 100644 --- a/src/Service/MollieApi/Shipment.php +++ b/src/Service/MollieApi/Shipment.php @@ -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( @@ -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) { @@ -134,6 +136,54 @@ public function shipItem(string $mollieOrderId, string $salesChannelId, string $ } } + /** + * @param \Mollie\Api\Resources\Order $mollieOrder + * @param array $options + * @return array + */ + 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 diff --git a/tests/PHPUnit/Service/MollieApi/ShipmentTest.php b/tests/PHPUnit/Service/MollieApi/ShipmentTest.php index 4718ecb8f..55ab5cbcd 100644 --- a/tests/PHPUnit/Service/MollieApi/ShipmentTest.php +++ b/tests/PHPUnit/Service/MollieApi/ShipmentTest.php @@ -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->createMock(OrderLineCollection::class)); + + + $this->shipmentApiService->shipItem('mollieOrderId', 'salesChannelId', 'mollieOrderLineId', 1, null); } @@ -146,6 +151,7 @@ public function testShipItemCannotBeShippedException() ->expects($this->once()) ->method('createShipment') ->willThrowException(new ApiException()); + $this->mollieOrder->method('lines')->willReturn($this->createMock(OrderLineCollection::class)); $this->expectException(MollieOrderCouldNotBeShippedException::class); From 159acfaf429f40b79a07194a7c6295dbb751e0e7 Mon Sep 17 00:00:00 2001 From: Vitalij Mik Date: Tue, 10 Dec 2024 14:14:00 +0100 Subject: [PATCH 2/2] NTR: fix tests --- tests/PHPUnit/Service/MollieApi/ShipmentTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/PHPUnit/Service/MollieApi/ShipmentTest.php b/tests/PHPUnit/Service/MollieApi/ShipmentTest.php index 55ab5cbcd..acf0818ca 100644 --- a/tests/PHPUnit/Service/MollieApi/ShipmentTest.php +++ b/tests/PHPUnit/Service/MollieApi/ShipmentTest.php @@ -135,7 +135,7 @@ public function testShipItem() ->method('createShipment') ->willReturn($this->createMock(MollieShipment::class)); - $this->mollieOrder->method('lines')->willReturn($this->createMock(OrderLineCollection::class)); + $this->mollieOrder->method('lines')->willReturn([]); @@ -151,7 +151,7 @@ public function testShipItemCannotBeShippedException() ->expects($this->once()) ->method('createShipment') ->willThrowException(new ApiException()); - $this->mollieOrder->method('lines')->willReturn($this->createMock(OrderLineCollection::class)); + $this->mollieOrder->method('lines')->willReturn([]); $this->expectException(MollieOrderCouldNotBeShippedException::class);