1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Application\Repository; |
6
|
|
|
|
7
|
|
|
use Application\DBAL\Types\AccountTypeType; |
8
|
|
|
use Application\Model\Account; |
9
|
|
|
use Application\Model\User; |
10
|
|
|
use Ecodev\Felix\Repository\LimitedAccessSubQuery; |
11
|
|
|
use Exception; |
12
|
|
|
use Money\Money; |
13
|
|
|
|
14
|
|
|
class AccountRepository extends AbstractRepository implements LimitedAccessSubQuery |
15
|
|
|
{ |
16
|
|
|
const ACCOUNT_ID_FOR_BANK = 10025; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* In memory max code that keep being incremented if we create several account at once without flushing in DB |
20
|
|
|
*/ |
21
|
|
|
private ?int $maxCode = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Clear all caches |
25
|
|
|
*/ |
26
|
115 |
|
public function clearCache(): void |
27
|
|
|
{ |
28
|
115 |
|
$this->maxCode = null; |
29
|
115 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Returns pure SQL to get ID of all objects that are accessible to given user. |
33
|
|
|
* |
34
|
|
|
* @param null|User $user |
35
|
|
|
*/ |
36
|
20 |
|
public function getAccessibleSubQuery(?\Ecodev\Felix\Model\User $user): string |
37
|
|
|
{ |
38
|
20 |
|
if (!$user) { |
39
|
1 |
|
return '-1'; |
40
|
|
|
} |
41
|
|
|
|
42
|
19 |
|
if (in_array($user->getRole(), [User::ROLE_RESPONSIBLE, User::ROLE_ADMINISTRATOR], true)) { |
43
|
9 |
|
return $this->getAllIdsQuery(); |
44
|
|
|
} |
45
|
|
|
|
46
|
10 |
|
return $this->getAllIdsForFamilyQuery($user); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Unsecured way to get a account from its ID. |
51
|
|
|
* |
52
|
|
|
* This should only be used in tests or controlled environment. |
53
|
|
|
*/ |
54
|
4 |
|
public function getOneById(int $id): Account |
55
|
|
|
{ |
56
|
4 |
|
$account = $this->getAclFilter()->runWithoutAcl(function () use ($id) { |
57
|
4 |
|
return $this->findOneById($id); |
58
|
4 |
|
}); |
59
|
|
|
|
60
|
4 |
|
if (!$account) { |
61
|
1 |
|
throw new Exception('Account #' . $id . ' not found'); |
62
|
|
|
} |
63
|
|
|
|
64
|
4 |
|
return $account; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* This will return, and potentially create, an account for the given user |
69
|
|
|
*/ |
70
|
21 |
|
public function getOrCreate(User $user): Account |
71
|
|
|
{ |
72
|
|
|
global $container; |
73
|
|
|
|
74
|
|
|
// If an account already exists, because getOrCreate was called once before without flushing in between, |
75
|
|
|
// then can return immediately |
76
|
21 |
|
if ($user->getAccount()) { |
77
|
14 |
|
return $user->getAccount(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// If user have an owner, then create account for the owner instead |
81
|
9 |
|
if ($user->getOwner()) { |
82
|
|
|
$user = $user->getOwner(); |
83
|
|
|
} |
84
|
|
|
|
85
|
9 |
|
$account = $this->getAclFilter()->runWithoutAcl(function () use ($user) { |
86
|
9 |
|
return $this->findOneByOwner($user); |
|
|
|
|
87
|
9 |
|
}); |
88
|
|
|
|
89
|
9 |
|
if (!$account) { |
90
|
9 |
|
$account = new Account(); |
91
|
9 |
|
$this->getEntityManager()->persist($account); |
92
|
9 |
|
$account->setOwner($user); |
93
|
9 |
|
$account->setType(AccountTypeType::LIABILITY); |
94
|
9 |
|
$account->setName($user->getName()); |
95
|
|
|
|
96
|
9 |
|
$config = $container->get('config'); |
97
|
9 |
|
$parentCode = (int) $config['accounting']['customerDepositsAccountCode']; |
98
|
9 |
|
$parent = $this->getAclFilter()->runWithoutAcl(function () use ($parentCode) { |
99
|
9 |
|
return $this->findOneByCode($parentCode); |
|
|
|
|
100
|
9 |
|
}); |
101
|
|
|
|
102
|
|
|
// Find the max account code, using the liability parent code as prefix |
103
|
9 |
|
if (!$this->maxCode) { |
|
|
|
|
104
|
9 |
|
$maxQuery = 'SELECT MAX(code) FROM account WHERE code LIKE ' . $this->getEntityManager()->getConnection()->quote($parent->getCode() . '%'); |
105
|
9 |
|
$this->maxCode = (int) $this->getEntityManager()->getConnection()->fetchColumn($maxQuery); |
|
|
|
|
106
|
|
|
|
107
|
|
|
// If there is no child account yet, reserve enough digits for many users |
108
|
9 |
|
if ($this->maxCode === $parent->getCode()) { |
109
|
1 |
|
$this->maxCode = $parent->getCode() * 10000; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
9 |
|
$nextCode = ++$this->maxCode; |
114
|
9 |
|
$account->setCode($nextCode); |
115
|
|
|
|
116
|
9 |
|
$account->setParent($parent); |
117
|
|
|
} |
118
|
|
|
|
119
|
9 |
|
return $account; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Sum balance by account type |
124
|
|
|
* |
125
|
|
|
* @API\Input(type="AccountType") |
126
|
|
|
*/ |
127
|
2 |
|
public function totalBalanceByType(string $accountType): Money |
128
|
|
|
{ |
129
|
2 |
|
$qb = $this->getEntityManager()->getConnection()->createQueryBuilder() |
130
|
2 |
|
->select('SUM(balance)') |
131
|
2 |
|
->from($this->getClassMetadata()->getTableName()) |
132
|
2 |
|
->where('type = :type'); |
133
|
|
|
|
134
|
2 |
|
$qb->setParameter('type', $accountType); |
135
|
|
|
|
136
|
2 |
|
$result = $qb->execute(); |
137
|
|
|
|
138
|
2 |
|
return Money::CHF($result->fetchColumn()); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Update accounts' balance |
143
|
|
|
* |
144
|
|
|
* @param null|Account $account the account to update, or null for all accounts |
145
|
|
|
*/ |
146
|
1 |
|
public function updateAccountBalance(?Account $account = null): void |
147
|
|
|
{ |
148
|
1 |
|
$connection = $this->getEntityManager()->getConnection(); |
149
|
1 |
|
$sql = 'CALL update_account_balance(?)'; |
150
|
|
|
|
151
|
1 |
|
if ($account) { |
152
|
|
|
$connection->executeQuery($sql, [$account->getId()]); |
153
|
|
|
} else { |
154
|
1 |
|
foreach ($this->findAll() as $a) { |
155
|
1 |
|
$connection->executeQuery($sql, [$a->getId()]); |
156
|
|
|
} |
157
|
|
|
} |
158
|
1 |
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Return the next available Account code |
162
|
|
|
*/ |
163
|
|
|
public function getNextCodeAvailable(): int |
164
|
|
|
{ |
165
|
|
|
$qb = _em()->getConnection()->createQueryBuilder() |
166
|
|
|
->select('IFNULL(MAX(a.code) + 1, 1)') |
167
|
|
|
->from('account', 'a'); |
168
|
|
|
|
169
|
|
|
return (int) $qb->execute()->fetchColumn(); |
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
2 |
|
public function deleteAccountOfNonFamilyOwnerWithoutAnyTransactions(): int |
173
|
|
|
{ |
174
|
|
|
$sql = <<<STRING |
175
|
2 |
|
DELETE account FROM account |
176
|
|
|
INNER JOIN user ON account.owner_id = user.id |
177
|
|
|
AND user.owner_id IS NOT NULL |
178
|
|
|
AND user.owner_id != user.id |
179
|
|
|
WHERE |
180
|
|
|
account.id NOT IN (SELECT credit_id FROM transaction_line WHERE credit_id IS NOT NULL) |
181
|
|
|
AND account.id NOT IN (SELECT debit_id FROM transaction_line WHERE debit_id IS NOT NULL) |
182
|
|
|
STRING; |
183
|
|
|
|
184
|
2 |
|
$count = $this->getEntityManager()->getConnection()->executeUpdate($sql); |
|
|
|
|
185
|
|
|
|
186
|
2 |
|
return $count; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|