Failed Conditions
Push — master ( 82004d...d07a9f )
by Adrien
06:13
created

Account::setBudgetAllowed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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
    /**
72
     * Constructor.
73
     */
74 36
    public function __construct()
75
    {
76 36
        $this->balance = Money::CHF(0);
77 36
        $this->totalBalance = Money::CHF(0);
78 36
        $this->children = new ArrayCollection();
79 36
        $this->debitTransactionLines = new ArrayCollection();
80 36
        $this->creditTransactionLines = new ArrayCollection();
81
    }
82
83
    /**
84
     * Get full name including code and name.
85
     */
86
    public function getFullName(): string
87
    {
88
        return implode(' - ', array_filter([$this->getCode(), $this->getName()]));
89
    }
90
91
    /**
92
     * Assign the account to an user.
93
     */
94 21
    public function setOwner(?User $owner): void
95
    {
96 21
        if ($this->getOwner()) {
97 1
            $this->getOwner()->accountRemoved();
98
        }
99
100 21
        parent::setOwner($owner);
101
102 21
        if ($this->getOwner()) {
103 21
            $owner->accountAdded($this);
0 ignored issues
show
Bug introduced by
The method accountAdded() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
            $owner->/** @scrutinizer ignore-call */ 
104
                    accountAdded($this);

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.

Loading history...
104
        }
105
    }
106
107 1
    public function getBudgetAllowed(): ?Money
108
    {
109 1
        return $this->budgetAllowed;
110
    }
111
112 1
    public function setBudgetAllowed(?Money $budgetAllowed): void
113
    {
114 1
        $this->budgetAllowed = $budgetAllowed;
115
    }
116
117 1
    public function getBudgetBalance(): ?Money
118
    {
119 1
        return $this->budgetBalance;
120
    }
121
122
    /**
123
     * Only members' liability accounts must have an owner
124
     * and there must be only an account per member.
125
     */
126 16
    protected function getOwnerForCreation(): ?User
127
    {
128 16
        return null;
129
    }
130
131
    /**
132
     * Set balance.
133
     */
134 2
    #[API\Exclude]
135
    public function setBalance(Money $balance): void
136
    {
137 2
        $this->balance = $balance;
138
    }
139
140 9
    public function getBalance(): Money
141
    {
142 9
        return $this->balance;
143
    }
144
145
    /**
146
     * Total balance, recursively including all child account if this account is a group.
147
     */
148 2
    public function getTotalBalance(): Money
149
    {
150 2
        return $this->totalBalance;
151
    }
152
153
    /**
154
     * Historical account's balance at a date in the past.
155
     */
156 3
    public function getBalanceAtDate(ChronosDate $date): Money
157
    {
158 3
        $today = ChronosDate::today();
159
160 3
        if ($date->greaterThan($today)) {
161
            throw new Exception('Cannot compute balance of account #' . $this->getId() . ' in the future on ' . $date->format('d.m.Y'));
162
        }
163
164 3
        if ($date->equals($today)) {
165
            if ($this->getType() === AccountTypeType::GROUP) {
166
                return $this->getTotalBalance();
167
            }
168
169
            return $this->getBalance();
170
        }
171
172 3
        $connection = _em()->getConnection();
173
174 3
        if ($this->getType() === AccountTypeType::GROUP) {
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 2
            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 3
            AccountTypeType::LIABILITY,
211 3
            AccountTypeType::EQUITY,
212 3
            AccountTypeType::REVENUE,
213 3
        ], 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 17
    public function setParent(?self $parent): void
228
    {
229 17
        if ($this->getParent()) {
230 1
            $this->getParent()->getChildren()->removeElement($this);
231
        }
232
233 17
        $this->parent = $parent;
234
235 17
        if ($this->getParent()) {
236 17
            $this->getParent()->getChildren()->add($this);
237
        }
238
    }
239
240 17
    public function getParent(): ?self
241
    {
242 17
        return $this->parent;
243
    }
244
245 18
    public function getChildren(): Collection
246
    {
247 18
        return $this->children;
248
    }
249
250
    /**
251
     * Set type.
252
     */
253 16
    #[API\Input(type: 'AccountType')]
254
    public function setType(string $type): void
255
    {
256 16
        $this->type = $type;
257
    }
258
259
    /**
260
     * Get type.
261
     */
262 5
    #[API\Field(type: 'AccountType')]
263
    public function getType(): string
264
    {
265 5
        return $this->type;
266
    }
267
268
    /**
269
     * Set code.
270
     */
271 16
    public function setCode(int $code): void
272
    {
273 16
        $this->code = $code;
274
    }
275
276
    /**
277
     * Get code.
278
     */
279 18
    public function getCode(): int
280
    {
281 18
        return $this->code;
282
    }
283
284
    /**
285
     * Notify when a transaction line is added
286
     * This should only be called by TransactionLine::setDebit().
287
     */
288 31
    public function debitTransactionLineAdded(TransactionLine $transactionLine): void
289
    {
290 31
        $this->debitTransactionLines->add($transactionLine);
291
    }
292
293
    /**
294
     * Notify when a transaction line is removed
295
     * This should only be called by TransactionLine::setDebit().
296
     */
297 1
    public function debitTransactionLineRemoved(TransactionLine $transactionLine): void
298
    {
299 1
        $this->debitTransactionLines->removeElement($transactionLine);
300
    }
301
302 5
    public function getDebitTransactionLines(): Collection
303
    {
304 5
        return $this->debitTransactionLines;
305
    }
306
307
    /**
308
     * Notify when a transaction line is added
309
     * This should only be called by TransactionLine::setCredit().
310
     */
311 31
    public function creditTransactionLineAdded(TransactionLine $transactionLine): void
312
    {
313 31
        $this->creditTransactionLines->add($transactionLine);
314
    }
315
316
    /**
317
     * Notify when a transaction line is removed
318
     * This should only be called by TransactionLine::setCredit().
319
     */
320 1
    public function creditTransactionLineRemoved(TransactionLine $transactionLine): void
321
    {
322 1
        $this->creditTransactionLines->removeElement($transactionLine);
323
    }
324
325 5
    public function getCreditTransactionLines(): Collection
326
    {
327 5
        return $this->creditTransactionLines;
328
    }
329
}
330