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