PrestatairesController.php 6.97 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
        $quiz = $prestataire->getSelfEvalPrestaQuiz();
        $formClass = Prestataire::DISTRIBUTOR === $prestataire->getMarketChannelFunction() ?
            DistributorSelfEvalPrestaQuizType::class : ProducerSelfEvalPrestaQuizType::class;
49
        $form = $this->createForm($formClass, $quiz, ["mode" => SelfEvalPrestaQuizType::READONLY]);
50

Julien Jorry committed
51 52
        return $this->render('@kohinos/presta/show.html.twig', [
            'presta' => $prestataire,
53
            'form' => $form->createView()
Julien Jorry committed
54 55 56 57
        ]);
    }

    /**
58
     * @Route("/prestataires/liste/{order}", name="liste_prestataire", defaults={"order": "raison"})
Julien Jorry committed
59
     */
60
    public function listePrestaAction($order = 'raison')
Julien Jorry committed
61 62 63 64
    {
        if (!$this->isFrontActivated()) {
            return $this->redirectToRoute('index');
        }
Julien Jorry committed
65 66

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

69 70 71 72 73
            return $this->render('@kohinos/presta/liste_prestataires_bygroupelocal.html.twig', [
                'prestas' => $prestas,
                'type' => 'Prestataires',
            ]);
        }
74
        $prestas = $this->em->getRepository(Prestataire::class)->findDefault('prestataire');
Julien Jorry committed
75 76 77 78 79 80 81

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

82 83 84 85 86 87 88 89
    /**
     * @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
90

91 92 93 94
        $prestas = $this->em->getRepository(Prestataire::class)->findByGroupe($groupe);

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

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

        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
123
        $prestas = $this->em->getRepository(Prestataire::class)->findDefault();
Julien Jorry committed
124 125 126 127 128 129 130 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

        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),
        ]);
    }
159 160

    /**
161 162
     * Get the total transactions amount towards the Prestataire since the last time it was fetched.
     * Exclude Reconversions from calculation.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
     * 
     * @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) {
191 192 193 194
            // Exclude reconversions from calculation
            if ($flux_item->getType() != "reconversion_prestataire") {
                $total_amount += $flux_item->getMontant();
            }
195 196 197 198 199 200 201 202 203 204 205 206 207 208
        }

        // 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
209
}