PrestatairesController.php 7.23 KB
Newer Older
Julien Jorry committed
1 2 3 4
<?php

namespace App\Controller;

5
use App\Entity\Groupe;
Julien Jorry committed
6 7
use App\Entity\Prestataire;
use App\Entity\Rubrique;
8
use App\Entity\Flux;
9 10 11
use App\Entity\SelfEvalPrestaQuiz;
use App\Form\Type\DistributorSelfEvalPrestaQuizType;
use App\Form\Type\ProducerSelfEvalPrestaQuizType;
12
use App\Form\Type\SelfEvalPrestaQuizType;
Julien Jorry committed
13 14 15
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Annotation\Route;
16 17
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
Julien Jorry committed
18 19 20 21 22 23
use Symfony\Component\Routing\RouterInterface;

class PrestatairesController extends FrontController
{
    protected $em;
    private $router;
24
    private $session;
Julien Jorry committed
25

26
    public function __construct(EntityManagerInterface $em, RouterInterface $router, SessionInterface $session)
Julien Jorry committed
27 28 29
    {
        $this->em = $em;
        $this->router = $router;
30
        $this->session = $session;
Julien Jorry committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    }

    /**
     * @Route("/prestataire/{slug}", name="show_prestataire")
     */
    public function showPrestaAction(Prestataire $prestataire)
    {
        if (!$this->isFrontActivated()) {
            return $this->redirectToRoute('index');
        }
        if ($prestataire->isMlc()) {
            // Ne pas montrer la page du prestataire recevant les cotisations !
            return new RedirectResponse($this->router->generate('index'));
        }

46 47 48 49 50 51
        $templateData = ['presta' => $prestataire];

        // If feature activated, display self evaluation form 
        if ($this->getParameter('presta_self_init_and_eval') == true) {
            $quiz = $prestataire->getSelfEvalPrestaQuiz();

52 53 54 55 56 57 58
            if ($quiz->isSubmitted) {
                $formClass = Prestataire::DISTRIBUTOR === $prestataire->getMarketChannelFunction() ?
                    DistributorSelfEvalPrestaQuizType::class : ProducerSelfEvalPrestaQuizType::class;
                $form = $this->createForm($formClass, $quiz, ["mode" => SelfEvalPrestaQuizType::READONLY]);
    
                $templateData['form'] = $form->createView();
            }
59 60 61
        }

        return $this->render('@kohinos/presta/show.html.twig', $templateData);
Julien Jorry committed
62 63 64
    }

    /**
65
     * @Route("/prestataires/liste/{order}", name="liste_prestataire", defaults={"order": "raison"})
Julien Jorry committed
66
     */
67
    public function listePrestaAction($order = 'raison')
Julien Jorry committed
68 69 70 71
    {
        if (!$this->isFrontActivated()) {
            return $this->redirectToRoute('index');
        }
Julien Jorry committed
72 73

        if ('groupelocal' == $order) {
74
            $prestas = $this->em->getRepository(Prestataire::class)->findDefault('prestataire', 'groupelocal');
Julien Jorry committed
75

76 77 78 79 80
            return $this->render('@kohinos/presta/liste_prestataires_bygroupelocal.html.twig', [
                'prestas' => $prestas,
                'type' => 'Prestataires',
            ]);
        }
81
        $prestas = $this->em->getRepository(Prestataire::class)->findDefault('prestataire');
Julien Jorry committed
82 83 84 85 86 87 88

        return $this->render('@kohinos/presta/liste_prestataires.html.twig', [
            'prestas' => $prestas,
            'type' => 'Prestataires',
        ]);
    }

89 90 91 92 93 94 95 96
    /**
     * @Route("/prestataires/groupe/{slug}/liste", name="liste_presta_by_groupe")
     */
    public function listePrestaByGroupeAction(Groupe $groupe)
    {
        if (!$this->isFrontActivated()) {
            return $this->redirectToRoute('index');
        }
Julien Jorry committed
97

98 99 100 101
        $prestas = $this->em->getRepository(Prestataire::class)->findByGroupe($groupe);

        return $this->render('@kohinos/presta/liste_prestataires.html.twig', [
            'prestas' => $prestas,
Julien Jorry committed
102
            'type' => 'Prestataires du groupe local ' . $groupe->getName(),
103 104 105
        ]);
    }

Julien Jorry committed
106 107 108 109 110 111 112 113
    /**
     * @Route("/partenaires/liste", name="liste_partenaire")
     */
    public function listePartnerAction()
    {
        if (!$this->isFrontActivated()) {
            return $this->redirectToRoute('index');
        }
114
        $partners = $this->em->getRepository(Prestataire::class)->findDefault('partenaire');
Julien Jorry committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

        return $this->render('@kohinos/presta/liste_prestataires.html.twig', [
            'prestas' => $partners,
            'type' => 'Partenaires',
        ]);
    }

