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