|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Application\Repository; |
|
6
|
|
|
|
|
7
|
|
|
use Application\Model\Account; |
|
8
|
|
|
use Application\Model\User; |
|
9
|
|
|
use Cake\Chronos\Chronos; |
|
10
|
|
|
use Cake\Chronos\Date; |
|
11
|
|
|
use Ecodev\Felix\Api\Exception; |
|
12
|
|
|
use Ecodev\Felix\Repository\LimitedAccessSubQuery; |
|
13
|
|
|
use Money\Money; |
|
14
|
|
|
|
|
15
|
|
|
class TransactionLineRepository extends AbstractRepository implements LimitedAccessSubQuery |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Returns pure SQL to get ID of all objects that are accessible to given user. |
|
19
|
|
|
* |
|
20
|
|
|
* @param null|User $user |
|
21
|
|
|
*/ |
|
22
|
25 |
|
public function getAccessibleSubQuery(?\Ecodev\Felix\Model\User $user): string |
|
23
|
|
|
{ |
|
24
|
25 |
|
if (!$user) { |
|
25
|
1 |
|
return '-1'; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
24 |
|
if (in_array($user->getRole(), [User::ROLE_ACCOUNTING_VERIFICATOR, User::ROLE_RESPONSIBLE, User::ROLE_ADMINISTRATOR], true)) { |
|
29
|
21 |
|
return $this->getAllIdsQuery(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
3 |
|
if ($user->getOwner()) { |
|
33
|
1 |
|
$id = $user->getOwner()->getId(); |
|
34
|
|
|
} else { |
|
35
|
2 |
|
$id = $user->getId(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return 'SELECT transaction_line.id FROM transaction_line |
|
39
|
|
|
JOIN account ON transaction_line.debit_id = account.id OR transaction_line.credit_id = account.id |
|
40
|
3 |
|
WHERE account.owner_id = ' . $id; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Compute the total balance by credit or debit account and date range |
|
45
|
|
|
* |
|
46
|
|
|
* @param null|Date $dateStart the lines from this date, included |
|
47
|
|
|
* @param null|Date $dateEnd the line until this date, included |
|
48
|
|
|
*/ |
|
49
|
4 |
|
public function totalBalance(?Account $debitAccount, ?Account $creditAccount, ?Date $dateStart = null, ?Date $dateEnd = null): Money |
|
50
|
|
|
{ |
|
51
|
4 |
|
if ($debitAccount === null && $creditAccount === null) { |
|
52
|
|
|
throw new Exception('At least one debit or credit account is needed to compute the total balance'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
4 |
|
$qb = $this->getEntityManager()->getConnection()->createQueryBuilder() |
|
56
|
4 |
|
->select('SUM(balance)') |
|
57
|
4 |
|
->from('transaction_line'); |
|
58
|
|
|
|
|
59
|
4 |
|
if ($debitAccount) { |
|
60
|
4 |
|
$qb->andWhere('debit_id = :debit') |
|
61
|
4 |
|
->setParameter('debit', $debitAccount->getId()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
4 |
|
if ($creditAccount) { |
|
65
|
4 |
|
$qb->andWhere('credit_id = :credit') |
|
66
|
4 |
|
->setParameter('credit', $creditAccount->getId()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
4 |
|
if ($dateStart) { |
|
70
|
1 |
|
$qb->andWhere('transaction_date >= :dateStart') |
|
71
|
1 |
|
->setParameter('dateStart', $dateStart); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
4 |
|
if ($dateEnd) { |
|
75
|
4 |
|
$qb->andWhere('transaction_date <= :dateEnd') |
|
76
|
4 |
|
->setParameter('dateEnd', $dateEnd); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
4 |
|
$result = $qb->execute(); |
|
80
|
|
|
|
|
81
|
4 |
|
return Money::CHF((int) $result->fetchOne()); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
13 |
|
public function importedExists(string $importedId, Chronos $transactionDate): bool |
|
85
|
|
|
{ |
|
86
|
13 |
|
$connection = $this->getEntityManager()->getConnection(); |
|
87
|
13 |
|
$count = $connection->fetchOne('SELECT COUNT(*) > 0 FROM transaction_line WHERE imported_id = :importedId AND transaction_date = :transactionDate', [ |
|
88
|
13 |
|
'importedId' => $importedId, |
|
89
|
13 |
|
'transactionDate' => $transactionDate, |
|
90
|
|
|
]); |
|
91
|
|
|
|
|
92
|
13 |
|
return (bool) $count; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
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.