FluxController.php 3.29 KB
Newer Older
1 2 3 4
<?php

namespace App\Controller;

5 6
use App\Events\MLCEvents;
use App\Events\FluxEvent;
7
use Doctrine\ORM\EntityManagerInterface;
8
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
10 11
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Form;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
Julien Jorry committed
14
use Symfony\Component\HttpFoundation\Session\SessionInterface;
15
use Symfony\Component\Routing\Annotation\Route;
16
use Symfony\Component\Security\Core\Security;
17
use Symfony\Component\Translation\TranslatorInterface;
18 19 20 21 22 23

/**
 *
 * Types de transfert : (Les transferts dans la structure sont les flux de billets détenus par les opérateurs.)
 *
 *  - SIEGE             =>     GROUPES LOCAUX           (Transfert du siège au groupe)
24
 *  - GROUPE            =>     SIEGE                    (Transfert du groupe au siège)
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 *  - GROUPES LOCAUX    =>     COMPTOIRS                (Transfert du groupe au comptoir)
 *  - COMPTOIRS         =>     GROUPES LOCAUX           (Transfert du comptoir au groupe)
 *  - COMPTOIRS         =>     ADHERENTS                (Diffusion de monnaie papier auprès des adhérents)
 *  - COMPTOIRS         =>     PRESTATAIRES             (Diffusion de monnaie papier auprès des prestataires)
 *  - PRESTATAIRES      =>     COMPTOIRS                (Reconversion)
 *
 * Types de transaction :
 *
 *   - PRESTATAIRES     =>    ADHERENTS         (Virement vers un adherent)
 *   - PRESTATAIRES     =>    PRESTATAIRES      (Virement entre prestataires)
 *   - ADHERENTS        =>    PRESTATAIRES      (Paiement numérique)
 *
 */
class FluxController extends AbstractController
{
40
    protected $em;
41
    protected $translator;
42
    protected $eventDispatcher;
Julien Jorry committed
43
    protected $session;
44

45
    public function __construct(Security $security, EntityManagerInterface $em, TranslatorInterface $translator, EventDispatcherInterface $eventDispatcher, SessionInterface $session)
46
    {
47
        $this->security = $security;
48 49
        $this->em = $em;
        $this->translator = $translator;
50
        $this->eventDispatcher = $eventDispatcher;
Julien Jorry committed
51
        $this->session = $session;
52
    }
53

54 55
    protected function manageFluxForm(Request $request, Form $form, $compte, $success, $title)
    {
56 57 58
        if ($this->security->getUser() == null) {
            throw new \Exception("[FLUX] Opération impossible ! Utilisateur déconnecté !");
        }
59 60 61 62 63 64 65 66 67
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $data = $form->getData();
            $this->em->persist($data);
            $this->em->flush();
            $this->addFlash(
                'success',
                $success
            );
68 69 70 71
            $this->eventDispatcher->dispatch(
                MLCEvents::FLUX,
                new FluxEvent($data)
            );
72 73 74 75 76 77 78
            $referer = $request->headers->get('referer');
            if ($referer && !$request->isXmlHttpRequest()) {
                return $this->redirect($referer);
            } elseif (!$request->isXmlHttpRequest()) {
                return new Response('', Response::HTTP_BAD_REQUEST);
            }
        }
79

80 81 82 83 84
        return $this->render('flux/transaction.html.twig', [
            'form' => $form->createView(),
            'title' => $title
        ]);
    }
85
}