UserAdherentController.php 9.71 KB
Newer Older
Julien Jorry committed
1 2 3 4 5 6 7
<?php

namespace App\Controller;

use App\Entity\AchatMonnaieAConfirmerAdherent;
use App\Entity\AchatMonnaieAdherent;
use App\Entity\Adherent;
8
use App\Entity\GlobalParameter;
Julien Jorry committed
9 10 11 12 13 14 15 16
use App\Entity\Payment;
use App\Entity\TransactionAdherentAdherent;
use App\Entity\TransactionAdherentPrestataire;
use App\Form\Type\AchatMonnaieAConfirmerAdherentFormType;
use App\Form\Type\AchatMonnaieAdherentFormType;
use App\Form\Type\AdherentInfosFormType;
use App\Form\Type\TransactionAdherentAdherentFormType;
use App\Form\Type\TransactionAdherentPrestataireFormType;
17
use App\Form\Type\SetPaymentCodeFormType;
Julien Jorry committed
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
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class UserAdherentController extends FluxController
{
    /**
     * @Route("/adherent/infos", name="adherent_infos")
     * @IsGranted("ROLE_ADHERENT")
     */
    public function adherentInfosAction(Request $request)
    {
        $form = $this->createForm(AdherentInfosFormType::class, $this->getUser()->getAdherent());
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->em->persist($form->getData());
            $this->em->flush();
            $this->addFlash(
                'success',
                $this->translator->trans('Infos de l\'adhérent modifiées !')
            );
            $referer = $request->headers->get('referer');
            if ($referer && !$request->isXmlHttpRequest()) {
                return $this->redirect($referer);
            } elseif (!$request->isXmlHttpRequest()) {
                return new Response('', Response::HTTP_BAD_REQUEST);
            }
        }

        return $this->redirectToRoute('index');
    }

    /**
     * @Route("/adherent/transaction/prestataire/", name="transactionAdherentPrestataire")
     * @IsGranted("ROLE_ADHERENT")
     */
    public function transactionAdherentPrestataireAction(Request $request)
    {
        $entity = new TransactionAdherentPrestataire();
        $entity->setOperateur($this->getUser());
        $entity->setExpediteur($this->getUser()->getAdherent());
        $form = $this->createForm(TransactionAdherentPrestataireFormType::class, $entity);

        return $this->manageFluxForm(
            $request,
            $form
        );
    }

    /**
     * @Route("/adherent/transaction/adherent/", name="transactionAdherentAdherent")
     * @IsGranted("ROLE_ADHERENT")
     */
    public function transactionAdherentAdherentAction(Request $request)
    {
        if (empty($this->getUser()) || empty($this->getUser()->getAdherent())) {
            return $this->redirectToRoute('index');
        }
        $entity = new TransactionAdherentAdherent();
        $entity->setOperateur($this->getUser());
        $entity->setExpediteur($this->getUser()->getAdherent());
        $form = $this->createForm(TransactionAdherentAdherentFormType::class, $entity);

        return $this->manageFluxForm(
            $request,
            $form
        );
    }

    /**
     * @Route("/adherent/achat-monnaie/", name="achatMonnaieAdherent")
     * @IsGranted("ROLE_ADHERENT")
     */
    public function achatMonnaieAdherentAction(Request $request)
    {
        if (empty($this->getUser()) || empty($this->getUser()->getAdherent())) {
            return $this->redirectToRoute('index');
        }

        $entity = new AchatMonnaieAdherent();
        $form = $this->createForm(AchatMonnaieAdherentFormType::class, $entity);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
104
            if ($form->has('payOther') && $form->get('payOther')->isClicked()) {
Julien Jorry committed
105
                return $this->redirectToRoute('achatMonnaieAConfirmerAdherent');
106
            } elseif ($form->has('save') && $form->get('save')->isClicked()) {
107 108 109 110
                return $this->forward('App\Controller\PaymentController::preparePaymentAction', [
                    'form'  => $form,
                    'type'  => Payment::TYPE_ACHAT_MONNAIE_ADHERENT
                ]);
111
            } elseif ($form->has('saveHelloAsso') && $form->get('saveHelloAsso')->isClicked()) {
112 113 114
                $url = $this->em->getRepository(GlobalParameter::class)->val(GlobalParameter::HELLOASSO_URL_EMLC_ADHERENT);

                return $this->redirect($url);
Julien Jorry committed
115 116 117 118 119 120 121 122 123
            }
        }

        return $this->render('@kohinos/flux/transaction.html.twig', [
            'form' => $form->createView(),
            'title' => $this->translator->trans('Achat de monnaie locale'),
        ]);
    }

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    /**
     * @Route("/adherent/paiement-cotis-tav/", name="paiementCotisTav")
     * @IsGranted("ROLE_ADHERENT")
     */
    public function paiementCotisTavAction(Request $request)
    {
        if (empty($this->getUser()) || empty($this->getUser()->getAdherent())) {
            return $this->redirectToRoute('index');
        }

        $entity = new AchatMonnaieAdherent();
        $form = $this->createForm(AchatMonnaieAdherentFormType::class, $entity);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
139
            // TODO: set CB payment when the functionality is validated
140

141 142
            $flux = $form->getData();

143 144 145 146 147 148 149 150 151 152
            // Look for existing cotisation
            if ($this->tavCotisationsUtils->checkExistingCotisation($flux)) {
                $this->addFlash(
                    'error',
                    $this->translator->trans('Cotisation déjà payée ce mois-ci.')
                );

                return $this->redirectToRoute('index');
            }

153 154 155 156 157 158 159 160 161 162 163 164
            $destinataire = $flux->getDestinataire();
            $profile = $destinataire->getProfilDeCotisation();

            if (is_null($profile)) {
                $this->addFlash(
                    'error',
                    $this->translator->trans('Opération impossible : vous n\'avez pas de profil de cotisation associé. Veuillez contacter un administrateur.')
                );

                return $this->redirectToRoute('index');
            }

Damien Moulard committed
165
            if (null == $flux->getDon() || 0 == $flux->getDon()->getMontant()) {
166 167 168
                $flux->setDon(null);
            }

169 170 171 172 173
            // TODO redirect to paiement
            return $this->forward('App\Controller\PaymentController::preparePaymentAction', [
                'form'  => $form,
                'type'  => Payment::TYPE_PAIEMENT_COTISATION_TAV // TODO
            ]);
174

175 176 177 178 179
            // $this->em->persist($flux);
            // $this->operationUtils->executeOperations($flux);

            // // Apply cotisation rate, create new flux
            // $this->tavCotisationsUtils->applyTauxCotisation($flux);
180
            
181 182 183 184 185
            // $this->em->flush();
            // $this->addFlash(
            //     'success',
            //     $this->translator->trans('Cotisation payée ! [Paiement via Payzen temporairement désactivé]')
            // );
186

187
            // return $this->redirectToRoute('index');
188 189 190 191 192 193 194 195
        }

        return $this->render('@kohinos/flux/transaction.html.twig', [
            'form' => $form->createView(),
            'title' => $this->translator->trans('Payer sa cotisation'),
        ]);
    }

