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