|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gweb\SyliusProductDepositPlugin\OrderProcessing; |
|
6
|
|
|
|
|
7
|
|
|
use Gweb\SyliusProductDepositPlugin\Entity\ProductVariantInterface; |
|
8
|
|
|
use Sylius\Component\Addressing\Matcher\ZoneMatcherInterface; |
|
9
|
|
|
use Sylius\Component\Addressing\Model\ZoneInterface; |
|
10
|
|
|
use Sylius\Component\Core\Model\OrderItemInterface; |
|
11
|
|
|
use Sylius\Component\Core\Model\Scope; |
|
12
|
|
|
use Sylius\Component\Core\Provider\ZoneProviderInterface; |
|
13
|
|
|
use Sylius\Component\Core\Taxation\Applicator\OrderTaxesApplicatorInterface; |
|
14
|
|
|
use Sylius\Component\Order\Model\OrderInterface; |
|
15
|
|
|
use Sylius\Component\Order\Processor\OrderProcessorInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Recalculates the order item unit price inclusive deposit |
|
19
|
|
|
* |
|
20
|
|
|
* @author Gerd Weitenberg <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
final class OrderDepositProcessor implements OrderProcessorInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var ZoneProviderInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $defaultTaxZoneProvider; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var ZoneMatcherInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $zoneMatcher; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var OrderTaxesApplicatorInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $orderDepositTaxesApplicator; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param ZoneProviderInterface $defaultTaxZoneProvider |
|
41
|
|
|
* @param ZoneMatcherInterface $zoneMatcher |
|
42
|
|
|
* @param OrderTaxesApplicatorInterface $orderDepositTaxesApplicator |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct( |
|
45
|
|
|
ZoneProviderInterface $defaultTaxZoneProvider, |
|
46
|
|
|
ZoneMatcherInterface $zoneMatcher, |
|
47
|
|
|
OrderTaxesApplicatorInterface $orderDepositTaxesApplicator |
|
48
|
|
|
) { |
|
49
|
|
|
$this->defaultTaxZoneProvider = $defaultTaxZoneProvider; |
|
50
|
|
|
$this->zoneMatcher = $zoneMatcher; |
|
51
|
|
|
$this->orderDepositTaxesApplicator = $orderDepositTaxesApplicator; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param \Sylius\Component\Core\Model\OrderInterface $order |
|
56
|
|
|
*/ |
|
57
|
|
|
public function process(OrderInterface $order): void |
|
58
|
|
|
{ |
|
59
|
|
|
$channel = $order->getChannel(); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** @var OrderItemInterface $item */ |
|
62
|
|
|
foreach ($order->getItems() as $item) { |
|
63
|
|
|
|
|
64
|
|
|
/** @var ProductVariantInterface $variant */ |
|
65
|
|
|
$variant = $item->getVariant(); |
|
66
|
|
|
|
|
67
|
|
|
$channelDeposit = $variant->getChannelDepositForChannel($channel); |
|
68
|
|
|
if (null == $channelDeposit) { |
|
69
|
|
|
continue; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$depositPrice = $channelDeposit->getPrice(); |
|
73
|
|
|
|
|
74
|
|
|
$item->setUnitPrice($item->getUnitPrice() + $depositPrice); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// apply deposit taxes |
|
78
|
|
|
$this->orderDepositTaxesApplicator->apply($order, $this->getTaxZone($order)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param \Sylius\Component\Core\Model\OrderInterface $order |
|
83
|
|
|
* |
|
84
|
|
|
* @return ZoneInterface|null |
|
85
|
|
|
*/ |
|
86
|
|
|
private function getTaxZone(OrderInterface $order): ?ZoneInterface |
|
87
|
|
|
{ |
|
88
|
|
|
$shippingAddress = $order->getShippingAddress(); |
|
|
|
|
|
|
89
|
|
|
$zone = null; |
|
90
|
|
|
|
|
91
|
|
|
if (null !== $shippingAddress) { |
|
92
|
|
|
$zone = $this->zoneMatcher->match($shippingAddress, Scope::TAX); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $zone ?: $this->defaultTaxZoneProvider->getZone($order); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|