UserPrestataireController.php 7.09 KB
Newer Older
1 2 3 4
<?php

namespace App\Controller;

5
use App\Entity\Flux;
6 7 8
use App\Entity\Prestataire;
use App\Entity\Rubrique;
use App\Entity\TransactionPrestataireAdherent;
Julien Jorry committed
9
use App\Entity\TransfertPrestataireSiege;
10
use App\Entity\TypePrestataire;
Julien Jorry committed
11
use App\Form\Type\GroupePrestataireInscriptionFormType;
12 13
use App\Form\Type\PrestataireInfosFormType;
use App\Form\Type\TransactionPrestataireAdherentFormType;
Julien Jorry committed
14
use App\Form\Type\TransfertPrestataireSiegeFormType;
15 16
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
17 18 19
use Sonata\Exporter\Handler;
use Sonata\Exporter\Source\DoctrineORMQuerySourceIterator;
use Sonata\Exporter\Writer\CsvWriter;
20 21 22
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
23
use Symfony\Component\Translation\TranslatorInterface;
24

25
class UserPrestataireController extends FluxController
26 27 28 29 30 31 32
{
    /**
     * @Route("/prestataireinfos", name="prestataire_infos")
     * @IsGranted("ROLE_PRESTATAIRE")
     */
    public function prestataireInfosAction(Request $request)
    {
Julien Jorry committed
33
        $form = $this->createForm(PrestataireInfosFormType::class, $this->session->get('_prestagere'));
34 35 36 37 38
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->em->persist($form->getData());
            $this->em->flush();
Julien Jorry committed
39 40
            $this->addFlash(
                'success',
41
                $this->translator->trans('Infos du prestataire modifiée !')
Julien Jorry committed
42 43 44 45 46 47 48
            );
            $referer = $request->headers->get('referer');
            if ($referer && !$request->isXmlHttpRequest()) {
                return $this->redirect($referer);
            } elseif (!$request->isXmlHttpRequest()) {
                return new Response('', Response::HTTP_BAD_REQUEST);
            }
49 50
        }

Julien Jorry committed
51 52 53 54 55 56 57 58 59
        return $this->redirectToRoute('index');
    }

    /**
     * @Route("/prestatairegroupes/inscription", name="groupeprestataire_inscription")
     * @IsGranted("ROLE_PRESTATAIRE")
     */
    public function groupePrestataireInscriptionAction(Request $request)
    {
Julien Jorry committed
60
        $form = $this->createForm(GroupePrestataireInscriptionFormType::class, $this->session->get('_prestagere'));
Julien Jorry committed
61 62 63 64 65 66 67 68
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $prestataire = $form->getData();
            $this->em->persist($prestataire);
            $this->em->flush();
            $this->addFlash(
                'success',
69
                $this->translator->trans('Information bien prise en compte !')
Julien Jorry committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83
            );
            if ($request->isXmlHttpRequest()) {
                return new JsonResponse(array('status' => 'success'));
            } else {
                $referer = $request->headers->get('referer');
                if ($referer && !$request->isXmlHttpRequest()) {
                    return $this->redirect($referer);
                } elseif (!$request->isXmlHttpRequest()) {
                    return new Response('', Response::HTTP_BAD_REQUEST);
                }
            }
        }

        return $this->redirectToRoute('index');
84 85 86 87 88 89 90
    }

    /**
     * @Route("/prestataire/transaction/{type}/", name="transactionPrestataireAdherent", defaults={"type": "adherent"})
     * @Route("/prestataire/transaction/{type}/", name="transactionPrestatairePrestataire", defaults={"type": "prestataire"})
     * @IsGranted("ROLE_PRESTATAIRE")
     */
91
    public function transactionPrestataireAction($type, Request $request)
92 93 94 95
    {
        $entityName = 'App\Entity\TransactionPrestataire'.ucwords($type);
        $entity = new $entityName();
        $entity->setOperateur($this->getUser());
Julien Jorry committed
96
        $entity->setExpediteur($this->session->get('_prestagere'));
97
        $form = $this->createForm('App\Form\Type\TransactionPrestataire'.ucwords($type).'FormType', $entity);
98 99 100
        return $this->manageFluxForm(
            $request,
            $form,
Julien Jorry committed
101
            $this->session->get('_prestagere')->getCompte(),
102 103 104
            $this->translator->trans('Transaction bien effectuée !'),
            $this->translator->trans('Transaction à un ').$type
        );
105 106
    }

Julien Jorry committed
107 108 109 110 111 112 113 114 115

    /**
     * @Route("/prestataire/transfert/siege/", name="transactionPrestataireSiege")
     * @IsGranted("ROLE_PRESTATAIRE")
     */
    public function transfertPrestataireSiegeAction(Request $request)
    {
        $entity = new TransfertPrestataireSiege();
        $entity->setOperateur($this->getUser());
Julien Jorry committed
116
        $entity->setExpediteur($this->session->get('_prestagere'));
Julien Jorry committed
117
        $form = $this->createForm(TransfertPrestataireSiegeFormType::class, $entity);
118 119 120
        return $this->manageFluxForm(
            $request,
            $form,
Julien Jorry committed
121
            $this->session->get('_prestagere')->getCompte(),
122 123 124
            $this->translator->trans('Reconversion envoyée, elle sera validée lorsque le virement sera effectué !'),
            $this->translator->trans('Reconversion de monnaie au siège')
        );
Julien Jorry committed
125
    }
126 127 128 129 130 131 132 133 134 135 136 137
    
    /**
     * Export all transferts / transactions for prestataire
     *
     * @param  Request $request Request
     * @param  String  $format  Format of export ('json', 'xml', 'csv', 'xls')
     * @Route("/prestataire/export/{format}/", name="exportPrestaFlux", defaults={"format": "csv"})
     * @IsGranted("ROLE_PRESTATAIRE")
     */
    public function exportFluxAction(Request $request, $format = 'csv')
    {
        // Prepare the data source
Julien Jorry committed
138
        $query = $this->em->getRepository(Flux::class)->getQueryByPrestataire($this->session->get('_prestagere'));
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 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 191
        $fields = ['expediteur', 'destinataire', 'type', 'parenttype', 'montant', 'moyen', 'operateur'];
        $source = new DoctrineORMQuerySourceIterator($query, $fields);

        $filename = sprintf(
            'export_flux_%s.%s',
            date('Y_m_d_H_i_s', strtotime('now')),
            $format
        );

        return $this->getResponse(
            $format,
            $filename,
            $source
        );
    }

    private function getResponse($format, $filename, SourceIteratorInterface $source)
    {
        switch ($format) {
            case 'xls':
                $writer = new XlsWriter('php://output');
                $contentType = 'application/vnd.ms-excel';

                break;
            case 'xml':
                $writer = new XmlWriter('php://output');
                $contentType = 'text/xml';

                break;
            case 'json':
                $writer = new JsonWriter('php://output');
                $contentType = 'application/json';

                break;
            case 'csv':
                $writer = new CsvWriter('php://output', ',', '"', '\\', true, true);
                $contentType = 'text/csv';

                break;
            default:
                throw new \RuntimeException('Invalid format');
        }

        $callback = static function () use ($source, $writer) {
            $handler = Handler::create($source, $writer);
            $handler->export();
        };

        return new StreamedResponse($callback, 200, [
            'Content-Type' => $contentType,
            'Content-Disposition' => sprintf('attachment; filename="%s"', $filename),
        ]);
    }
192
}