    /**
     * @Route("/prestataires/carte", name="carte_prestataire")
     */
    public function cartePrestaAction()
    {
        if (!$this->isFrontActivated()) {
            return $this->redirectToRoute('index');
        }
Julien Jorry committed
130
        $prestas = $this->em->getRepository(Prestataire::class)->findDefault();
Julien Jorry committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

        return $this->render('@kohinos/presta/carte.html.twig', [
            'prestas' => $prestas,
        ]);
    }

    /**
     * @Route("/prestataires/rubriques", name="rubriques_prestataire")
     */
    public function rubriquesAction()
    {
        if (!$this->isFrontActivated()) {
            return $this->redirectToRoute('index');
        }
        $rubriques = $this->em->getRepository(Rubrique::class)->findBy(['enabled' => true], ['name' => 'ASC']);

        return $this->render('@kohinos/presta/rubriques.html.twig', [
            'rubriques' => $rubriques,
        ]);
    }

    /**
     * @Route("/prestataires/rubrique/{slug}", name="show_rubrique")
     */
    public function showRubriqueAction(Rubrique $rubrique)
    {
        if (!$this->isFrontActivated()) {
            return $this->redirectToRoute('index');
        }

        return $this->render('@kohinos/presta/show_rubrique.html.twig', [
            'rubrique' => $rubrique,
            'prestataires' => $this->em->getRepository(Prestataire::class)->findByRubrique($rubrique),
        ]);
    }
166 167

    /**
168 169
     * Get the total transactions amount towards the Prestataire since the last time it was fetched.
     * Exclude Reconversions from calculation.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
     * 
     * @Route("/prestataires/get_last_transactions", name="get_presta_last_transactions")
     * @IsGranted({"ROLE_CAISSIER", "ROLE_PRESTATAIRE"})
     */
    public function getLastTransactionsAmount()
    {
        if (!$this->session->has('_prestagere')) {
            return null;
        }

        // Get last export datetime (presta creation if first export)
        $presta = $this->em->getRepository(Prestataire::class)->findOneById($this->session->get('_prestagere')->getId());

        $datetime_last_export = $presta->getLastTransactionsExportDatetime();
        if (null == $datetime_last_export) {
            $datetime_last_export = $presta->getCreatedAt();
        }

        // Get total amount
        $flux = $this->em->getRepository(Flux::class)->getQueryByPrestataire(
            $this->session->get('_prestagere'),
            null,
            null,
            $datetime_last_export->format(("Y-m-d H:i:s"))
        )->getResult();

        $total_amount = 0;
        foreach ($flux as $flux_item) {
198 199 200 201
            // Exclude reconversions from calculation
            if ($flux_item->getType() != "reconversion_prestataire") {
                $total_amount += $flux_item->getMontant();
            }
202 203 204 205 206 207 208 209 210 211 212 213 214 215
        }

        // Set now as this presta last export date
        $presta->setLastTransactionsExportDatetime(new \Datetime('now'));
        $this->em->persist($presta);
        $this->em->flush();

        $str_datetime = $datetime_last_export->format('d/m/Y H\hi');

        return $this->render('@kohinos/tav/last_transactions_amount.html.twig', [
            'amount' => $total_amount,
            'datetime_last_export' => $str_datetime
        ]);
    }
Julien Jorry committed
216
}