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