1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Rafał Muszyński <[email protected]> |
5
|
|
|
* @copyright 2015 Sourcefabric z.ú. |
6
|
|
|
* @license http://www.gnu.org/licenses/gpl-3.0.txt |
7
|
|
|
*/ |
8
|
|
|
namespace Newscoop\PaywallBundle\Entity; |
9
|
|
|
|
10
|
|
|
use Doctrine\ORM\Mapping as ORM; |
11
|
|
|
use Doctrine\Common\Collections\Collection; |
12
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
13
|
|
|
use Newscoop\Entity\User; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Order entity. |
17
|
|
|
* |
18
|
|
|
* @ORM\Entity(repositoryClass="Newscoop\PaywallBundle\Entity\Repository\OrderRepository") |
19
|
|
|
* @ORM\Table(name="plugin_paywall_order") |
20
|
|
|
*/ |
21
|
|
|
class Order implements OrderInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @ORM\Id |
25
|
|
|
* @ORM\GeneratedValue |
26
|
|
|
* @ORM\Column(type="integer", name="id") |
27
|
|
|
* |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
protected $id; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @ORM\OneToMany(targetEntity="UserSubscription", mappedBy="order", orphanRemoval=true, cascade={"all"}) |
34
|
|
|
* |
35
|
|
|
* @var Collection |
36
|
|
|
*/ |
37
|
|
|
protected $items; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @ORM\Column(type="string", name="currency", length=3) |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $currency; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @ORM\Column(type="integer", name="total") |
48
|
|
|
* |
49
|
|
|
* @var int |
50
|
|
|
*/ |
51
|
|
|
protected $total = 0; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @ORM\Column(type="integer", name="items_total") |
55
|
|
|
* |
56
|
|
|
* @var int |
57
|
|
|
*/ |
58
|
|
|
protected $itemsTotal = 0; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @ORM\Column(type="integer", name="discount_total") |
62
|
|
|
* |
63
|
|
|
* @var int |
64
|
|
|
*/ |
65
|
|
|
protected $discountTotal = 0; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @ORM\ManyToOne(targetEntity="Newscoop\Entity\User") |
69
|
|
|
* @ORM\JoinColumn(name="user_id", referencedColumnName="Id") |
70
|
|
|
* |
71
|
|
|
* @var User |
72
|
|
|
*/ |
73
|
|
|
protected $user; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @ORM\OneToMany(targetEntity="Modification", mappedBy="order", orphanRemoval=true, cascade={"all"}) |
77
|
|
|
* |
78
|
|
|
* @var Collection |
79
|
|
|
*/ |
80
|
|
|
protected $modifications; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @ORM\OneToMany(targetEntity="Payment", mappedBy="order", orphanRemoval=true, cascade={"all"}) |
84
|
|
|
* |
85
|
|
|
* @var Collection |
86
|
|
|
*/ |
87
|
|
|
protected $payments; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @ORM\ManyToMany(targetEntity="Discount", cascade={"persist"}) |
91
|
|
|
* @ORM\JoinTable(name="plugin_paywall_order_discount", |
92
|
|
|
* joinColumns={ |
93
|
|
|
* @ORM\JoinColumn(name="order_id", referencedColumnName="id") |
94
|
|
|
* }, |
95
|
|
|
* inverseJoinColumns={ |
96
|
|
|
* @ORM\JoinColumn(name="discount_id", referencedColumnName="id") |
97
|
|
|
* } |
98
|
|
|
* ) |
99
|
|
|
* |
100
|
|
|
* @var Collection |
101
|
|
|
*/ |
102
|
|
|
protected $discounts; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @ORM\Column(type="string", name="payment_state") |
106
|
|
|
* |
107
|
|
|
* @var string |
108
|
|
|
*/ |
109
|
|
|
protected $paymentState = PaymentInterface::STATE_NEW; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @ORM\Column(type="datetime", name="updated_at", nullable=true) |
113
|
|
|
* |
114
|
|
|
* @var \DateTime |
115
|
|
|
*/ |
116
|
|
|
protected $updatedAt; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @ORM\Column(type="datetime", name="created_at") |
120
|
|
|
* |
121
|
|
|
* @var \DateTime |
122
|
|
|
*/ |
123
|
|
|
protected $createdAt; |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Constructor. |
127
|
|
|
*/ |
128
|
|
|
public function __construct() |
129
|
|
|
{ |
130
|
|
|
$this->items = new ArrayCollection(); |
131
|
|
|
$this->modifications = new ArrayCollection(); |
132
|
|
|
$this->payments = new ArrayCollection(); |
133
|
|
|
$this->discounts = new ArrayCollection(); |
134
|
|
|
$this->createdAt = new \DateTime(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
|
|
public function getItems() |
141
|
|
|
{ |
142
|
|
|
return $this->items; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
|
|
public function setItems(Collection $items) |
149
|
|
|
{ |
150
|
|
|
$this->items = $items; |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
|
|
public function countItems() |
159
|
|
|
{ |
160
|
|
|
return $this->items->count(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritdoc} |
165
|
|
|
*/ |
166
|
|
|
public function getUser() |
167
|
|
|
{ |
168
|
|
|
return $this->user; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* {@inheritdoc} |
173
|
|
|
*/ |
174
|
|
|
public function setUser(User $user) |
175
|
|
|
{ |
176
|
|
|
$this->user = $user; |
177
|
|
|
|
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* {@inheritdoc} |
183
|
|
|
*/ |
184
|
|
|
public function addItem($item) |
185
|
|
|
{ |
186
|
|
|
if ($this->hasItem($item)) { |
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$this->items->add($item); |
191
|
|
|
|
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* {@inheritdoc} |
197
|
|
|
*/ |
198
|
|
|
public function removeItem($item) |
199
|
|
|
{ |
200
|
|
|
if ($this->hasItem($item)) { |
201
|
|
|
$this->items->removeElement($item); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* {@inheritdoc} |
209
|
|
|
*/ |
210
|
|
|
public function hasItem($item) |
211
|
|
|
{ |
212
|
|
|
return $this->items->contains($item); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* {@inheritdoc} |
217
|
|
|
*/ |
218
|
|
|
public function getToPay() |
219
|
|
|
{ |
220
|
|
|
return $this->getTotal(); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* {@inheritdoc} |
225
|
|
|
*/ |
226
|
|
|
public function setToPay($total) |
227
|
|
|
{ |
228
|
|
|
$this->setTotal($total); |
229
|
|
|
|
230
|
|
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Gets the value of total. |
235
|
|
|
* |
236
|
|
|
* @return int |
237
|
|
|
*/ |
238
|
|
|
public function getTotal() |
239
|
|
|
{ |
240
|
|
|
return $this->total / 100; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Sets the value of total. |
245
|
|
|
* |
246
|
|
|
* @param int $total the total |
247
|
|
|
* |
248
|
|
|
* @return self |
249
|
|
|
*/ |
250
|
|
|
public function setTotal($total) |
251
|
|
|
{ |
252
|
|
|
$this->total = $total * 100; |
253
|
|
|
|
254
|
|
|
return $this; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Gets the value of id. |
259
|
|
|
* |
260
|
|
|
* @return int |
261
|
|
|
*/ |
262
|
|
|
public function getId() |
263
|
|
|
{ |
264
|
|
|
return $this->id; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Sets the value of id. |
269
|
|
|
* |
270
|
|
|
* @param int $id the id |
271
|
|
|
* |
272
|
|
|
* @return self |
273
|
|
|
*/ |
274
|
|
|
public function setId($id) |
275
|
|
|
{ |
276
|
|
|
$this->id = $id; |
277
|
|
|
|
278
|
|
|
return $this; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Gets the value of updatedAt. |
283
|
|
|
* |
284
|
|
|
* @return \DateTime |
285
|
|
|
*/ |
286
|
|
|
public function getUpdatedAt() |
287
|
|
|
{ |
288
|
|
|
return $this->updatedAt; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Sets the value of updatedAt. |
293
|
|
|
* |
294
|
|
|
* @param \DateTime $updatedAt the updated at |
295
|
|
|
* |
296
|
|
|
* @return self |
297
|
|
|
*/ |
298
|
|
|
public function setUpdatedAt(\DateTime $updatedAt) |
299
|
|
|
{ |
300
|
|
|
$this->updatedAt = $updatedAt; |
301
|
|
|
|
302
|
|
|
return $this; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Gets the value of createdAt. |
307
|
|
|
* |
308
|
|
|
* @return \DateTime |
309
|
|
|
*/ |
310
|
|
|
public function getCreatedAt() |
311
|
|
|
{ |
312
|
|
|
return $this->createdAt; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Sets the value of createdAt. |
317
|
|
|
* |
318
|
|
|
* @param \DateTime $createdAt the created at |
319
|
|
|
* |
320
|
|
|
* @return self |
321
|
|
|
*/ |
322
|
|
|
public function setCreatedAt(\DateTime $createdAt) |
323
|
|
|
{ |
324
|
|
|
$this->createdAt = $createdAt; |
325
|
|
|
|
326
|
|
|
return $this; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Gets the value of currency. |
331
|
|
|
* |
332
|
|
|
* @return string |
333
|
|
|
*/ |
334
|
|
|
public function getCurrency() |
335
|
|
|
{ |
336
|
|
|
return $this->currency; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Sets the value of currency. |
341
|
|
|
* |
342
|
|
|
* @param string $currency the currency |
343
|
|
|
* |
344
|
|
|
* @return self |
345
|
|
|
*/ |
346
|
|
|
public function setCurrency($currency) |
347
|
|
|
{ |
348
|
|
|
$this->currency = $currency; |
349
|
|
|
|
350
|
|
|
return $this; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* {@inheritdoc} |
355
|
|
|
*/ |
356
|
|
|
public function getDiscountTotal() |
357
|
|
|
{ |
358
|
|
|
return $this->discountTotal / 100; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* {@inheritdoc} |
363
|
|
|
*/ |
364
|
|
|
public function setDiscountTotal($discountTotal) |
365
|
|
|
{ |
366
|
|
|
$this->discountTotal = $discountTotal * 100; |
367
|
|
|
|
368
|
|
|
return $this; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
public function addDiscount($discount) |
372
|
|
|
{ |
373
|
|
|
if (!$this->hasDiscount($discount)) { |
374
|
|
|
$this->discounts->add($discount); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
return $this; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
public function hasDiscount($discount) |
381
|
|
|
{ |
382
|
|
|
return $this->discounts->contains($discount); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
public function removeDiscount($discount) |
386
|
|
|
{ |
387
|
|
|
if ($this->hasDiscount($discount)) { |
388
|
|
|
$this->discounts->removeElement($discount); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
return $this; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Gets the discounts. |
396
|
|
|
* |
397
|
|
|
* @return Collection |
398
|
|
|
*/ |
399
|
|
|
public function getDiscounts() |
400
|
|
|
{ |
401
|
|
|
return $this->discounts; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* Sets the discounts. |
406
|
|
|
* |
407
|
|
|
* @param Collection $discounts the discounts |
408
|
|
|
* |
409
|
|
|
* @return self |
410
|
|
|
*/ |
411
|
|
|
public function setDiscounts(Collection $discounts) |
412
|
|
|
{ |
413
|
|
|
$this->discounts = $discounts; |
414
|
|
|
|
415
|
|
|
return $this; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Calculates total order price including |
420
|
|
|
* all discounts. |
421
|
|
|
* |
422
|
|
|
* @return self |
423
|
|
|
*/ |
424
|
|
|
public function calculateTotal() |
425
|
|
|
{ |
426
|
|
|
$this->calculateItemsTotal(); |
427
|
|
|
$this->total = ($this->itemsTotal / 100 + $this->discountTotal / 100) * 100; |
428
|
|
|
if ($this->total < 0) { |
429
|
|
|
$this->total = 0; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
return $this; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Calculates all items discounts and unit prices. |
437
|
|
|
* |
438
|
|
|
* @return self |
439
|
|
|
*/ |
440
|
|
|
public function calculateItemsTotal() |
441
|
|
|
{ |
442
|
|
|
$itemsTotal = 0; |
443
|
|
|
$this->discountTotal = 0; |
444
|
|
|
foreach ($this->items as $item) { |
445
|
|
|
$item->calculateToPay(); |
446
|
|
|
$itemsTotal += $item->getToPay(); |
447
|
|
|
|
448
|
|
|
$this->discountTotal -= $item->getDiscountTotal() * 100; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
$this->itemsTotal = $itemsTotal * 100; |
452
|
|
|
|
453
|
|
|
return $this; |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* Gets the value of itemsTotal. |
458
|
|
|
* |
459
|
|
|
* @return int |
460
|
|
|
*/ |
461
|
|
|
public function getItemsTotal() |
462
|
|
|
{ |
463
|
|
|
return $this->itemsTotal / 100; |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* Sets the value of itemsTotal. |
468
|
|
|
* |
469
|
|
|
* @param int $itemsTotal the items total |
470
|
|
|
* |
471
|
|
|
* @return self |
472
|
|
|
*/ |
473
|
|
|
public function setItemsTotal($itemsTotal) |
474
|
|
|
{ |
475
|
|
|
$this->itemsTotal = $itemsTotal * 100; |
476
|
|
|
|
477
|
|
|
return $this; |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* Gets the payments. |
482
|
|
|
* |
483
|
|
|
* @return Payment |
484
|
|
|
*/ |
485
|
|
|
public function getPayments() |
486
|
|
|
{ |
487
|
|
|
return $this->payments; |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
public function getPaymentState() |
491
|
|
|
{ |
492
|
|
|
return $this->paymentState; |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
public function setPaymentState($paymentState) |
496
|
|
|
{ |
497
|
|
|
$this->paymentState = $paymentState; |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
public function addPayment(PaymentInterface $payment) |
501
|
|
|
{ |
502
|
|
|
if (!$this->hasPayment($payment)) { |
503
|
|
|
$this->payments->add($payment); |
504
|
|
|
$payment->setOrder($this); |
505
|
|
|
$this->setPaymentState($payment->getState()); |
506
|
|
|
} |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
public function removePayment(PaymentInterface $payment) |
510
|
|
|
{ |
511
|
|
|
if ($this->hasPayment($payment)) { |
512
|
|
|
$this->payments->removeElement($payment); |
513
|
|
|
$payment->setOrder(null); |
514
|
|
|
} |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
public function hasPayment(PaymentInterface $payment) |
518
|
|
|
{ |
519
|
|
|
return $this->payments->contains($payment); |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
public function getOrder() |
523
|
|
|
{ |
524
|
|
|
return $this; |
525
|
|
|
} |
526
|
|
|
} |
527
|
|
|
|