UserAdherentController.php 12.4 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
use App\Entity\Payment;
use App\Entity\TransactionAdherentAdherent;
use App\Entity\TransactionAdherentPrestataire;
use App\Form\Type\AchatMonnaieAConfirmerAdherentFormType;
use App\Form\Type\AchatMonnaieAdherentFormType;
14
use App\Form\Type\AchatMonnaieAdherentRecurrentFormType;
Julien Jorry committed
15 16 17
use App\Form\Type\AdherentInfosFormType;
use App\Form\Type\TransactionAdherentAdherentFormType;
use App\Form\Type\TransactionAdherentPrestataireFormType;
18
use App\Form\Type\SetPaymentCodeFormType;
Julien Jorry committed
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
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()) {
105
            if ($form->has('payOther') && $form->get('payOther')->isClicked()) {
Julien Jorry committed
106
                return $this->redirectToRoute('achatMonnaieAConfirmerAdherent');
107
            } elseif ($form->has('save') && $form->get('save')->isClicked()) {
108 109 110 111
                return $this->forward('App\Controller\PaymentController::preparePaymentAction', [
                    'form'  => $form,
                    'type'  => Payment::TYPE_ACHAT_MONNAIE_ADHERENT
                ]);
112
            } elseif ($form->has('saveHelloAsso') && $form->get('saveHelloAsso')->isClicked()) {
113 114 115
                $url = $this->em->getRepository(GlobalParameter::class)->val(GlobalParameter::HELLOASSO_URL_EMLC_ADHERENT);

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

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

125 126 127 128 129 130
    /**
     * Validations before proceeding to payment.
     * 
     * Returns error message or null if no error.
     */
    private function paiementCotisTavValidation($flux) {
131 132 133 134 135
        //Look for existing recurring payment
        if($reason = $this->tavCotisationsUtils->checkExistingRecurringPayment($flux)) {
            return $reason;
        }

136 137
        // Look for existing cotisation
        if ($this->tavCotisationsUtils->checkExistingCotisation($flux)) {
138
            return "Cotisation déjà payée ce mois-ci.";
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
        }

        $destinataire = $flux->getDestinataire();

        // Look for cotisation data depending on active process
        if (true == $this->getParameter('household_based_allowance')) {
            $cotisationAmount = $destinataire->getCotisationAmount();

            if (is_null($cotisationAmount) || is_null($destinataire->getHouseholdAdultCount())) {
                return "Opération impossible : votre profil est incomplet, informations de cotisation manquantes. Veuillez contacter un administrateur.";
            }
        } else {
            $profile = $destinataire->getProfilDeCotisation();

            if (is_null($profile)) {
                return "Opération impossible : vous n'avez pas de profil de cotisation associé. Veuillez contacter un administrateur.";
            }
        }

        return null;
    }
    
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    /**
     * @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()) {
            $flux = $form->getData();

178 179
            $validationError = $this->paiementCotisTavValidation($flux);
            if (!is_null($validationError)) {
180 181
                $this->addFlash(
                    'error',
182
                    $this->translator->trans($validationError)
183 184 185 186 187
                );

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

Damien Moulard committed
188
            if (null == $flux->getDon() || 0 == $flux->getDon()->getMontant()) {
189 190 191
                $flux->setDon(null);
            }

192
            // Redirect to payment
193 194
            return $this->forward('App\Controller\PaymentController::preparePaymentAction', [
                'form'  => $form,
195
                'type'  => Payment::TYPE_PAIEMENT_COTISATION_TAV
196
            ]);
197

Damien Moulard committed
198
            /* For test purposes, comment redirection and uncomment following part to skip payment */
199 200 201 202 203
            // $this->em->persist($flux);
            // $this->operationUtils->executeOperations($flux);

            // // Apply cotisation rate, create new flux
            // $this->tavCotisationsUtils->applyTauxCotisation($flux);
204
            
205 206 207 208 209
            // $this->em->flush();
            // $this->addFlash(
            //     'success',
            //     $this->translator->trans('Cotisation payée ! [Paiement via Payzen temporairement désactivé]')
            // );
210

211
            // return $this->redirectToRoute('index');
212 213 214 215 216 217 218 219
        }

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

220 221 222 223 224 225 226 227 228 229 230 231 232 233
    /**
     * @Route("/adherent/paiement-reccurent-cotis-tav/", name="paiementRecurrentCotisTav")
     * @IsGranted("ROLE_ADHERENT")
     */
    public function paiementRecurrentCotisTavAction(Request $request)
    {
        if (empty($this->getUser()) || empty($this->getUser()->getAdherent())) {
            return $this->redirectToRoute('index');
        }

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

234 235 236 237 238 239 240
        if (!$form->isValid()) {
            foreach($form->getErrors(true) as $error) {
                $this->addFlash('error', $error->getMessage());
            }
            return $this->redirectToRoute('index');
        }

241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
        if ($form->isSubmitted() && $form->isValid()) {
            $flux = $form->getData();

            $validationError = $this->paiementCotisTavValidation($flux);
            if (!is_null($validationError)) {
                $this->addFlash(
                    'error',
                    $this->translator->trans($validationError)
                );

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

            if (null == $flux->getDon() || 0 == $flux->getDon()->getMontant()) {
                $flux->setDon(null);
            }

            // Redirect to payment
            return $this->forward('App\Controller\PaymentController::preparePaymentAction', [
                'form'  => $form,
                'type'  => Payment::TYPE_PAIEMENT_RECURRENT_COTISATION_TAV
            ]);
        }

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

Julien Jorry committed
271 272
    /**
     * @Route("/adherent/demande/achat-monnaie/", name="achatMonnaieAConfirmerAdherent")
273
     * @IsGranted("ROLE_ADHERENT")
Julien Jorry committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
     */
    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,
        ]);
    }
307 308 309 310 311

    /**
     * @Route("/adherent/set-payment-code", name="adherentSetPaymentCode")
     * @IsGranted("ROLE_ADHERENT")
     */
312
    public function setPaymentCodeAction(Request $request)
313 314 315 316 317 318 319 320 321
    {
        $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
322
            if (ctype_digit($plainCode) && strlen($plainCode) >= 4 && strlen($plainCode) <= 8) {
323 324
                $encoded = crypt($plainCode, $this->getUser()->getSalt());

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
                $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
342
}