Julien Jorry committed
196 197
    /**
     * @Route("/adherent/demande/achat-monnaie/", name="achatMonnaieAConfirmerAdherent")
198
     * @IsGranted("ROLE_ADHERENT")
Julien Jorry committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
     */
    public function achatMonnaieAConfirmerAdherentAction(Request $request)
    {
        if (empty($this->getUser()) || empty($this->getUser()->getAdherent())) {
            return $this->redirectToRoute('index');
        }
        $entity = new AchatMonnaieAConfirmerAdherent();
        $form = $this->createForm(AchatMonnaieAConfirmerAdherentFormType::class, $entity);

        return $this->manageFluxForm(
            $request,
            $form,
            '@kohinos/flux/demande_achat_monnaie.html.twig',
            ['title' => $this->translator->trans("Demande d'achat de monnaie locale numérique")]
        );
    }

    /**
     * @Route("/adherent/liste/demande/achat-monnaie/", name="listachatMonnaieAConfirmerAdherent")
     * @IsGranted("ROLE_ADHERENT")
     */
    public function listachatMonnaieAConfirmerAdherentAction(Request $request)
    {
        if (empty($this->getUser()) || empty($this->getUser()->getAdherent())) {
            return $this->redirectToRoute('index');
        }

        $q = $this->em->getRepository(AchatMonnaieAConfirmerAdherent::class)->findBy(['destinataire' => $this->getUser()->getAdherent()], ['createdAt' => 'DESC']);

        return $this->render('@kohinos/flux/list_demande_achat_monnaie.html.twig', [
            'datas' => $q,
        ]);
    }
232 233 234 235 236

    /**
     * @Route("/adherent/set-payment-code", name="adherentSetPaymentCode")
     * @IsGranted("ROLE_ADHERENT")
     */
237
    public function setPaymentCodeAction(Request $request)
238 239 240 241 242 243 244 245 246
    {
        $adherent = $this->getUser()->getAdherent();
        $form = $this->createForm(SetPaymentCodeFormType::class, $adherent);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $data = $form->getData();
            $plainCode = $data->getPaymentCode();

Félicie committed
247
            if (ctype_digit($plainCode) && strlen($plainCode) >= 4 && strlen($plainCode) <= 8) {
248 249
                $encoded = crypt($plainCode, $this->getUser()->getSalt());

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
                $adherent->setPaymentCode($encoded);
    
                $this->em->flush();
                $this->addFlash(
                    'success',
                    $this->translator->trans('Code de paiement modifié !')
                );
            } else {
                $this->addFlash(
                    'error',
                    $this->translator->trans('Le code de paiement doit être composé de 4 à 8 chiffres.')
                );
            }
        }

        return $this->redirectToRoute('index');
    }
Julien Jorry committed
267
}