Periode.php 4.46 KB
Newer Older
1
2
3
4
<?php

namespace Application\Service;

5
use Application\Entity\Db\Structure;
lecluse's avatar
#3930    
lecluse committed
6
use Application\Entity\Db\Periode as PeriodeEntity;
lecluse's avatar
lecluse committed
7

8
use Application\Service\Traits\IntervenantServiceAwareTrait;
9
10
use Application\Service\Traits\MiseEnPaiementAwareTrait;
use Application\Service\Traits\MiseEnPaiementIntervenantStructureAwareTrait;
11
12
13
14
15
16
17
18
use Doctrine\ORM\QueryBuilder;


/**
 * Description of Periode
 *
 * @author Laurent LÉCLUSE <laurent.lecluse at unicaen.fr>
 */
19
class Periode extends AbstractEntityService
20
{
21
    use MiseEnPaiementAwareTrait;
22
    use IntervenantServiceAwareTrait;
23
24
    use MiseEnPaiementIntervenantStructureAwareTrait;

25
26
27
    /**
     * Périodes d'enseignement
     *
lecluse's avatar
#3930    
lecluse committed
28
     * @var PeriodeEntity[]
29
30
31
     */
    protected $enseignement;

32
33
34
35
36
    /**
     * @var PeriodeEntity[]
     */
    private $cache;

37
38


39
    /**
40
     * retourne la classe des entités
41
     *
42
43
     * @return string
     * @throws RuntimeException
44
     */
45
46
    public function getEntityClass()
    {
lecluse's avatar
#3930    
lecluse committed
47
        return PeriodeEntity::class;
48
    }
49

lecluse's avatar
#3930    
lecluse committed
50
51


52
    /**
53
     * Retourne l'alias d'entité courante
54
     *
55
     * @return string
56
     */
lecluse's avatar
#3930    
lecluse committed
57
58
    public function getAlias()
    {
59
        return 'per';
60
61
    }

lecluse's avatar
#3930    
lecluse committed
62
63


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    /**
     *
     * @param string $code
     *
     * @return PeriodeEntity
     */
    public function getByCode($code)
    {
        if (!isset($this->cache[$code])) {
            $this->cache[$code] = $this->getRepo()->findOneBy(['code' => $code]);
        }

        return $this->cache[$code];
    }



lecluse's avatar
lecluse committed
81
82
83
    /**
     *
     * @param \DateTime $date
lecluse's avatar
#3930    
lecluse committed
84
     *
lecluse's avatar
lecluse committed
85
86
     * @return PeriodeEntity
     */
lecluse's avatar
#3930    
lecluse committed
87
    public function getPeriodePaiement(\DateTime $date = null)
lecluse's avatar
lecluse committed
88
    {
89
        $anneeDateDebut = $this->getServiceContext()->getAnnee()->getDateDebut();
lecluse's avatar
#3930    
lecluse committed
90
91
        $aY             = (int)$anneeDateDebut->format('Y');
        $aM             = (int)$anneeDateDebut->format('n');
92

lecluse's avatar
lecluse committed
93
        if (empty($date)) $date = new \DateTime;
94
95
96
        $dY = (int)$date->format('Y');
        $dM = (int)$date->format('n');

lecluse's avatar
#3930    
lecluse committed
97
        $ecartMoisPaiement = ($dY - $aY) * 12 + $dM - $aM;
98
99

        return $this->getRepo()->findOneBy(['paiement' => true, 'ecartMoisPaiement' => $ecartMoisPaiement]);
lecluse's avatar
lecluse committed
100
101
    }

lecluse's avatar
#3930    
lecluse committed
102
103


104
105
106
107
    /**
     * Retourne la liste des périodes d'enseignement
     *
     * @param QueryBuilder|null $queryBuilder
lecluse's avatar
#3930    
lecluse committed
108
     *
109
110
     * @return QueryBuilder
     */
lecluse's avatar
#3930    
lecluse committed
111
    public function finderByEnseignement(QueryBuilder $qb = null, $alias = null)
112
    {
lecluse's avatar
#3930    
lecluse committed
113
        list($qb, $alias) = $this->initQuery($qb, $alias);
114
        $qb->andWhere("$alias.enseignement = 1");
lecluse's avatar
#3930    
lecluse committed
115

116
117
        return $qb;
    }
118

lecluse's avatar
#3930    
lecluse committed
119
120


121
    public function finderByMiseEnPaiement(Structure $structure = null, QueryBuilder $qb = null, $alias = null)
lecluse's avatar
lecluse committed
122
    {
123
        $serviceMIS = $this->getServiceMiseEnPaiementIntervenantStructure();
lecluse's avatar
lecluse committed
124

lecluse's avatar
#3930    
lecluse committed
125
        list($qb, $alias) = $this->initQuery($qb, $alias);
lecluse's avatar
lecluse committed
126

lecluse's avatar
#3930    
lecluse committed
127
        $this->join($serviceMIS, $qb, 'miseEnPaiementIntervenantStructure', false, $alias);
128
129
        $serviceMIS->join($this->getServiceMiseEnPaiement(), $qb, 'miseEnPaiement');
        $serviceMIS->join($this->getServiceIntervenant(), $qb, 'intervenant', false);
lecluse's avatar
lecluse committed
130

lecluse's avatar
#3930    
lecluse committed
131
132
        if ($structure) {
            $serviceMIS->finderByStructure($structure, $qb);
lecluse's avatar
lecluse committed
133
134
        }

135
        $this->getServiceIntervenant()->finderByAnnee($this->getServiceContext()->getAnnee(), $qb);
136

lecluse's avatar
lecluse committed
137
138
139
        return $qb;
    }

lecluse's avatar
#3930    
lecluse committed
140
141


142
143
    /**
     *
lecluse's avatar
#3930    
lecluse committed
144
145
     * @param QueryBuilder|null $qb
     * @param string|null       $alias
146
     */
lecluse's avatar
#3930    
lecluse committed
147
    public function orderBy(QueryBuilder $qb = null, $alias = null)
148
    {
lecluse's avatar
#3930    
lecluse committed
149
150
        list($qb, $alias) = $this->initQuery($qb, $alias);

151
        $qb->orderBy("$alias.ordre");
lecluse's avatar
#3930    
lecluse committed
152
153

        return $qb;
154
    }
lecluse's avatar
lecluse committed
155

lecluse's avatar
#3930    
lecluse committed
156
157


158
    /**
lecluse's avatar
#3930    
lecluse committed
159
     * Retourne la liste des périodes d'enseignement
160
     *
lecluse's avatar
#3930    
lecluse committed
161
     * @return PeriodeEntity[]
162
163
164
     */
    public function getEnseignement()
    {
lecluse's avatar
#3930    
lecluse committed
165
166
        if (!$this->enseignement) {
            $this->enseignement = $this->getList($this->finderByEnseignement());
167
        }
lecluse's avatar
#3930    
lecluse committed
168

169
170
171
        return $this->enseignement;
    }

lecluse's avatar
#3930    
lecluse committed
172
173


lecluse's avatar
lecluse committed
174
175
176
    /**
     * Retourne le semestre 1
     *
lecluse's avatar
#3930    
lecluse committed
177
     * @return PeriodeEntity
lecluse's avatar
lecluse committed
178
179
180
     */
    public function getSemestre1()
    {
181
        return $this->getRepo()->findOneBy(['code' => PeriodeEntity::SEMESTRE_1]);
lecluse's avatar
lecluse committed
182
183
    }

lecluse's avatar
#3930    
lecluse committed
184
185


lecluse's avatar
lecluse committed
186
187
188
    /**
     * Retourne le semestre 2
     *
lecluse's avatar
#3930    
lecluse committed
189
     * @return PeriodeEntity
lecluse's avatar
lecluse committed
190
191
192
     */
    public function getSemestre2()
    {
193
        return $this->getRepo()->findOneBy(['code' => PeriodeEntity::SEMESTRE_2]);
lecluse's avatar
lecluse committed
194
    }
lecluse's avatar
#3930    
lecluse committed
195
196
197
198
199
200
201
202
203
204
205
206



    /**
     * Retourne le paiement tardif
     *
     * @return PeriodeEntity
     */
    public function getPaiementTardif()
    {
        return $this->getRepo()->findOneBy(['code' => PeriodeEntity::PAIEMENT_TARDIF]);
    }
207
}