AchatMonnaieAdmin.php 4.71 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
<?php

namespace App\Admin;

use App\Entity\Flux;
use App\Entity\GlobalParameter;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

/**
 * Administration des flux 'Achat de monnaie'.
 *
 * KOHINOS : Outil de gestion de Monnaie Locale Complémentaire
 *
 * @author Damien Moulard <dam.moulard@gmail.com>
 */
class AchatMonnaieAdmin extends FluxAdmin
{
    protected $baseRouteName = 'achat_monnaie';
    protected $baseRoutePattern = 'achat_monnaie';

    /**
     * {@inheritdoc}
     */
    public function createQuery($context = 'list')
    {
        $query = parent::createQuery($context);
30 31 32 33 34 35 36 37 38
        //In mode ssa_friendly_flux_type_names, we display on this page all cotisation
        //initial operation (i.e. we display vente_emlc operations in addition to achat_monnaie)
        if($this->getConfigurationPool()->getContainer()->getParameter('tav_env')
            && $this->getConfigurationPool()->getContainer()->getParameter('ssa_friendly_flux_type_names')) {
            $query->andWhere($query->getRootAliases()[0] . '.parenttype = :parenttype' . ' OR ' . $query->getRootAliases()[0] . '.parenttype = :parenttype2')
                ->setParameter('parenttype', Flux::TYPE_ACHAT)
                ->setParameter('parenttype2', Flux::TYPE_VENTE_EMLC);
        } else {
            $query->andWhere($query->getRootAliases()[0] . '.parenttype = :parenttype')
Julien Jorry committed
39
                ->setParameter('parenttype', Flux::TYPE_ACHAT);
40
        }
Julien Jorry committed
41 42 43 44 45 46 47 48 49 50 51 52 53
        return $query;
    }

    // protected function configureRoutes(RouteCollection $collection)
    // {
    //     $collection->clearExcept(['create', 'list', 'export']);
    // }

    /**
     * {@inheritdoc}
     */
    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
    {
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
        //In mode ssa_friendly_flux_type_names, we display on this page all cotisation
        //initial operation (i.e. we display vente_emlc operations in addition to achat_monnaie)
        //therefore filtering by type achat_monnaie_adherent VS achat_monnaie_prestataire
        //would be confusing (selecting one of them would hide all vente_emlc operations)
        if(!$this->getConfigurationPool()->getContainer()->getParameter('tav_env')
            || !$this->getConfigurationPool()->getContainer()->getParameter('ssa_friendly_flux_type_names')) {
            $datagridMapper->add('type', null, [
                'advanced_filter' => false,
                'show_filter' => true,
                'field_type' => ChoiceType::class,
                'field_options' => [
                    'choices' => ['Adhérent' => 'achat_monnaie_adherent', 'Prestataire' => 'achat_monnaie_prestataire'],
                    'placeholder' => 'Indifférent',
                    'expanded' => true,
                    'multiple' => false,
                ],
            ]);
        }
Julien Jorry committed
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
    }

    /**
     * {@inheritdoc}
     */
    protected function configureListFields(ListMapper $listMapper)
    {
        parent::configureListFields($listMapper);
        $listMapper
            ->remove('type')
            ->remove('expediteur')
        ;
    }

    public function getTotalLabel()
    {
        $em = $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager();

        return $this->translator->trans('Total des achats de e' . $em->getRepository(GlobalParameter::class)->val(GlobalParameter::MLC_SYMBOL));
    }

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

        $query = clone $datagrid->getQuery();
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        if($this->getConfigurationPool()->getContainer()->getParameter('tav_env')
            && $this->getConfigurationPool()->getContainer()->getParameter('ssa_friendly_flux_type_names')) {
            $query
                ->select('SUM( ' . $query->getRootAlias() . '.montant) as total')
                ->andWhere($query->getRootAlias() . '.parenttype = :parenttype' . ' OR ' . $query->getRootAliases()[0] . '.parenttype = :parenttype2')
                ->setParameter('parenttype', Flux::TYPE_ACHAT)
                ->setParameter('parenttype2', Flux::TYPE_VENTE_EMLC)
                ->setFirstResult(null)
                ->setMaxResults(null);
        } else {
            $query
                ->select('SUM( ' . $query->getRootAlias() . '.montant) as total')
                ->andWhere($query->getRootAlias() . '.parenttype = :parenttype')
                ->setParameter('parenttype', Flux::TYPE_ACHAT)
                ->setFirstResult(null)
                ->setMaxResults(null);
        }

Julien Jorry committed
117 118 119 120 121 122

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

        return $result;
    }
}