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\Order\Model; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
15
|
|
|
use Doctrine\Common\Collections\Collection; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class Order implements OrderInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var mixed |
24
|
|
|
*/ |
25
|
|
|
protected $id; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \DateTime |
29
|
|
|
*/ |
30
|
|
|
protected $completedAt; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $number; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $additionalInformation; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Collection|OrderItemInterface[] |
44
|
|
|
*/ |
45
|
|
|
protected $items; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var int |
49
|
|
|
*/ |
50
|
|
|
protected $itemsTotal = 0; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var Collection|AdjustmentInterface[] |
54
|
|
|
*/ |
55
|
|
|
protected $adjustments; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var Collection|CommentInterface[] |
59
|
|
|
*/ |
60
|
|
|
protected $comments; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var Collection|IdentityInterface[] |
64
|
|
|
*/ |
65
|
|
|
protected $identities; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var int |
69
|
|
|
*/ |
70
|
|
|
protected $adjustmentsTotal = 0; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Calculated total. |
74
|
|
|
* Items total + adjustments total. |
75
|
|
|
* |
76
|
|
|
* @var int |
77
|
|
|
*/ |
78
|
|
|
protected $total = 0; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var \DateTime |
82
|
|
|
*/ |
83
|
|
|
protected $createdAt; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var \DateTime |
87
|
|
|
*/ |
88
|
|
|
protected $updatedAt; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var \DateTime |
92
|
|
|
*/ |
93
|
|
|
protected $deletedAt; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @var string |
97
|
|
|
*/ |
98
|
|
|
protected $state = OrderInterface::STATE_CART; |
99
|
|
|
|
100
|
|
|
public function __construct() |
101
|
|
|
{ |
102
|
|
|
$this->items = new ArrayCollection(); |
103
|
|
|
$this->adjustments = new ArrayCollection(); |
104
|
|
|
$this->comments = new ArrayCollection(); |
105
|
|
|
$this->identities = new ArrayCollection(); |
106
|
|
|
$this->createdAt = new \DateTime(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function getId() |
113
|
|
|
{ |
114
|
|
|
return $this->id; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function isCompleted() |
121
|
|
|
{ |
122
|
|
|
return null !== $this->completedAt; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
|
public function complete() |
129
|
|
|
{ |
130
|
|
|
$this->completedAt = new \DateTime(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
|
|
public function getCompletedAt() |
137
|
|
|
{ |
138
|
|
|
return $this->completedAt; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
|
|
public function setCompletedAt(\DateTime $completedAt = null) |
145
|
|
|
{ |
146
|
|
|
$this->completedAt = $completedAt; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
|
|
public function getNumber() |
153
|
|
|
{ |
154
|
|
|
return $this->number; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritdoc} |
159
|
|
|
*/ |
160
|
|
|
public function setNumber($number) |
161
|
|
|
{ |
162
|
|
|
$this->number = $number; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* {@inheritdoc} |
167
|
|
|
*/ |
168
|
|
|
public function getSequenceType() |
169
|
|
|
{ |
170
|
|
|
return 'order'; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* {@inheritdoc} |
175
|
|
|
*/ |
176
|
|
|
public function getItems() |
177
|
|
|
{ |
178
|
|
|
return $this->items; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* {@inheritdoc} |
183
|
|
|
*/ |
184
|
|
|
public function clearItems() |
185
|
|
|
{ |
186
|
|
|
$this->items->clear(); |
187
|
|
|
|
188
|
|
|
$this->recalculateItemsTotal(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* {@inheritdoc} |
193
|
|
|
*/ |
194
|
|
|
public function countItems() |
195
|
|
|
{ |
196
|
|
|
return $this->items->count(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* {@inheritdoc} |
201
|
|
|
*/ |
202
|
|
|
public function addItem(OrderItemInterface $item) |
203
|
|
|
{ |
204
|
|
|
if ($this->hasItem($item)) { |
205
|
|
|
return; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$this->itemsTotal += $item->getTotal(); |
209
|
|
|
$this->items->add($item); |
210
|
|
|
$item->setOrder($this); |
211
|
|
|
|
212
|
|
|
$this->recalculateTotal(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* {@inheritdoc} |
217
|
|
|
*/ |
218
|
|
|
public function removeItem(OrderItemInterface $item) |
219
|
|
|
{ |
220
|
|
|
if ($this->hasItem($item)) { |
221
|
|
|
$item->setOrder(null); |
222
|
|
|
$this->items->removeElement($item); |
223
|
|
|
$this->itemsTotal -= $item->getTotal(); |
224
|
|
|
|
225
|
|
|
$this->recalculateTotal(); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* {@inheritdoc} |
231
|
|
|
*/ |
232
|
|
|
public function hasItem(OrderItemInterface $item) |
233
|
|
|
{ |
234
|
|
|
return $this->items->contains($item); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* {@inheritdoc} |
239
|
|
|
*/ |
240
|
|
|
public function getItemsTotal() |
241
|
|
|
{ |
242
|
|
|
return $this->itemsTotal; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* {@inheritdoc} |
247
|
|
|
*/ |
248
|
|
|
public function recalculateItemsTotal() |
249
|
|
|
{ |
250
|
|
|
$this->itemsTotal = 0; |
251
|
|
|
foreach ($this->items as $item) { |
252
|
|
|
$this->itemsTotal += $item->getTotal(); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
$this->recalculateTotal(); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* {@inheritdoc} |
260
|
|
|
*/ |
261
|
|
|
public function getComments() |
262
|
|
|
{ |
263
|
|
|
return $this->comments; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* {@inheritdoc} |
268
|
|
|
*/ |
269
|
|
|
public function addComment(CommentInterface $comment) |
270
|
|
|
{ |
271
|
|
|
if (!$this->comments->contains($comment)) { |
272
|
|
|
$comment->setOrder($this); |
273
|
|
|
$this->comments->add($comment); |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* {@inheritdoc} |
279
|
|
|
*/ |
280
|
|
|
public function removeComment(CommentInterface $comment) |
281
|
|
|
{ |
282
|
|
|
if ($this->comments->contains($comment)) { |
283
|
|
|
$comment->setOrder(null); |
284
|
|
|
$this->comments->removeElement($comment); |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* {@inheritdoc} |
290
|
|
|
*/ |
291
|
|
|
public function getTotal() |
292
|
|
|
{ |
293
|
|
|
return $this->total; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* {@inheritdoc} |
298
|
|
|
*/ |
299
|
|
|
public function getCreatedAt() |
300
|
|
|
{ |
301
|
|
|
return $this->createdAt; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* {@inheritdoc} |
306
|
|
|
*/ |
307
|
|
|
public function setCreatedAt(\DateTime $createdAt) |
308
|
|
|
{ |
309
|
|
|
$this->createdAt = $createdAt; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* {@inheritdoc} |
314
|
|
|
*/ |
315
|
|
|
public function getUpdatedAt() |
316
|
|
|
{ |
317
|
|
|
return $this->updatedAt; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* {@inheritdoc} |
322
|
|
|
*/ |
323
|
|
|
public function setUpdatedAt(\DateTime $updatedAt) |
324
|
|
|
{ |
325
|
|
|
$this->updatedAt = $updatedAt; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* {@inheritdoc} |
330
|
|
|
*/ |
331
|
|
|
public function isDeleted() |
332
|
|
|
{ |
333
|
|
|
return null !== $this->deletedAt && new \DateTime() >= $this->deletedAt; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* {@inheritdoc} |
338
|
|
|
*/ |
339
|
|
|
public function getDeletedAt() |
340
|
|
|
{ |
341
|
|
|
return $this->deletedAt; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* {@inheritdoc} |
346
|
|
|
*/ |
347
|
|
|
public function setDeletedAt(\DateTime $deletedAt = null) |
348
|
|
|
{ |
349
|
|
|
$this->deletedAt = $deletedAt; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* {@inheritdoc} |
354
|
|
|
*/ |
355
|
|
|
public function setState($state) |
356
|
|
|
{ |
357
|
|
|
$this->state = $state; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* {@inheritdoc} |
362
|
|
|
*/ |
363
|
|
|
public function getState() |
364
|
|
|
{ |
365
|
|
|
return $this->state; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* {@inheritdoc} |
370
|
|
|
*/ |
371
|
|
|
public function getTotalItems() |
372
|
|
|
{ |
373
|
|
|
return $this->countItems(); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* {@inheritdoc} |
378
|
|
|
*/ |
379
|
|
|
public function getTotalQuantity() |
380
|
|
|
{ |
381
|
|
|
$quantity = 0; |
382
|
|
|
|
383
|
|
|
foreach ($this->items as $item) { |
384
|
|
|
$quantity += $item->getQuantity(); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
return $quantity; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* {@inheritdoc} |
392
|
|
|
*/ |
393
|
|
|
public function isEmpty() |
394
|
|
|
{ |
395
|
|
|
return $this->items->isEmpty(); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* {@inheritdoc} |
401
|
|
|
*/ |
402
|
|
|
public function addIdentity(IdentityInterface $identity) |
403
|
|
|
{ |
404
|
|
|
if (!$this->hasIdentity($identity)) { |
405
|
|
|
$this->identities->add($identity); |
406
|
|
|
|
407
|
|
|
$identity->setOrder($this); |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* {@inheritdoc} |
413
|
|
|
*/ |
414
|
|
|
public function hasIdentity(IdentityInterface $identity) |
415
|
|
|
{ |
416
|
|
|
return $this->identities->contains($identity); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* {@inheritdoc} |
421
|
|
|
*/ |
422
|
|
|
public function getIdentities() |
423
|
|
|
{ |
424
|
|
|
return $this->identities; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* {@inheritdoc} |
429
|
|
|
*/ |
430
|
|
|
public function removeIdentity(IdentityInterface $identity) |
431
|
|
|
{ |
432
|
|
|
if ($this->hasIdentity($identity)) { |
433
|
|
|
$identity->setOrder(null); |
434
|
|
|
$this->identities->removeElement($identity); |
435
|
|
|
} |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* {@inheritdoc} |
440
|
|
|
*/ |
441
|
|
|
public function getAdjustments($type = null) |
442
|
|
|
{ |
443
|
|
|
if (null === $type) { |
444
|
|
|
return $this->adjustments; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
return $this->adjustments->filter(function (AdjustmentInterface $adjustment) use ($type) { |
448
|
|
|
return $type === $adjustment->getType(); |
449
|
|
|
}); |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* {@inheritdoc} |
454
|
|
|
*/ |
455
|
|
|
public function addAdjustment(AdjustmentInterface $adjustment) |
456
|
|
|
{ |
457
|
|
|
if (!$this->hasAdjustment($adjustment)) { |
458
|
|
|
$adjustment->setAdjustable($this); |
459
|
|
|
$this->adjustments->add($adjustment); |
460
|
|
|
$this->addToAdjustmentsTotal($adjustment); |
461
|
|
|
} |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* {@inheritdoc} |
466
|
|
|
*/ |
467
|
|
|
public function removeAdjustment(AdjustmentInterface $adjustment) |
468
|
|
|
{ |
469
|
|
|
if (!$adjustment->isLocked() && $this->hasAdjustment($adjustment)) { |
470
|
|
|
$adjustment->setAdjustable(null); |
471
|
|
|
$this->adjustments->removeElement($adjustment); |
472
|
|
|
$this->subtractFromAdjustmentsTotal($adjustment); |
473
|
|
|
} |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* {@inheritdoc} |
478
|
|
|
*/ |
479
|
|
|
public function hasAdjustment(AdjustmentInterface $adjustment) |
480
|
|
|
{ |
481
|
|
|
return $this->adjustments->contains($adjustment); |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* {@inheritdoc} |
486
|
|
|
*/ |
487
|
|
|
public function getAdjustmentsTotal($type = null) |
488
|
|
|
{ |
489
|
|
|
if (null === $type) { |
490
|
|
|
return $this->adjustmentsTotal; |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
$total = 0; |
494
|
|
|
foreach ($this->getAdjustments($type) as $adjustment) { |
495
|
|
|
$total += $adjustment->getAmount(); |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
return $total; |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* {@inheritdoc} |
503
|
|
|
*/ |
504
|
|
|
public function removeAdjustments($type) |
505
|
|
|
{ |
506
|
|
|
foreach ($this->getAdjustments($type) as $adjustment) { |
507
|
|
|
if ($adjustment->isLocked()) { |
508
|
|
|
continue; |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
$this->removeAdjustment($adjustment); |
512
|
|
|
} |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* {@inheritdoc} |
517
|
|
|
*/ |
518
|
|
|
public function clearAdjustments() |
519
|
|
|
{ |
520
|
|
|
$this->adjustments->clear(); |
521
|
|
|
$this->recalculateAdjustmentsTotal(); |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
/** |
525
|
|
|
* {@inheritdoc} |
526
|
|
|
*/ |
527
|
|
|
public function recalculateAdjustmentsTotal() |
528
|
|
|
{ |
529
|
|
|
$this->adjustmentsTotal = 0; |
530
|
|
|
|
531
|
|
|
foreach ($this->adjustments as $adjustment) { |
532
|
|
|
if (!$adjustment->isNeutral()) { |
533
|
|
|
$this->adjustmentsTotal += $adjustment->getAmount(); |
534
|
|
|
} |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
$this->recalculateTotal(); |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
/** |
541
|
|
|
* Calculate total. |
542
|
|
|
* Items total + Adjustments total. |
543
|
|
|
*/ |
544
|
|
|
protected function recalculateTotal() |
545
|
|
|
{ |
546
|
|
|
$this->total = $this->itemsTotal + $this->adjustmentsTotal; |
547
|
|
|
|
548
|
|
|
if ($this->total < 0) { |
549
|
|
|
$this->total = 0; |
550
|
|
|
} |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
/** |
554
|
|
|
* @param AdjustmentInterface $adjustment |
555
|
|
|
*/ |
556
|
|
|
protected function addToAdjustmentsTotal(AdjustmentInterface $adjustment) |
557
|
|
|
{ |
558
|
|
|
if (!$adjustment->isNeutral()) { |
559
|
|
|
$this->adjustmentsTotal += $adjustment->getAmount(); |
560
|
|
|
$this->recalculateTotal(); |
561
|
|
|
} |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
/** |
565
|
|
|
* @param AdjustmentInterface $adjustment |
566
|
|
|
*/ |
567
|
|
|
protected function subtractFromAdjustmentsTotal(AdjustmentInterface $adjustment) |
568
|
|
|
{ |
569
|
|
|
if (!$adjustment->isNeutral()) { |
570
|
|
|
$this->adjustmentsTotal -= $adjustment->getAmount(); |
571
|
|
|
$this->recalculateTotal(); |
572
|
|
|
} |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
/** |
576
|
|
|
* {@inheritdoc} |
577
|
|
|
*/ |
578
|
|
|
public function getAdditionalInformation() |
579
|
|
|
{ |
580
|
|
|
return $this->additionalInformation; |
581
|
|
|
} |
582
|
|
|
|
583
|
|
|
/** |
584
|
|
|
* {@inheritdoc} |
585
|
|
|
*/ |
586
|
|
|
public function setAdditionalInformation($information) |
587
|
|
|
{ |
588
|
|
|
$this->additionalInformation = $information; |
589
|
|
|
} |
590
|
|
|
} |
591
|
|
|
|