Payment   A
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 439
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 38
eloc 76
dl 0
loc 439
rs 9.36
c 0
b 0
f 0

31 Methods

Rating   Name   Duplication   Size   Complexity  
A removePaidTicket() 0 12 3
A removeTicket() 0 11 4
A setUpdatedAt() 0 5 1
A getBaseAmount() 0 3 1
A getId() 0 3 1
A setGate() 0 5 1
A setAmount() 0 5 1
A getGate() 0 3 1
A setRefundedAmount() 0 5 1
A isPaid() 0 3 1
A __construct() 0 3 1
A getFwdaysAmount() 0 3 1
A addTicket() 0 3 2
A setCreatedAt() 0 5 1
A isPending() 0 3 1
A getAmount() 0 3 1
A markedAsPaid() 0 5 1
A setUser() 0 5 1
A setFwdaysAmount() 0 5 1
A getCreatedAt() 0 3 1
A getTickets() 0 3 1
A getUpdatedAt() 0 3 1
A setBaseAmount() 0 5 1
A setTickets() 0 3 1
A __toString() 0 5 1
A getRefundedAmount() 0 3 1
A isReturned() 0 3 1
A getStatus() 0 3 1
A setStatus() 0 3 1
A getUser() 0 3 1
A setPaidWithGate() 0 10 2
1
<?php
2
3
namespace Stfalcon\Bundle\EventBundle\Entity;
4
5
use Application\Bundle\DefaultBundle\Entity\TicketCost;
6
use Application\Bundle\UserBundle\Entity\User;
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\ORM\Mapping as ORM;
9
use Gedmo\Mapping\Annotation as Gedmo;
10
11
/**
12
 * Stfalcon\Bundle\EventBundle\Entity\Payment.
13
 *
14
 * @ORM\Table(name="payments")
15
 * @ORM\Entity(repositoryClass="Stfalcon\Bundle\EventBundle\Repository\PaymentRepository")
16
 */
