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

Save reset stock in order line item custom field with refund #356

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ abstract class AbstractItem
* @param float $promotionDiscount
* @param int $promotionAffectedQty
* @param int $refundedQty
* @param int $resetStockQuantity
* @return array<mixed>
*/
protected function buildArray(string $id, string $label, string $referenceNumber, bool $isPromotion, bool $isDelivery, float $unitPrice, int $quantity, float $totalPrice, float $promotionDiscount, int $promotionAffectedQty, int $refundedQty): array
protected function buildArray(string $id, string $label, string $referenceNumber, bool $isPromotion, bool $isDelivery, float $unitPrice, int $quantity, float $totalPrice, float $promotionDiscount, int $promotionAffectedQty, int $refundedQty, int $resetStockQuantity): array
{
return [
'refunded' => $refundedQty,
Expand All @@ -37,6 +38,7 @@ protected function buildArray(string $id, string $label, string $referenceNumber
],
'isPromotion' => $isPromotion,
'isDelivery' => $isDelivery,
'resetStockQuantity' => $resetStockQuantity
],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public function toArray(): array
$this->delivery->getShippingCosts()->getTotalPrice(),
0,
0,
$this->alreadyRefundedQty
$this->alreadyRefundedQty,
0
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kiener\MolliePayments\Components\RefundManager\RefundData\OrderItem;

use Kiener\MolliePayments\Struct\OrderLineItemEntity\OrderLineItemEntityAttributes;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;

class ProductItem extends AbstractItem
Expand Down Expand Up @@ -68,6 +69,9 @@ private function extractPromotionDiscounts(array $promotionCompositions)
*/
public function toArray(): array
{
$lineItemAttributes = new OrderLineItemEntityAttributes($this->lineItem);
$resetStockQuantity = $lineItemAttributes->getResetStockQuantity();

return $this->buildArray(
$this->lineItem->getId(),
$this->lineItem->getLabel(),
Expand All @@ -79,7 +83,8 @@ public function toArray(): array
$this->lineItem->getTotalPrice(),
$this->promotionDiscount,
$this->promotionAffectedQuantity,
$this->alreadyRefundedQty
$this->alreadyRefundedQty,
$resetStockQuantity
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public function toArray(): array
$this->lineItem->getTotalPrice(),
0,
0,
$this->alreadyRefundedQty
$this->alreadyRefundedQty,
0
);
}

Expand Down
27 changes: 24 additions & 3 deletions src/Components/RefundManager/RefundManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;

class RefundManager implements RefundManagerInterface
{
Expand Down Expand Up @@ -74,6 +75,11 @@ class RefundManager implements RefundManagerInterface
*/
private $logger;

/**
* @var EntityRepositoryInterface
*/
private $orderLineItemRepository;


/**
* @param RefundDataBuilder $refundDataBuilder
Expand All @@ -84,20 +90,20 @@ class RefundManager implements RefundManagerInterface
* @param FlowBuilderEventFactory $flowBuilderEventFactory
* @param StockManagerInterface $stockUpdater
* @param LoggerInterface $logger
* @param EntityRepositoryInterface $orderLineItemRepository
* @throws \Exception
*/
public function __construct(RefundDataBuilder $refundDataBuilder, OrderServiceInterface $orderService, RefundServiceInterface $refundService, Order $mollieOrder, FlowBuilderFactoryInterface $flowBuilderFactory, FlowBuilderEventFactory $flowBuilderEventFactory, StockManagerInterface $stockUpdater, LoggerInterface $logger)
public function __construct(RefundDataBuilder $refundDataBuilder, OrderServiceInterface $orderService, RefundServiceInterface $refundService, Order $mollieOrder, FlowBuilderFactoryInterface $flowBuilderFactory, FlowBuilderEventFactory $flowBuilderEventFactory, StockManagerInterface $stockUpdater, LoggerInterface $logger, EntityRepositoryInterface $orderLineItemRepository)
{
$this->builderData = $refundDataBuilder;
$this->orderService = $orderService;
$this->mollie = $mollieOrder;
$this->refundService = $refundService;
$this->stockManager = $stockUpdater;
$this->logger = $logger;

$this->flowBuilderEventFactory = $flowBuilderEventFactory;

$this->flowBuilderDispatcher = $flowBuilderFactory->createDispatcher();
$this->orderLineItemRepository = $orderLineItemRepository;
}


Expand Down Expand Up @@ -241,6 +247,21 @@ public function refund(OrderEntity $order, RefundRequest $request, Context $cont
$refund->id
);
}
# also add reset stock to order line item, so we can retrieve it through API
# multiple refunds for a single line item are possible, so if previous reset stock exists, increase it
$lineItemAttributes = new OrderLineItemEntityAttributes($orderItem);
$alreadyResetStock = $lineItemAttributes->getResetStockQuantity() ?? 0;

#todo: The mollieOrderLineID always gets removed when upserting/updating the repository, for now update the array
$customFields = $orderItem->getCustomFields();
$customFields['mollie_payments']['reset_stock_quantity'] = $alreadyResetStock + $item->getStockIncreaseQty();

$data = [
'id' => $orderItem->getId(),
'customFields' => $customFields
];

$this->orderLineItemRepository->upsert([$data], $context);
}

