TransfertAdmin.php 5.05 KB
Newer Older
1 2 3 4 5 6
<?php

namespace App\Admin;

use App\Admin\FluxAdmin;
use App\Entity\User;
Julien Jorry committed
7 8
use App\Entity\Flux;
use App\Entity\Prestataire;
9 10 11 12 13 14 15 16
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Form\Type\Filter\ChoiceType;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Show\ShowMapper;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType as SChoiceType;
Julien Jorry committed
17
use Symfony\Component\HttpFoundation\Session\SessionInterface;
18
use Symfony\Component\Security\Core\Security;
19 20
use Symfony\Component\Translation\TranslatorInterface;

21 22 23
/**
 * Administration des transferts
 *
Julien Jorry committed
24
 * KOHINOS : Outil de gestion de Monnaie Locale Complémentaire
25 26
 * @author Julien Jorry <julien.jorry@gmail.com>
 */
27 28
class TransfertAdmin extends FluxAdmin
{
29
    protected $security;
Julien Jorry committed
30
    protected $session;
31 32 33 34 35
    protected $datagridValues = [
        '_sort_order' => 'DESC',
        '_sort_by' => 'createdAt',
    ];

36
    public function setSecurity(Security $security)
37
    {
38
        $this->security = $security;
39 40
    }

Julien Jorry committed
41 42 43 44 45
    public function setSession(SessionInterface $session)
    {
        $this->session = $session;
    }

46 47 48 49 50
    protected function configureRoutes(RouteCollection $collection)
    {
        $collection->clearExcept(array('list', 'export'));
    }

51 52 53 54 55
    /**
    * {@inheritdoc}
    */
    public function createQuery($context = 'list')
    {
56
        $user = $this->security->getUser();
57
        $query = parent::createQuery($context);
Julien Jorry committed
58 59 60
        // $query->andWhere($query->getRootAliases()[0].".parenttype = :type")
        // ->setParameter('type', 'transfert');

61
        $em = $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager();
Julien Jorry committed
62
        $fluxtable =  $em->getMetadataFactory()->getMetadataFor(Flux::class)->getTableName();
63
        if ($this->security->getUser()->isGranted('ROLE_GESTION_GROUPE') || $this->security->getUser()->isGranted('ROLE_CONTACT') || $this->security->getUser()->isGranted('ROLE_TRESORIER')) {
Julien Jorry committed
64
            if (empty($this->getRequest()->getSession()->get('_groupegere'))) {
65
                $query->andWhere('false = true');
66
            } else {
Julien Jorry committed
67
                $groupe = $this->getRequest()->getSession()->get('_groupegere');
68
                $connection = $em->getConnection();
Julien Jorry committed
69
                $prestatable =  $em->getMetadataFactory()->getMetadataFor(Prestataire::class)->getTableName();
70
                $statement = $connection->prepare('SELECT f.id FROM '.$fluxtable.' f WHERE f.groupe_id = '.$groupe->getId().' OR (f.type = \'reconversion\' AND f.prestataire_id IN (SELECT p.id FROM '.$fluxtable.' p WHERE p.groupe_id = '.$groupe->getId().'))');
71 72 73 74 75 76 77
                $statement->execute();
                $ids = $statement->fetchAll();
                $query
                    ->andWhere($query->expr()->in($query->getRootAliases()[0].'.id', ':ids'))
                    ->setParameter('ids', $ids)
                ;
            }
78
        } elseif ($this->security->getUser()->isGranted('ROLE_COMPTOIR')) {
79
            if (empty($this->getRequest()->getSession()->get('_comptoirgere'))) {
80
                $query->andWhere('false = true');
81
            } else {
82 83
                $comptoir = $this->getRequest()->getSession()->get('_comptoirgere');
                $em = $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager();
84
                $connection = $em->getConnection();
Julien Jorry committed
85
                $statement = $connection->prepare('SELECT f.id FROM '.$fluxtable.' f WHERE f.comptoir_id = '.$comptoir->getId().' OR f.user_id = '.$user->getId());
86 87 88 89 90 91 92 93
                $statement->execute();
                $ids = $statement->fetchAll();
                $query
                    ->andWhere($query->expr()->in($query->getRootAliases()[0].'.id', ':ids'))
                    ->setParameter('ids', $ids)
                ;
            }
        }
94 95 96
        return $query;
    }

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    public function getTotalLabel()
    {
        return $this->translator->trans('Total des transferts :');
    }

    public function getTotal()
    {
        $datagrid = $this->getDatagrid();
        $datagrid->buildPager();

        $query = clone $datagrid->getQuery();
        $query
            ->select('SUM( ' . $query->getRootAlias() . '.montant) as total')
            ->andWhere($query->getRootAlias().".parenttype = :type")
            ->setParameter('type', 'transfert')
            ->setFirstResult(null)
            ->setMaxResults(null);

        $result = $query->execute(array(), \Doctrine\ORM\Query::HYDRATE_SINGLE_SCALAR);

        return $result;
    }

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    /**
    * {@inheritdoc}
    */
    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
    {
        $datagridMapper
            ->add('type', null, array(
                'label' => 'Type de transfert',
                'advanced_filter' => false,
                'show_filter' => true
            ))
            ->add('operateur', null, array(
                'label' => 'Operateur',
                'advanced_filter' => false,
                'show_filter' => true
            ))
        ;
    }
}