|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Shopware\Core\System\SalesChannel; |
|
4
|
|
|
|
|
5
|
|
|
use Shopware\Core\Checkout\Cart\Delivery\Struct\ShippingLocation; |
|
6
|
|
|
use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRule; |
|
7
|
|
|
use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRuleCollection; |
|
8
|
|
|
use Shopware\Core\Checkout\Customer\Aggregate\CustomerGroup\CustomerGroupEntity; |
|
9
|
|
|
use Shopware\Core\Checkout\Customer\CustomerEntity; |
|
10
|
|
|
use Shopware\Core\Checkout\Payment\PaymentMethodEntity; |
|
11
|
|
|
use Shopware\Core\Checkout\Shipping\ShippingMethodEntity; |
|
12
|
|
|
use Shopware\Core\Framework\Context; |
|
13
|
|
|
use Shopware\Core\Framework\Struct\Struct; |
|
14
|
|
|
use Shopware\Core\System\Currency\CurrencyEntity; |
|
15
|
|
|
use Shopware\Core\System\SalesChannel\Exception\ContextPermissionsLockedException; |
|
16
|
|
|
use Shopware\Core\System\SalesChannel\Exception\ContextRulesLockedException; |
|
17
|
|
|
use Shopware\Core\System\Tax\Exception\TaxNotFoundException; |
|
18
|
|
|
use Shopware\Core\System\Tax\TaxCollection; |
|
19
|
|
|
|
|
20
|
|
|
class SalesChannelContext extends Struct |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Unique token for context, e.g. stored in session or provided in request headers |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $token; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var CustomerGroupEntity |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $currentCustomerGroup; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var CustomerGroupEntity |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $fallbackCustomerGroup; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var CurrencyEntity |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $currency; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var SalesChannelEntity |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $salesChannel; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var TaxCollection |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $taxRules; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var CustomerEntity|null |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $customer; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var PaymentMethodEntity |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $paymentMethod; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var ShippingMethodEntity |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $shippingMethod; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var ShippingLocation |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $shippingLocation; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var array |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $rulesIds; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @var bool |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $rulesLocked = false; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @var array |
|
86
|
|
|
*/ |
|
87
|
|
|
protected $permissions = []; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @var bool |
|
91
|
|
|
*/ |
|
92
|
|
|
protected $permisionsLocked = false; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @var Context |
|
96
|
|
|
*/ |
|
97
|
|
|
private $context; |
|
98
|
|
|
|
|
99
|
|
|
public function __construct( |
|
100
|
|
|
Context $baseContext, |
|
101
|
|
|
string $token, |
|
102
|
|
|
SalesChannelEntity $salesChannel, |
|
103
|
|
|
CurrencyEntity $currency, |
|
104
|
|
|
CustomerGroupEntity $currentCustomerGroup, |
|
105
|
|
|
CustomerGroupEntity $fallbackCustomerGroup, |
|
106
|
|
|
TaxCollection $taxRules, |
|
107
|
|
|
PaymentMethodEntity $paymentMethod, |
|
108
|
|
|
ShippingMethodEntity $shippingMethod, |
|
109
|
|
|
ShippingLocation $shippingLocation, |
|
110
|
|
|
?CustomerEntity $customer, |
|
111
|
|
|
array $rulesIds = [] |
|
112
|
|
|
) { |
|
113
|
|
|
$this->currentCustomerGroup = $currentCustomerGroup; |
|
114
|
|
|
$this->fallbackCustomerGroup = $fallbackCustomerGroup; |
|
115
|
|
|
$this->currency = $currency; |
|
116
|
|
|
$this->salesChannel = $salesChannel; |
|
117
|
|
|
$this->taxRules = $taxRules; |
|
118
|
|
|
$this->customer = $customer; |
|
119
|
|
|
$this->paymentMethod = $paymentMethod; |
|
120
|
|
|
$this->shippingMethod = $shippingMethod; |
|
121
|
|
|
$this->shippingLocation = $shippingLocation; |
|
122
|
|
|
$this->rulesIds = $rulesIds; |
|
123
|
|
|
$this->token = $token; |
|
124
|
|
|
$this->context = $baseContext; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function getCurrentCustomerGroup(): CustomerGroupEntity |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->currentCustomerGroup; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function getFallbackCustomerGroup(): CustomerGroupEntity |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->fallbackCustomerGroup; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function getCurrency(): CurrencyEntity |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->currency; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function getSalesChannel(): SalesChannelEntity |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->salesChannel; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function getTaxRules(): TaxCollection |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->taxRules; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Get the tax rules depend on the customer billing address |
|
154
|
|
|
* respectively the shippingLocation if there is no customer |
|
155
|
|
|
*/ |
|
156
|
|
|
public function buildTaxRules(string $taxId): TaxRuleCollection |
|
157
|
|
|
{ |
|
158
|
|
|
$tax = $this->taxRules->get($taxId); |
|
159
|
|
|
|
|
160
|
|
|
if ($tax === null || $tax->getRules() === null) { |
|
161
|
|
|
throw new TaxNotFoundException($taxId); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
if ($tax->getRules()->first() !== null) { |
|
165
|
|
|
return new TaxRuleCollection([ |
|
166
|
|
|
new TaxRule($tax->getRules()->first()->getTaxRate(), 100), |
|
167
|
|
|
]); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return new TaxRuleCollection([ |
|
171
|
|
|
new TaxRule($tax->getTaxRate(), 100), |
|
172
|
|
|
]); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
public function getCustomer(): ?CustomerEntity |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->customer; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
public function getPaymentMethod(): PaymentMethodEntity |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->paymentMethod; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public function getShippingMethod(): ShippingMethodEntity |
|
186
|
|
|
{ |
|
187
|
|
|
return $this->shippingMethod; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
public function getShippingLocation(): ShippingLocation |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->shippingLocation; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public function getContext(): Context |
|
196
|
|
|
{ |
|
197
|
|
|
return $this->context; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
public function getRuleIds(): array |
|
201
|
|
|
{ |
|
202
|
|
|
return $this->rulesIds; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
public function setRuleIds(array $ruleIds): void |
|
206
|
|
|
{ |
|
207
|
|
|
if ($this->rulesLocked) { |
|
208
|
|
|
throw new ContextRulesLockedException(); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
$this->rulesIds = array_filter(array_values($ruleIds)); |
|
212
|
|
|
$this->getContext()->setRuleIds($this->rulesIds); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
public function lockRules(): void |
|
216
|
|
|
{ |
|
217
|
|
|
$this->rulesLocked = true; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
public function lockPermissions(): void |
|
221
|
|
|
{ |
|
222
|
|
|
$this->permisionsLocked = true; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
public function getToken(): string |
|
226
|
|
|
{ |
|
227
|
|
|
return $this->token; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
public function getTaxState(): string |
|
231
|
|
|
{ |
|
232
|
|
|
return $this->context->getTaxState(); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
public function setTaxState(string $taxState): void |
|
236
|
|
|
{ |
|
237
|
|
|
$this->context->setTaxState($taxState); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
public function getTaxCalculationType(): string |
|
241
|
|
|
{ |
|
242
|
|
|
return $this->getSalesChannel()->getTaxCalculationType(); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
public function getPermissions(): array |
|
246
|
|
|
{ |
|
247
|
|
|
return $this->permissions; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
public function setPermissions(array $permissions): void |
|
251
|
|
|
{ |
|
252
|
|
|
if ($this->permisionsLocked) { |
|
253
|
|
|
throw new ContextPermissionsLockedException(); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
$this->permissions = array_filter($permissions); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
public function getApiAlias(): string |
|
260
|
|
|
{ |
|
261
|
|
|
return 'sales_channel_context'; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
public function hasPermission(string $permission): bool |
|
265
|
|
|
{ |
|
266
|
|
|
return $this->permissions[$permission] ?? false; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
public function getSalesChannelId(): string |
|
270
|
|
|
{ |
|
271
|
|
|
return $this->getSalesChannel()->getId(); |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
|