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
|
|
|
|
11
|
|
|
class AccountRepository extends AbstractRepository implements LimitedAccessSubQueryInterface |
12
|
|
|
{ |
13
|
|
|
private const PARENT_ACCOUNT_ID_FOR_USER = 10011; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Returns pure SQL to get ID of all objects that are accessible to given user. |
17
|
|
|
* |
18
|
|
|
* @param null|User $user |
19
|
|
|
* |
20
|
|
|
* @return string |
21
|
|
|
*/ |
22
|
11 |
|
public function getAccessibleSubQuery(?User $user): string |
23
|
|
|
{ |
24
|
11 |
|
if (!$user) { |
25
|
1 |
|
return '-1'; |
26
|
|
|
} |
27
|
|
|
|
28
|
10 |
|
if (in_array($user->getRole(), [User::ROLE_RESPONSIBLE, User::ROLE_ADMINISTRATOR], true)) { |
29
|
2 |
|
return $this->getAllIdsQuery(); |
30
|
|
|
} |
31
|
|
|
|
32
|
8 |
|
return $this->getAllIdsForOwnerQuery($user); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Unsecured way to get a account from its ID. |
37
|
|
|
* |
38
|
|
|
* This should only be used in tests or controlled environment. |
39
|
|
|
* |
40
|
|
|
* @param int $id |
41
|
|
|
* |
42
|
|
|
* @return null|Account |
43
|
|
|
*/ |
44
|
4 |
|
private function getOneById(int $id): ?Account |
45
|
|
|
{ |
46
|
4 |
|
$this->getAclFilter()->setEnabled(false); |
47
|
4 |
|
$account = $this->findOneById($id); |
48
|
4 |
|
$this->getAclFilter()->setEnabled(true); |
49
|
|
|
|
50
|
4 |
|
return $account; |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* This will return, and potentially create, an account for the given user |
55
|
|
|
* |
56
|
|
|
* @param User $user |
57
|
|
|
* |
58
|
|
|
* @return Account |
59
|
|
|
*/ |
60
|
8 |
|
public function getOrCreate(User $user): Account |
61
|
|
|
{ |
62
|
8 |
|
$this->getAclFilter()->setEnabled(false); |
63
|
8 |
|
$account = $this->findOneByOwner($user); |
|
|
|
|
64
|
8 |
|
$this->getAclFilter()->setEnabled(true); |
65
|
|
|
|
66
|
8 |
|
if (!$account) { |
67
|
4 |
|
$account = new Account(); |
68
|
4 |
|
$this->getEntityManager()->persist($account); |
69
|
4 |
|
$account->setOwner($user); |
70
|
4 |
|
$account->setType(AccountTypeType::LIABILITY); |
71
|
4 |
|
$account->setName($user->getName()); |
72
|
|
|
|
73
|
4 |
|
$maxCode = $this->getEntityManager()->getConnection()->fetchColumn('SELECT MAX(code) FROM account WHERE parent_id = ' . self::PARENT_ACCOUNT_ID_FOR_USER); |
74
|
4 |
|
$newCode = ++$maxCode; |
75
|
4 |
|
$account->setCode((string) $newCode); |
76
|
|
|
|
77
|
4 |
|
$parent = $this->getOneById(self::PARENT_ACCOUNT_ID_FOR_USER); |
78
|
4 |
|
if (!$parent) { |
79
|
|
|
throw new \Exception('Cannot find parent account for creation of user account'); |
80
|
|
|
} |
81
|
4 |
|
$account->setParent($parent); |
82
|
|
|
} |
83
|
|
|
|
84
|
8 |
|
return $account; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Sum balance by account type |
89
|
|
|
* |
90
|
|
|
* @API\Input(type="AccountType") |
91
|
|
|
* |
92
|
|
|
* @param string $accountType |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
1 |
|
public function totalBalanceByType(string $accountType): string |
97
|
|
|
{ |
98
|
1 |
|
$qb = $this->getEntityManager()->getConnection()->createQueryBuilder() |
99
|
1 |
|
->select('SUM(balance)') |
100
|
1 |
|
->from($this->getClassMetadata()->getTableName()) |
101
|
1 |
|
->where('type = :type'); |
102
|
|
|
|
103
|
1 |
|
$qb->setParameter('type', $accountType); |
104
|
|
|
|
105
|
1 |
|
$result = $qb->execute(); |
106
|
|
|
|
107
|
1 |
|
return (string) $result->fetchColumn(); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|