CotisationUtils.php 7.03 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
<?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;

class CotisationUtils
{
    private $em;
    private $session;
    private $security;
    private $eventDispatcher;

    public function __construct(CustomEntityManager $em, SessionInterface $session, Security $security, EventDispatcherInterface $eventDispatcher)
    {
        $this->em = $em;
        $this->session = $session;
        $this->security = $security;
        $this->eventDispatcher = $eventDispatcher;
    }

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

66
    /**
67
     * Get first Cotisation For Presta ?
68 69 70 71
     *
     * @param Prestataire $presta
     *
     * @return bool|date
72 73 74 75 76 77 78 79 80 81 82
     */
    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];
83

84 85 86 87 88 89 90
                return $firstCotis->getCotisationInfos()->getDebut();
            }
        }

        return false;
    }

91
    /**
92
     * Is the Cotisation Valid For Presta ?
93 94 95 96
     *
     * @param Prestataire $presta
     *
     * @return bool|date
97 98
     */
    public function isCotisationValidForPresta(?Prestataire $presta)
99 100 101 102 103 104 105 106 107
    {
        $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')) {
108
                    return $cotisation->getCotisationInfos()->getFin();
109 110 111 112 113 114 115
                }
            }
        }

        return false;
    }

116
    /**
117 118 119 120 121
     * Get first Cotisation For Adherent.
     *
     * @param Adherent $adherent
     *
     * @return bool|date
122 123 124 125 126 127 128 129 130 131 132
     */
    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];
133

134 135 136 137 138 139 140
                return $firstCotis->getCotisationInfos()->getDebut();
            }
        }

        return false;
    }

141
    /**
142 143 144 145 146
     * Is the Cotisation Valid For Adherent.
     *
     * @param Adherent $adherent
     *
     * @return bool|date
147 148
     */
    public function isCotisationValidForAdherent(?Adherent $adherent)
149 150 151 152 153 154 155 156 157
    {
        $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')) {
158
                    return $cotisation->getCotisationInfos()->getFin();
159 160 161 162 163 164 165
                }
            }
        }

        return false;
    }

166
    /**
167 168 169 170 171
     * Is Cotisation Valid.
     *
     * @param UserInterface $user
     *
     * @return bool|date
172 173
     */
    public function isCotisationValid(?UserInterface $user)
Julien Jorry committed
174 175 176
    {
        $query = null;
        if (null === $user && null != $this->security->getUser()) {
177
            if ($this->security->isGranted('ROLE_ADHERENT') && null != $this->security->getUser()->getAdherent()) {
Julien Jorry committed
178
                $query = $this->em->getRepository(Flux::class)->getQueryByAdherent($this->security->getUser()->getAdherent(), 'cotisation');
179
            } elseif ($this->security->isGranted('ROLE_PRESTATAIRE') && null != $this->session->get('_prestagere')) {
Julien Jorry committed
180 181 182
                $query = $this->em->getRepository(Flux::class)->getQueryByPrestataire($this->session->get('_prestagere'), 'cotisation');
            }
        } elseif (null !== $user) {
183
            if ($user->isGranted('ROLE_ADHERENT') && null != $user->getAdherent()) {
Julien Jorry committed
184 185 186 187 188 189 190 191 192
                $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')) {
193
                    return $cotisation->getCotisationInfos()->getFin();
Julien Jorry committed
194 195 196 197 198 199 200
                }
            }
        }

        return false;
    }
}