OperationsController.php 3.9 KB
Newer Older
Julien Jorry committed
1 2 3 4 5 6 7 8 9 10 11
<?php

namespace App\Controller;

use App\Enum\CurrencyEnum;
use App\Form\Type\ListOperationFormType;
use App\Utils\OperationUtils;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Session\SessionInterface;
Julien Jorry committed
13
use Symfony\Component\Routing\Annotation\Route;
14
use Symfony\Component\Security\Core\Security as Secur;
Julien Jorry committed
15 16 17 18 19 20

class OperationsController extends AbstractController
{
    protected $em;
    protected $paginator;
    protected $operationUtils;
21 22
    protected $security;
    private $session;
Julien Jorry committed
23

24 25

    public function __construct(EntityManagerInterface $em, PaginatorInterface $paginator, OperationUtils $operationUtils, Secur $security, SessionInterface $session)
Julien Jorry committed
26 27 28 29
    {
        $this->em = $em;
        $this->paginator = $paginator;
        $this->operationUtils = $operationUtils;
30 31
        $this->security = $security;
        $this->session = $session;
Julien Jorry committed
32 33 34 35 36 37 38 39 40 41
    }

    /**
     * @Route("/show/operations/{currency}", name="show_operations", defaults={"currency": "emlc"})
     */
    public function showOperationsAction(Request $request, $currency = CurrencyEnum::CURRENCY_MLC)
    {
        if (empty($this->getUser())) {
            return $this->redirectToRoute('index');
        }
42 43 44 45 46 47 48 49 50 51 52 53 54 55

        //Prepare query depending on user role
        $user = $this->security->getUser();
        $role = "";
        if (null != $this->session->get('_prestagere') && $user->isGranted('ROLE_PRESTATAIRE')) {
            $role = "ROLE_PRESTATAIRE";
        } elseif (null != $user->getAdherent() && $user->isGranted('ROLE_ADHERENT')) {
            $role = "ROLE_ADHERENT";
        } elseif (null != $this->session->get('_comptoirgere') && $user->isGranted('ROLE_COMPTOIR')) {
            $role = "ROLE_COMPTOIR";
        } elseif (null != $this->session->get('_groupegere') && $user->isGranted('ROLE_GESTION_GROUPE')) {
            $role = "ROLE_GESTION_GROUPE";
        }

Julien Jorry committed
56 57 58 59 60 61 62
        if (!in_array($currency, CurrencyEnum::getAvailableTypes())) {
            throw new \Exception('Opération impossible ! Type de currency  ' . $currency . ' inexistant');
        }

        $datemin = new \DateTime('first day of this month');
        $datemin->setTime(0, 0, 0);
        $datemax = new \DateTime('now');
63
        $form = $this->createForm(ListOperationFormType::class, null, array("role" => $role));
Julien Jorry committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            if (!$form->get('submit')->isClicked()) {
                $format = 'csv'; // CSV by default
                if ($form->get('json')->isClicked()) {
                    $format = 'json';
                } elseif ($form->get('csv')->isClicked()) {
                    $format = 'csv';
                } elseif ($form->get('xls')->isClicked()) {
                    $format = 'xls';
                } elseif ($form->get('xml')->isClicked()) {
                    $format = 'xml';
                }

                return $this->forward('App\Controller\FluxController::exportUserOperationAction', [
                    'format' => $format,
                    'currency' => $currency,
                ]);
            }
        }

        $query = $this->operationUtils->getUserOperationsByCurrency($request, $currency);
        $operations = [];
        if (null !== $query) {
            $operations = $this->paginator->paginate(
                $query, /* query NOT result */
                $request->query->getInt('page', 1)/*page number*/ ,
                20/*limit per page*/
            );
            $operations->setCustomParameters([
                'align' => 'center',
            ]);
        }

        return $this->render('@kohinos/flux/operations.html.twig', [
            'operations' => $operations,
            'currency' => $currency,
            'searchForm' => $form->createView(),
        ]);
    }
}