SolidoumeExtension.php 4.41 KB
Newer Older
Julien Jorry committed
1 2 3 4
<?php

namespace App\Twig;

5 6 7
use App\Entity\Adherent;
use App\Entity\Flux;
use App\Entity\Prestataire;
Julien Jorry committed
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
use App\Entity\SolidoumeItem;
use App\Entity\SolidoumeParameter;
use App\Form\Type\SolidoumeFormType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Security;
use Twig\Extension\AbstractExtension;

class SolidoumeExtension extends AbstractExtension
{
    protected $em;
    protected $formFactory;
    protected $router;
    protected $security;

    public function __construct(
        EntityManagerInterface $em,
        FormFactoryInterface $formFactory,
        RouterInterface $router,
        Security $security
    ) {
        $this->em = $em;
        $this->formFactory = $formFactory;
        $this->router = $router;
        $this->security = $security;
    }

    /**
     * {@inheritdoc}
     */
    public function getFunctions()
    {
        return [
            new \Twig_SimpleFunction('getSolidoumeParam', [$this, 'getSolidoumeParam']),
            new \Twig_SimpleFunction('getSolidoumeForm', [$this, 'getSolidoumeForm']),
            new \Twig_SimpleFunction('getSolidoumeParticipation', [$this, 'getSolidoumeParticipation']),
45
            new \Twig_SimpleFunction('getSolidoumePrelevements', [$this, 'getSolidoumePrelevements']),
46
            new \Twig_SimpleFunction('getSolidoumeRedistributions', [$this, 'getSolidoumeRedistributions']),
Julien Jorry committed
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
            new \Twig_SimpleFunction('getSolidoumeDatas', [$this, 'getSolidoumeDatas']),
        ];
    }

    public function getSolidoumeParam(?string $name)
    {
        return $this->em->getRepository(SolidoumeParameter::class)->getValueOf($name);
    }

    public function getSolidoumeForm()
    {
        if (null == $this->security->getUser() || null == $this->security->getUser()->getAdherent() || !$this->security->getUser()->isGranted('ROLE_ADHERENT')) {
            return null;
        }
        $data = $this->em->getRepository(SolidoumeItem::class)->findOneBy(['enabled' => true, 'adherent' => $this->security->getUser()->getAdherent()]);
        $form = $this->formFactory->create(SolidoumeFormType::class, $data, ['action' => $this->router->generate('solidoume_adherer')]);

        return $form->createView();
    }

    public function getSolidoumeParticipation()
    {
        if (null == $this->security->getUser() || null == $this->security->getUser()->getAdherent() || !$this->security->getUser()->isGranted('ROLE_ADHERENT')) {
            return null;
        }
        $data = $this->em->getRepository(SolidoumeItem::class)->findOneBy(['enabled' => true, 'adherent' => $this->security->getUser()->getAdherent()]);

        return $data;
    }

77 78
    public function getSolidoumePrelevements(Adherent $adherent)
    {
79 80 81 82 83 84 85 86
        $datas = $this->em->getRepository(Flux::class)->getQueryByAdherentAndDestinataire($adherent, $this->em->getRepository(Prestataire::class)->getPrestataireSolidoume(), 'adherent_prestataire');

        return $datas;
    }

    public function getSolidoumeRedistributions(Adherent $adherent)
    {
        $datas = $this->em->getRepository(Flux::class)->getQueryByAdherentAndDestinataire($adherent, $this->em->getRepository(Prestataire::class)->getPrestataireSolidoume(), 'prestataire_adherent');
87 88 89 90

        return $datas;
    }

Julien Jorry committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    public function getSolidoumeDatas()
    {
        $param = $this->em->getRepository(SolidoumeParameter::class)->findTheOne();
        if (empty($param)) {
            return null;
        }
        $items = $this->em->getRepository(SolidoumeItem::class)->findBy(['enabled' => true]);
        if (empty($items)) {
            return null;
        }
        $total = 0;
        $countPerson = 0;
        $countParticipants = 0;
        foreach ($items as $item) {
            $total += $item->getAmount();
            if (!$item->getIsDonation()) {
                ++$countParticipants;
            }
            ++$countPerson;
        }

        return [
            'total' => $total,
            'countPersons' => $countPerson,
            'countDonateurs' => ($countPerson - $countParticipants),
            'countParticipants' => $countParticipants,
            'totalByParticipant' => ($total / $countParticipants),
118
            'totalByParticipantWithCommission' => round((($total / $countParticipants) * ((100 - $param->getCommission()) / 100)), 2, PHP_ROUND_HALF_DOWN),
Julien Jorry committed
119 120 121 122
            'commission' => $param->getCommission(),
        ];
    }
}