ServiceReferentielController.php 16.3 KB
Newer Older
1
2
3
4
<?php

namespace Application\Controller;

lecluse's avatar
#10030    
lecluse committed
5
use Application\Entity\Db\Intervenant;
6
use Application\Entity\Db\ServiceReferentiel;
7
use Application\Form\ServiceReferentiel\Traits\SaisieAwareTrait;
8
use Plafond\Processus\PlafondProcessusAwareTrait;
9
use Application\Processus\Traits\ServiceReferentielProcessusAwareTrait;
10
11
use Application\Processus\Traits\ValidationReferentielProcessusAwareTrait;
use Application\Provider\Privilege\Privileges;
12
use Application\Service\Traits\EtatVolumeHoraireServiceAwareTrait;
13
14
15
use Application\Service\Traits\LocalContextServiceAwareTrait;
use Application\Service\Traits\ServiceServiceAwareTrait;
use Application\Service\Traits\ServiceReferentielServiceAwareTrait;
16
use Application\Service\Traits\TypeVolumeHoraireServiceAwareTrait;
17
18
use Application\Service\Traits\ValidationServiceAwareTrait;
use Application\Service\Traits\VolumeHoraireReferentielServiceAwareTrait;
19
use Application\Entity\Service\Recherche;
20
use Application\Service\Traits\ContextServiceAwareTrait;
lecluse's avatar
#10030    
lecluse committed
21
use Application\Service\Traits\WorkflowServiceAwareTrait;
22
use UnicaenApp\View\Model\MessengerViewModel;
23
24
25
26
27
28

/**
 * Description of ServiceReferentielController
 *
 * @author Laurent LÉCLUSE <laurent.lecluse at unicaen.fr>
 */
