1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Del\Expenses\Repository; |
4
|
|
|
|
5
|
|
|
use Del\Expenses\Entity\EntryInterface; |
6
|
|
|
use Del\Expenses\Criteria\EntryCriteria; |
7
|
|
|
use Del\Expenses\Entity\Expenditure; |
8
|
|
|
use Del\Expenses\Entity\Income; |
9
|
|
|
use Doctrine\ORM\EntityRepository; |
10
|
|
|
use Doctrine\ORM\QueryBuilder; |
11
|
|
|
|
12
|
|
|
class EntryRepository extends EntityRepository |
13
|
|
|
{ |
14
|
|
|
/** @var QueryBuilder */ |
15
|
|
|
private $qb; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param EntryInterface $entry |
19
|
|
|
* @return EntryInterface|Income|Expenditure |
20
|
|
|
*/ |
21
|
3 |
|
public function save(EntryInterface $entry) |
22
|
|
|
{ |
23
|
3 |
|
$this->_em->persist($entry); |
24
|
3 |
|
$this->_em->flush(); |
25
|
3 |
|
return $entry; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param EntryInterface $entry |
30
|
|
|
*/ |
31
|
3 |
|
public function delete(EntryInterface $entry) |
32
|
|
|
{ |
33
|
3 |
|
$this->_em->remove($entry); |
34
|
3 |
|
$this->_em->flush(); |
35
|
3 |
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param EntryCriteria $criteria |
40
|
|
|
* @return EntryInterface|Income|Expenditure|null |
41
|
|
|
*/ |
42
|
2 |
|
public function findOneByCriteria(EntryCriteria $criteria) |
43
|
|
|
{ |
44
|
2 |
|
$results = $this->findByCriteria($criteria); |
45
|
2 |
|
return isset($results[0]) ? $results[0] : null; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param EntryCriteria $criteria |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
3 |
|
public function findByCriteria(EntryCriteria $criteria) |
55
|
|
|
{ |
56
|
3 |
|
$this->qb = $this->createQueryBuilder('e'); |
57
|
3 |
|
$this->checkId($criteria); |
58
|
3 |
|
$this->checkUserId($criteria); |
59
|
3 |
|
$this->checkDate($criteria); |
60
|
3 |
|
$this->checkDateRange($criteria); |
61
|
3 |
|
$this->checkAmount($criteria); |
62
|
3 |
|
$this->checkCategory($criteria); |
63
|
3 |
|
$this->checkDescription($criteria); |
64
|
3 |
|
$this->checkNote($criteria); |
65
|
3 |
|
$this->checkType($criteria); |
66
|
3 |
|
$this->checkOrder($criteria); |
67
|
3 |
|
$this->checkLimit($criteria); |
68
|
3 |
|
$this->checkOffset($criteria); |
69
|
3 |
|
$query = $this->qb->getQuery(); |
70
|
3 |
|
unset($this->qb); |
71
|
3 |
|
return $query->getResult(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param EntryCriteria $criteria |
76
|
|
|
*/ |
77
|
3 |
|
private function checkId(EntryCriteria $criteria) |
78
|
|
|
{ |
79
|
3 |
|
if($criteria->hasId()) { |
80
|
2 |
|
$this->qb->andWhere('e.id = :id'); |
81
|
2 |
|
$this->qb->setParameter('id', $criteria->getId()); |
82
|
2 |
|
} |
83
|
3 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param EntryCriteria $criteria |
87
|
|
|
*/ |
88
|
3 |
|
private function checkUserId(EntryCriteria $criteria) |
89
|
|
|
{ |
90
|
3 |
|
if($criteria->hasUserId()) { |
91
|
|
|
$this->qb->andWhere('e.userId = :userid'); |
92
|
|
|
$this->qb->setParameter('userid', $criteria->getUserId()); |
93
|
|
|
} |
94
|
3 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param EntryCriteria $criteria |
98
|
|
|
*/ |
99
|
3 |
|
private function checkDate(EntryCriteria $criteria) |
100
|
|
|
{ |
101
|
3 |
|
if($criteria->hasDate()) { |
102
|
1 |
|
$this->qb->andWhere('e.date = :date'); |
103
|
1 |
|
$this->qb->setParameter('date', $criteria->getDate()); |
104
|
1 |
|
} |
105
|
3 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param EntryCriteria $criteria |
109
|
|
|
*/ |
110
|
3 |
|
private function checkDateRange(EntryCriteria $criteria) |
111
|
|
|
{ |
112
|
3 |
|
if($criteria->hasDateRange()) { |
113
|
1 |
|
$this->qb->andWhere('e.date BETWEEN :fromdate AND :todate'); |
114
|
1 |
|
$this->qb->setParameter('fromdate', $criteria->getDateRange()[0]); |
115
|
1 |
|
$this->qb->setParameter('todate', $criteria->getDateRange()[1]); |
116
|
1 |
|
} |
117
|
3 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param EntryCriteria $criteria |
121
|
|
|
*/ |
122
|
3 |
|
private function checkAmount(EntryCriteria $criteria) |
123
|
|
|
{ |
124
|
3 |
|
if($criteria->hasAmount()) { |
125
|
|
|
$this->qb->andWhere('e.amount = :amount'); |
126
|
|
|
$this->qb->setParameter('amount', $criteria->getAmount()); |
127
|
|
|
} |
128
|
3 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param EntryCriteria $criteria |
132
|
|
|
*/ |
133
|
3 |
|
private function checkCategory(EntryCriteria $criteria) |
134
|
|
|
{ |
135
|
3 |
|
if($criteria->hasCategory()) { |
136
|
1 |
|
$this->qb->andWhere('e.category = :category'); |
137
|
1 |
|
$this->qb->setParameter('category', $criteria->getCategory()); |
138
|
1 |
|
} |
139
|
3 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param EntryCriteria $criteria |
143
|
|
|
*/ |
144
|
3 |
|
private function checkDescription(EntryCriteria $criteria) |
145
|
|
|
{ |
146
|
3 |
|
if($criteria->hasDescription()) { |
147
|
1 |
|
$this->qb->andWhere('e.description = :description'); |
148
|
1 |
|
$this->qb->setParameter('description', $criteria->getDescription()); |
149
|
1 |
|
} |
150
|
3 |
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param EntryCriteria $criteria |
154
|
|
|
*/ |
155
|
3 |
|
private function checkNote(EntryCriteria $criteria) |
156
|
|
|
{ |
157
|
3 |
|
if($criteria->hasNote()) { |
158
|
1 |
|
$this->qb->andWhere('e.note = :note'); |
159
|
1 |
|
$this->qb->setParameter('note', $criteria->getNote()); |
160
|
1 |
|
} |
161
|
3 |
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param EntryCriteria $criteria |
165
|
|
|
*/ |
166
|
3 |
|
private function checkType(EntryCriteria $criteria) |
167
|
|
|
{ |
168
|
3 |
|
if($criteria->hasType()) { |
169
|
1 |
|
$this->processType($criteria); |
170
|
1 |
|
} |
171
|
3 |
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param EntryCriteria $criteria |
175
|
|
|
*/ |
176
|
1 |
|
private function processType(EntryCriteria $criteria) |
177
|
|
|
{ |
178
|
1 |
|
switch($criteria->getType()) { |
179
|
1 |
|
case 'IN': |
180
|
1 |
|
$this->qb->andWhere('e INSTANCE OF Del\Expenses\Entity\Income'); |
181
|
1 |
|
break; |
182
|
1 |
|
case 'OUT': |
183
|
1 |
|
$this->qb->andWhere('e INSTANCE OF Del\Expenses\Entity\Expenditure'); |
184
|
1 |
|
break; |
185
|
1 |
|
} |
186
|
1 |
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param EntryCriteria $criteria |
190
|
|
|
*/ |
191
|
3 |
|
private function checkOrder(EntryCriteria $criteria) |
192
|
|
|
{ |
193
|
3 |
|
$criteria->hasOrder() ? $this->qb->addOrderBy('e.'.$criteria->getOrder(), $criteria->getOrderDirection()) : null; |
194
|
|
|
|
195
|
3 |
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param EntryCriteria $criteria |
199
|
|
|
*/ |
200
|
3 |
|
private function checkLimit(EntryCriteria $criteria) |
201
|
|
|
{ |
202
|
3 |
|
$criteria->hasLimit() ? $this->qb->setMaxResults($criteria->getLimit()) : null; |
203
|
3 |
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param EntryCriteria $criteria |
207
|
|
|
*/ |
208
|
3 |
|
private function checkOffset(EntryCriteria $criteria) |
209
|
|
|
{ |
210
|
3 |
|
$criteria->hasOffset() ? $this->qb->setFirstResult($criteria->getOffset()) : null; |
211
|
3 |
|
} |
212
|
|
|
|
213
|
|
|
} |
214
|
|
|
|