Completed
Push — master ( 4ea7f1...ba6dcf )
by Louis
14s
created

TransactionRepository::getSoldBeers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 19
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
namespace KI\FoyerBundle\Repository;
3
4
use Doctrine\ORM\EntityRepository;
5
use KI\FoyerBundle\Entity\Transaction;
6
use KI\UserBundle\Entity\User;
7
8
class TransactionRepository extends EntityRepository
9
{
10
    /**
11
     * @return object[]
12
     */
13
    public function getHallOfFame()
14
    {
15
        $sept1 = strtotime("September 1st -1Year");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal September 1st -1Year does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
16
17
        $hallOfFame = $this->getEntityManager()->createQuery('SELECT usr AS user, SUM(beer.volume) AS liters FROM
18
            KIUserBundle:User usr,
19
            KIFoyerBundle:Transaction transac,
20
            KIFoyerBundle:Beer beer
21
            WHERE transac.user = usr
22
            AND transac.beer = beer
23
            AND transac.beer IS NOT NULL
24
            AND usr.balance > 0
25
            AND transac.date > :schoolYear
26
            GROUP BY usr.id
27
            ORDER BY liters DESC
28
            ')
29
            ->setParameter('schoolYear', $sept1)
30
            ->setMaxResults(10)
31
            ->getResult();
32
33
        foreach ($hallOfFame as &$data){
34
            $data['liters'] = round($data['liters'], 2);
35
        }
36
37
        return $hallOfFame;
38
    }
39
40
    /**
41
     * @return object[]
42
     */
43
    public function getPromoBalances()
44
    {
45
        $promoBalances = $this->getEntityManager()->createQuery('SELECT
46
            SUM(usr.balance) AS promoBalance, usr.promo as promo FROM
47
            KIUserBundle:User usr
48
            GROUP BY promo
49
            ORDER BY promo ASC
50
            ')
51
            ->getArrayResult();
52
53
        foreach ($promoBalances as &$promoBalance){
54
            $promoBalance['promoBalance'] = round($promoBalance['promoBalance'], 2);
55
        }
56
57
        return $promoBalances;
58
    }
59
60
    /**
61
     * @return object[]
62
     */
63
    public function getSoldBeers()
64
    {
65
        $sept1 = strtotime("September 1st -1Year");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal September 1st -1Year does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
66
67
        $soldBeers = $this->getEntityManager()->createQuery('SELECT
68
            COUNT(beer.id) AS soldBeer, beer.name as name FROM
69
            KIFoyerBundle:Transaction transac,
70
            KIFoyerBundle:Beer beer
71
            WHERE transac.beer = beer
72
            AND transac.beer IS NOT NULL
73
            AND transac.date > :schoolYear
74
            GROUP BY beer.id
75
            ORDER BY soldBeer DESC
76
            ')
77
            ->setParameter('schoolYear', $sept1)
78
            ->getArrayResult();
79
80
        return $soldBeers;
81
    }
82
83
84
85
86
    /**
87
     * Retourne les statistiques d'un utilisateur particulier
88
     * @param  User $user
89
     * @return array
90
     */
91
    public function getUserStatistics(User $user)
92
    {
93
        $timeGraph = [];
94
        $pieGraph = [];
95
        $beersCountArray = [];
96
        $volume = 0;
97
        $beerCount = 0;
98
99
        $transactions = $this->findBy(['user' => $user], ['date' => 'ASC']);
100
101
        foreach ($transactions as $transaction) {
102
            $beer = $transaction->getBeer();
103
            // Compte crédité, pas une conso
104
            if ($beer === null) {
105
                continue;
106
            }
107
            $volume += $beer->getVolume();
108
109
            $timeGraph[$transaction->getDate()] = $volume;
110
111
            $beerCount++;
112
113
            if (!isset($pieGraph[$beer->getSlug()])) {
114
                $pieGraph[$beer->getSlug()] = ['beer' => $beer, 'count' => 0];
115
                $beersCountArray[$beer->getSlug()] = 0;
116
            }
117
            $pieGraph[$beer->getSlug()]['count']++;
118
            $beersCountArray[$beer->getSlug()]++;
119
        }
120
121
        array_multisort($beersCountArray, SORT_DESC, $pieGraph);
122
123
        return [
124
            'beersDrunk'    => $pieGraph,
125
            'stackedLiters' => $timeGraph,
126
            'totalLiters'   => $volume,
127
            'totalBeers'    => $beerCount
128
        ];
129
    }
130
}
131