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

namespace App\Utils;

use App\Entity\Adherent;
use App\Entity\Flux;
use App\Entity\Prestataire;
use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Security;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
Julien Jorry committed
13 14 15 16 17 18 19

class CotisationUtils
{
    private $em;
    private $session;
    private $security;
    private $eventDispatcher;
20 21 22 23 24 25 26 27 28
    private $container;

    public function __construct(
        CustomEntityManager $em,
        SessionInterface $session,
        Security $security,
        EventDispatcherInterface $eventDispatcher,
        ContainerInterface $container
    ) {
Julien Jorry committed
29 30 31 32
        $this->em = $em;
        $this->session = $session;
        $this->security = $security;
        $this->eventDispatcher = $eventDispatcher;
33
        $this->container = $container;
Julien Jorry committed
34 35 36 37 38
    }

    public function isUserCotisationExpired(UserInterface $user, int $nbDay = 30): bool
    {
        if (null === $user && null != $this->security->getUser()) {
39
            if ($this->security->isGranted('ROLE_ADHERENT') && null != $this->security->getUser()->getAdherent()) {
Julien Jorry committed
40
                return $this->isCotisationExpired($this->security->getUser()->getAdherent());
41
            } elseif ($this->security->isGranted('ROLE_PRESTATAIRE') && null != $this->session->get('_prestagere')) {
Julien Jorry committed
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
                return $this->isCotisationExpired($this->session->get('_prestagere'));
            }
        } elseif (null !== $user) {
            if ($user->isGranted('ROLE_ADHERENT') && null != $user->getAdherent()) {
                return $this->isCotisationExpired($user->getAdherent());
            } elseif ($user->isGranted('ROLE_PRESTATAIRE') && null != $this->session->get('_prestagere')) {
                return $this->isCotisationExpired($this->session->get('_prestagere'));
            }
        }

        return false;
    }

