1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Component\Core\Model; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
15
|
|
|
use Doctrine\Common\Collections\Collection; |
16
|
|
|
use Sylius\Component\Product\Model\ProductVariant as BaseVariant; |
17
|
|
|
use Sylius\Component\Shipping\Model\ShippingCategoryInterface; |
18
|
|
|
use Sylius\Component\Taxation\Model\TaxCategoryInterface; |
19
|
|
|
use Webmozart\Assert\Assert; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class ProductVariant extends BaseVariant implements ProductVariantInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
protected $version = 1; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
protected $onHold = 0; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
protected $onHand = 0; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
protected $tracked = false; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var float |
48
|
|
|
*/ |
49
|
|
|
protected $weight; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var float |
53
|
|
|
*/ |
54
|
|
|
protected $width; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var float |
58
|
|
|
*/ |
59
|
|
|
protected $height; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var float |
63
|
|
|
*/ |
64
|
|
|
protected $depth; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var TaxCategoryInterface |
68
|
|
|
*/ |
69
|
|
|
protected $taxCategory; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var ShippingCategoryInterface |
73
|
|
|
*/ |
74
|
|
|
protected $shippingCategory; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var Collection |
78
|
|
|
*/ |
79
|
|
|
protected $channelPricings; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var bool |
83
|
|
|
*/ |
84
|
|
|
protected $shippingRequired = true; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var Collection|ProductImageInterface[] |
88
|
|
|
*/ |
89
|
|
|
protected $images; |
90
|
|
|
|
91
|
|
|
public function __construct() |
92
|
|
|
{ |
93
|
|
|
parent::__construct(); |
94
|
|
|
|
95
|
|
|
$this->channelPricings = new ArrayCollection(); |
96
|
|
|
$this->images = new ArrayCollection(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function __toString() |
103
|
|
|
{ |
104
|
|
|
$string = (string) $this->getProduct()->getName(); |
105
|
|
|
|
106
|
|
|
if (!$this->getOptionValues()->isEmpty()) { |
107
|
|
|
$string .= '('; |
108
|
|
|
|
109
|
|
|
foreach ($this->getOptionValues() as $option) { |
110
|
|
|
$string .= $option->getOption()->getName().': '.$option->getValue().', '; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$string = substr($string, 0, -2).')'; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $string; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
|
|
public function getVersion() |
123
|
|
|
{ |
124
|
|
|
return $this->version; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
|
|
public function setVersion($version) |
131
|
|
|
{ |
132
|
|
|
$this->version = $version; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
|
public function isInStock() |
139
|
|
|
{ |
140
|
|
|
return 0 < $this->onHand; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
|
|
public function getOnHold() |
147
|
|
|
{ |
148
|
|
|
return $this->onHold; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
|
|
public function setOnHold($onHold) |
155
|
|
|
{ |
156
|
|
|
$this->onHold = $onHold; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
|
|
public function getOnHand() |
163
|
|
|
{ |
164
|
|
|
return $this->onHand; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* {@inheritdoc} |
169
|
|
|
*/ |
170
|
|
|
public function setOnHand($onHand) |
171
|
|
|
{ |
172
|
|
|
$this->onHand = (0 > $onHand) ? 0 : $onHand; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* {@inheritdoc} |
177
|
|
|
*/ |
178
|
|
|
public function isTracked() |
179
|
|
|
{ |
180
|
|
|
return $this->tracked; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritdoc} |
185
|
|
|
*/ |
186
|
|
|
public function setTracked($tracked) |
187
|
|
|
{ |
188
|
|
|
Assert::boolean($tracked); |
189
|
|
|
|
190
|
|
|
$this->tracked = $tracked; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* {@inheritdoc} |
195
|
|
|
*/ |
196
|
|
|
public function getInventoryName() |
197
|
|
|
{ |
198
|
|
|
return $this->getProduct()->getName(); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* {@inheritdoc} |
203
|
|
|
*/ |
204
|
|
|
public function getShippingCategory() |
205
|
|
|
{ |
206
|
|
|
return $this->shippingCategory; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* {@inheritdoc} |
211
|
|
|
*/ |
212
|
|
|
public function setShippingCategory(ShippingCategoryInterface $category = null) |
213
|
|
|
{ |
214
|
|
|
$this->shippingCategory = $category; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* {@inheritdoc} |
219
|
|
|
*/ |
220
|
|
|
public function getWeight() |
221
|
|
|
{ |
222
|
|
|
return $this->weight; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* {@inheritdoc} |
227
|
|
|
*/ |
228
|
|
|
public function setWeight($weight) |
229
|
|
|
{ |
230
|
|
|
$this->weight = $weight; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* {@inheritdoc} |
235
|
|
|
*/ |
236
|
|
|
public function getWidth() |
237
|
|
|
{ |
238
|
|
|
return $this->width; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* {@inheritdoc} |
243
|
|
|
*/ |
244
|
|
|
public function setWidth($width) |
245
|
|
|
{ |
246
|
|
|
$this->width = $width; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* {@inheritdoc} |
251
|
|
|
*/ |
252
|
|
|
public function getHeight() |
253
|
|
|
{ |
254
|
|
|
return $this->height; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* {@inheritdoc} |
259
|
|
|
*/ |
260
|
|
|
public function setHeight($height) |
261
|
|
|
{ |
262
|
|
|
$this->height = $height; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* {@inheritdoc} |
267
|
|
|
*/ |
268
|
|
|
public function getDepth() |
269
|
|
|
{ |
270
|
|
|
return $this->depth; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* {@inheritdoc} |
275
|
|
|
*/ |
276
|
|
|
public function setDepth($depth) |
277
|
|
|
{ |
278
|
|
|
$this->depth = $depth; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* {@inheritdoc} |
283
|
|
|
*/ |
284
|
|
|
public function getShippingWeight() |
285
|
|
|
{ |
286
|
|
|
return $this->getWeight(); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* {@inheritdoc} |
291
|
|
|
*/ |
292
|
|
|
public function getShippingWidth() |
293
|
|
|
{ |
294
|
|
|
return $this->getWidth(); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* {@inheritdoc} |
299
|
|
|
*/ |
300
|
|
|
public function getShippingHeight() |
301
|
|
|
{ |
302
|
|
|
return $this->getHeight(); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* {@inheritdoc} |
307
|
|
|
*/ |
308
|
|
|
public function getShippingDepth() |
309
|
|
|
{ |
310
|
|
|
return $this->getDepth(); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* {@inheritdoc} |
315
|
|
|
*/ |
316
|
|
|
public function getShippingVolume() |
317
|
|
|
{ |
318
|
|
|
return $this->depth * $this->height * $this->width; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* {@inheritdoc} |
323
|
|
|
*/ |
324
|
|
|
public function getTaxCategory() |
325
|
|
|
{ |
326
|
|
|
return $this->taxCategory; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* {@inheritdoc} |
331
|
|
|
*/ |
332
|
|
|
public function setTaxCategory(TaxCategoryInterface $category = null) |
333
|
|
|
{ |
334
|
|
|
$this->taxCategory = $category; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* {@inheritdoc} |
339
|
|
|
*/ |
340
|
|
|
public function getChannelPricings() |
341
|
|
|
{ |
342
|
|
|
return $this->channelPricings; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* {@inheritdoc} |
347
|
|
|
*/ |
348
|
|
|
public function getChannelPricingForChannel(ChannelInterface $channel) |
349
|
|
|
{ |
350
|
|
|
if ($this->channelPricings->containsKey($channel->getCode())) { |
351
|
|
|
return $this->channelPricings->get($channel->getCode()); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
return null; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* {@inheritdoc} |
359
|
|
|
*/ |
360
|
|
|
public function hasChannelPricingForChannel(ChannelInterface $channel) |
361
|
|
|
{ |
362
|
|
|
return null !== $this->getChannelPricingForChannel($channel); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* {@inheritdoc} |
367
|
|
|
*/ |
368
|
|
|
public function hasChannelPricing(ChannelPricingInterface $channelPricing) |
369
|
|
|
{ |
370
|
|
|
return $this->channelPricings->contains($channelPricing); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* {@inheritdoc} |
375
|
|
|
*/ |
376
|
|
|
public function addChannelPricing(ChannelPricingInterface $channelPricing) |
377
|
|
|
{ |
378
|
|
|
if (!$this->hasChannelPricing($channelPricing)) { |
379
|
|
|
$channelPricing->setProductVariant($this); |
380
|
|
|
$this->channelPricings->set($channelPricing->getChannelCode(), $channelPricing); |
381
|
|
|
} |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* {@inheritdoc} |
386
|
|
|
*/ |
387
|
|
|
public function removeChannelPricing(ChannelPricingInterface $channelPricing) |
388
|
|
|
{ |
389
|
|
|
if ($this->hasChannelPricing($channelPricing)) { |
390
|
|
|
$channelPricing->setProductVariant(null); |
391
|
|
|
$this->channelPricings->remove($channelPricing->getChannelCode()); |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* {@inheritdoc} |
397
|
|
|
*/ |
398
|
|
|
public function isShippingRequired() |
399
|
|
|
{ |
400
|
|
|
return $this->shippingRequired; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* {@inheritdoc} |
405
|
|
|
*/ |
406
|
|
|
public function setShippingRequired($shippingRequired) |
407
|
|
|
{ |
408
|
|
|
$this->shippingRequired = $shippingRequired; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* {@inheritdoc} |
413
|
|
|
*/ |
414
|
|
|
public function getImages() |
415
|
|
|
{ |
416
|
|
|
return $this->images; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* {@inheritdoc} |
421
|
|
|
*/ |
422
|
|
|
public function getImagesByType($type) |
423
|
|
|
{ |
424
|
|
|
return $this->images->filter(function (ProductImageInterface $image) use ($type) { |
425
|
|
|
return $type === $image->getType(); |
426
|
|
|
}); |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* {@inheritdoc} |
431
|
|
|
*/ |
432
|
|
|
public function hasImages() |
433
|
|
|
{ |
434
|
|
|
return !$this->images->isEmpty(); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* {@inheritdoc} |
439
|
|
|
*/ |
440
|
|
|
public function hasImage(ProductImageInterface $image) |
441
|
|
|
{ |
442
|
|
|
return $this->images->contains($image); |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* {@inheritdoc} |
447
|
|
|
*/ |
448
|
|
|
public function addImage(ProductImageInterface $image) |
449
|
|
|
{ |
450
|
|
|
if ($this->hasImage($image)) { |
451
|
|
|
return; |
452
|
|
|
} |
453
|
|
|
$image->setOwner($this->getProduct()); |
454
|
|
|
$image->addProductVariant($this); |
455
|
|
|
$this->images->add($image); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* {@inheritdoc} |
460
|
|
|
*/ |
461
|
|
|
public function removeImage(ProductImageInterface $image) |
462
|
|
|
{ |
463
|
|
|
if ($this->hasImage($image)) { |
464
|
|
|
$this->images->removeElement($image); |
465
|
|
|
} |
466
|
|
|
} |
467
|
|
|
} |
468
|
|
|
|