17
class Payment
18
{
19
    const STATUS_PENDING = 'pending';
20
    const STATUS_PAID = 'paid';
21
    const STATUS_RETURNED = 'returned'; //доданий для статусу, коли платіж повернений користувачу
22
23
    const ADMIN_GATE = 'admin';
24
    const INTERKASSA_GATE = 'interkassa';
25
    const WAYFORPAY_GATE = 'wayforpay';
26
    const BONUS_GATE = 'bonus';
27
    const PROMOCODE_GATE = 'promocode';
28
29
    private $gates = [self::ADMIN_GATE, self::WAYFORPAY_GATE, self::BONUS_GATE, self::PROMOCODE_GATE];
30
31
    /**
32
     * @var int
33
     *
34
     * @ORM\Column(name="id", type="integer")
35
     * @ORM\Id
36
     * @ORM\GeneratedValue(strategy="AUTO")
37
     */
38
    private $id;
39
40
    /**
41
     * Кто оплатил. Т.е. провел транзакцию.
42
     *
43
     * @var User
44
     *
45
     * @ORM\ManyToOne(targetEntity="Application\Bundle\UserBundle\Entity\User")
46
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")
47
     */
48
    private $user;
49
50
    /**
51
     * Сумма для оплаты.
52
     *
53
     * @var float
54
     *
55
     * @ORM\Column(name="amount", type="decimal", precision=10, scale=2)
56
     */
57
    private $amount = 0;
58
59
    /**
60
     * Базова/початкова сума платежа, до застосування промокода чи скидки.
61
     *
62
     * @var float
63
     *
64
     * @ORM\Column(name="base_amount", type="decimal", precision=10, scale=2)
65
     */
66
    private $baseAmount = 0;
67
68
    /**
69
     * Використанно валюти з балансу користувача,
70
     * яку він отримує за рефералів або за повернення коштів при відсутності євента.
71
     *
72
     * @var float
73
     *
74
     * @ORM\Column(name="fwdays_amount", type="decimal", precision=10, scale=2, nullable=true)
75
     */
76
    private $fwdaysAmount = 0;
77
78
    /**
79
     * @var string
80
     *
81
     * @ORM\Column(name="status", type="string")
82
     */
83
    private $status = self::STATUS_PENDING;
84
85
    /**
86
     * @var string
87
     *
88
     * @ORM\Column()
89
     */
90
    private $gate = Payment::WAYFORPAY_GATE;
91
92
    /**
93
     * @var \DateTime
94
     *
95
     * @ORM\Column(name="created_at", type="datetime")
96
     *
97
     * @Gedmo\Timestampable(on="create")
98
     */
99
    private $createdAt;
100
101
    /**
102
     * @var \DateTime
103
     *
104
     * @ORM\Column(name="updated_at", type="datetime")
105
     *
106
     * @Gedmo\Timestampable(on="update")
107
     */
108
    private $updatedAt;
109
110
    /**
111
     * @var Ticket[]|ArrayCollection
112
     *
113
     * @ORM\OneToMany(targetEntity="Stfalcon\Bundle\EventBundle\Entity\Ticket", mappedBy="payment")
114
     * @ORM\OrderBy({"createdAt" = "ASC"})
115
     */
116
    private $tickets;
117
118
    /**
119
     * @var float
120
     *
121
     * @ORM\Column(name="refunded_amount", type="decimal", precision=10, scale=2, nullable=true)
122
     */
123
    private $refundedAmount = 0;
124
125
    /**
126
     * @param mixed $tickets
127
     */
128
    public function setTickets($tickets)
129
    {
130
        $this->tickets = $tickets;
131
    }
132
133
    /**
134
     * @return ArrayCollection
135
     */
136
    public function getTickets()
137
    {
138
        return $this->tickets;
139
    }
140
141
    /**
142
     * Constructor. Set default status to new payment.
143
     */
144
    public function __construct()
145
    {
146
        $this->tickets = new ArrayCollection();
147
    }
148
149
    /**
150
     * @param Ticket $ticket
151
     *
152
     * @return bool
153
     */
154
    public function addTicket(Ticket $ticket)
155
    {
156
        return !$this->tickets->contains($ticket) && $this->tickets->add($ticket);
157
    }
158
159
    /**
160
     * @param Ticket $ticket
161
     *
162
     * @return bool
163
     */
164
    public function removeTicket(Ticket $ticket)
165
    {
166
        if ($ticket->isPaid()) {
167
            return $this->removePaidTicket($ticket);
168
        }
169
170
        if ($ticket->getTicketCost() instanceof TicketCost) {
0 ignored issues
show
introduced by
$ticket->getTicketCost() is always a sub-type of Application\Bundle\DefaultBundle\Entity\TicketCost.
Loading history...
171
            $ticket->getTicketCost()->recalculateSoldCount();
172
        }
173
174
        return $this->tickets->contains($ticket) && $this->tickets->removeElement($ticket);
175
    }
176
177
    /**
178
     * @param Ticket $ticket
179
     *
180
     * @return bool
181
     */
182
    public function removePaidTicket(Ticket $ticket)
183
    {
184
        if ($this->tickets->contains($ticket)) {
185
            if ($ticket->isPaid()) {
186
                $this->refundedAmount += $ticket->getAmount();
187
            }
188
            $ticket->setPayment(null);
189
190
            return $this->tickets->removeElement($ticket);
191
        }
192
193
        return false;
194
    }
195
196
    /**
197
     * @return float
198
     */
199
    public function getRefundedAmount()
200
    {
201
        return $this->refundedAmount;
202
    }
203
204
    /**
205
     * @param float $refundedAmount
206
     *
207
     * @return $this
208
     */
209
    public function setRefundedAmount($refundedAmount)
210
    {
211
        $this->refundedAmount = $refundedAmount;
212
213
        return $this;
214
    }
215
216
    /**
217
     * Get id.
218
     *
219
     * @return int
220
     */
221
    public function getId()
222
    {
223
        return $this->id;
224
    }
225
226
    /**
227
     * Set amount.
228
     *
229
     * @param float $amount
230
     *
231
     * @return $this
232
     */
233
    public function setAmount($amount)
234
    {
235
        $this->amount = $amount;
236
237
        return $this;
238
    }
239
240
    /**
241
     * Get amount.
242
     *
243
     * @return float
244
     */
245
    public function getAmount()
246
    {
247
        return $this->amount;
248
    }
249
250
    /**
251
     * Set status.
252
     *
253
     * @param string $status
254
     */
255
    // @todo тут треба міняти на приват. і юзати методи MarkedAsPaid
256
257
    /**
258
     * @param string $status
259
     */
260
    public function setStatus($status)
261
    {
262
        $this->status = $status;
263
    }
264
265
    /**
266
     * Get status.
267
     *
268
     * @return string
269
     */
270
    public function getStatus()
271
    {
272
        return $this->status;
273
    }
274
275
    /**
276
     * @param User $user
277
     *
278
     * @return $this
279
     */
280
    public function setUser(User $user)
281
    {
282
        $this->user = $user;
283
284
        return $this;
285
    }
286
287
    /**
288
     * @return User
289
     */
290
    public function getUser()
291
    {
292
        return $this->user;
293
    }
294
295
    /**
296
     * @return \DateTime
297
     */
298
    public function getCreatedAt()
299
    {
300
        return $this->createdAt;
301
    }
302
303
    /**
304
     * @param \DateTime $createdAt
305
     *
306
     * @return $this
307
     */
308
    public function setCreatedAt($createdAt)
309
    {
310
        $this->createdAt = $createdAt;
311
312
        return $this;
313
    }
314
315
    /**
316
     * @return \DateTime
317
     */
318
    public function getUpdatedAt()
319
    {
320
        return $this->updatedAt;
321
    }
322
323
    /**
324
     * @param \DateTime $updatedAt
325
     *
326
     * @return $this
327
     */
328
    public function setUpdatedAt($updatedAt)
329
    {
330
        $this->updatedAt = $updatedAt;
331
332
        return $this;
333
    }
334
335
    /**
336
     * @return bool
337
     */
338
    public function isPaid()
339
    {
340
        return self::STATUS_PAID === $this->getStatus();
341
    }
342
343
    /**
344
     * @return bool
345
     */
346
    public function isPending()
347
    {
348
        return self::STATUS_PENDING === $this->getStatus();
349
    }
350
351
    /**
352
     * @return bool
353
     */
354
    public function isReturned()
355
    {
356
        return self::STATUS_RETURNED === $this->getStatus();
357
    }
358
359
    /**
360
     * @return string
361
     */
362
    public function getGate()
363
    {
364
        return $this->gate;
365
    }
366
367
    /**
368
     * @param string $gate
369
     *
370
     * @return $this
371
     */
372
    public function setGate($gate)
373
    {
374
        $this->gate = $gate;
375
376
        return $this;
377
    }
378
379
    /**
380
     * Get status of payment.
381
     *
382
     * @return string
383
     */
384
    public function __toString()
385
    {
386
        $string = "{$this->getStatus()} (#{$this->getId()})"; // для зручності перегляду платежів в списку квитків додав id
387
388
        return $string;
389
    }
390
391
    /**
392
     * @return $this
393
     */
394
    public function markedAsPaid()
395
    {
396
        $this->setStatus(self::STATUS_PAID);
397
398
        return $this;
399
    }
400
401
    /**
402
     * @param string $gate
403
     *
404
     * @return $this
405
     */
406
    public function setPaidWithGate($gate)
407
    {
408
        $this->setStatus(self::STATUS_PAID);
409
        if (in_array($gate, $this->gates, true)) {
410
            $this->setGate($gate);
411
        } else {
412
            $this->setGate(self::WAYFORPAY_GATE);
413
        }
414
415
        return $this;
416
    }
417
418
    /**
419
     * @return float
420
     */
421
    public function getBaseAmount()
422
    {
423
        return $this->baseAmount;
424
    }
425
426
    /**
427
     * @param float $baseAmount
428
     *
429
     * @return $this
430
     */
431
    public function setBaseAmount($baseAmount)
432
    {
433
        $this->baseAmount = $baseAmount;
434
435
        return $this;
436
    }
437
438
    /**
439
     * @return float
440
     */
441
    public function getFwdaysAmount()
442
    {
443
        return $this->fwdaysAmount;
444
    }
445
446
    /**
447
     * @param float $fwdaysAmount
448
     *
449
     * @return $this
450
     */
451
    public function setFwdaysAmount($fwdaysAmount)
452
    {
453
        $this->fwdaysAmount = $fwdaysAmount;
454
455
        return $this;
456
    }
457
}
458