    public function isCotisationExpired($object, int $nbDay = 30): bool
    {
        if ($object instanceof Adherent) {
            $cotisations = $this->em->getRepository(Flux::class)->getQueryByAdherent($adherent, 'cotisation')->getResult();
        } elseif ($object instanceof Prestataire) {
            $cotisations = $this->em->getRepository(Flux::class)->getQueryByPrestataire($prestataire, 'cotisation')->getResult();
        } else {
            return false;
        }
        $hasCotisationValidForPeriod = false;
        foreach ($cotisations as $cotisation) {
            if ($cotisation->isRecu() && $cotisation->getCotisationInfos()->getFin() > new \DateTime('now - ' . $nbDay . 'days')) {
                $hasCotisationValidForPeriod = true;
            }
        }

        return $hasCotisationValidForPeriod;
    }

74
    /**
75
     * Get first Cotisation For Presta ?
76 77 78 79
     *
     * @param Prestataire $presta
     *
     * @return bool|date
80 81 82 83 84 85 86 87 88 89 90
     */
    public function getFirstCotisationForPresta(?Prestataire $presta)
    {
        $query = null;
        if (null !== $presta) {
            $query = $this->em->getRepository(Flux::class)->getQueryByPrestataire($presta, 'cotisation');
        }
        if (null !== $query) {
            $cotisations = $query->getResult();
            if (count($cotisations) > 0) {
                $firstCotis = $cotisations[0];
91

92 93 94 95 96 97 98
                return $firstCotis->getCotisationInfos()->getDebut();
            }
        }

        return false;
    }

99
    /**
100
     * Is the Cotisation Valid For Presta ?
101 102 103 104
     *
     * @param Prestataire $presta
     *
     * @return bool|date
105 106
     */
    public function isCotisationValidForPresta(?Prestataire $presta)
107
    {
Damien Moulard committed
108
        // In TAV mode, no standard cotisation required. Return next year date to make all accounts valid.
109 110 111 112
        if ($this->container->getParameter('tav_env')) {
            return date('Y-m-d', strtotime('+1 year'));
        }

113 114 115 116 117 118 119 120
        $query = null;
        if (null !== $presta) {
            $query = $this->em->getRepository(Flux::class)->getQueryByPrestataire($presta, 'cotisation');
        }
        if (null !== $query) {
            $cotisations = $query->getResult();
            foreach ($cotisations as $cotisation) {
                if ($cotisation->isRecu() && $cotisation->getCotisationInfos()->getFin() > new \DateTime('now')) {
121
                    return $cotisation->getCotisationInfos()->getFin();
122 123 124 125 126 127 128
                }
            }
        }

        return false;
    }

129
    /**
130 131 132 133 134
     * Get first Cotisation For Adherent.
     *
     * @param Adherent $adherent
     *
     * @return bool|date
135 136 137 138 139 140 141 142 143 144 145
     */
    public function getFirstCotisationForAdherent(?Adherent $adherent)
    {
        $query = null;
        if (null !== $adherent) {
            $query = $this->em->getRepository(Flux::class)->getQueryByAdherent($adherent, 'cotisation');
        }
        if (null !== $query) {
            $cotisations = $query->getResult();
            if (count($cotisations) > 0) {
                $firstCotis = $cotisations[0];
146

147 148 149 150 151 152 153
                return $firstCotis->getCotisationInfos()->getDebut();
            }
        }

        return false;
    }

154
    /**
155 156 157 158 159
     * Is the Cotisation Valid For Adherent.
     *
     * @param Adherent $adherent
     *
     * @return bool|date
160 161
     */
    public function isCotisationValidForAdherent(?Adherent $adherent)
162
    {
Damien Moulard committed
163
        // In TAV mode, no standard cotisation required. Return next year date to make all accounts valid.
164 165 166 167
        if ($this->container->getParameter('tav_env')) {
            return date('Y-m-d', strtotime('+1 year'));
        }

168 169 170 171 172 173 174 175
        $query = null;
        if (null !== $adherent) {
            $query = $this->em->getRepository(Flux::class)->getQueryByAdherent($adherent, 'cotisation');
        }
        if (null !== $query) {
            $cotisations = $query->getResult();
            foreach ($cotisations as $cotisation) {
                if ($cotisation->isRecu() && $cotisation->getCotisationInfos()->getFin() > new \DateTime('now')) {
176
                    return $cotisation->getCotisationInfos()->getFin();
177 178 179 180 181 182 183
                }
            }
        }

        return false;
    }

184
    /**
185 186 187 188 189
     * Is Cotisation Valid.
     *
     * @param UserInterface $user
     *
     * @return bool|date
190 191
     */
    public function isCotisationValid(?UserInterface $user)
Julien Jorry committed
192
    {
Damien Moulard committed
193
        // In TAV mode, no standard cotisation required. Return next year date to make all accounts valid.
194 195 196 197
        if ($this->container->getParameter('tav_env')) {
            return date('Y-m-d', strtotime('+1 year'));
        }

Julien Jorry committed
198 199
        $query = null;
        if (null === $user && null != $this->security->getUser()) {
200
            if ($this->security->isGranted('ROLE_ADHERENT') && null != $this->security->getUser()->getAdherent()) {
Julien Jorry committed
201
                $query = $this->em->getRepository(Flux::class)->getQueryByAdherent($this->security->getUser()->getAdherent(), 'cotisation');
202
            } elseif ($this->security->isGranted('ROLE_PRESTATAIRE') && null != $this->session->get('_prestagere')) {
Julien Jorry committed
203 204 205
                $query = $this->em->getRepository(Flux::class)->getQueryByPrestataire($this->session->get('_prestagere'), 'cotisation');
            }
        } elseif (null !== $user) {
206
            if ($user->isGranted('ROLE_ADHERENT') && null != $user->getAdherent()) {
Julien Jorry committed
207 208 209 210 211 212 213 214 215
                $query = $this->em->getRepository(Flux::class)->getQueryByAdherent($user->getAdherent(), 'cotisation');
            } elseif ($user->isGranted('ROLE_PRESTATAIRE') && null != $this->session->get('_prestagere')) {
                $query = $this->em->getRepository(Flux::class)->getQueryByPrestataire($this->session->get('_prestagere'), 'cotisation');
            }
        }
        if (null !== $query) {
            $cotisations = $query->getResult();
            foreach ($cotisations as $cotisation) {
                if ($cotisation->isRecu() && $cotisation->getCotisationInfos()->getFin() > new \DateTime('now')) {
216
                    return $cotisation->getCotisationInfos()->getFin();
Julien Jorry committed
217 218 219 220 221 222 223
                }
            }
        }

        return false;
    }
}