OperationsController.php 3.02 KB
Newer Older
Julien Jorry committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
<?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;
use Symfony\Component\Routing\Annotation\Route;

class OperationsController extends AbstractController
{
    protected $em;
    protected $paginator;
    protected $operationUtils;

    public function __construct(EntityManagerInterface $em, PaginatorInterface $paginator, OperationUtils $operationUtils)
    {
        $this->em = $em;
        $this->paginator = $paginator;
        $this->operationUtils = $operationUtils;
    }

    /**
     * @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');
        }
        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');
        $form = $this->createForm(ListOperationFormType::class, [
            'datemin' => $datemin,
            'datemax' => $datemax,
        ]);
        $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(),
        ]);
    }
}