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