Completed
Push — master ( 429264...faaec3 )
by Paweł
25s
created

Order::setNotes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 1
cc 1
eloc 2
nc 1
nop 1
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 $notes;
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 getNotes()
157
    {
158
        return $this->notes;
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164
    public function setNotes($notes)
165
    {
166
        $this->notes = $notes;
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    public function getSequenceType()
173
    {
174
        return 'order';
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function getItems()
181
    {
182
        return $this->items;
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188
    public function clearItems()
189
    {
190
        $this->items->clear();
191
192
        $this->recalculateItemsTotal();
193
    }
194
195
    /**
196
     * {@inheritdoc}
197
     */
198
    public function countItems()
199
    {
200
        return $this->items->count();
201
    }
202
203
    /**
204
     * {@inheritdoc}
205
     */
206
    public function addItem(OrderItemInterface $item)
207
    {
208
        if ($this->hasItem($item)) {
209
            return;
210
        }
211
212
        $this->itemsTotal += $item->getTotal();
213
        $this->items->add($item);
214
        $item->setOrder($this);
215
216
        $this->recalculateTotal();
217
    }
218
219
    /**
220
     * {@inheritdoc}
221
     */
222
    public function removeItem(OrderItemInterface $item)
223
    {
224
        if ($this->hasItem($item)) {
225
            $this->items->removeElement($item);
226
            $this->itemsTotal -= $item->getTotal();
227
            $this->recalculateTotal();
228
            $item->setOrder(null);
229
        }
230
    }
231
232
    /**
233
     * {@inheritdoc}
234
     */
235
    public function hasItem(OrderItemInterface $item)
236
    {
237
        return $this->items->contains($item);
238
    }
239
240
    /**
241
     * {@inheritdoc}
242
     */
243
    public function getItemsTotal()
244
    {
245
        return $this->itemsTotal;
246
    }
247
248
    /**
249
     * {@inheritdoc}
250
     */
251
    public function recalculateItemsTotal()
252
    {
253
        $this->itemsTotal = 0;
254
        foreach ($this->items as $item) {
255
            $this->itemsTotal += $item->getTotal();
256
        }
257
258
        $this->recalculateTotal();
259
    }
260
261
    /**
262
     * {@inheritdoc}
263
     */
264
    public function getComments()
265
    {
266
        return $this->comments;
267
    }
268
269
    /**
270
     * {@inheritdoc}
271
     */
272
    public function addComment(CommentInterface $comment)
273
    {
274
        if (!$this->comments->contains($comment)) {
275
            $comment->setOrder($this);
276
            $this->comments->add($comment);
277
        }
278
    }
279
280
    /**
281
     * {@inheritdoc}
282
     */
283
    public function removeComment(CommentInterface $comment)
284
    {
285
        if ($this->comments->contains($comment)) {
286
            $comment->setOrder(null);
287
            $this->comments->removeElement($comment);
288
        }
289
    }
290
291
    /**
292
     * {@inheritdoc}
293
     */
294
    public function getTotal()
295
    {
296
        return $this->total;
297
    }
298
299
    /**
300
     * {@inheritdoc}
301
     */
302
    public function setState($state)
303
    {
304
        $this->state = $state;
305
    }
306
307
    /**
308
     * {@inheritdoc}
309
     */
310
    public function getState()
311
    {
312
        return $this->state;
313
    }
314
315
    /**
316
     * {@inheritdoc}
317
     */
318
    public function getTotalQuantity()
319
    {
320
        $quantity = 0;
321
322
        foreach ($this->items as $item) {
323
            $quantity += $item->getQuantity();
324
        }
325
326
        return $quantity;
327
    }
328
329
    /**
330
     * {@inheritdoc}
331
     */
332
    public function isEmpty()
333
    {
334
        return $this->items->isEmpty();
335
    }
336
337
    /**
338
     * {@inheritdoc}
339
     */
340
    public function addIdentity(IdentityInterface $identity)
341
    {
342
        if (!$this->hasIdentity($identity)) {
343
            $this->identities->add($identity);
344
345
            $identity->setOrder($this);
346
        }
347
    }
348
349
    /**
350
     * {@inheritdoc}
351
     */
352
    public function hasIdentity(IdentityInterface $identity)
353
    {
354
        return $this->identities->contains($identity);
355
    }
356
357
    /**
358
     * {@inheritdoc}
359
     */
360
    public function getIdentities()
361
    {
362
        return $this->identities;
363
    }
364
365
    /**
366
     * {@inheritdoc}
367
     */
368
    public function removeIdentity(IdentityInterface $identity)
369
    {
370
        if ($this->hasIdentity($identity)) {
371
            $identity->setOrder(null);
372
            $this->identities->removeElement($identity);
373
        }
374
    }
375
376
    /**
377
     * {@inheritdoc}
378
     */
379
    public function getAdjustments($type = null)
380
    {
381
        if (null === $type) {
382
            return $this->adjustments;
383
        }
384
385
        return $this->adjustments->filter(function (AdjustmentInterface $adjustment) use ($type) {
386
            return $type === $adjustment->getType();
387
        });
388
    }
389
390
    /**
391
     * {@inheritdoc}
392
     */
393
    public function getAdjustmentsRecursively($type = null)
394
    {
395
        $adjustments = $this->getAdjustments($type)->toArray();
396
        foreach ($this->items as $item) {
397
            $adjustments = array_merge($adjustments, $item->getAdjustmentsRecursively($type));
398
        }
399
400
        return $adjustments;
401
    }
402
403
    /**
404
     * {@inheritdoc}
405
     */
406
    public function addAdjustment(AdjustmentInterface $adjustment)
407
    {
408
        if (!$this->hasAdjustment($adjustment)) {
409
            $this->adjustments->add($adjustment);
410
            $this->addToAdjustmentsTotal($adjustment);
411
            $adjustment->setAdjustable($this);
412
        }
413
    }
414
415
    /**
416
     * {@inheritdoc}
417
     */
418
    public function removeAdjustment(AdjustmentInterface $adjustment)
419
    {
420
        if (!$adjustment->isLocked() && $this->hasAdjustment($adjustment)) {
421
            $this->adjustments->removeElement($adjustment);
422
            $this->subtractFromAdjustmentsTotal($adjustment);
423
            $adjustment->setAdjustable(null);
424
        }
425
    }
426
427
    /**
428
     * {@inheritdoc}
429
     */
430
    public function hasAdjustment(AdjustmentInterface $adjustment)
431
    {
432
        return $this->adjustments->contains($adjustment);
433
    }
434
435
    /**
436
     * {@inheritdoc}
437
     */
438
    public function getAdjustmentsTotal($type = null)
439
    {
440
        if (null === $type) {
441
            return $this->adjustmentsTotal;
442
        }
443
444
        $total = 0;
445
        foreach ($this->getAdjustments($type) as $adjustment) {
446
            if (!$adjustment->isNeutral()) {
447
                $total += $adjustment->getAmount();
448
            }
449
        }
450
451
        return $total;
452
    }
453
454
    /**
455
     * {@inheritdoc}
456
     */
457
    public function getAdjustmentsTotalRecursively($type = null)
458
    {
459
        $total = 0;
460
        foreach ($this->getAdjustmentsRecursively($type) as $adjustment) {
461
            if (!$adjustment->isNeutral()) {
462
                $total += $adjustment->getAmount();
463
            }
464
        }
465
466
        return $total;
467
    }
468
469
    /**
470
     * {@inheritdoc}
471
     */
472
    public function removeAdjustments($type)
473
    {
474
        foreach ($this->getAdjustments($type) as $adjustment) {
475
            if ($adjustment->isLocked()) {
476
                continue;
477
            }
478
479
            $this->removeAdjustment($adjustment);
480
        }
481
    }
482
483
    /**
484
     * {@inheritdoc}
485
     */
486
    public function removeAdjustmentsRecursively($type = null)
487
    {
488
        $this->removeAdjustments($type);
489
        foreach ($this->items as $item) {
490
            $item->removeAdjustmentsRecursively($type);
491
        }
492
    }
493
494
    /**
495
     * {@inheritdoc}
496
     */
497
    public function recalculateAdjustmentsTotal()
498
    {
499
        $this->adjustmentsTotal = 0;
500
501
        foreach ($this->adjustments as $adjustment) {
502
            if (!$adjustment->isNeutral()) {
503
                $this->adjustmentsTotal += $adjustment->getAmount();
504
            }
505
        }
506
507
        $this->recalculateTotal();
508
    }
509
510
    /**
511
     * Calculate total.
512
     * Items total + Adjustments total.
513
     */
514
    protected function recalculateTotal()
515
    {
516
        $this->total = $this->itemsTotal + $this->adjustmentsTotal;
517
518
        if ($this->total < 0) {
519
            $this->total = 0;
520
        }
521
    }
522
523
    /**
524
     * @param AdjustmentInterface $adjustment
525
     */
526
    protected function addToAdjustmentsTotal(AdjustmentInterface $adjustment)
527
    {
528
        if (!$adjustment->isNeutral()) {
529
            $this->adjustmentsTotal += $adjustment->getAmount();
530
            $this->recalculateTotal();
531
        }
532
    }
533
534
    /**
535
     * @param AdjustmentInterface $adjustment
536
     */
537
    protected function subtractFromAdjustmentsTotal(AdjustmentInterface $adjustment)
538
    {
539
        if (!$adjustment->isNeutral()) {
540
            $this->adjustmentsTotal -= $adjustment->getAmount();
541
            $this->recalculateTotal();
542
        }
543
    }
544
}
545