29
class ServiceReferentielController extends AbstractController
30
{
31
    use ContextServiceAwareTrait;
32
33
34
    use LocalContextServiceAwareTrait;
    use ServiceServiceAwareTrait;
    use ServiceReferentielServiceAwareTrait;
35
36
    use TypeVolumeHoraireServiceAwareTrait;
    use EtatVolumeHoraireServiceAwareTrait;
37
    use VolumeHoraireReferentielServiceAwareTrait;
38
    use SaisieAwareTrait;
39
    use ValidationServiceAwareTrait;
40
    use ServiceReferentielProcessusAwareTrait;
41
    use ValidationReferentielProcessusAwareTrait;
lecluse's avatar
#10030    
lecluse committed
42
    use WorkflowServiceAwareTrait;
43
    use PlafondProcessusAwareTrait;
lecluse's avatar
lecluse committed
44
45


46
    protected function initFilters()
47
    {
48
        $this->em()->getFilters()->enable('historique')->init([
49
50
            \Application\Entity\Db\ServiceReferentiel::class,
            \Application\Entity\Db\VolumeHoraireReferentiel::class,
51
            \Application\Entity\Db\Validation::class,
52
        ]);
53
54
    }

lecluse's avatar
lecluse committed
55
56


gauthierb's avatar
Début.    
gauthierb committed
57
    public function indexAction()
58
    {
59
60
61
        $typeVolumeHoraireCode = $this->params()->fromRoute('type-volume-horaire-code', 'PREVU');
        $viewHelperParams      = $this->params()->fromPost('params', $this->params()->fromQuery('params'));
        $role                  = $this->getServiceContext()->getSelectedIdentityRole();
Laurent Lécluse's avatar
Laurent Lécluse committed
62
63
64
        /** @var Intervenant $intervenant */
        $intervenant = $this->params()->fromRoute('intervenant');
        $viewModel   = new \Laminas\View\Model\ViewModel();
65

66
        if (!$intervenant) {
lecluse's avatar
lecluse committed
67
68
69
70
            $action             = $this->getRequest()->getQuery('action', null); // ne pas afficher par défaut, sauf si demandé explicitement
            $params             = $this->getEvent()->getRouteMatch()->getParams();
            $params['action']   = 'recherche';
            $rechercheViewModel = $this->forward()->dispatch('Application\Controller\Service', $params);
71
            $viewModel->addChild($rechercheViewModel, 'recherche');
72

73
            $recherche = $this->getServiceService()->loadRecherche();
lecluse's avatar
lecluse committed
74
75
        } else {
            $this->getServiceLocalContext()->setIntervenant($intervenant); // passage au contexte pour le présaisir dans le formulaire de saisie
76
            $action    = 'afficher'; // Affichage par défaut
gauthierb's avatar
Début.    
gauthierb committed
77
            $recherche = new Recherche;
78
79
80
81
            $recherche->setTypeVolumeHoraire($this->getServiceTypeVolumehoraire()->getByCode($typeVolumeHoraireCode));
            $recherche->setEtatVolumeHoraire($this->getServiceEtatVolumeHoraire()->getSaisi());
            $this->getEvent()->setParam('typeVolumeHoraire', $recherche->getTypeVolumeHoraire());
            $this->getEvent()->setParam('etatVolumeHoraire', $recherche->getEtatVolumeHoraire());
82
83
        }

gauthierb's avatar
Début.    
gauthierb committed
84
        /* Préparation et affichage */
85
86
        if ('afficher' === $action) {
            $services = $this->getProcessusServiceReferentiel()->getServices($intervenant, $recherche);
lecluse's avatar
lecluse committed
87
        } else {
gauthierb's avatar
Début.    
gauthierb committed
88
            $services = [];
89
        }
90

91
        $renderReferentiel = $intervenant && $intervenant->getStatut()->estPermanent();
gauthierb's avatar
Début.    
gauthierb committed
92
        $typeVolumeHoraire = $recherche->getTypeVolumeHoraire();
93
        $params            = $viewHelperParams;
94

95
        $viewModel->setVariables(compact('services', 'typeVolumeHoraire', 'action', 'role', 'intervenant', 'renderReferentiel', 'params'));
96

97
98
99
        return $viewModel;
    }

lecluse's avatar
lecluse committed
100
101


102
103
104
    public function saisieAction()
    {
        $this->initFilters();
105
106
107
        $this->em()->getFilters()->enable('historique')->init([
            \Application\Entity\Db\Structure::class,
        ]);
lecluse's avatar
lecluse committed
108
109
110
        $id                = (int)$this->params()->fromRoute('id');
        $typeVolumeHoraire = $this->params()->fromQuery('type-volume-horaire', $this->params()->fromPost('type-volume-horaire'));
        if (empty($typeVolumeHoraire)) {
111
            $typeVolumeHoraire = $this->getServiceTypeVolumehoraire()->getPrevu();
lecluse's avatar
lecluse committed
112
113
        } else {
            $typeVolumeHoraire = $this->getServiceTypeVolumehoraire()->get($typeVolumeHoraire);
114
115
        }
        $service = $this->getServiceServiceReferentiel();
116
        //$role    = $this->getServiceContext()->getSelectedIdentityRole();
117
        $form = $this->getFormServiceReferentielSaisie();
gauthierb's avatar
gauthierb committed
118
        $form->get('type-volume-horaire')->setValue($typeVolumeHoraire->getId());
lecluse's avatar
lecluse committed
119
120

        $intervenant = $this->getServiceLocalContext()->getIntervenant();
121
122
123
124
125

        if ($id) {
            $entity = $service->get($id);
            $entity->setTypeVolumeHoraire($typeVolumeHoraire);
            $form->bind($entity);
lecluse's avatar
lecluse committed
126
            $title = "Modification de référentiel";
127
128
129
        } else {
            $entity = $service->newEntity();
            $entity->setTypeVolumeHoraire($typeVolumeHoraire);
130
            $entity->setIntervenant($intervenant);
131
132
            $form->bind($entity);
            $form->initFromContext();
lecluse's avatar
lecluse committed
133
            $title = "Ajout de référentiel";
134
        }
lecluse's avatar
lecluse committed
135

136
        $assertionEntity = $service->newEntity();
137
        $assertionEntity
lecluse's avatar
lecluse committed
138
139
            ->setTypeVolumeHoraire($typeVolumeHoraire)
            ->setIntervenant($intervenant);
140
        if (!$this->isAllowed($assertionEntity, Privileges::REFERENTIEL_EDITION)) {
141
            throw new \LogicException("Cette opération n'est pas autorisée.");
142
        }
143
        $hDeb    = $entity->getVolumeHoraireReferentielListe()->getHeures();
144
145
146
147
148
        $request = $this->getRequest();
        if ($request->isPost()) {
            $form->setData($request->getPost());
            $form->saveToContext();
            if ($form->isValid()) {
149
                $this->getProcessusPlafond()->beginTransaction();
150
                try {
151
                    $entity->setIntervenant($intervenant); // car après $form->isValid(), $entity->getIntervenant() === null
152
153
                    $entity = $service->save($entity);
                    $form->get('service')->get('id')->setValue($entity->getId()); // transmet le nouvel ID
lecluse's avatar
lecluse committed
154
                } catch (\Exception $e) {
155
                    $this->flashMessenger()->addErrorMessage($this->translate($e));
156
                }
157
                $hFin = $entity->getVolumeHoraireReferentielListe()->getHeures();
158
                $this->updateTableauxBord($intervenant);
159
160
161
                if (!$this->getProcessusPlafond()->endTransaction($intervenant, $typeVolumeHoraire, $hFin < $hDeb)) {
                    $this->updateTableauxBord($intervenant);
                }
lecluse's avatar
lecluse committed
162
            } else {
163
                $this->flashMessenger()->addErrorMessage('La validation du formulaire a échoué. L\'enregistrement des données n\'a donc pas été fait.');
164
165
            }
        }
166

167
        return compact('form', 'title');
168
    }
169

lecluse's avatar
lecluse committed
170
171


172
173
174
175
    public function rafraichirLigneAction()
    {
        $this->initFilters();

lecluse's avatar
lecluse committed
176
177
178
        $params      = $this->params()->fromPost('params', $this->params()->fromQuery('params'));
        $details     = 1 == (int)$this->params()->fromQuery('details', (int)$this->params()->fromPost('details', 0));
        $onlyContent = 1 == (int)$this->params()->fromQuery('only-content', 0);
179
        $service     = $this->getEvent()->getParam('serviceReferentiel');
lecluse's avatar
lecluse committed
180
181
182
183
184
185
        /* @var $service ServiceReferentiel */

        if (isset($params['type-volume-horaire'])) {
            $typeVolumeHoraire = $this->getServiceTypeVolumeHoraire()->get((int)$params['type-volume-horaire']);
            $service->setTypeVolumeHoraire($typeVolumeHoraire);
        }
186

187
188
189
        return compact('service', 'params', 'details', 'onlyContent');
    }

lecluse's avatar
lecluse committed
190
191


lecluse's avatar
#3357    
lecluse committed
192
193
194
    public function initialisationAction()
    {
        $intervenant = $this->getEvent()->getParam('intervenant');
195
        $this->getProcessusPlafond()->beginTransaction();
lecluse's avatar
#3357    
lecluse committed
196
        $this->getServiceServiceReferentiel()->setPrevusFromPrevus($intervenant);
197
        $this->getProcessusPlafond()->endTransaction($intervenant, $this->getServiceTypeVolumeHoraire()->getPrevu());
lecluse's avatar
#10030    
lecluse committed
198
        $this->updateTableauxBord($intervenant);
lecluse's avatar
#3357    
lecluse committed
199
        $errors = [];
lecluse's avatar
lecluse committed
200

lecluse's avatar
#3357    
lecluse committed
201
202
203
        return compact('errors');
    }

lecluse's avatar
lecluse committed
204
205


gauthierb's avatar
gauthierb committed
206
207
    public function constatationAction()
    {
208
        $this->initFilters();
gauthierb's avatar
gauthierb committed
209
        $services = $this->params()->fromQuery('services');
lecluse's avatar
lecluse committed
210
        if ($services) {
211
212
            $typeVolumeHoraire = $this->getServiceTypeVolumeHoraire()->getRealise();

lecluse's avatar
lecluse committed
213
214
215
            $services = explode(',', $services);
            foreach ($services as $sid) {
                $service = $this->getServiceServiceReferentiel()->get($sid);
216
                $service->setTypeVolumeHoraire($typeVolumeHoraire);
217
                if ($this->isAllowed($service, Privileges::REFERENTIEL_EDITION)) {
218
                    $this->getProcessusPlafond()->beginTransaction();
lecluse's avatar
lecluse committed
219
                    $this->getServiceServiceReferentiel()->setRealisesFromPrevus($service);
lecluse's avatar
#10030    
lecluse committed
220
                    $this->updateTableauxBord($service->getIntervenant());
221
                    $this->getProcessusPlafond()->endTransaction($service->getIntervenant(), $typeVolumeHoraire);
222
                }
gauthierb's avatar
gauthierb committed
223
224
            }
        }
225
226

        return new MessengerViewModel;
gauthierb's avatar
gauthierb committed
227
228
    }

lecluse's avatar
lecluse committed
229
230


231
232
    public function suppressionAction()
    {
lecluse's avatar
lecluse committed
233
234
        $typeVolumeHoraire = $this->params()->fromQuery('type-volume-horaire', $this->params()->fromPost('type-volume-horaire'));
        if (empty($typeVolumeHoraire)) {
gauthierb's avatar
gauthierb committed
235
            $typeVolumeHoraire = $this->getServiceTypeVolumehoraire()->getPrevu();
lecluse's avatar
lecluse committed
236
237
        } else {
            $typeVolumeHoraire = $this->getServiceTypeVolumehoraire()->get($typeVolumeHoraire);
gauthierb's avatar
gauthierb committed
238
        }
239
        $id      = (int)$this->params()->fromRoute('id', null);
lecluse's avatar
lecluse committed
240
        $service = $this->getServiceServiceReferentiel()->get($id);
241
242
243
244
245
246
247
        /* @var $service ServiceReferentiel */

        if (!$service) {
            throw new \LogicException('Le service référentiel n\'existe pas');
        }
        $service->setTypeVolumeHoraire($typeVolumeHoraire);
        if (!$this->isAllowed($service, Privileges::REFERENTIEL_EDITION)) {
248
            throw new \LogicException("Cette opération n'est pas autorisée.");
249
250
        }
        if ($this->getRequest()->isPost()) {
251
            $this->getProcessusPlafond()->beginTransaction();
252
            try {
253
                $this->getServiceServiceReferentiel()->delete($service);
lecluse's avatar
#10030    
lecluse committed
254
                $this->updateTableauxBord($service->getIntervenant());
255
                $this->flashMessenger()->addSuccessMessage('Suppression effectuée');
lecluse's avatar
lecluse committed
256
            } catch (\Exception $e) {
257
                $this->flashMessenger()->addErrorMessage($this->translate($e));
258
            }
259
            $this->getProcessusPlafond()->endTransaction($service->getIntervenant(), $typeVolumeHoraire, true);
260
261
        }

262
        return new MessengerViewModel;
263
    }
lecluse's avatar
lecluse committed
264
265
266



267
268
269
270
271
272
    public function validationAction()
    {
        $this->initFilters();

        $role = $this->getServiceContext()->getSelectedIdentityRole();

lecluse's avatar
v4.0.8    
lecluse committed
273
        $filterStructure = null;//$role->getStructure(); // pour filtrer les affichages à la structure concernée uniquement
274
275
276

        $intervenant = $this->getEvent()->getParam('intervenant');
        /* @var $intervenant Intervenant */
277
        if (!$intervenant) {
278
279
            throw new \LogicException('Intervenant non précisé ou inexistant');
        }
280
281
282
283
284
285
286
287
288
289
290

        $typeVolumeHoraire = $this->getServiceTypeVolumeHoraire()->getByCode($this->params()->fromRoute('type-volume-horaire-code', 'PREVU'));

        $title = "Validation du référentiel";

        if ($typeVolumeHoraire->isPrevu()) {
            $title .= " prévisionnel";
        } elseif ($typeVolumeHoraire->isRealise()) {
            $title .= " réalisé";
        }

291
        $services = [
292
293
294
295
296
            'valides'     => [],
            'non-valides' => [],
        ];

        $validations = $this->getProcessusValidationReferentiel()->lister($typeVolumeHoraire, $intervenant, $filterStructure);
297
298
299
300
        foreach ($validations as $validation) {
            $key                  = $validation->getId() ? 'valides' : 'non-valides';
            $vid                  = $this->getProcessusValidationReferentiel()->getValidationId($validation);
            $sList                = $this->getProcessusValidationReferentiel()->getServices($typeVolumeHoraire, $validation);
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
            $services[$key][$vid] = $sList;
        }


        /* Messages */
        if (empty($services['non-valides'])) {
            if ($role->getIntervenant()) {
                $message = sprintf(
                    "Tous votre référentiel %s a été validé.",
                    $typeVolumeHoraire->isPrevu() ? "prévisionnel" : "réalisé"
                );
            } else {
                $message = sprintf(
                    "Aucun référentiel %s n'est en attente de validation.",
                    $typeVolumeHoraire->isPrevu() ? "prévisionnel" : "réalisé"
                );
            }
            $this->flashMessenger()->addSuccessMessage($message);
        }

        return compact('title', 'typeVolumeHoraire', 'intervenant', 'validations', 'services');
    }



    public function validerAction()
    {
        $this->initFilters();

        $typeVolumeHoraire = $this->getEvent()->getParam('typeVolumeHoraire');
        /* @var $typeVolumeHoraire TypeVolumeHoraire */

        $intervenant = $this->getEvent()->getParam('intervenant');
        /* @var $intervenant Intervenant */

        $structure = $this->getEvent()->getParam('structure');
        /* @var $structure Structure */


        $validation = $this->getProcessusValidationReferentiel()->creer($intervenant, $structure);

        if ($this->isAllowed($validation, Privileges::REFERENTIEL_VALIDATION)) {
            if ($this->getRequest()->isPost()) {
                try {
                    $this->getProcessusValidationReferentiel()->enregistrer($typeVolumeHoraire, $validation);
346
                    $this->updateTableauxBord($intervenant, true);
347
348
349
350
                    $this->flashMessenger()->addSuccessMessage(
                        "Validation effectuée avec succès."
                    );
                } catch (\Exception $e) {
351
                    $this->flashMessenger()->addErrorMessage($this->translate($e));
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
                }
            }
        } else {
            $this->flashMessenger()->addErrorMessage('Vous n\'avez pas le droit de valider ce référentiel.');
        }

        return new MessengerViewModel();
    }



    public function devaliderAction()
    {
        $this->initFilters();

        $validation = $this->getEvent()->getParam('validation');
        /* @var $structure Structure */

        if ($this->isAllowed($validation, Privileges::REFERENTIEL_DEVALIDATION)) {
            if ($this->getRequest()->isPost()) {
                try {
                    $this->getProcessusValidationReferentiel()->supprimer($validation);
374
                    $this->updateTableauxBord($validation->getIntervenant(), true);
375
376
377
378
                    $this->flashMessenger()->addSuccessMessage(
                        "Dévalidation effectuée avec succès."
                    );
                } catch (\Exception $e) {
379
                    $this->flashMessenger()->addErrorMessage($this->translate($e));
380
381
382
383
384
385
386
387
388
                }
            }
        } else {
            $this->flashMessenger()->addErrorMessage('Vous n\'avez pas le droit de dévalider ce référentiel.');
        }

        return new MessengerViewModel();
    }

lecluse's avatar
#10030    
lecluse committed
389
390


391
    private function updateTableauxBord(Intervenant $intervenant, $validation = false)
lecluse's avatar
#10030    
lecluse committed
392
    {
393
394
395
        $this->getServiceWorkflow()->calculerTableauxBord(['formule', 'validation_referentiel', 'service_referentiel'], $intervenant);
        if (!$validation) {
            $this->getServiceWorkflow()->calculerTableauxBord(['service_saisie', 'piece_jointe_fournie'], $intervenant);
lecluse's avatar
#10030    
lecluse committed
396
397
398
        }
    }

399
}