return $refund;
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services/components.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<argument type="service" id="Kiener\MolliePayments\Compatibility\Bundles\FlowBuilder\FlowBuilderEventFactory"/>
<argument type="service" id="Kiener\MolliePayments\Service\Stock\StockManager"/>
<argument type="service" id="mollie_payments.logger"/>
<argument type="service" id="order_line_item.repository"/>
</service>

<service id="Kiener\MolliePayments\Components\RefundManager\Builder\RefundDataBuilder">
Expand Down
14 changes: 14 additions & 0 deletions src/Struct/OrderLineItemEntity/OrderLineItemEntityAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ class OrderLineItemEntityAttributes
*/
private $subscriptionRepetitionCount;

/**
* @var int
*/
private $resetStockQuantity;


/**
* @param OrderLineItemEntity $lineItem
Expand All @@ -47,6 +52,7 @@ public function __construct(OrderLineItemEntity $lineItem)
{
$this->voucherType = $this->getCustomFieldValue($lineItem, 'voucher_type');
$this->mollieOrderLineID = $this->getCustomFieldValue($lineItem, 'order_line_id');
$this->resetStockQuantity = (int)$this->getCustomFieldValue($lineItem, 'reset_stock_quantity');

$this->subscriptionProduct = (bool)$this->getCustomFieldValue($lineItem, 'subscription_enabled');
$this->subscriptionInterval = (int)$this->getCustomFieldValue($lineItem, 'subscription_interval');
Expand Down Expand Up @@ -81,6 +87,14 @@ public function getMollieOrderLineID(): string
return $this->mollieOrderLineID;
}

/**
* @return int
*/
public function getResetStockQuantity(): int
{
return $this->resetStockQuantity;
}

/**
* @return bool
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public function testCartItems()
$lineItem->setTotalPrice(2 * 19.99);
$lineItem->setReferencedId('product-id-1');
$lineItem->setPayload(['productNumber' => 'P123']);
$lineItem->setCustomFields([
'mollie_payments' => [
'reset_stock_quantity' => 2
],
]);

$items[] = new ProductItem($lineItem, [], 2);

Expand All @@ -78,6 +83,7 @@ public function testCartItems()
],
'isPromotion' => false,
'isDelivery' => false,
'resetStockQuantity' => 2
],
]
];
Expand Down
8 changes: 6 additions & 2 deletions tests/PHPUnit/Components/RefundManager/RefundManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Kiener\MolliePayments\Components\RefundManager\Request\RefundRequest;
use Kiener\MolliePayments\Components\RefundManager\Request\RefundRequestItem;
use Kiener\MolliePayments\Service\MollieApi\Order;
use MolliePayments\Tests\Fakes\FakeEntityRepository;
use MolliePayments\Tests\Fakes\FakeOrderService;
use MolliePayments\Tests\Fakes\FakeRefundService;
use MolliePayments\Tests\Fakes\FlowBuilder\FakeFlowBuilderDispatcher;
Expand All @@ -19,6 +20,7 @@
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemCollection;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemDefinition;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\Context;
Expand Down Expand Up @@ -60,6 +62,7 @@ protected function setUp(): void

$fakeOrderService = new FakeOrderService($order);
$fakeRefundService = new FakeRefundService('r-xyz-123', 9999);
$fakeOrderLineItemRepository = new FakeEntityRepository(new OrderLineItemDefinition());
$this->fakeStockUpdater = new FakeStockManager();

/** @var Order $fakeOrder */
Expand All @@ -77,7 +80,8 @@ protected function setUp(): void
new FakeFlowBuilderFactory($this->fakeFlowBuilderDispatcher),
$flowBuilderEventFactory,
$this->fakeStockUpdater,
new NullLogger()
new NullLogger(),
$fakeOrderLineItemRepository
);
}

Expand Down Expand Up @@ -162,4 +166,4 @@ public function testStockReset()
$this->assertEquals('r-xyz-123', $this->fakeStockUpdater->getMollieRefundID());
}

}
}