TAVCotisationUtils.php 3.81 KB
Newer Older
1 2 3 4
<?php

namespace App\Utils;

5
use App\Entity\Adherent;
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
use App\Entity\Siege;
use App\Entity\Flux;
use App\Entity\TauxCotisationReversement;
use App\Entity\TauxCotisationPrelevement;
use App\Enum\MoyenEnum;
use App\Utils\CustomEntityManager;
use Symfony\Component\Security\Core\Security;

class TAVCotisationUtils
{
    private $em;
    private $security;
    private $operationUtils;

    public function __construct (
        CustomEntityManager $em,
        Security $security,
        OperationUtils $operationUtils
    ) {
        $this->em = $em;
        $this->security = $security;
        $this->operationUtils = $operationUtils;
    }

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    /**
     * Check if cotisation already exist this month the the recipient of the given Flux.
     */
    public function checkExistingCotisation(Flux $flux) 
    {
        $first_day_this_month = date('Y-m-01');
        $last_day_this_month  = date('Y-m-t');

        $existing = $this->em->getRepository(Flux::class)->getTavCotisationsBetweenDates(
            $flux->getDestinataire(),
            $first_day_this_month,
            $last_day_this_month
        );

        return count($existing) > 0;
    }

47 48 49 50 51 52 53 54 55 56 57 58
    /**
     * Apply the cotisation profile rate to the amount paid 
     * and register the complement as a new flux
     * 
     * Warning: EntityManager not flushed here.
     */
    public function applyTauxCotisation(Flux $flux)
    {
        // get the mlc amount the user will receive
        $profile = $flux->getDestinataire()->getProfilDeCotisation();
        $cotisationAmount = $profile->getMontant();
        $cotisationTaux = $profile->getTauxCotisation();
59
        $mlcAmount = round($cotisationAmount * $cotisationTaux);
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

        // get the difference between what the user paid and what he•she's supposed to receive
        $amountDiff = $mlcAmount - $cotisationAmount;

        if ($flux->getExpediteur() instanceof Siege) {
            $siege = $flux->getExpediteur();
        } else {
            $siege = $flux->getExpediteur()->getGroupe()->getSiege();
        }

        if ($amountDiff > 0) {
            // User should receive more than he•she paid: send a new flux to the user to complete its cotisation
            $fluxCotis = new TauxCotisationReversement();
            $fluxCotis->setExpediteur($siege);
            $fluxCotis->setDestinataire($flux->getDestinataire());
            $fluxCotis->setMontant($amountDiff);
            $fluxCotis->setReference("Reversement cotisation après paiement de " . $cotisationAmount . "€ et application du taux " . $cotisationTaux);
        } else {
            // User should receive less than he•she paid: fetch the difference from his account 
            $fluxCotis = new TauxCotisationPrelevement();
            $fluxCotis->setExpediteur($flux->getDestinataire());
            $fluxCotis->setDestinataire($siege);
            $fluxCotis->setMontant(-$amountDiff);
            $fluxCotis->setReference("Prélèvement cotisation après paiement de " . $cotisationAmount . "€ et application du taux " . $cotisationTaux);
        }

        $fluxCotis->setOperateur($this->security->getUser());
        $fluxCotis->setRole($this->security->getUser()->getGroups()[0]->__toString());
        $fluxCotis->setMoyen(MoyenEnum::MOYEN_EMLC);
        $this->em->persist($fluxCotis);
        $this->operationUtils->executeOperations($fluxCotis);
    }
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

    /**
     * Get the last cotisation of an adhérent
     *
     * @param Adherent $adherent
     *
     * @return bool|date
     */
    public function getLastTavCotisationForAdherent(?Adherent $adherent)
    {
        $cotisations = [];
        if (null !== $adherent) {
            $cotisations = $this->em->getRepository(Flux::class)->getLastTavCotisation($adherent);
        }

        if (count($cotisations) > 0) {
            return $cotisations[0]["created_at"];
        }

        return false;
    }
113
}