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