HelloassoUtils.php 4.86 KB
Newer Older
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 31 32 33 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 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 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
<?php

namespace App\Utils;

use App\Entity\AchatMonnaie;
use App\Entity\AchatMonnaieAdherent;
use App\Entity\AchatMonnaiePrestataire;
use App\Entity\Cotisation;
use App\Entity\CotisationAdherent;
use App\Entity\CotisationPrestataire;
use App\Entity\Don;
use App\Entity\DonAdherent;
use App\Entity\DonPrestataire;
use App\Entity\Flux;
use App\Entity\HelloAsso;
use App\Entity\Prestataire;
use App\Entity\Siege;
use App\Enum\MoyenEnum;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Core\Security;

class HelloassoUtils
{
    protected $em;
    protected $security;
    protected $eventDispatcher;
    protected $operationUtils;
    protected $client;

    public function __construct(
        Security $security,
        EntityManagerInterface $em,
        EventDispatcherInterface $eventDispatcher,
        LoggerInterface $logger,
        OperationUtils $operationUtils
    ) {
        $this->security = $security;
        $this->logger = $logger;
        $this->em = $em;
        $this->eventDispatcher = $eventDispatcher;
        $this->operationUtils = $operationUtils;
    }

    public function addDonation(HelloAsso $helloasso): ?Don
    {
        if (!empty($helloasso->getAdherent())) {
            $flux = new DonAdherent();
            $flux->setExpediteur($helloasso->getAdherent());
            $flux->setDestinataire($this->em->getRepository(Prestataire::class)->findOneBy(['mlc' => true]));
        } elseif (!empty($helloasso->getPrestataire())) {
            $flux = new DonPrestataire();
            $flux->setExpediteur($helloasso->getPrestataire());
            $flux->setDestinataire($this->em->getRepository(Prestataire::class)->findOneBy(['mlc' => true]));
        }
        $flux->setRole('ROLE_HELLOASSO');
        $flux->setDestinataire($this->em->getRepository(Prestataire::class)->getPrestataireMLC());
        $flux->setMontant($helloasso->getAmount());
        $flux->setMoyen(MoyenEnum::MOYEN_HELLOASSO);
        $flux->setReference('Helloasso #' . $helloasso->getHelloassoid());
        $flux->setData(['Helloasso ' => '#' . $helloasso->getHelloassoid()]);

        $this->em->persist($flux);
        $this->em->flush();

        // Write operations for this flux !
        $this->operationUtils->executeOperations($flux);

        return $flux;
    }

    public function addAchatEmlc(HelloAsso $helloasso): ?AchatMonnaie
    {
        if (!empty($helloasso->getAdherent())) {
            $flux = new AchatMonnaieAdherent();
            $flux->setDestinataire($helloasso->getAdherent());
        } elseif (!empty($helloasso->getPrestataire())) {
            $flux = new AchatMonnaiePrestataire();
            $flux->setDestinataire($helloasso->getPrestataire());
        } else {
            return null;
        }
        // $flux->setOperateur(null);
        $flux->setRole('ROLE_HELLOASSO');
        $flux->setExpediteur($this->em->getRepository(Siege::class)->getTheOne());
        $flux->setMontant($helloasso->getAmount());
        $flux->setMoyen(MoyenEnum::MOYEN_HELLOASSO);
        $flux->setReference('Helloasso #' . $helloasso->getHelloassoid());
        $flux->setReconverti(true);
        $flux->setData(['Helloasso' => '#' . $helloasso->getHelloassoid()]);

        $this->em->persist($flux);
        $this->em->flush();

        // Write operations for this flux !
        $this->operationUtils->executeOperations($flux);

        return $flux;
    }

    public function addCotisation(HelloAsso $helloasso): ?Cotisation
    {
        if (!empty($helloasso->getAdherent())) {
            $cotisation = new CotisationAdherent();
            $cotisation->setExpediteur($helloasso->getAdherent());
        } elseif (!empty($helloasso->getPrestataire())) {
            $cotisation = new CotisationPrestataire();
            $cotisation->setExpediteur($helloasso->getPrestataire());
        } else {
            throw new \Exception('Impossible de créer la cotisation, ni adherent ni prestataire trouvé !');
        }
        $now = new DateTime();
        $cotisation->setRecu(true);
        $cotisation->setReference('Helloasso #' . $helloasso->getHelloassoid());
        // $cotisation->setOperateur(null);
        $cotisation->setRole('ROLE_HELLOASSO');
        $mlcPrestataire = $this->em->getRepository(Prestataire::class)->findOneBy(['mlc' => true]);
        $cotisation->setDestinataire($mlcPrestataire);
        $cotisation->setMoyen(MoyenEnum::MOYEN_HELLOASSO);

        $cotisation->setMontant($helloasso->getAmount());
        $cotisation->getCotisationInfos()->setAnnee(date('Y'));
        $cotisation->getCotisationInfos()->setDebut($now);
        $cotisation->getCotisationInfos()->setFin(new DateTime('+ 1 year'));

        $this->em->persist($cotisation);
        $this->em->flush();

        $this->operationUtils->executeOperations($cotisation);

        return $cotisation;
    }
}