PrestataireAdminController.php 6.28 KB
Newer Older
1 2 3 4
<?php

namespace App\Controller;

5
use App\Entity\CotisationAdherent;
6 7 8
use App\Entity\CotisationPrestataire;
use App\Entity\Prestataire;
use App\Enum\MoyenEnum;
9 10
use App\Form\Type\DistributorSelfEvalPrestaQuizType;
use App\Form\Type\ProducerSelfEvalPrestaQuizType;
11
use App\Form\Type\SelfEvalPrestaQuizType;
12 13 14 15
use App\Utils\CustomEntityManager;
use DateTime;
use Sonata\AdminBundle\Controller\CRUDController;
use Symfony\Component\HttpFoundation\RedirectResponse;
16
use Symfony\Component\HttpFoundation\Request;
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Security;

class PrestataireAdminController extends CRUDController
{
    protected $em;
    protected $security;

    public function __construct(CustomEntityManager $em, Security $security)
    {
        $this->em = $em;
        $this->security = $security;
    }

    /**
33 34 35 36
     * Ajouter une cotisation gratuite à l'adhérent.
     *
     * @param Uuid $id Id du prestataire
     *
37 38 39 40 41 42 43 44 45 46 47
     * @return Response
     */
    public function addfreecotisationadhAction($id): Response
    {
        $prestataire = $this->admin->getSubject();

        if (!$prestataire) {
            throw new NotFoundHttpException(sprintf('Impossible de trouver le prestataire avec l\'id: %s', $id));
        }

        $managers = $prestataire->getUsers();
48
        if (1 == count($managers)) {
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
            $manager = $managers[0];
            if (null != $manager->getAdherent()) {
                $cotisation = new CotisationAdherent();
                $cotisation->setExpediteur($manager->getAdherent());
                $now = new DateTime();
                $cotisation->setRecu(true);
                $cotisation->setReference('Cotisation gratuite');
                // $cotisation->setOperateur(null);
                $cotisation->setRole($this->getUser()->getGroups()[0]->__toString());
                $mlcPrestataire = $this->em->getRepository(Prestataire::class)->findOneBy(['mlc' => true]);
                $cotisation->setDestinataire($mlcPrestataire);
                $cotisation->setMoyen(MoyenEnum::MOYEN_AUTRE);

                $cotisation->setMontant(0);
                $cotisation->getCotisationInfos()->setAnnee(date('Y'));
                $cotisation->getCotisationInfos()->setDebut($now);
                $cotisation->getCotisationInfos()->setFin(new DateTime('+ 1 year'));

                $this->em->persist($cotisation);
                $this->em->flush();
                $this->addFlash('sonata_flash_success', sprintf('Cotisation gratuite ajouté au gestionnaire %s du prestataire %s', $manager->__toString(), $prestataire->__toString()));
            } else {
                $this->addFlash('sonata_flash_error', sprintf('Impossible d\ajouter une cotisation gratuite au gestionnaire %s du prestataire %s car il n\'a pas de compte adhérent !', $manager->getEmail(), $prestataire->__toString()));
            }
        } else {
            $this->addFlash('sonata_flash_error', sprintf('Impossible d\ajouter une cotisation gratuite à tous les gestionnaires du prestataire %s !', $prestataire->__toString()));
        }

        return new RedirectResponse(
            $this->admin->generateUrl('list', ['filter' => $this->admin->getFilterParameters()])
        );
    }

    /**
83 84 85 86
     * Ajouter une cotisation gratuite au prestataire.
     *
     * @param Uuid $id Id du prestataire
     *
87
     * @return Response
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
     */
    public function addfreecotisationprestaAction($id): Response
    {
        $prestataire = $this->admin->getSubject();

        if (!$prestataire) {
            throw new NotFoundHttpException(sprintf('Impossible de trouver le prestataire avec l\'id: %s', $id));
        }

        $cotisation = new CotisationPrestataire();
        $cotisation->setExpediteur($prestataire);
        $now = new DateTime();
        $cotisation->setRecu(true);
        $cotisation->setReference('Cotisation gratuite');
        // $cotisation->setOperateur(null);
        $cotisation->setRole($this->getUser()->getGroups()[0]->__toString());
        $mlcPrestataire = $this->em->getRepository(Prestataire::class)->findOneBy(['mlc' => true]);
        $cotisation->setDestinataire($mlcPrestataire);
        $cotisation->setMoyen(MoyenEnum::MOYEN_AUTRE);

        $cotisation->setMontant(0);
        $cotisation->getCotisationInfos()->setAnnee(date('Y'));
        $cotisation->getCotisationInfos()->setDebut($now);
        $cotisation->getCotisationInfos()->setFin(new DateTime('+ 1 year'));

        $this->em->persist($cotisation);
        $this->em->flush();

        $this->addFlash('sonata_flash_success', sprintf('Cotisation gratuite ajouté au prestataire %s', $prestataire->__toString()));

        return new RedirectResponse(
            $this->admin->generateUrl('list', ['filter' => $this->admin->getFilterParameters()])
        );
    }
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

    /**
     * Visualisation et complétion de la partie réservée à l'admin sur un questionnaire prestataire
     *
     * @param Request $request
     * @param Uuid $id Id du prestataire
     *
     * @return Response
     */
    public function reviewprestaquizAction(Request $request, $id): Response
    {
        $prestataire = $this->admin->getSubject();

        if (!$prestataire) {
            throw new NotFoundHttpException(sprintf('Impossible de trouver le prestataire avec l\'id: %s', $id));
        }

        $quiz = $prestataire->getSelfEvalPrestaQuiz();
        $formClass = Prestataire::DISTRIBUTOR === $prestataire->getMarketChannelFunction() ?
            DistributorSelfEvalPrestaQuizType::class : ProducerSelfEvalPrestaQuizType::class;
142
        $form = $this->createForm($formClass, $quiz, ["mode" => SelfEvalPrestaQuizType::ADMIN_EDIT]);
143 144 145 146 147 148 149 150 151 152 153

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->em->flush();
            return new RedirectResponse(
                $this->admin->generateUrl('list', ['filter' => $this->admin->getFilterParameters()])
            );
        }

        $tmpl = Prestataire::DISTRIBUTOR === $prestataire->getMarketChannelFunction() ?
154 155
            '@kohinos/tav/prestaquiz/distributor.html.twig'
            : '@kohinos/tav/prestaquiz/producer.html.twig';
156 157 158

        return $this->render($tmpl, [
            'form' => $form->createView(),
Yvon committed
159
            'prestataire' => $prestataire
160 161
        ]);
    }
162
}