From e3f43c4f23d86436dcf2d434e8919d261bd4e56a Mon Sep 17 00:00:00 2001 From: m-muxfeld-diw <143803170+m-muxfeld-diw@users.noreply.github.com> Date: Tue, 10 Sep 2024 12:27:10 +0200 Subject: [PATCH] PISHPS-253: MollieRefundConfigService now uses the refundable quantity (#832) --- .../Services/MollieRefundConfigService.php | 10 ++++-- .../OrderLineItemStructCollection.php | 11 +++++++ .../Structs/OrderLineItemStruct.php | 32 +++++++++---------- .../MollieRefundConfigServiceTest.php | 23 +++++++------ 4 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/Controller/Api/PluginConfig/Services/MollieRefundConfigService.php b/src/Controller/Api/PluginConfig/Services/MollieRefundConfigService.php index 8dbf163e9..c28036873 100644 --- a/src/Controller/Api/PluginConfig/Services/MollieRefundConfigService.php +++ b/src/Controller/Api/PluginConfig/Services/MollieRefundConfigService.php @@ -65,7 +65,7 @@ public function createConfigControllerResponse(string $orderId, MollieSettingStr foreach ($mollieOrder->lines() as $line) { $structs[] = OrderLineItemStruct::createWithId($line->metadata->orderLineItemId) - ->setIsShipped($line->quantityShipped >= 1) + ->setRefundableQuantity($line->refundableQuantity) ->setOrderedQuantity($line->quantity); } @@ -106,9 +106,13 @@ public function createResponse(OrderLineItemStructCollection $lineItems, MollieS break; } - // only shipped items can be refunded // only items that have not been fully refunded can be refunded - if ($lineItem->isShipped() && $lineItem->getRefundedCount() < $lineItem->getOrderedQuantity()) { + if ($lineItem->getRefundedCount() < $lineItem->getOrderedQuantity()) { + $hasRefundableItems = true; + break; + } + + if ($lineItem->getRefundableQuantity() > 0) { $hasRefundableItems = true; break; } diff --git a/src/Controller/Api/PluginConfig/Structs/Collections/OrderLineItemStructCollection.php b/src/Controller/Api/PluginConfig/Structs/Collections/OrderLineItemStructCollection.php index b3fc0c81d..fb423b3fd 100644 --- a/src/Controller/Api/PluginConfig/Structs/Collections/OrderLineItemStructCollection.php +++ b/src/Controller/Api/PluginConfig/Structs/Collections/OrderLineItemStructCollection.php @@ -35,4 +35,15 @@ public function getById(string $id): OrderLineItemStruct throw LineItemNotPartOfCollectionConfigException::create($id); } + + public function getRefundableQuantity(): int + { + $refundableQuantity = 0; + + foreach ($this as $item) { + $refundableQuantity += $item->getRefundableQuantity(); + } + + return $refundableQuantity; + } } diff --git a/src/Controller/Api/PluginConfig/Structs/OrderLineItemStruct.php b/src/Controller/Api/PluginConfig/Structs/OrderLineItemStruct.php index ad554b593..df0316333 100644 --- a/src/Controller/Api/PluginConfig/Structs/OrderLineItemStruct.php +++ b/src/Controller/Api/PluginConfig/Structs/OrderLineItemStruct.php @@ -12,11 +12,6 @@ class OrderLineItemStruct extends Struct */ private $id; - /** - * @var bool - */ - private $isShipped = false; - /** * @var int */ @@ -32,6 +27,11 @@ class OrderLineItemStruct extends Struct */ private $orderedQuantity = 0; + /** + * @var int + */ + private $refundableQuantity = 0; + final private function __construct(string $id) { $this->id = $id; @@ -42,17 +42,6 @@ public static function createWithId(string $id): self return new self($id); } - public function isShipped(): bool - { - return $this->isShipped; - } - - public function setIsShipped(bool $isShipped): OrderLineItemStruct - { - $this->isShipped = $isShipped; - return $this; - } - public function getId(): string { return $this->id; @@ -90,4 +79,15 @@ public function setRefundedCount(int $refundedCount): OrderLineItemStruct $this->refundedCount = $refundedCount; return $this; } + + public function getRefundableQuantity(): int + { + return $this->refundableQuantity; + } + + public function setRefundableQuantity(int $refundableQuantity): OrderLineItemStruct + { + $this->refundableQuantity = $refundableQuantity; + return $this; + } } diff --git a/tests/PHPUnit/Controller/Api/PluginConfig/Services/MollieRefundConfigServiceTest.php b/tests/PHPUnit/Controller/Api/PluginConfig/Services/MollieRefundConfigServiceTest.php index a30bb73ef..22998dd7a 100644 --- a/tests/PHPUnit/Controller/Api/PluginConfig/Services/MollieRefundConfigServiceTest.php +++ b/tests/PHPUnit/Controller/Api/PluginConfig/Services/MollieRefundConfigServiceTest.php @@ -62,7 +62,7 @@ public function testEnablesRefundManagerWhenPendingRefundExists(): void ->method('hasPendingRefund') ->willReturn(true); - $lineItem->expects($this->never())->method('isShipped'); + $lineItem->expects($this->never())->method('getRefundableQuantity'); $lineItem->expects($this->never())->method('getRefundedCount'); $lineItem->expects($this->never())->method('getOrderedQuantity'); @@ -74,7 +74,7 @@ public function testEnablesRefundManagerWhenPendingRefundExists(): void ); } - public function testRefundManagerIsDisabledWhenNoItemIsMarkedAsShipped(): void + public function testRefundManagerIsEnabledWhenRefundableQuantityForLineItemIsGreaterThanZero(): void { $lineItems = OrderLineItemStructCollection::create( $lineItem = $this->createMock(OrderLineItemStruct::class) @@ -85,16 +85,21 @@ public function testRefundManagerIsDisabledWhenNoItemIsMarkedAsShipped(): void ->willReturn(false); $lineItem->expects($this->once()) - ->method('isShipped') - ->willReturn(false); + ->method('getRefundableQuantity') + ->willReturn(1); - $lineItem->expects($this->never())->method('getRefundedCount'); - $lineItem->expects($this->never())->method('getOrderedQuantity'); + $lineItem->expects($this->once()) + ->method('getRefundedCount') + ->willReturn(6); + + $lineItem->expects($this->once()) + ->method('getOrderedQuantity') + ->willReturn(5); $result = $this->service->createResponse($lineItems, $this->config); static::assertSame( - '{"enabled":false,"autoStockReset":true,"verifyRefund":true,"showInstructions":true}', + '{"enabled":true,"autoStockReset":true,"verifyRefund":true,"showInstructions":true}', $result->getContent() ); } @@ -110,8 +115,8 @@ public function testRefundManagerIsDisabledWhenAllLineItemsHaveBeenRefunded(): v ->willReturn(false); $lineItem->expects($this->once()) - ->method('isShipped') - ->willReturn(true); + ->method('getRefundableQuantity') + ->willReturn(0); $lineItem->expects($this->once()) ->method('getRefundedCount')