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