1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file has been created by developers from BitBag. |
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
6
|
|
|
* another great project. |
7
|
|
|
* You can find more information about us on https://bitbag.shop and write us |
8
|
|
|
* an email on [email protected]. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace BitBag\SyliusMolliePlugin\Creator; |
14
|
|
|
|
15
|
|
|
use BitBag\SyliusMolliePlugin\DTO\PartialRefundItems; |
16
|
|
|
use BitBag\SyliusMolliePlugin\Exceptions\OfflineRefundPaymentMethodNotFound; |
17
|
|
|
use BitBag\SyliusMolliePlugin\Helper\ConvertOrderInterface; |
18
|
|
|
use BitBag\SyliusMolliePlugin\Refund\Units\UnitsItemOrderRefundInterface; |
19
|
|
|
use BitBag\SyliusMolliePlugin\Refund\Units\UnitsShipmentOrderRefundInterface; |
20
|
|
|
use Mollie\Api\Resources\Order; |
21
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
22
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
23
|
|
|
use Sylius\RefundPlugin\Command\RefundUnits; |
24
|
|
|
use Sylius\RefundPlugin\Provider\RefundPaymentMethodsProviderInterface; |
25
|
|
|
use Webmozart\Assert\Assert; |
26
|
|
|
|
27
|
|
|
final class OrderRefundCommandCreator implements OrderRefundCommandCreatorInterface |
28
|
|
|
{ |
29
|
|
|
/** @var RepositoryInterface */ |
30
|
|
|
private $orderRepository; |
31
|
|
|
|
32
|
|
|
/** @var UnitsItemOrderRefundInterface */ |
33
|
|
|
private $unitsItemOrderRefund; |
34
|
|
|
|
35
|
|
|
/** @var UnitsShipmentOrderRefundInterface */ |
36
|
|
|
private $shipmentOrderRefund; |
37
|
|
|
|
38
|
|
|
/** @var RefundPaymentMethodsProviderInterface */ |
39
|
|
|
private $refundPaymentMethodProvider; |
40
|
|
|
|
41
|
|
|
public function __construct |
42
|
|
|
( |
43
|
|
|
RepositoryInterface $orderRepository, |
44
|
|
|
UnitsItemOrderRefundInterface $unitsItemOrderRefund, |
45
|
|
|
UnitsShipmentOrderRefundInterface $shipmentOrderRefund, |
46
|
|
|
RefundPaymentMethodsProviderInterface $refundPaymentMethodProvider |
47
|
|
|
) { |
48
|
|
|
$this->orderRepository = $orderRepository; |
49
|
|
|
$this->unitsItemOrderRefund = $unitsItemOrderRefund; |
50
|
|
|
$this->shipmentOrderRefund = $shipmentOrderRefund; |
51
|
|
|
$this->refundPaymentMethodProvider = $refundPaymentMethodProvider; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function fromOrder(Order $order): RefundUnits |
55
|
|
|
{ |
56
|
|
|
$orderId = $order->metadata->order_id; |
57
|
|
|
/** @var OrderInterface $syliusOrder */ |
58
|
|
|
$syliusOrder = $this->orderRepository->findOneBy(['id' => $orderId]); |
59
|
|
|
Assert::notNull($order, sprintf('Cannot find order id with id %s', $orderId)); |
60
|
|
|
|
61
|
|
|
$partialRefundItems = new PartialRefundItems(); |
62
|
|
|
|
63
|
|
|
foreach ($order->lines as $line) { |
64
|
|
|
if ($line->status === 'paid' && $line->type === ConvertOrderInterface::PHYSICAL_TYPE) { |
65
|
|
|
$getRefundedQuantity = $this->unitsItemOrderRefund->getActualRefundedQuantity($syliusOrder, $line->metadata->item_id); |
66
|
|
|
$partialRefundItems->addPartialRefundItemByQuantity( |
67
|
|
|
$line->metadata->item_id, |
68
|
|
|
$line->type, |
69
|
|
|
$line->quantityRefunded - $getRefundedQuantity |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$refundMethods = $this->refundPaymentMethodProvider->findForChannel($syliusOrder->getChannel()); |
|
|
|
|
75
|
|
|
|
76
|
|
|
if (empty($refundMethods)) { |
77
|
|
|
throw new OfflineRefundPaymentMethodNotFound( |
78
|
|
|
sprintf('Not found offline payment method on this channel with code :%s', $syliusOrder->getChannel()->getCode()) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$refundMethod = current($refundMethods); |
83
|
|
|
|
84
|
|
|
$unitsToRefund = $this->unitsItemOrderRefund->refund($syliusOrder, $partialRefundItems); |
85
|
|
|
$shipmentToRefund = $this->shipmentOrderRefund->refund($order, $syliusOrder); |
86
|
|
|
|
87
|
|
|
return new RefundUnits($syliusOrder->getNumber(), $unitsToRefund, $shipmentToRefund, $refundMethod->getId(), ''); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|