Completed
Push — master ( 854d5e...fc0783 )
by Stepan
05:04
created

TicketRepository::getBoughtTicketsCountForTheLastGroupedByDateForChart()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 47
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 47
rs 8.7857
c 0
b 0
f 0
cc 6
nc 20
nop 1
1
<?php
2
3
namespace Stfalcon\Bundle\EventBundle\Repository;
4
5
use Doctrine\ORM\EntityRepository;
6
use Application\Bundle\UserBundle\Entity\User;
7
use Stfalcon\Bundle\EventBundle\Entity\Event;
8
use Stfalcon\Bundle\EventBundle\Entity\Payment;
9
10
/**
11
 * TicketRepository.
12
 *
13
 * This class was generated by the Doctrine ORM. Add your own custom
14
 * repository methods below.
15
 */
16
class TicketRepository extends EntityRepository
17
{
18
    // @todo це ппц. половина методів незрозуміло для чого. мені треба пошук квитка для юзера на івент.
19
    // підозрюю, що він тут є, але так сходу не вгадаєш
20
    // треба передивитись методи і забрати зайве, а решту нормально назвати
21
22
    /**
23
     * Find tickets of active events for some user.
24
     *
25
     * @param User $user
26
     *
27
     * @return array
28
     */
29
    public function findTicketsOfActiveEventsForUser(User $user)
30
    {
31
        $qb = $this->createQueryBuilder('t');
32
33
        return $qb->join('t.event', 'e')
34
                    ->where($qb->expr()->eq('e.active', ':active'))
35
                    ->andWhere($qb->expr()->eq('t.user', ':user'))
36
                    ->setParameters(['user' => $user, 'active' => true])
37
                    ->orderBy('e.date', 'ASC')
38
                    ->getQuery()
39
                    ->getResult();
40
    }
41
42
    /**
43
     * @param Event $event  Event
44
     * @param null  $status Status
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $status is correct as it would always require null to be passed?
Loading history...
45
     *
46
     * @return array
47
     */
48
    public function findUsersByEventAndStatus(Event $event = null, $status = null)
49
    {
50
        $query = $this->getEntityManager()
51
            ->createQueryBuilder()
52
            ->select('u', 't', 'p')
53
            ->from('StfalconEventBundle:Ticket', 't')
54
            ->join('t.user', 'u')
55
            ->join('t.event', 'e')
56
            ->join('t.payment', 'p')
57
            ->andWhere('e.active = 1');
58
59
        if (null !== $event) {
60
            $query->andWhere('t.event = :event')
61
                ->setParameter(':event', $event);
62
        }
63
        if (null !== $status) {
0 ignored issues
show
introduced by
The condition null !== $status is always false.
Loading history...
64
            $query->andWhere('p.status = :status')
65
                ->setParameter(':status', $status);
66
        }
67
68
        $query = $query->getQuery();
69
70
        $users = array();
71
        foreach ($query->execute() as $result) {
72
            $users[] = $result->getUser();
73
        }
74
75
        return $users;
76
    }
77
78
    /**
79
     * Find users by event and status.
80
     *
81
     * @param array $events Events
82
     * @param null  $status Status
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $status is correct as it would always require null to be passed?
Loading history...
83
     *
84
     * @return \Doctrine\ORM\QueryBuilder
85
     */
86
    public function findUsersByEventsAndStatusQueryBuilder($events = null, $status = null)
87
    {
88
        $qb = $this->getEntityManager()->createQueryBuilder();
89
90
        $qb->select('u')
91
            ->addSelect('t')
92
            ->from('StfalconEventBundle:Ticket', 't')
93
            ->join('t.user', 'u')
94
            ->join('t.event', 'e')
95
            //@todo hot fix необходимо сделать рассылку для предыдущих ивентов
96
            //->andWhere('e.active = :eventStatus')
97
            //->setParameter(':eventStatus', true)
98
            ->groupBy('u');
99
100
        if (null !== $events) {
101
            $qb->andWhere($qb->expr()->in('t.event', ':events'))
102
                ->setParameter(':events', $events->toArray());
103
        }
104
        if (null !== $status) {
0 ignored issues
show
introduced by
The condition null !== $status is always false.
Loading history...
105
            $statusOr = $qb->expr()->orX('p.status = :status');
106
            if ('pending' == $status) {
107
                $statusOr->add('p.status IS NULL');
108
            }
109
            $qb->leftJoin('t.payment', 'p')
110
                ->andWhere($statusOr)
111
                ->setParameter(':status', $status);
112
        }
113
114
        return $qb;
115
    }
116
117
    /**
118
     * Find users by event and status.
119
     *
120
     * @param array $events Events
121
     * @param null  $status Status
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $status is correct as it would always require null to be passed?
Loading history...
122
     *
123
     * @return \Doctrine\ORM\QueryBuilder
124
     */
125
    public function findUsersSubscribedByEventsAndStatus($events = null, $status = null)
126
    {
127
        $qb = $this->findUsersByEventsAndStatusQueryBuilder($events, $status);
128
        $qb->andWhere('u.subscribe = 1');
129
130
        $users = [];
131
132
        foreach ($qb->getQuery()->execute() as $result) {
133
            $users[] = $result->getUser();
134
        }
135
136
        return $users;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $users returns the type array which is incompatible with the documented return type Doctrine\ORM\QueryBuilder.
Loading history...
137
    }
138
139
    /**
140
     * Find users by event and status.
141
     *
142
     * @param array $events Events
143
     * @param null  $status Status
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $status is correct as it would always require null to be passed?
Loading history...
144
     *
145
     * @return array
146
     */
147
    public function findUsersByEventsAndStatus($events = null, $status = null)
148
    {
149
        $users = [];
150
151
        foreach ($this->findUsersByEventsAndStatusQueryBuilder($events, $status)->getQuery()->execute() as $result) {
152
            $users[] = $result->getUser();
153
        }
154
155
        return $users;
156
    }
157
158
    /**
159
     * Find tickets by event.
160
     *
161
     * @param Event $event
162
     *
163
     * @return array
164
     */
165
    public function findTicketsByEvent(Event $event)
166
    {
167
        return $this->getEntityManager()
168
            ->createQuery('
169
                SELECT t
170
                FROM StfalconEventBundle:Ticket t
171
                JOIN t.event e
172
                WHERE e.active = TRUE
173
                    AND t.event = :event
174
                GROUP BY t.user
175
            ')
176
            ->setParameter('event', $event)
177
            ->getResult();
178
    }
179
180
    /**
181
     * Find tickets by event group by user.
182
     *
183
     * @param Event $event
184
     * @param int   $count
185
     * @param int   $offset
186
     *
187
     * @return array
188
     */
189
    public function findTicketsByEventGroupByUser(Event $event, $count = null, $offset = null)
190
    {
191
        $qb = $this->createQueryBuilder('t');
192
193
        $qb->select('t')
194
            ->join('t.event', 'e')
195
            ->where('e.active = true')
196
            ->andWhere('t.event = :event')
197
            ->groupBy('t.user')
198
            ->setParameter('event', $event);
199
200
        if (isset($count) && $count > 0) {
201
            $qb->setMaxResults($count);
202
        }
203
204
        if (isset($offset) && $offset > 0) {
205
            $qb->setFirstResult($offset);
206
        }
207
208
        return $qb->getQuery()->getResult();
209
    }
210
211
    /**
212
     * Find ticket for some user and event with not null payment.
213
     *
214
     * @param User  $user  User
215
     * @param Event $event Event
216
     *
217
     * @return array
218
     */
219
    public function findOneByUserAndEvent($user, $event)
220
    {
221
        $qb = $this->createQueryBuilder('t');
222
223
        return $qb->select('t')
224
            ->where('t.event = :event')
225
            ->andWhere('t.user = :user')
226
            ->andWhere($qb->expr()->isNotNull('t.payment'))
227
            ->setParameter('event', $event)
228
            ->setParameter('user', $user)
229
            ->setMaxResults(1)
230
            ->getQuery()
231
            ->getOneOrNullResult();
232
    }
233
234
    /**
235
     * Get all tickets for payment.
236
     *
237
     * @param Payment $payment
238
     *
239
     * @return array
240
     */
241
    public function getAllTicketsByPayment(Payment $payment)
242
    {
243
        return $this->findBy(['payment' => $payment]);
244
    }
245
246
    /**
247
     * @return array
248
     */
249
    public function getPaidTicketsCount()
250
    {
251
        $qb = $this->createQueryBuilder('t');
252
        $qb->select('COUNT(t)')
253
            ->addSelect('u.id')
254
            ->join('t.payment', 'p')
255
            ->join('t.user', 'u')
256
            ->where($qb->expr()->eq('p.status', ':status'))
257
            ->setParameter('status', 'paid')
258
            ->groupBy('u.id');
259
260
        return  $qb->getQuery()->getResult();
261
    }
262
263
    /**
264
     * @return array
265
     */
266
    public function getTicketsCountByEventGroup()
267
    {
268
        $qb = $this->createQueryBuilder('t');
269
        $qb->select('g.name')
270
            ->addSelect('u.id')
271
            ->addSelect('COUNT(t.id)')
272
            ->join('t.event', 'e')
273
            ->join('t.payment', 'p')
274
            ->join('e.group', 'g')
275
            ->join('t.user', 'u')
276
            ->where($qb->expr()->eq('p.status', ':status'))
277
            ->setParameter('status', 'paid')
278
            ->groupBy('u.id')
279
            ->addGroupBy('g.name')
280
        ;
281
282
        return $qb->getQuery()->getResult();
283
    }
284
285
    /**
286
     * @param int $event1Id
287
     * @param int $event2Id
288
     *
289
     * @return int
290
     */
291
    public function getUserVisitsEventCount($event1Id, $event2Id)
292
    {
293
        $qb = $this->createQueryBuilder('t');
294
        $qb->select('COUNT(t.user) AS cnt')
295
            ->from('Stfalcon\Bundle\EventBundle\Entity\Ticket', 't2')
296
            ->join('t.payment', 'p1')
297
            ->join('t2.payment', 'p2')
298
            ->where($qb->expr()->eq('p1.status', ':status'))
299
            ->andWhere($qb->expr()->eq('p2.status', ':status'))
300
            ->andWhere($qb->expr()->eq('t.user', 't2.user'))
301
            ->andWhere($qb->expr()->eq('t.event', ':event1'))
302
            ->andWhere($qb->expr()->eq('t2.event', ':event2'))
303
            ->setParameters(['status' => 'paid', 'event1' => $event1Id, 'event2' => $event2Id])
304
        ;
305
306
        return $qb->getQuery()->getSingleScalarResult();
307
    }
308
309
    /**
310
     * Get All event with paid tickets.
311
     *
312
     * @return array
313
     */
314
    public function getEventWithTicketsCount()
315
    {
316
        $qb = $this->createQueryBuilder('t');
317
        $qb->select('e.id', 'e.slug', 'COUNT(t.id) AS cnt')
318
            ->join('t.payment', 'p')
319
            ->join('t.event', 'e')
320
            ->where($qb->expr()->eq('p.status', ':status'))
321
            ->setParameter('status', 'paid')
322
            ->groupBy('e.id')
323
            ->orderBy('e.id', 'DESC')
324
        ;
325
326
        return $qb->getQuery()->getResult();
327
    }
328
329
    /**
330
     * @param Event $event
331
     *
332
     * @return int
333
     */
334
    public function getEventTicketsWithoutTicketCostCount(Event $event)
335
    {
336
        $qb = $this->createQueryBuilder('t');
337
        $qb
338
            ->select('COUNT(t.id)')
339
            ->join('t.payment', 'p')
340
            ->where($qb->expr()->eq('p.status', ':status'))
341
            ->andWhere($qb->expr()->eq('t.event', ':event'))
342
            ->andWhere($qb->expr()->isNull('t.ticketCost'))
343
            ->setParameters([
344
                'status' => 'paid',
345
                'event' => $event,
346
            ])
347
        ;
348
349
        return $qb->getQuery()->getSingleScalarResult();
350
    }
351
}
352