|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Application\Model; |
|
6
|
|
|
|
|
7
|
|
|
use Application\Traits\HasName; |
|
8
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
9
|
|
|
use Doctrine\Common\Collections\Collection; |
|
10
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* A transaction account held for a member, or club's bank account |
|
14
|
|
|
* |
|
15
|
|
|
* @ORM\Entity(repositoryClass="Application\Repository\AccountRepository") |
|
16
|
|
|
*/ |
|
17
|
|
|
class Account extends AbstractModel |
|
18
|
|
|
{ |
|
19
|
|
|
use hasName; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var User |
|
23
|
|
|
* |
|
24
|
|
|
* @ORM\OneToOne(targetEntity="User", mappedBy="account") |
|
25
|
|
|
* @ORM\JoinColumns({ |
|
26
|
|
|
* @ORM\JoinColumn(onDelete="SET NULL") |
|
27
|
|
|
* }) |
|
28
|
|
|
*/ |
|
29
|
|
|
private $user; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var float |
|
33
|
|
|
* |
|
34
|
|
|
* @ORM\Column(type="decimal", precision=7, scale=2, options={"default" = "0.00"}) |
|
35
|
|
|
*/ |
|
36
|
|
|
private $balance = 0; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
* |
|
41
|
|
|
* @ORM\Column(type="string", length=32, unique=true) |
|
42
|
|
|
*/ |
|
43
|
|
|
private $iban; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var Collection |
|
47
|
|
|
* @ORM\OneToMany(targetEntity="Transaction", mappedBy="account") |
|
48
|
|
|
*/ |
|
49
|
|
|
private $transactions; |
|
50
|
|
|
|
|
51
|
|
|
public function __construct(string $name = '') |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
$this->transactions = new ArrayCollection(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Assign the transaction account to an user |
|
58
|
|
|
* |
|
59
|
|
|
* @param null|User $user |
|
60
|
|
|
*/ |
|
61
|
|
|
public function setUser(?User $user): void |
|
62
|
|
|
{ |
|
63
|
|
|
$previousUser = $this->user; |
|
64
|
|
|
|
|
65
|
|
|
$this->user = $user; |
|
66
|
|
|
if ($user) { |
|
67
|
|
|
$user->setAccount($this); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if ($previousUser && $previousUser !== $user) { |
|
71
|
|
|
$previousUser->setAccount(null); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Get the user holding the account, or null we are a bank account |
|
77
|
|
|
* |
|
78
|
|
|
* @return null|User |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getUser(): ?User |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->user; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Set balance |
|
87
|
|
|
* |
|
88
|
|
|
* @param float $balance |
|
89
|
|
|
*/ |
|
90
|
|
|
public function setBalance(float $balance): void |
|
91
|
|
|
{ |
|
92
|
|
|
$this->balance = $balance; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return float |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getBalance(): float |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->balance; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Set the IBAN (international bank account number) |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $iban |
|
107
|
|
|
*/ |
|
108
|
|
|
public function setIban(string $iban): void |
|
109
|
|
|
{ |
|
110
|
|
|
$this->iban = $iban; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get the IBAN (international bank account number) |
|
115
|
|
|
* |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getIban(): string |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->iban; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Get all transactions |
|
125
|
|
|
* |
|
126
|
|
|
* @return Collection |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getTransactions(): Collection |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->transactions; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Notify the account that it has a new transaction |
|
135
|
|
|
* This should only be called by Transaction::setAccount() |
|
136
|
|
|
* |
|
137
|
|
|
* @param Transaction $transaction |
|
138
|
|
|
*/ |
|
139
|
|
|
public function transactionAdded(Transaction $transaction): void |
|
140
|
|
|
{ |
|
141
|
|
|
$this->transactions->add($transaction); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Notify the account that a transaction was removed |
|
146
|
|
|
* This should only be called by Transaction::setAccount() |
|
147
|
|
|
* |
|
148
|
|
|
* @param Transaction $transaction |
|
149
|
|
|
*/ |
|
150
|
|
|
public function transactionRemoved(Transaction $transaction): void |
|
151
|
|
|
{ |
|
152
|
|
|
$this->transactions->removeElement($transaction); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Notify that an user was added |
|
157
|
|
|
* |
|
158
|
|
|
* @param null|User $user |
|
159
|
|
|
*/ |
|
160
|
|
|
public function userAdded(?User $user): void |
|
161
|
|
|
{ |
|
162
|
|
|
$this->user = $user; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.