1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Application\Model; |
6
|
|
|
|
7
|
|
|
use Application\DBAL\Types\AccountTypeType; |
8
|
|
|
use Application\Repository\AccountRepository; |
9
|
|
|
use Application\Repository\TransactionLineRepository; |
10
|
|
|
use Application\Traits\HasIban; |
11
|
|
|
use Cake\Chronos\Date; |
12
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
13
|
|
|
use Doctrine\Common\Collections\Collection; |
14
|
|
|
use Doctrine\ORM\Mapping as ORM; |
15
|
|
|
use Ecodev\Felix\Api\Exception; |
16
|
|
|
use Ecodev\Felix\Model\Traits\HasName; |
17
|
|
|
use GraphQL\Doctrine\Annotation as API; |
18
|
|
|
use Money\Money; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Financial account. |
22
|
|
|
* |
23
|
|
|
* @ORM\Entity(repositoryClass="Application\Repository\AccountRepository") |
24
|
|
|
* @ORM\AssociationOverrides({ |
25
|
|
|
* @ORM\AssociationOverride( |
26
|
|
|
* name="owner", |
27
|
|
|
* inversedBy="accounts", |
28
|
|
|
* joinColumns=@ORM\JoinColumn(unique=true, onDelete="SET NULL") |
29
|
|
|
* ) |
30
|
|
|
* }) |
31
|
|
|
*/ |
32
|
|
|
class Account extends AbstractModel |
33
|
|
|
{ |
34
|
|
|
use HasIban; |
35
|
|
|
use HasName; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @ORM\Column(type="Money", options={"default" = 0}) |
39
|
|
|
*/ |
40
|
|
|
private \Money\Money $balance; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @ORM\ManyToOne(targetEntity="Account", inversedBy="children") |
44
|
|
|
* @ORM\JoinColumns({ |
45
|
|
|
* @ORM\JoinColumn(onDelete="CASCADE") |
46
|
|
|
* }) |
47
|
|
|
*/ |
48
|
|
|
private ?\Application\Model\Account $parent = null; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var Collection<Account> |
52
|
|
|
* @ORM\OneToMany(targetEntity="Account", mappedBy="parent") |
53
|
|
|
* @ORM\OrderBy({"code" = "ASC"}) |
54
|
|
|
*/ |
55
|
|
|
private Collection $children; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @ORM\Column(type="AccountType", length=10) |
59
|
|
|
*/ |
60
|
|
|
private string $type; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @ORM\Column(type="integer", nullable=false, unique=true, options={"unsigned" = true}) |
64
|
|
|
*/ |
65
|
|
|
private int $code; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var Collection<TransactionLine> |
69
|
|
|
* @ORM\OneToMany(targetEntity="TransactionLine", mappedBy="debit") |
70
|
|
|
*/ |
71
|
|
|
private Collection $debitTransactionLines; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var Collection<TransactionLine> |
75
|
|
|
* @ORM\OneToMany(targetEntity="TransactionLine", mappedBy="credit") |
76
|
|
|
*/ |
77
|
|
|
private Collection $creditTransactionLines; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @ORM\Column(type="Money", options={"default" = 0}) |
81
|
|
|
*/ |
82
|
|
|
private \Money\Money $totalBalance; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Constructor. |
86
|
|
|
*/ |
87
|
35 |
|
public function __construct() |
88
|
|
|
{ |
89
|
35 |
|
$this->balance = Money::CHF(0); |
90
|
35 |
|
$this->totalBalance = Money::CHF(0); |
91
|
35 |
|
$this->children = new ArrayCollection(); |
92
|
35 |
|
$this->debitTransactionLines = new ArrayCollection(); |
93
|
35 |
|
$this->creditTransactionLines = new ArrayCollection(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get full name including code and name. |
98
|
|
|
*/ |
99
|
|
|
public function getFullName(): string |
100
|
|
|
{ |
101
|
|
|
return implode(' - ', array_filter([$this->getCode(), $this->getName()])); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Assign the account to an user. |
106
|
|
|
*/ |
107
|
20 |
|
public function setOwner(?User $owner): void |
108
|
|
|
{ |
109
|
20 |
|
if ($this->getOwner()) { |
110
|
1 |
|
$this->getOwner()->accountRemoved(); |
111
|
|
|
} |
112
|
|
|
|
113
|
20 |
|
parent::setOwner($owner); |
114
|
|
|
|
115
|
20 |
|
if ($this->getOwner()) { |
116
|
20 |
|
$owner->accountAdded($this); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Only members' liability accounts must have an owner |
122
|
|
|
* and there must be only an account per member. |
123
|
|
|
*/ |
124
|
15 |
|
protected function getOwnerForCreation(): ?User |
125
|
|
|
{ |
126
|
15 |
|
return null; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Set balance. |
131
|
|
|
* |
132
|
|
|
* @API\Exclude |
133
|
|
|
*/ |
134
|
2 |
|
public function setBalance(Money $balance): void |
135
|
|
|
{ |
136
|
2 |
|
$this->balance = $balance; |
137
|
|
|
} |
138
|
|
|
|
139
|
9 |
|
public function getBalance(): Money |
140
|
|
|
{ |
141
|
9 |
|
return $this->balance; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Total balance, recursively including all child account if this account is a group. |
146
|
|
|
*/ |
147
|
1 |
|
public function getTotalBalance(): Money |
148
|
|
|
{ |
149
|
1 |
|
return $this->totalBalance; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Historical account's balance at a date in the past. |
154
|
|
|
*/ |
155
|
3 |
|
public function getBalanceAtDate(Date $date): Money |
156
|
|
|
{ |
157
|
3 |
|
$today = Date::today(); |
158
|
|
|
|
159
|
3 |
|
if ($date->greaterThan($today)) { |
160
|
|
|
throw new Exception('Cannot compute balance of account #' . $this->getId() . ' in the future on ' . $date->format('d.m.Y')); |
161
|
|
|
} |
162
|
|
|
|
163
|
3 |
|
if ($date->equals($today)) { |
164
|
|
|
if ($this->getType() === AccountTypeType::GROUP) { |
165
|
|
|
return $this->getTotalBalance(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $this->getBalance(); |
169
|
|
|
} |
170
|
|
|
|
171
|
3 |
|
$connection = _em()->getConnection(); |
172
|
|
|
|
173
|
3 |
|
if ($this->getType() === AccountTypeType::GROUP) { |
174
|
|
|
|
175
|
|
|
// Get all child accounts that are not group account (= they have their own balance) |
176
|
2 |
|
$sql = 'WITH RECURSIVE child AS |
177
|
|
|
(SELECT id, parent_id, `type`, balance |
178
|
|
|
FROM account WHERE id = ? |
179
|
|
|
UNION |
180
|
|
|
SELECT account.id, account.parent_id, account.type, account.balance |
181
|
|
|
FROM account |
182
|
|
|
JOIN child ON account.parent_id = child.id) |
183
|
|
|
SELECT child.id FROM child WHERE `type` <> ?'; |
184
|
|
|
|
185
|
2 |
|
$result = $connection->executeQuery($sql, [$this->getId(), AccountTypeType::GROUP]); |
186
|
|
|
|
187
|
2 |
|
$ids = $result->fetchFirstColumn(); |
188
|
|
|
|
189
|
2 |
|
$totals = []; |
190
|
2 |
|
$totalForChildren = Money::CHF(0); |
191
|
|
|
|
192
|
|
|
/** @var AccountRepository $accountRepository */ |
193
|
2 |
|
$accountRepository = _em()->getRepository(self::class); |
194
|
2 |
|
foreach ($ids as $idAccount) { |
195
|
2 |
|
$child = $accountRepository->getOneById((int) $idAccount); |
196
|
2 |
|
$childBalance = $child->getBalanceAtDate($date); |
197
|
2 |
|
$totalForChildren = $totalForChildren->add($childBalance); |
198
|
2 |
|
$totals[(int) $idAccount] = $totalForChildren; |
199
|
|
|
} |
200
|
|
|
|
201
|
2 |
|
return $totalForChildren; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** @var TransactionLineRepository $transactionLineRepository */ |
205
|
3 |
|
$transactionLineRepository = _em()->getRepository(TransactionLine::class); |
206
|
|
|
|
207
|
3 |
|
$totalDebit = $transactionLineRepository->totalBalance($this, null, null, $date); |
208
|
3 |
|
$totalCredit = $transactionLineRepository->totalBalance(null, $this, null, $date); |
209
|
3 |
|
if (in_array($this->getType(), [ |
210
|
|
|
AccountTypeType::LIABILITY, |
211
|
|
|
AccountTypeType::EQUITY, |
212
|
|
|
AccountTypeType::REVENUE, |
213
|
|
|
], true)) { |
214
|
2 |
|
$balance = $totalCredit->subtract($totalDebit); |
215
|
3 |
|
} elseif (in_array($this->getType(), [AccountTypeType::ASSET, AccountTypeType::EXPENSE], true)) { |
216
|
3 |
|
$balance = $totalDebit->subtract($totalCredit); |
217
|
|
|
} else { |
218
|
|
|
throw new Exception('Do not know how to compute past balance of account #' . $this->getId() . ' of type ' . $this->getType()); |
219
|
|
|
} |
220
|
|
|
|
221
|
3 |
|
return $balance; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Set parent. |
226
|
|
|
* |
227
|
|
|
* @param null|Account $parent |
228
|
|
|
*/ |
229
|
16 |
|
public function setParent(?self $parent): void |
230
|
|
|
{ |
231
|
16 |
|
if ($this->getParent()) { |
232
|
1 |
|
$this->getParent()->getChildren()->removeElement($this); |
233
|
|
|
} |
234
|
|
|
|
235
|
16 |
|
$this->parent = $parent; |
236
|
|
|
|
237
|
16 |
|
if ($this->getParent()) { |
238
|
16 |
|
$this->getParent()->getChildren()->add($this); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return null|Account |
244
|
|
|
*/ |
245
|
16 |
|
public function getParent(): ?self |
246
|
|
|
{ |
247
|
16 |
|
return $this->parent; |
248
|
|
|
} |
249
|
|
|
|
250
|
17 |
|
public function getChildren(): Collection |
251
|
|
|
{ |
252
|
17 |
|
return $this->children; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Set type. |
257
|
|
|
* |
258
|
|
|
* @API\Input(type="AccountType") |
259
|
|
|
*/ |
260
|
15 |
|
public function setType(string $type): void |
261
|
|
|
{ |
262
|
15 |
|
$this->type = $type; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Get type. |
267
|
|
|
* |
268
|
|
|
* @API\Field(type="AccountType") |
269
|
|
|
*/ |
270
|
5 |
|
public function getType(): string |
271
|
|
|
{ |
272
|
5 |
|
return $this->type; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Set code. |
277
|
|
|
*/ |
278
|
15 |
|
public function setCode(int $code): void |
279
|
|
|
{ |
280
|
15 |
|
$this->code = $code; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Get code. |
285
|
|
|
*/ |
286
|
18 |
|
public function getCode(): int |
287
|
|
|
{ |
288
|
18 |
|
return $this->code; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Notify when a transaction line is added |
293
|
|
|
* This should only be called by TransactionLine::setDebit(). |
294
|
|
|
*/ |
295
|
29 |
|
public function debitTransactionLineAdded(TransactionLine $transactionLine): void |
296
|
|
|
{ |
297
|
29 |
|
$this->debitTransactionLines->add($transactionLine); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Notify when a transaction line is removed |
302
|
|
|
* This should only be called by TransactionLine::setDebit(). |
303
|
|
|
*/ |
304
|
1 |
|
public function debitTransactionLineRemoved(TransactionLine $transactionLine): void |
305
|
|
|
{ |
306
|
1 |
|
$this->debitTransactionLines->removeElement($transactionLine); |
307
|
|
|
} |
308
|
|
|
|
309
|
5 |
|
public function getDebitTransactionLines(): Collection |
310
|
|
|
{ |
311
|
5 |
|
return $this->debitTransactionLines; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Notify when a transaction line is added |
316
|
|
|
* This should only be called by TransactionLine::setCredit(). |
317
|
|
|
*/ |
318
|
29 |
|
public function creditTransactionLineAdded(TransactionLine $transactionLine): void |
319
|
|
|
{ |
320
|
29 |
|
$this->creditTransactionLines->add($transactionLine); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Notify when a transaction line is removed |
325
|
|
|
* This should only be called by TransactionLine::setCredit(). |
326
|
|
|
*/ |
327
|
1 |
|
public function creditTransactionLineRemoved(TransactionLine $transactionLine): void |
328
|
|
|
{ |
329
|
1 |
|
$this->creditTransactionLines->removeElement($transactionLine); |
330
|
|
|
} |
331
|
|
|
|
332
|
5 |
|
public function getCreditTransactionLines(): Collection |
333
|
|
|
{ |
334
|
5 |
|
return $this->creditTransactionLines; |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.