1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gewebe\SyliusProductDepositPlugin\OrderProcessing; |
6
|
|
|
|
7
|
|
|
use Gewebe\SyliusProductDepositPlugin\Entity\AdjustmentInterface; |
8
|
|
|
use Gewebe\SyliusProductDepositPlugin\Entity\ProductVariantInterface; |
9
|
|
|
use Sylius\Component\Addressing\Matcher\ZoneMatcherInterface; |
10
|
|
|
use Sylius\Component\Addressing\Model\ZoneInterface; |
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\Core\Model\OrderInterface; |
15
|
|
|
use Sylius\Component\Order\Factory\AdjustmentFactoryInterface; |
16
|
|
|
use Sylius\Component\Order\Model\OrderInterface as BaseOrderInterface; |
17
|
|
|
use Sylius\Component\Order\Model\OrderItemUnitInterface; |
18
|
|
|
use Sylius\Component\Order\Processor\OrderProcessorInterface; |
19
|
|
|
use Webmozart\Assert\Assert; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Apply deposit adjustment to order item units |
23
|
|
|
*/ |
24
|
|
|
final class OrderDepositProcessor implements OrderProcessorInterface |
25
|
|
|
{ |
26
|
|
|
/** @var AdjustmentFactoryInterface */ |
27
|
|
|
private $adjustmentFactory; |
28
|
|
|
|
29
|
|
|
/** @var OrderTaxesApplicatorInterface */ |
30
|
|
|
private $orderDepositTaxesApplicator; |
31
|
|
|
|
32
|
|
|
/** @var ZoneMatcherInterface */ |
33
|
|
|
private $zoneMatcher; |
34
|
|
|
|
35
|
|
|
/** @var ZoneProviderInterface */ |
36
|
|
|
private $defaultTaxZoneProvider; |
37
|
|
|
|
38
|
|
|
public function __construct( |
39
|
|
|
AdjustmentFactoryInterface $adjustmentFactory, |
40
|
|
|
OrderTaxesApplicatorInterface $orderDepositTaxesApplicator, |
41
|
|
|
ZoneMatcherInterface $zoneMatcher, |
42
|
|
|
ZoneProviderInterface $defaultTaxZoneProvider |
43
|
|
|
) { |
44
|
|
|
$this->adjustmentFactory = $adjustmentFactory; |
45
|
|
|
$this->orderDepositTaxesApplicator = $orderDepositTaxesApplicator; |
46
|
|
|
$this->zoneMatcher = $zoneMatcher; |
47
|
|
|
$this->defaultTaxZoneProvider = $defaultTaxZoneProvider; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function process(BaseOrderInterface $order): void |
51
|
|
|
{ |
52
|
|
|
Assert::isInstanceOf($order, OrderInterface::class); |
53
|
|
|
|
54
|
|
|
$channel = $order->getChannel(); |
|
|
|
|
55
|
|
|
if (null === $channel) { |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
foreach ($order->getItems() as $item) { |
60
|
|
|
/** @var ProductVariantInterface $variant */ |
61
|
|
|
$variant = $item->getVariant(); |
62
|
|
|
|
63
|
|
|
$depositPrice = $variant->getDepositPriceByChannel($channel); |
64
|
|
|
if (null === $depositPrice) { |
65
|
|
|
continue; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
foreach ($item->getUnits() as $unit) { |
69
|
|
|
$this->addAdjustment($unit, $depositPrice); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// apply deposit taxes |
74
|
|
|
$zone = $this->getTaxZone($order); |
75
|
|
|
if (null !== $zone) { |
76
|
|
|
$this->orderDepositTaxesApplicator->apply($order, $zone); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
private function addAdjustment(OrderItemUnitInterface $unit, int $amount): void |
82
|
|
|
{ |
83
|
|
|
$adjustment = $this->adjustmentFactory->createWithData( |
84
|
|
|
AdjustmentInterface::DEPOSIT_ADJUSTMENT, |
85
|
|
|
'Deposit', |
86
|
|
|
$amount |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$unit->addAdjustment($adjustment); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function getTaxZone(OrderInterface $order): ?ZoneInterface |
93
|
|
|
{ |
94
|
|
|
$shippingAddress = $order->getShippingAddress(); |
95
|
|
|
$zone = null; |
96
|
|
|
|
97
|
|
|
if (null !== $shippingAddress) { |
98
|
|
|
$zone = $this->zoneMatcher->match($shippingAddress, Scope::TAX); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $zone ?? $this->defaultTaxZoneProvider->getZone($order); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|